ctkVTKDataSetModel.h 5.8 KB

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