[Kst] branches/work/kst/portto4/kst/src/libkstapp

Mike Fenton mike at staikos.net
Mon Oct 27 18:51:55 CET 2008


SVN commit 876618 by fenton:

Redo of Scalar Model to display all scalars in the proper hierarchy.


 M  +219 -151  scalarmodel.cpp  
 M  +31 -8     scalarmodel.h  


--- branches/work/kst/portto4/kst/src/libkstapp/scalarmodel.cpp #876617:876618
@@ -18,12 +18,66 @@
 #include <generatedvector.h>
 #include <datamatrix.h>
 #include <generatedmatrix.h>
+#include <datasource.h>
 
 namespace Kst {
 
+ScalarTreeItem::ScalarTreeItem(const QList<QVariant> &data, ScalarTreeItem *parent) {
+  parentItem = parent;
+  itemData = data; 
+  if (parent) {
+    parent->addChild(this);
+  }
+}
+
+
+ScalarTreeItem::~ScalarTreeItem() {
+  qDeleteAll(childItems);
+}
+
+
+void ScalarTreeItem::addChild(ScalarTreeItem *item) {
+  childItems.append(item);
+}
+
+
+ScalarTreeItem *ScalarTreeItem::child(int row) {
+    return childItems.value(row);
+}
+
+int ScalarTreeItem::childCount() const {
+    return childItems.count();
+}
+
+
+int ScalarTreeItem::row() const {
+    if (parentItem)
+        return parentItem->childItems.indexOf(const_cast<ScalarTreeItem*>(this));
+
+    return 0;
+}
+
+
+int ScalarTreeItem::columnCount() const {
+    return itemData.count();
+}
+
+
+QVariant ScalarTreeItem::data(int column) const {
+    return itemData.value(column);
+}
+
+ScalarTreeItem *ScalarTreeItem::parent() {
+    return parentItem;
+}
+
+
 ScalarModel::ScalarModel(ObjectStore *store)
 : QAbstractItemModel(), _store(store) {
-  generateObjectList();
+  QList<QVariant> rootData;
+  rootData << "Scalars";
+  _rootItem = new ScalarTreeItem(rootData);
+  createTree();
 }
 
 
@@ -37,161 +91,190 @@
 }
 
 
