Slicer  4.11
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
qMRMLSceneModel.h
Go to the documentation of this file.
1 /*==============================================================================
2 
3  Program: 3D Slicer
4 
5  Copyright (c) Kitware Inc.
6 
7  See COPYRIGHT.txt
8  or http://www.slicer.org/copyright/copyright.txt for details.
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16  This file was originally developed by Julien Finet, Kitware Inc.
17  and was partially funded by NIH grant 3P41RR013218-12S1
18 
19 ==============================================================================*/
20 
21 #ifndef __qMRMLSceneModel_h
22 #define __qMRMLSceneModel_h
23 
24 // Qt includes
25 #include <QStandardItemModel>
26 
27 // CTK includes
28 #include <ctkPimpl.h>
29 #include <ctkVTKObject.h>
30 
31 // qMRML includes
32 #include "qMRMLWidgetsExport.h"
33 
34 class vtkMRMLNode;
35 class vtkMRMLScene;
36 
37 class qMRMLSceneModelPrivate;
38 
57 class QMRML_WIDGETS_EXPORT qMRMLSceneModel : public QStandardItemModel
58 {
59  Q_OBJECT
60  QVTK_OBJECT
61 
62  Q_ENUMS(NodeTypes)
63 
64 
65  Q_PROPERTY (NodeTypes listenNodeModifiedEvent READ listenNodeModifiedEvent WRITE setListenNodeModifiedEvent)
70 
71 
72  Q_PROPERTY (bool lazyUpdate READ lazyUpdate WRITE setLazyUpdate)
76 
77 
78  Q_PROPERTY (int nameColumn READ nameColumn WRITE setNameColumn)
83  Q_PROPERTY (int idColumn READ idColumn WRITE setIDColumn)
86  Q_PROPERTY (int checkableColumn READ checkableColumn WRITE setCheckableColumn)
89  Q_PROPERTY (int visibilityColumn READ visibilityColumn WRITE setVisibilityColumn)
92  Q_PROPERTY (int toolTipNameColumn READ toolTipNameColumn WRITE setToolTipNameColumn)
95  Q_PROPERTY( int extraItemColumn READ extraItemColumn WRITE setExtraItemColumn)
96 public:
97  typedef QStandardItemModel Superclass;
98  qMRMLSceneModel(QObject *parent=nullptr);
99  ~qMRMLSceneModel() override;
100 
102  {
103  NoNodes = 0,
105  OnlyVisibleNodes
106  };
108  {
110  UIDRole = Qt::UserRole + 1,
119  LastRole
120  };
121 
123  Q_INVOKABLE virtual void setMRMLScene(vtkMRMLScene* scene);
124  Q_INVOKABLE vtkMRMLScene* mrmlScene()const;
125 
127  QStandardItem* mrmlSceneItem()const;
128 
130  QModelIndex mrmlSceneIndex()const;
131 
134  inline vtkMRMLNode* mrmlNodeFromIndex(const QModelIndex &nodeIndex)const;
135  vtkMRMLNode* mrmlNodeFromItem(QStandardItem* nodeItem)const;
136  QModelIndex indexFromNode(vtkMRMLNode* node, int column = 0)const;
137  // Utility function
138  QStandardItem* itemFromNode(vtkMRMLNode* node, int column = 0)const;
139  // Return all the QModelIndexes (all the columns) for a given node
140  QModelIndexList indexes(vtkMRMLNode* node)const;
141 
146  void setListenNodeModifiedEvent(NodeTypes nodesToListen);
149  NodeTypes listenNodeModifiedEvent()const;
150 
151  bool lazyUpdate()const;
152  void setLazyUpdate(bool lazy);
153 
154  int nameColumn()const;
155  void setNameColumn(int column);
156 
157  int idColumn()const;
158  void setIDColumn(int column);
159 
160  int checkableColumn()const;
161  void setCheckableColumn(int column);
162 
163  int visibilityColumn()const;
164  void setVisibilityColumn(int column);
165 
166  int toolTipNameColumn()const;
167  void setToolTipNameColumn(int column);
168 
169  int extraItemColumn()const;
170  void setExtraItemColumn(int column);
171 
174  void setPreItems(const QStringList& extraItems, QStandardItem* parent);
175  QStringList preItems(QStandardItem* parent)const;
176 
179  void setPostItems(const QStringList& extraItems, QStandardItem* parent);
180  QStringList postItems(QStandardItem* parent)const;
181 
184  Qt::DropActions supportedDropActions()const override;
185  QMimeData* mimeData(const QModelIndexList& indexes)const override;
186  bool dropMimeData(const QMimeData *data, Qt::DropAction action,
187  int row, int column, const QModelIndex &parent) override;
188 
194  virtual vtkMRMLNode* parentNode(vtkMRMLNode* node)const;
198  virtual int nodeIndex(vtkMRMLNode* node)const;
200  virtual bool canBeAChild(vtkMRMLNode* node)const;
203  virtual bool canBeAParent(vtkMRMLNode* node)const;
206  virtual bool reparent(vtkMRMLNode* node, vtkMRMLNode* newParent);
210  bool isParentNode(vtkMRMLNode* child, vtkMRMLNode* parent)const;
214  bool isAffiliatedNode(vtkMRMLNode* nodeA, vtkMRMLNode* nodeB)const;
215 
218  virtual void observeNode(vtkMRMLNode* node);
219 
220 protected slots:
221 
222  virtual void onMRMLSceneNodeAboutToBeAdded(vtkMRMLScene* scene, vtkMRMLNode* node);
223  virtual void onMRMLSceneNodeAboutToBeRemoved(vtkMRMLScene* scene, vtkMRMLNode* node);
224  virtual void onMRMLSceneNodeAdded(vtkMRMLScene* scene, vtkMRMLNode* node);
225  virtual void onMRMLSceneNodeRemoved(vtkMRMLScene* scene, vtkMRMLNode* node);
226 
227  virtual void onMRMLSceneAboutToBeImported(vtkMRMLScene* scene);
228  virtual void onMRMLSceneImported(vtkMRMLScene* scene);
229  virtual void onMRMLSceneAboutToBeClosed(vtkMRMLScene* scene);
230  virtual void onMRMLSceneClosed(vtkMRMLScene* scene);
231  virtual void onMRMLSceneStartBatchProcess(vtkMRMLScene* scene);
232  virtual void onMRMLSceneEndBatchProcess(vtkMRMLScene* scene);
233 
234  void onMRMLSceneDeleted(vtkObject* scene);
235 
236  void onMRMLNodeModified(vtkObject* node);
239  void onMRMLNodeIDChanged(vtkObject* node, void* callData);
240  virtual void onItemChanged(QStandardItem * item);
241  virtual void delayedItemChanged();
242 
246  void updateColumnCount();
247 
248 signals:
251  void aboutToReparentByDragAndDrop(vtkMRMLNode* node, vtkMRMLNode* newParent);
252 
255  void reparentedByDragAndDrop(vtkMRMLNode* node, vtkMRMLNode* newParent);
256 
258  void sceneAboutToBeUpdated();
259 
261  void sceneUpdated();
262 
263 protected:
264  qMRMLSceneModel(qMRMLSceneModelPrivate* pimpl, QObject *parent=nullptr);
265 
266  virtual void updateScene();
267  virtual void populateScene();
268  virtual QStandardItem* insertNode(vtkMRMLNode* node);
269  virtual QStandardItem* insertNode(vtkMRMLNode* node, QStandardItem* parent, int row = -1);
270 
271  virtual bool isANode(const QStandardItem* item)const;
272  virtual QFlags<Qt::ItemFlag> nodeFlags(vtkMRMLNode* node, int column)const;
273 
275  void updateNodeItems();
276 
281  virtual void updateItemFromNode(QStandardItem* item, vtkMRMLNode* node, int column);
282 
298  virtual void updateItemDataFromNode(QStandardItem* item, vtkMRMLNode* node, int column);
303  virtual void updateNodeFromItem(vtkMRMLNode* node, QStandardItem* item);
319  virtual void updateNodeFromItemData(vtkMRMLNode* node, QStandardItem* item);
320 
322  void updateNodeItems(vtkMRMLNode* node, const QString& uid);
323 
324  static void onMRMLSceneEvent(vtkObject* vtk_obj, unsigned long event,
325  void* client_data, void* call_data);
326 
328  virtual int maxColumnId()const;
329 protected:
330  QScopedPointer<qMRMLSceneModelPrivate> d_ptr;
331 
332 private:
333  Q_DECLARE_PRIVATE(qMRMLSceneModel);
334  Q_DISABLE_COPY(qMRMLSceneModel);
335 };
337 
338 void printStandardItem(QStandardItem* item, const QString& offset);
339 
340 // -----------------------------------------------------------------------------
341 vtkMRMLNode* qMRMLSceneModel::mrmlNodeFromIndex(const QModelIndex &nodeIndex)const
342 {
343  return this->mrmlNodeFromItem(this->itemFromIndex(nodeIndex));
344 }
345 
346 #endif
Pointer (as long long) of the item if it is a scene or a node.
virtual int nodeIndex(vtkMRMLNode *node) const
vtkMRMLNode * mrmlNodeFromItem(QStandardItem *nodeItem) const
QScopedPointer< qMRMLSceneModelPrivate > d_ptr
Q_DECLARE_METATYPE(QList< QVariantMap >)
A set of MRML Nodes that supports serialization and undo/redo.
Definition: vtkMRMLScene.h:56
vtkMRMLNode * mrmlNodeFromIndex(const QModelIndex &nodeIndex) const
void printStandardItem(QStandardItem *item, const QString &offset)
Map (QMap<QString, QVariant>) for parent items of extra items.
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:139
QStandardItemModel Superclass