1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkQtTreeView.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /*-------------------------------------------------------------------------
16   Copyright 2008 Sandia Corporation.
17   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18   the U.S. Government retains certain rights in this software.
19 -------------------------------------------------------------------------*/
20 /**
21  * @class   vtkQtTreeView
22  * @brief   A VTK view based on a Qt tree view.
23  *
24  *
25  * vtkQtTreeView is a VTK view using an underlying QTreeView.
26  *
27  * @par Thanks:
28  * Thanks to Brian Wylie from Sandia National Laboratories for implementing
29  * this class
30  */
31 
32 #ifndef vtkQtTreeView_h
33 #define vtkQtTreeView_h
34 
35 #include "vtkQtView.h"
36 #include "vtkViewsQtModule.h" // For export macro
37 
38 #include "vtkSmartPointer.h" // Needed for member variables
39 #include <QList>             // Needed for member variables
40 #include <QPointer>          // Needed for member variables
41 
42 class QAbstractItemDelegate;
43 class QAbstractItemView;
44 class QFilterTreeProxyModel;
45 class QColumnView;
46 class QItemSelection;
47 class QModelIndex;
48 class QTreeView;
49 class vtkApplyColors;
50 class QVBoxLayout;
51 class vtkQtTreeModelAdapter;
52 class QItemSelectionModel;
53 
54 class VTKVIEWSQT_EXPORT vtkQtTreeView : public vtkQtView
55 {
56   Q_OBJECT
57 
58 Q_SIGNALS:
59   void expanded(const QModelIndex&);
60   void collapsed(const QModelIndex&);
61   void updatePreviewWidget(const QModelIndex&);
62 
63 public:
64   static vtkQtTreeView* New();
65   vtkTypeMacro(vtkQtTreeView, vtkQtView);
66   void PrintSelf(ostream& os, vtkIndent indent) override;
67 
68   /**
69    * Get the main container of this view (a  QWidget).
70    * The application typically places the view with a call
71    * to GetWidget(): something like this
72    * this->ui->box->layout()->addWidget(this->View->GetWidget());
73    */
74   QWidget* GetWidget() override;
75 
76   /**
77    * Have the view show/hide its column headers (default is ON)
78    */
79   void SetShowHeaders(bool);
80 
81   /**
82    * Have the view alternate its row colors (default is OFF)
83    */
84   void SetAlternatingRowColors(bool);
85 
86   /**
87    * Have the view alternate its row colors (default is OFF)
88    */
89   void SetEnableDragDrop(bool);
90 
91   /**
92    * Show the root node of the tree (default is OFF)
93    */
94   void SetShowRootNode(bool);
95 
96   /**
97    * Hide the column of the given index from being shown in the view
98    */
99   void HideColumn(int i);
100 
101   /**
102    * Show the column of the given index in the view
103    */
104   void ShowColumn(int i);
105 
106   /**
107    * Hide all but the first column in the view
108    */
109   void HideAllButFirstColumn();
110 
111   /**
112    * The column used to filter on
113    */
114   void SetFilterColumn(int i);
115 
116   /**
117    * The column used to filter on
118    */
119 #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
120   void SetFilterRegExp(const QRegularExpression& pattern);
121 #else
122   void SetFilterRegExp(const QRegExp& pattern);
123 #endif
124 
125   /**
126    * The column used to filter on
127    */
128   void SetFilterTreeLevel(int level);
129 
130   /**
131    * Collapses the model item specified by the index.
132    */
133   void Collapse(const QModelIndex& index);
134 
135   /**
136    * Collapses all expanded items.
137    */
138   void CollapseAll();
139 
140   /**
141    * Expands the model item specified by the index.
142    */
143   void Expand(const QModelIndex& index);
144 
145   /**
146    * Expands all expandable items.
147    * Warning: if the model contains a large number of items,
148    * this function will take some time to execute.
149    */
150   void ExpandAll();
151 
152   /**
153    * Expands all expandable items to the given depth.
154    */
155   void ExpandToDepth(int depth);
156 
157   /**
158    * Resizes the column given to the size of its contents.
159    */
160   void ResizeColumnToContents(int column);
161 
162   /**
163    * Set whether to use a QColumnView (QTreeView is the default)
164    */
165   void SetUseColumnView(int state);
166 
167   /**
168    * Updates the view.
169    */
170   void Update() override;
171 
172   /**
173    * Set item delegate to something custom
174    */
175   void SetItemDelegate(QAbstractItemDelegate* delegate);
176 
177   ///@{
178   /**
179    * The array to use for coloring items in view.  Default is "color".
180    */
181   void SetColorArrayName(const char* name);
182   const char* GetColorArrayName();
183   ///@}
184 
185   ///@{
186   /**
187    * Whether to color vertices.  Default is off.
188    */
189   void SetColorByArray(bool vis);
190   bool GetColorByArray();
191   vtkBooleanMacro(ColorByArray, bool);
192   ///@}
193 
194   void ApplyViewTheme(vtkViewTheme* theme) override;
195 
196 protected:
197   vtkQtTreeView();
198   ~vtkQtTreeView() override;
199 
200   void AddRepresentationInternal(vtkDataRepresentation* rep) override;
201   void RemoveRepresentationInternal(vtkDataRepresentation* rep) override;
202 
203 private Q_SLOTS:
204   void slotQtSelectionChanged(const QItemSelection&, const QItemSelection&);
205 
206 private:
207   void SetVTKSelection();
208   vtkMTimeType CurrentSelectionMTime;
209   vtkMTimeType LastInputMTime;
210 
211   vtkSetStringMacro(ColorArrayNameInternal);
212   vtkGetStringMacro(ColorArrayNameInternal);
213 
214   QPointer<QTreeView> TreeView;
215   QPointer<QColumnView> ColumnView;
216   QPointer<QWidget> Widget;
217   QPointer<QVBoxLayout> Layout;
218   QPointer<QItemSelectionModel> SelectionModel;
219   QList<int> HiddenColumns;
220   vtkQtTreeModelAdapter* TreeAdapter;
221   QAbstractItemView* View;
222   char* ColorArrayNameInternal;
223   QFilterTreeProxyModel* TreeFilter;
224 
225   vtkSmartPointer<vtkApplyColors> ApplyColors;
226 
227   vtkQtTreeView(const vtkQtTreeView&) = delete;
228   void operator=(const vtkQtTreeView&) = delete;
229 };
230 
231 #endif
232