ctkVTKDataSetModel.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.s
  42. /// By default all attributes are considered.
  43. /// \sa ctkVTKDataSetModel::AllAttribute
  44. Q_PROPERTY(AttributeTypes attributeTypes READ attributeTypes WRITE setAttributeTypes)
  45. public:
  46. typedef ctkVTKDataSetModel Self;
  47. typedef QStandardItemModel Superclass;
  48. ctkVTKDataSetModel(QObject *parent=0);
  49. virtual ~ctkVTKDataSetModel();
  50. enum AttributeType
  51. {
  52. NoAttribute = 0x1,
  53. ScalarsAttribute = 0x2,
  54. VectorsAttribute = 0x4,
  55. NormalsAttribute = 0x8,
  56. TCoordsAttribute = 0x10,
  57. TensorsAttribute = 0x20,
  58. GlobalIDsAttribute = 0x40,
  59. PedigreeIDsAttribute = 0x80,
  60. EdgeFlagAttribute = 0x100,
  61. AllAttribute = NoAttribute | ScalarsAttribute | VectorsAttribute | NormalsAttribute | TCoordsAttribute | TensorsAttribute | GlobalIDsAttribute | PedigreeIDsAttribute | EdgeFlagAttribute
  62. };
  63. Q_DECLARE_FLAGS(AttributeTypes, AttributeType)
  64. virtual void setDataSet(vtkDataSet* dataSet);
  65. vtkDataSet* dataSet()const;
  66. AttributeTypes attributeTypes()const;
  67. void setAttributeTypes(const AttributeTypes& attributeTypes);
  68. /// Return the vtkAbstractArray associated to the index.
  69. /// 0 if the index doesn't contain a vtkAbstractArray
  70. inline vtkAbstractArray* arrayFromIndex(const QModelIndex& arrayIndex)const;
  71. /// Return the location from a given item. Fails and returns -1 if either
  72. /// the given index points to a null item or an invisible item.
  73. /// \sa locationFromItem()
  74. inline int locationFromIndex(const QModelIndex& arrayIndex)const;
  75. vtkAbstractArray* arrayFromItem(QStandardItem* nodeItem)const;
  76. /// Return the location from a given item. Fails and returns -1 if either
  77. /// the given item is null or should be invisible).
  78. /// \sa locationFromIndex(), invisibleRootItem()
  79. int locationFromItem(QStandardItem* nodeItem)const;
  80. inline QModelIndex indexFromArray(vtkAbstractArray* array, int column = 0)const;
  81. QStandardItem* itemFromArray(vtkAbstractArray* array, int column = 0)const;
  82. QModelIndexList indexes(vtkAbstractArray* array)const;
  83. protected Q_SLOTS:
  84. void onDataSetModified(vtkObject* dataSet);
  85. void onArrayModified(vtkObject* array);
  86. void onItemChanged(QStandardItem * item);
  87. protected:
  88. ctkVTKDataSetModel(ctkVTKDataSetModelPrivate* pimpl, QObject *parent=0);
  89. virtual void insertArray(vtkAbstractArray* array, int location);
  90. virtual void insertArray(vtkAbstractArray* array, int location, int row);
  91. virtual void updateItemFromArray(QStandardItem* item, vtkAbstractArray* array, int location, int column);
  92. virtual void updateArrayFromItem(vtkAbstractArray* array, QStandardItem* item);
  93. virtual void updateDataSet();
  94. virtual void populateDataSet();
  95. protected:
  96. QScopedPointer<ctkVTKDataSetModelPrivate> d_ptr;
  97. private:
  98. Q_DECLARE_PRIVATE(ctkVTKDataSetModel);
  99. Q_DISABLE_COPY(ctkVTKDataSetModel);
  100. };
  101. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkVTKDataSetModel::AttributeTypes);
  102. // -----------------------------------------------------------------------------
  103. vtkAbstractArray* ctkVTKDataSetModel::arrayFromIndex(const QModelIndex &nodeIndex)const
  104. {
  105. return this->arrayFromItem(this->itemFromIndex(nodeIndex));
  106. }
  107. // -----------------------------------------------------------------------------
  108. int ctkVTKDataSetModel::locationFromIndex(const QModelIndex &nodeIndex)const
  109. {
  110. return this->locationFromItem(this->itemFromIndex(nodeIndex));
  111. }
  112. // -----------------------------------------------------------------------------
  113. QModelIndex ctkVTKDataSetModel::indexFromArray(vtkAbstractArray* array, int column)const
  114. {
  115. QStandardItem* item = this->itemFromArray(array, column);
  116. return item ? item->index() : QModelIndex();
  117. }
  118. #endif