[Kst] branches/work/kst/portto4/kst/src/libkstapp
Peter Kümmel
syntheticpp at gmx.net
Wed Jan 19 14:55:16 CET 2011
SVN commit 1215743 by kuemmel:
cleanup: split out PrimitiveModel code
AM primitivemodel.cpp [License: GPL (v2+)]
AM primitivemodel.h [License: GPL (v2+)]
M +0 -162 scalarmodel.cpp
M +2 -102 scalarmodel.h
M +1 -1 stringmodel.h
--- branches/work/kst/portto4/kst/src/libkstapp/scalarmodel.cpp #1215742:1215743
@@ -23,85 +23,7 @@
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;
-}
-
-
-PrimitiveModel::PrimitiveModel(ObjectStore *store)
-: QAbstractItemModel(), _store(store) {
- QList<QVariant> rootData;
- rootData << "Scalars";
- _rootItem = new ScalarTreeItem(rootData);
-}
-
-
-PrimitiveModel::~PrimitiveModel() {
-}
-
-
-int PrimitiveModel::columnCount(const QModelIndex& parent) const {
- Q_UNUSED(parent)
- return 2;
-}
-
-
-ScalarTreeItem* PrimitiveModel::addScalarTreeItem(const QList<QVariant>& data, ScalarTreeItem* parent) {
- ScalarTreeItem* parentItem;
- if (parent) {
- parentItem = parent;
- } else {
- parentItem = _rootItem;
- }
- return new ScalarTreeItem(data, parentItem);
-}
-
-
void ScalarModel::addDataSource(DataSourcePtr dataSource, ScalarTreeItem* parent) {
ScalarTreeItem* item = addScalarTreeItem(QList<QVariant>() << dataSource->Name(), parent);
@@ -120,90 +42,6 @@
-
-int PrimitiveModel::rowCount(const QModelIndex& parent) const {
- ScalarTreeItem *parentItem;
- if (parent.column() > 0)
- return 0;
-
- if (!parent.isValid())
- parentItem = _rootItem;
- else
- parentItem = static_cast<ScalarTreeItem*>(parent.internalPointer());
-
- return parentItem->childCount();
}
-
-QVariant PrimitiveModel::data(const QModelIndex& index, int role) const {
- if (!index.isValid()) {
- return QVariant();
- }
-
- if (role != Qt::DisplayRole) {
- return QVariant();
- }
-
- ScalarTreeItem *item = static_cast<ScalarTreeItem*>(index.internalPointer());
-
- return item->data(index.column());
-}
-
-
-QModelIndex PrimitiveModel::index(int row, int col, const QModelIndex& parent) const {
- if (row < 0 || col < 0 || col > 1) {
- return QModelIndex();
- }
-
- if (!hasIndex(row, col, parent))
- 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 PrimitiveModel::parent(const QModelIndex& index) const {
- Q_ASSERT(_store);
-
- if (!index.isValid())
- return QModelIndex();
-
- ScalarTreeItem *childItem = static_cast<ScalarTreeItem*>(index.internalPointer());
- ScalarTreeItem *parentItem = childItem->parent();
-
- if (parentItem == _rootItem)
- return QModelIndex();
-
- return createIndex(parentItem->row(), 0, parentItem);
-}
-
-
-QVariant PrimitiveModel::headerData(int section, Qt::Orientation orientation, int role) const {
- if (role != Qt::DisplayRole) {
- return QAbstractItemModel::headerData(section, orientation, role);
- }
- switch (section) {
- case Name:
- return tr("Name");
- case Value:
- return tr("Value");
- default:
- break;
- }
- return QVariant();
-}
-
-}
-
// vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/scalarmodel.h #1215742:1215743
@@ -13,112 +13,12 @@
#ifndef SCALARMODEL_H
#define SCALARMODEL_H
-#include <QAbstractItemModel>
-#include "dataobject.h"
-#include "datasource.h"
-#include "object.h"
+#include "primitivemodel.h"
+
namespace Kst {
-class ObjectStore;
- class ScalarTreeItem
- {
- public:
- explicit 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 PrimitiveModel : public QAbstractItemModel
-{
-
- enum ColumnID { Name, Value };
-
-public:
- PrimitiveModel(ObjectStore *store);
- ~PrimitiveModel();
-
- int columnCount(const QModelIndex& parent = QModelIndex()) const;
- int rowCount(const QModelIndex& parent = QModelIndex()) const;
- QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
- 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;
-
- template<class T>
- void createTree() {
- QList<ObjectPtr> objects = _store->objectList();
- foreach(const ObjectPtr& obj, objects) {
- if (kst_cast<Primitive>(obj)) {
- if (kst_cast<T>(obj) && !kst_cast<T>(obj)->orphan()) {
- continue;
- }
- addPrimitivesScalars<T>(kst_cast<Primitive>(obj));
- } else if (kst_cast<DataSource>(obj) && !kst_cast<DataSource>(obj)->scalar().list().isEmpty()) {
- addDataSource(kst_cast<DataSource>(obj));
- } else if (kst_cast<DataObject>(obj)) {
- addDataObject<T>(kst_cast<DataObject>(obj));
- }
- }
- }
-
-
- template<class T>
- void addScalar(T* scalar, ScalarTreeItem* parent = 0) {
- addScalarTreeItem(QList<QVariant>() << scalar->Name() << scalar->value(), parent);
- }
-
- template<class T>
- void addScalars(const PrimitiveMap& scalarMap, ScalarTreeItem* parent) {
- foreach(const PrimitivePtr& scalar, scalarMap) {
- if(kst_cast<T>(scalar)) {
- addScalar<T>(kst_cast<T>(scalar), parent);
- }
- }
- }
-
- template<class T>
- void addPrimitivesScalars(const PrimitivePtr& prim, ScalarTreeItem* parent = 0) {
- ScalarTreeItem* item = addScalarTreeItem(QList<QVariant>() << prim->Name(), parent);
- addScalars<T>(prim->metas(), item);
- }
-
- template<class T>
- void addDataObject(DataObjectPtr dataObject, ScalarTreeItem* parent = 0) {
- ScalarTreeItem* item = addScalarTreeItem(QList<QVariant>() << dataObject->Name(), parent);
-
- ObjectList<Primitive> primitives = dataObject->outputPrimitives();
- foreach(PrimitivePtr prim, primitives) {
- if (!kst_cast<String>(prim)) {
- addPrimitivesScalars<T>(prim.data(), item);
- }
- }
- }
-
- virtual void addDataSource(DataSourcePtr dataSource, ScalarTreeItem* parent = 0) = 0;
-
-protected:
- ScalarTreeItem* addScalarTreeItem(const QList<QVariant>& data, ScalarTreeItem* parent);
-
-private:
- ObjectStore *_store;
- ScalarTreeItem *_rootItem;
-};
-
class ScalarModel : public PrimitiveModel
{
--- branches/work/kst/portto4/kst/src/libkstapp/stringmodel.h #1215742:1215743
@@ -13,7 +13,7 @@
#ifndef STRINGMODEL_H
#define STRINGMODEL_H
-#include "scalarmodel.h"
+#include "primitivemodel.h"
namespace Kst {
More information about the Kst
mailing list