-void ScalarModel::generateObjectList() {
-  ObjectList<DataVector> dvol = _store->getObjects<DataVector>();
-  ObjectList<GeneratedVector> gvol = _store->getObjects<GeneratedVector>();
-  ObjectList<DataMatrix> dmol = _store->getObjects<DataMatrix>();
-  ObjectList<GeneratedMatrix> gmol = _store->getObjects<GeneratedMatrix>();
-  ObjectList<DataObject> dol = _store->getObjects<DataObject>();
-  ObjectList<Scalar> sol = _store->getObjects<Scalar>();
-
-  foreach(DataVector* vector, dvol) {
-    _objectList.append(vector);
+void ScalarModel::addScalar(ScalarPtr scalar, ScalarTreeItem* parent) {
+  ScalarTreeItem* parentItem;
+  if (parent) {
+    parentItem = parent;
+  } else {
+    parentItem = _rootItem;
   }
+  QList<QVariant> data;
+  data << scalar->Name() << scalar->value();
+  new ScalarTreeItem(data, parentItem);
+}
 
-  foreach(GeneratedVector* vector, gvol) {
-    _objectList.append(vector);
-  }
 
-  foreach(DataMatrix* matrix, dmol) {
-    _objectList.append(matrix);
+void ScalarModel::addScalars(const QHash<QString, Kst::Scalar*> scalarMap, ScalarTreeItem* parent) {
+  QMap<QString, ScalarPtr> map;
+  foreach(Scalar* scalar, scalarMap) {
+    map.insert(scalar->Name(), scalar);
   }
-
-  foreach(GeneratedMatrix* matrix, gmol) {
-    _objectList.append(matrix);
+  QMapIterator<QString, ScalarPtr> iObject(map);
+  while (iObject.hasNext()) {
+    iObject.next();
+    addScalar(iObject.value(), parent);
   }
+}
 
-  foreach(DataObject* dataObject, dol) {
-    foreach(VectorPtr vector, dataObject->outputVectors()) {
-      _objectList.append(vector);
-    }
-    foreach(MatrixPtr matrix, dataObject->outputMatrices()) {
-      _objectList.append(matrix);
-    }
-  }
 
-  foreach(Scalar* scalar, sol) {
-    if (scalar->orphan()) {
-      _objectList.append(scalar);
-    }
+void ScalarModel::addVector(VectorPtr vector, ScalarTreeItem* parent) {
+  ScalarTreeItem* parentItem;
+  if (parent) {
+    parentItem = parent;
+  } else {
+    parentItem = _rootItem;
   }
+  QList<QVariant> data;
+  data << vector->Name();
+  ScalarTreeItem* item = new ScalarTreeItem(data, parentItem);
+  addScalars(vector->scalars(), item);
 }
 
-int ScalarModel::rowCount(const QModelIndex& parent) const {
-  int rc = 0;
-  if (!parent.isValid()) {
-    rc = _objectList.count();
-  } else if (!parent.parent().isValid()) {
-    if (VectorPtr vector = kst_cast<Vector>(_objectList.at(parent.row()))) {
-      vector->readLock();
-      rc = vector->scalars().count();
-      vector->unlock();
-    } else if (MatrixPtr matrix = kst_cast<Matrix>(_objectList.at(parent.row()))) {
-      matrix->readLock();
-      rc = matrix->scalars().count();
-      matrix->unlock();
-    }
+
+void ScalarModel::addMatrix(MatrixPtr matrix, ScalarTreeItem* parent) {
+  ScalarTreeItem* parentItem;
+  if (parent) {
+    parentItem = parent;
+  } else {
+    parentItem = _rootItem;
   }
-  return rc;
+  QList<QVariant> data;
+  data << matrix->Name();
+  ScalarTreeItem* item = new ScalarTreeItem(data, parentItem);
+  addScalars(matrix->scalars(), item);
 }
 
 
-QVariant ScalarModel::data(const QModelIndex& index, int role) const {
-  if (!index.isValid()) {
-    return QVariant();
+void ScalarModel::addDataObject(DataObjectPtr dataObject, ScalarTreeItem* parent) {
+  ScalarTreeItem* parentItem;
+  if (parent) {
+    parentItem = parent;
+  } else {
+    parentItem = _rootItem;
   }
+  QList<QVariant> data;
+  data << dataObject->Name();
+  ScalarTreeItem* item = new ScalarTreeItem(data, parentItem);
 
-  if (role != Qt::DisplayRole) {
-    return QVariant();
+  QMap<QString, ObjectPtr> objectMap;
+  foreach(Scalar* scalar, dataObject->outputScalars()) {
+    objectMap.insert(scalar->Name(), scalar);
   }
-
-  if (index.internalPointer()) {
-    Object* object = static_cast<Object*>(index.internalPointer());
-    if (!object) {
-      return QVariant();
-    }
-
-    if (VectorPtr parent = kst_cast<Vector>(object)) {
-      return vectorOutputData(parent, index);
-    } else if (MatrixPtr parent = kst_cast<Matrix>(object)) {
-      return matrixOutputData(parent, index);
-    }
+  foreach(Vector* vector, dataObject->outputVectors()) {
+    objectMap.insert(vector->Name(), vector);
   }
+  foreach(Matrix* matrix, dataObject->outputMatrices()) {
+    objectMap.insert(matrix->Name(), matrix);
+  }
 
-  const int row = index.row();
-  if (row < _objectList.count()) {
-    if (ScalarPtr p = kst_cast<Scalar>(_objectList.at(row))) {
-      return scalarData(p, index);
-    } else if (ObjectPtr p = kst_cast<Object>(_objectList.at(row))) {
-      return objectData(p, index);
+  QMapIterator<QString, ObjectPtr> iObject(objectMap);
+  while (iObject.hasNext()) {
+    iObject.next();
+    if (VectorPtr vector = kst_cast<Vector>(iObject.value())) {
+      addVector(vector, item);
+    } else if (MatrixPtr matrix = kst_cast<Matrix>(iObject.value())) {
+      addMatrix(matrix, item);
+    } else if (ScalarPtr scalar = kst_cast<Scalar>(iObject.value())) {
+      addScalar(scalar, item);
     }
   }
-
-  return QVariant();
 }
 
 
-QVariant ScalarModel::vectorOutputData(VectorPtr parent, const QModelIndex& index) const {
-  QVariant rc;
-
+void ScalarModel::addDataSource(DataSourcePtr dataSource, ScalarTreeItem* parent) {
+  ScalarTreeItem* parentItem;
   if (parent) {
-    if (parent->scalars().count() > index.row()) {
-      if (ScalarPtr scalar = parent->scalars().values()[index.row()]) {
-        return scalarData(scalar, index);
-      }
-    }
+    parentItem = parent;
+  } else {
+    parentItem = _rootItem;
   }
+  QList<QVariant> data;
+  data << dataSource->shortName();
+  ScalarTreeItem* item = new ScalarTreeItem(data, parentItem);
 
-  return rc;
+  QStringList scalars = dataSource->scalarList();
+  scalars.sort();
+  foreach(QString scalar, scalars) {
+    QList<QVariant> data;
+    double value;
+    dataSource->readScalar(value, scalar);
+    data << scalar << value;
+    new ScalarTreeItem(data, item);
+  }
 }
 
 
-QVariant ScalarModel::matrixOutputData(MatrixPtr parent, const QModelIndex& index) const {
-  QVariant rc;
+void ScalarModel::createTree() {
+  QMap<QString, ObjectPtr> objectMap;
+  foreach(DataVector* vector, _store->getObjects<DataVector>()) {
+    objectMap.insert(vector->Name(), vector);
+  }
+  foreach(GeneratedVector* vector, _store->getObjects<GeneratedVector>()) {
+    objectMap.insert(vector->Name(), vector);
+  }
+  foreach(DataMatrix* matrix, _store->getObjects<DataMatrix>()) {
+    objectMap.insert(matrix->Name(), matrix);
+  }
+  foreach(GeneratedMatrix* matrix, _store->getObjects<GeneratedMatrix>()) {
+    objectMap.insert(matrix->Name(), matrix);
+  }
+  foreach(DataObjectPtr dataObject, _store->getObjects<DataObject>()) {
+    objectMap.insert(dataObject->Name(), dataObject);
+  }
+  foreach(Scalar* scalar, _store->getObjects<Scalar>()) {
+    if (scalar->orphan()) {
+      objectMap.insert(scalar->Name(), scalar);
+    }
+  }
+  foreach(DataSource* ds, _store->dataSourceList()) {
+    if (!ds->scalarList().isEmpty()) {
+      objectMap.insert(ds->shortName(), ds);
+    }
+  }
 
-  if (parent) {
-    if (parent->scalars().count() > index.row()) {
-      if (ScalarPtr scalar = parent->scalars().values()[index.row()]) {
-        return scalarData(scalar, index);
-      }
+  QMapIterator<QString, ObjectPtr> iObject(objectMap);
+  while (iObject.hasNext()) {
+    iObject.next();
+    if (VectorPtr vector = kst_cast<Vector>(iObject.value())) {
+      addVector(vector);
+    } else if (MatrixPtr matrix = kst_cast<Matrix>(iObject.value())) {
+      addMatrix(matrix);
+    } else if (DataObjectPtr dataObject = kst_cast<DataObject>(iObject.value())) {
+      addDataObject(dataObject);
+    } else if (ScalarPtr scalar = kst_cast<Scalar>(iObject.value())) {
+      addScalar(scalar);
+    } else if (DataSourcePtr dataSource = kst_cast<DataSource>(iObject.value())) {
+      addDataSource(dataSource);
     }
   }
-
-  return rc;
 }
 
+int ScalarModel::rowCount(const QModelIndex& parent) const {
+  ScalarTreeItem *parentItem;
+  if (parent.column() > 0)
+      return 0;
 
-QVariant ScalarModel::objectData(ObjectPtr object, const QModelIndex& index) const {
-  QVariant rc;
+  if (!parent.isValid())
+      parentItem = _rootItem;
+  else
+      parentItem = static_cast<ScalarTreeItem*>(parent.internalPointer());
 
-  if (object) {
-    if (index.column() == Name) {
-      object->readLock();
-      rc.setValue(object->Name());
-      object->unlock();
-    }
-  }
-
-  return rc;
+  return parentItem->childCount();
 }
 
 
-QVariant ScalarModel::scalarData(ScalarPtr scalar, const QModelIndex& index) const {
-  QVariant rc;
+QVariant ScalarModel::data(const QModelIndex& index, int role) const {
+  if (!index.isValid()) {
+    return QVariant();
+  }
 
-  if (scalar) {
-    if (index.column() == Name) {
-      scalar->readLock();
-      rc.setValue(scalar->Name());
-      scalar->unlock();
-    } else if (index.column() == Value) {
-      scalar->readLock();
-      rc = QVariant(scalar->value());
-      scalar->unlock();
-    }
+  if (role != Qt::DisplayRole) {
+    return QVariant();
   }
 
-  return rc;
+  ScalarTreeItem *item = static_cast<ScalarTreeItem*>(index.internalPointer());
+
+  return item->data(index.column());
 }
 
 
@@ -200,52 +283,37 @@
     return QModelIndex();
   }
 
-  const int count = _objectList.count();
-  ObjectPtr object = 0;
-  if (!parent.isValid()) {
-    if (row < count) {
-      return createIndex(row, col);
-    }
-  } else if (!parent.parent().isValid()) {
-    if (parent.row() >= 0 && parent.row() < count) { 
-      if (VectorPtr vector = kst_cast<Vector>(_objectList.at(parent.row()))) {
-        vector->readLock();
-        if (row < vector->scalars().count()) {
-          object = vector;
-        }
-        vector->unlock();
-      } else if (MatrixPtr matrix = kst_cast<Matrix>(_objectList.at(parent.row()))) {
-        matrix->readLock();
-        if (row < matrix->scalars().count()) {
-          object = matrix;
-        }
-        matrix->unlock();
-      }
-    }
-  }
+  if (!hasIndex(row, col, parent))
+      return QModelIndex();
 
-  if (object) {
-    return createIndex(row, col, object);
-  } else {
-    return QModelIndex();
-  }
+  ScalarTreeItem *parentItem;
+
+  if (!parent.isValid())
+      parentItem = _rootItem;
+  else
+      parentItem = static_cast<ScalarTreeItem*>(parent.internalPointer());
+
+  ScalarTreeItem *childItem = parentItem->child(row);
+  if (childItem)
+      return createIndex(row, col, childItem);
+  else
+      return QModelIndex();
 }
 
 
 QModelIndex ScalarModel::parent(const QModelIndex& index) const {
   Q_ASSERT(_store);
-  int row = -1;
 
-  if (Vector *vector = static_cast<Vector*>(index.internalPointer())) {
-    row = _objectList.indexOf(vector);
-  } else if (Matrix *matrix = static_cast<Matrix*>(index.internalPointer())) {
-    row = _objectList.indexOf(matrix);
-  }
+  if (!index.isValid())
+      return QModelIndex();
 
-  if (row < 0) {
-    return QModelIndex();
-  }
-  return createIndex(row, index.column());
+  ScalarTreeItem *childItem = static_cast<ScalarTreeItem*>(index.internalPointer());
+  ScalarTreeItem *parentItem = childItem->parent();
+
+  if (parentItem == _rootItem)
+      return QModelIndex();
+
+  return createIndex(parentItem->row(), 0, parentItem);
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/scalarmodel.h #876617:876618
@@ -14,12 +14,34 @@
 
 #include <QAbstractItemModel>
 #include "dataobject.h"
+#include "datasource.h"
 #include "object.h"
 
 namespace Kst {
 
 class ObjectStore;
 
+ class ScalarTreeItem
+ {
+ public:
+     ScalarTreeItem(const QList<QVariant> &data, ScalarTreeItem *parent = 0);
+     ~ScalarTreeItem();
+
+     void addChild(ScalarTreeItem *child);
+
+     ScalarTreeItem *child(int row);
+     int childCount() const;
+     int columnCount() const;
+     QVariant data(int column) const;
+     int row() const;
+     ScalarTreeItem *parent();
+
+ private:
+     QList<ScalarTreeItem*> childItems;
+     QList<QVariant> itemData;
+     ScalarTreeItem *parentItem;
+ };
+
 class ScalarModel : public QAbstractItemModel
 {
 
@@ -35,17 +57,18 @@
   QModelIndex index(int row, int col, const QModelIndex& parent = QModelIndex()) const;
   QModelIndex parent(const QModelIndex& index) const;
   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
-  void generateObjectList();
+  void createTree();
+  void addScalar(ScalarPtr scalar, ScalarTreeItem* parent = 0);
+  void addVector(VectorPtr vector, ScalarTreeItem* parent = 0);
+  void addMatrix(MatrixPtr matrix, ScalarTreeItem* parent = 0);
+  void addDataObject(DataObjectPtr dataObject, ScalarTreeItem* parent = 0);
+  void addDataSource(DataSourcePtr dataSource, ScalarTreeItem* parent = 0);
+  void addScalars(const QHash<QString, Kst::Scalar*> scalarMap, ScalarTreeItem* parent = 0);
 
 private:
-  QVariant dataObjectOutputData(DataObjectPtr parent, const QModelIndex& index) const;
-  QVariant vectorOutputData(VectorPtr parent, const QModelIndex& index) const;
-  QVariant matrixOutputData(MatrixPtr parent, const QModelIndex& index) const;
-  QVariant objectData(ObjectPtr parent, const QModelIndex& index) const;
-  QVariant scalarData(ScalarPtr parent, const QModelIndex& index) const;
+  ObjectStore *_store;
+  ScalarTreeItem *_rootItem;
 
-  ObjectStore *_store;
-  ObjectList<Object> _objectList;
 };
 
 }


More information about the Kst mailing list