1 /***************************************************************************
2 * *
3 * copyright : (C) 2011 The University of Toronto *
4 * netterfield@astro.utoronto.ca *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 ***************************************************************************/
12
13 #ifndef PRIMITIVE_MODEL_H
14 #define PRIMITIVE_MODEL_H
15
16 #include <QAbstractItemModel>
17 #include "dataobject.h"
18 #include "datasource.h"
19 #include "object.h"
20 #include "objectstore.h"
21
22
23 #include "datavector.h"
24 #include "generatedvector.h"
25 #include "editablematrix.h"
26 #include "editablevector.h"
27 #include "datamatrix.h"
28 #include "generatedmatrix.h"
29
30 namespace Kst {
31
32 class ObjectStore;
33
34 class PrimitiveTreeItem
35 {
36 public:
37 explicit PrimitiveTreeItem(const QList<QVariant> &data, PrimitiveTreeItem *parent = 0);
38 ~PrimitiveTreeItem();
39
40 void addChild(PrimitiveTreeItem *child);
41
42 PrimitiveTreeItem *child(int row);
43 int childCount() const;
44 int columnCount() const;
45 QVariant data(int column) const;
46 int row() const;
47 PrimitiveTreeItem *parent();
48
49 private:
50 QList<PrimitiveTreeItem*> childItems;
51 QList<QVariant> itemData;
52 PrimitiveTreeItem *parentItem;
53 };
54
55
56
57 class PrimitiveModel : public QAbstractItemModel
58 {
59 Q_OBJECT
60 enum ColumnID { Name, Value };
61
62 public:
63 explicit PrimitiveModel(ObjectStore *store);
64 ~PrimitiveModel();
65
66 int columnCount(const QModelIndex& parent = QModelIndex()) const;
67 int rowCount(const QModelIndex& parent = QModelIndex()) const;
68 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
69 QModelIndex index(int row, int col, const QModelIndex& parent = QModelIndex()) const;
70 QModelIndex parent(const QModelIndex& index) const;
71 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
72
73 template<class T>
74 void createTree();
75
76 template<class T>
77 void addMeta(T* m, PrimitiveTreeItem* parent = 0);
78
79 template<class T>
80 void addOrphanMeta(T* m, PrimitiveTreeItem* parent = 0);
81
82 template<class T>
83 void addMetas(const PrimitiveMap& metarMap, PrimitiveTreeItem* parent);
84
85 template<class T>
86 void addPrimitivesMetas(const PrimitivePtr& prim, PrimitiveTreeItem* parent = 0);
87
88 template<class T>
89 void addDataObjectsMetas(DataObjectPtr dataObject, PrimitiveTreeItem* parent = 0);
90
91 virtual void addDataSourcesMetas(DataSourcePtr dataSource, PrimitiveTreeItem* parent = 0) = 0;
92
93 protected:
94 PrimitiveTreeItem* addPrimitiveTreeItem(const QList<QVariant>& data, PrimitiveTreeItem* parent);
95
96
97 private:
98 ObjectStore *_store;
99 PrimitiveTreeItem *_rootItem;
100 };
101
102
103
104 template<class T>
createTree()105 void PrimitiveModel::createTree() {
106 QList<ObjectPtr> objects = _store->objectList();
107 foreach(const ObjectPtr& obj, objects) {
108 if ( kst_cast<Vector>(obj)
109 || kst_cast<Matrix>(obj)
110 || kst_cast<DataVector>(obj)
111 || kst_cast<DataMatrix>(obj)
112 || kst_cast<EditableVector>(obj)
113 || kst_cast<EditableMatrix>(obj)
114 || kst_cast<GeneratedVector>(obj)
115 || kst_cast<GeneratedMatrix>(obj))
116 {
117 addPrimitivesMetas<T>(kst_cast<Primitive>(obj));
118 } else if (kst_cast<T>(obj)) {
119 if (kst_cast<T>(obj) && kst_cast<T>(obj)->orphan() && !(kst_cast<T>(obj)->hidden())) {
120 addOrphanMeta<T>(kst_cast<T>(obj));
121 }
122 } else if (kst_cast<DataObject>(obj)) {
123 addDataObjectsMetas<T>(kst_cast<DataObject>(obj));
124 }
125 }
126 DataSourceList datasoucres = _store->dataSourceList();
127 foreach(const DataSourcePtr& ds, datasoucres) {
128 addDataSourcesMetas(ds);
129 }
130 }
131
132
133 template<class T>
addMeta(T * m,PrimitiveTreeItem * parent)134 void PrimitiveModel::addMeta(T* m, PrimitiveTreeItem* parent) {
135 addPrimitiveTreeItem(QList<QVariant>() << QString("%1 (%2)").arg(m->slaveName()).arg(m->shortName()) << m->value(), parent);
136 }
137
138 template<class T>
addOrphanMeta(T * m,PrimitiveTreeItem * parent)139 void PrimitiveModel::addOrphanMeta(T* m, PrimitiveTreeItem* parent) {
140 addPrimitiveTreeItem(QList<QVariant>() << m->Name() << m->value(), parent);
141 }
142
143
144 template<class T>
addMetas(const PrimitiveMap & metarMap,PrimitiveTreeItem * parent)145 void PrimitiveModel::addMetas(const PrimitiveMap& metarMap, PrimitiveTreeItem* parent) {
146 foreach(const PrimitivePtr& m, metarMap) {
147 if(kst_cast<T>(m)) {
148 addMeta<T>(kst_cast<T>(m), parent);
149 }
150 }
151 }
152
153
154 template<class T>
addPrimitivesMetas(const PrimitivePtr & prim,PrimitiveTreeItem * parent)155 void PrimitiveModel::addPrimitivesMetas(const PrimitivePtr& prim, PrimitiveTreeItem* parent) {
156 bool metas_available = false;
157 PrimitiveMap metas = prim->metas();
158 foreach(const PrimitivePtr& m, metas) {
159 if (kst_cast<T>(m)) {
160 metas_available = true;
161 break;
162 }
163 }
164 if (metas_available) {
165 PrimitiveTreeItem* item = addPrimitiveTreeItem(QList<QVariant>() << prim->Name(), parent);
166 addMetas<T>(prim->metas(), item);
167 }
168 }
169
170
171 template<class T>
addDataObjectsMetas(DataObjectPtr dataObject,PrimitiveTreeItem * parent)172 void PrimitiveModel::addDataObjectsMetas(DataObjectPtr dataObject, PrimitiveTreeItem* parent) {
173 PrimitiveTreeItem* item = addPrimitiveTreeItem(QList<QVariant>() << dataObject->Name(), parent);
174
175 ObjectList<Primitive> primitives = dataObject->outputPrimitives(false);
176 foreach(PrimitivePtr prim, primitives) {
177 if ( kst_cast<Vector>(prim)
178 || kst_cast<Matrix>(prim)
179 || kst_cast<T>(prim))
180 {
181 addPrimitivesMetas<T>(prim.data(), item);
182 }
183 if (kst_cast<T>(prim)) {
184 addMeta<T>(kst_cast<T>(prim), item);
185 }
186 }
187 }
188
189
190 }
191
192 #endif
193
194 // vim: ts=2 sw=2 et
195