1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 #ifndef __ctkVTKDataSetModel_h
22 #define __ctkVTKDataSetModel_h
23 
24 // Qt includes
25 #include <QStandardItemModel>
26 
27 // CTK includes
28 #include <ctkVTKObject.h>
29 
30 // CTK includes
31 #include "ctkVisualizationVTKWidgetsExport.h"
32 
33 class vtkDataSet;
34 class vtkAbstractArray;
35 
36 /// \ingroup Visualization_VTK_Widgets
37 namespace ctkVTK
38 {
39  enum ItemDataRole {
40    PointerRole = Qt::UserRole + 1,
41    LocationRole,
42  };
43 };
44 
45 class ctkVTKDataSetModelPrivate;
46 
47 //------------------------------------------------------------------------------
48 /// \ingroup Visualization_VTK_Widgets
49 class CTK_VISUALIZATION_VTK_WIDGETS_EXPORT ctkVTKDataSetModel
50   : public QStandardItemModel
51 {
52   Q_OBJECT
53   QVTK_OBJECT
54   Q_FLAGS(AttributeType AttributeTypes)
55 
56   /// This property holds the type of attribute that should be listed in the model.
57   /// By default all attributes are considered.
58   /// \sa ctkVTKDataSetModel::AllAttribute
59   Q_PROPERTY(AttributeTypes attributeTypes READ attributeTypes WRITE setAttributeTypes)
60 
61   /// This property allows adding a 'Null' item to the model, which is useful when
62   /// it is necessary to offer the user an option to not select any of the items
63   /// (for example, in a combo box there is always a selected item and it may be
64   /// necessary to allow the user to not select any of the attributes).
65   /// By default no 'Null' item is included.
66   Q_PROPERTY(bool includeNullItem READ includeNullItem WRITE setIncludeNullItem)
67 
68 public:
69   typedef ctkVTKDataSetModel Self;
70   typedef QStandardItemModel Superclass;
71   ctkVTKDataSetModel(QObject *parent=0);
72   virtual ~ctkVTKDataSetModel();
73 
74   enum AttributeType
75     {
76     NoAttribute = 0x1,
77     ScalarsAttribute = 0x2,
78     VectorsAttribute = 0x4,
79     NormalsAttribute = 0x8,
80     TCoordsAttribute = 0x10,
81     TensorsAttribute = 0x20,
82     GlobalIDsAttribute = 0x40,
83     PedigreeIDsAttribute = 0x80,
84     EdgeFlagAttribute = 0x100,
85     AllAttribute = NoAttribute | ScalarsAttribute | VectorsAttribute | NormalsAttribute | TCoordsAttribute | TensorsAttribute | GlobalIDsAttribute | PedigreeIDsAttribute | EdgeFlagAttribute
86     };
87   Q_DECLARE_FLAGS(AttributeTypes, AttributeType)
88 
89   virtual void setDataSet(vtkDataSet* dataSet);
90   vtkDataSet* dataSet()const;
91 
92   AttributeTypes attributeTypes()const;
93   void setAttributeTypes(const AttributeTypes& attributeTypes);
94 
95   bool includeNullItem()const;
96   void setIncludeNullItem(bool includeNullItem);
97   int nullItemLocation()const;
98 
99   /// Return the vtkAbstractArray associated to the index.
100   /// 0 if the index doesn't contain a vtkAbstractArray
101   inline vtkAbstractArray* arrayFromIndex(const QModelIndex& arrayIndex)const;
102 
103   /// Return the location from a given item. Fails and returns -1 if either
104   /// the given index points to a null item or an invisible item.
105   /// \sa locationFromItem()
106   inline int locationFromIndex(const QModelIndex& arrayIndex)const;
107 
108   vtkAbstractArray* arrayFromItem(QStandardItem* nodeItem)const;
109 
110   /// Return the location from a given item. Fails and returns -1 if either
111   /// the given item is null or should be invisible).
112   /// \sa locationFromIndex(), invisibleRootItem()
113   int locationFromItem(QStandardItem* nodeItem)const;
114 
115   inline QModelIndex indexFromArray(vtkAbstractArray* array, int column = 0)const;
116   QStandardItem* itemFromArray(vtkAbstractArray* array, int column = 0)const;
117   QModelIndexList indexes(vtkAbstractArray* array)const;
118 
119 protected Q_SLOTS:
120   void onDataSetModified(vtkObject* dataSet);
121   void onDataSetPointDataModified(vtkObject* dataSetPointData);
122   void onDataSetCellDataModified(vtkObject* dataSetCellData);
123   void onArrayModified(vtkObject* array);
124   void onItemChanged(QStandardItem * item);
125 
126 protected:
127 
128   ctkVTKDataSetModel(ctkVTKDataSetModelPrivate* pimpl, QObject *parent=0);
129 
130   virtual void insertArray(vtkAbstractArray* array, int location);
131   virtual void insertArray(vtkAbstractArray* array, int location, int row);
132   virtual void updateItemFromArray(QStandardItem* item, vtkAbstractArray* array, int location, int column);
133   virtual void updateArrayFromItem(vtkAbstractArray* array, QStandardItem* item);
134   virtual void updateDataSet();
135   virtual void populateDataSet();
136   virtual void insertNullItem();
137   virtual void removeNullItem();
138 
139 protected:
140   QScopedPointer<ctkVTKDataSetModelPrivate> d_ptr;
141   int NullItemLocation;
142 
143 private:
144   Q_DECLARE_PRIVATE(ctkVTKDataSetModel);
145   Q_DISABLE_COPY(ctkVTKDataSetModel);
146 };
147 Q_DECLARE_OPERATORS_FOR_FLAGS(ctkVTKDataSetModel::AttributeTypes);
148 
149 // -----------------------------------------------------------------------------
arrayFromIndex(const QModelIndex & nodeIndex)150 vtkAbstractArray* ctkVTKDataSetModel::arrayFromIndex(const QModelIndex &nodeIndex)const
151 {
152   return this->arrayFromItem(this->itemFromIndex(nodeIndex));
153 }
154 
155 // -----------------------------------------------------------------------------
locationFromIndex(const QModelIndex & nodeIndex)156 int ctkVTKDataSetModel::locationFromIndex(const QModelIndex &nodeIndex)const
157 {
158   return this->locationFromItem(this->itemFromIndex(nodeIndex));
159 }
160 
161 // -----------------------------------------------------------------------------
indexFromArray(vtkAbstractArray * array,int column)162 QModelIndex ctkVTKDataSetModel::indexFromArray(vtkAbstractArray* array, int column)const
163 {
164   QStandardItem* item = this->itemFromArray(array, column);
165   return item ? item->index() : QModelIndex();
166 }
167 
168 #endif
169