File indexing completed on 2024-12-22 04:17:44

0001 /***************************************************************************
0002 *                                                                         *
0003 *   copyright : (C) 2011 The University of Toronto                        *
0004 *                   netterfield@astro.utoronto.ca                         *
0005 *                                                                         *
0006 *   This program is free software; you can redistribute it and/or modify  *
0007 *   it under the terms of the GNU General Public License as published by  *
0008 *   the Free Software Foundation; either version 2 of the License, or     *
0009 *   (at your option) any later version.                                   *
0010 *                                                                         *
0011 ***************************************************************************/
0012 
0013 #ifndef PRIMITIVE_MODEL_H
0014 #define PRIMITIVE_MODEL_H
0015 
0016 #include <QAbstractItemModel>
0017 #include "dataobject.h"
0018 #include "datasource.h"
0019 #include "object.h"
0020 #include "objectstore.h"
0021 
0022 
0023 #include "datavector.h"
0024 #include "generatedvector.h"
0025 #include "editablematrix.h"
0026 #include "editablevector.h"
0027 #include "datamatrix.h"
0028 #include "generatedmatrix.h"
0029 
0030 namespace Kst {
0031 
0032 class ObjectStore;
0033 
0034 class PrimitiveTreeItem
0035 {
0036 public:
0037   explicit PrimitiveTreeItem(const QList<QVariant> &data, PrimitiveTreeItem *parent = 0);
0038   ~PrimitiveTreeItem();
0039 
0040   void addChild(PrimitiveTreeItem *child);
0041 
0042   PrimitiveTreeItem *child(int row);
0043   int childCount() const;
0044   int columnCount() const;
0045   QVariant data(int column) const;
0046   int row() const;
0047   PrimitiveTreeItem *parent();
0048 
0049 private:
0050   QList<PrimitiveTreeItem*> childItems;
0051   QList<QVariant> itemData;
0052   PrimitiveTreeItem *parentItem;
0053 };
0054 
0055 
0056 
0057 class PrimitiveModel : public QAbstractItemModel
0058 {
0059   Q_OBJECT
0060   enum ColumnID { Name, Value };
0061 
0062 public:
0063   explicit PrimitiveModel(ObjectStore *store);
0064   ~PrimitiveModel();
0065 
0066   int columnCount(const QModelIndex& parent = QModelIndex()) const;
0067   int rowCount(const QModelIndex& parent = QModelIndex()) const;
0068   QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
0069   QModelIndex index(int row, int col, const QModelIndex& parent = QModelIndex()) const;
0070   QModelIndex parent(const QModelIndex& index) const;
0071   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
0072 
0073   template<class T>
0074   void createTree();
0075 
0076   template<class T>
0077   void addMeta(T* m, PrimitiveTreeItem* parent = 0);
0078 
0079   template<class T>
0080   void addOrphanMeta(T* m, PrimitiveTreeItem* parent = 0);
0081 
0082   template<class T>
0083   void addMetas(const PrimitiveMap& metarMap, PrimitiveTreeItem* parent);
0084 
0085   template<class T>
0086   void addPrimitivesMetas(const PrimitivePtr& prim, PrimitiveTreeItem* parent = 0);
0087 
0088   template<class T>
0089   void addDataObjectsMetas(DataObjectPtr dataObject, PrimitiveTreeItem* parent = 0);
0090 
0091   virtual void addDataSourcesMetas(DataSourcePtr dataSource, PrimitiveTreeItem* parent = 0) = 0;
0092 
0093 protected:
0094   PrimitiveTreeItem* addPrimitiveTreeItem(const QList<QVariant>& data, PrimitiveTreeItem* parent);
0095 
0096 
0097 private:
0098   ObjectStore *_store;
0099   PrimitiveTreeItem *_rootItem;
0100 };
0101 
0102 
0103 
0104 template<class T>
0105 void PrimitiveModel::createTree() {
0106   QList<ObjectPtr> objects = _store->objectList();
0107   foreach(const ObjectPtr& obj, objects) {
0108     if (   kst_cast<Vector>(obj)
0109         || kst_cast<Matrix>(obj)
0110         || kst_cast<DataVector>(obj)
0111         || kst_cast<DataMatrix>(obj)
0112         || kst_cast<EditableVector>(obj)
0113         || kst_cast<EditableMatrix>(obj)
0114         || kst_cast<GeneratedVector>(obj)
0115         || kst_cast<GeneratedMatrix>(obj))
0116     {
0117       addPrimitivesMetas<T>(kst_cast<Primitive>(obj));
0118     } else if (kst_cast<T>(obj)) {
0119       if (kst_cast<T>(obj) && kst_cast<T>(obj)->orphan() && !(kst_cast<T>(obj)->hidden())) {
0120         addOrphanMeta<T>(kst_cast<T>(obj));
0121       }
0122     } else if (kst_cast<DataObject>(obj)) {
0123       addDataObjectsMetas<T>(kst_cast<DataObject>(obj));
0124     }
0125   }
0126   DataSourceList datasoucres = _store->dataSourceList();
0127   foreach(const DataSourcePtr& ds, datasoucres) {
0128     addDataSourcesMetas(ds);
0129   }
0130 }
0131 
0132 
0133 template<class T>
0134 void PrimitiveModel::addMeta(T* m, PrimitiveTreeItem* parent) {
0135   addPrimitiveTreeItem(QList<QVariant>() << QString("%1 (%2)").arg(m->slaveName()).arg(m->shortName()) << m->value(), parent);
0136 }
0137 
0138 template<class T>
0139 void PrimitiveModel::addOrphanMeta(T* m, PrimitiveTreeItem* parent) {
0140   addPrimitiveTreeItem(QList<QVariant>() << m->Name() << m->value(), parent);
0141 }
0142 
0143 
0144 template<class T>
0145 void PrimitiveModel::addMetas(const PrimitiveMap& metarMap, PrimitiveTreeItem* parent) {
0146   foreach(const PrimitivePtr& m, metarMap) {
0147     if(kst_cast<T>(m)) {
0148       addMeta<T>(kst_cast<T>(m), parent);
0149     }
0150   }
0151 }
0152 
0153 
0154 template<class T>
0155 void PrimitiveModel::addPrimitivesMetas(const PrimitivePtr& prim, PrimitiveTreeItem* parent) {
0156   bool metas_available = false;
0157   PrimitiveMap metas = prim->metas();
0158   foreach(const PrimitivePtr& m, metas) {
0159     if (kst_cast<T>(m)) {
0160       metas_available = true;
0161       break;
0162     }
0163   }
0164   if (metas_available) {
0165     PrimitiveTreeItem* item = addPrimitiveTreeItem(QList<QVariant>() << prim->Name(), parent);
0166     addMetas<T>(prim->metas(), item);
0167   }
0168 }
0169 
0170 
0171 template<class T>
0172 void PrimitiveModel::addDataObjectsMetas(DataObjectPtr dataObject, PrimitiveTreeItem* parent) {
0173   PrimitiveTreeItem* item = addPrimitiveTreeItem(QList<QVariant>() << dataObject->Name(), parent);
0174 
0175   ObjectList<Primitive> primitives = dataObject->outputPrimitives(false);
0176   foreach(PrimitivePtr prim, primitives) {
0177     if (   kst_cast<Vector>(prim)
0178         || kst_cast<Matrix>(prim)
0179         || kst_cast<T>(prim))
0180     {
0181       addPrimitivesMetas<T>(prim.data(), item);
0182     }
0183     if (kst_cast<T>(prim)) {
0184       addMeta<T>(kst_cast<T>(prim), item);
0185     }
0186   }
0187 }
0188 
0189 
0190 }
0191 
0192 #endif
0193 
0194 // vim: ts=2 sw=2 et