pqFlatTreeView.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3 // SPDX-License-Identifier: BSD-3-Clause
4 
10 #ifndef pqFlatTreeView_h
11 #define pqFlatTreeView_h
12 
13 #include "pqWidgetsModule.h"
14 #include <QAbstractScrollArea>
15 #include <QModelIndex> // Needed for return type
16 #include <QStyleOptionViewItem> // Needed for return type
17 
18 class pqFlatTreeViewItem;
19 class pqFlatTreeViewItemRows;
20 class pqFlatTreeViewInternal;
21 
22 class QAbstractItemModel;
23 class QColor;
24 class QFontMetrics;
25 class QHeaderView;
26 class QItemSelection;
27 class QItemSelectionModel;
28 class QPoint;
29 class QRect;
30 
46 class PQWIDGETS_EXPORT pqFlatTreeView : public QAbstractScrollArea
47 {
48  Q_OBJECT
49 
50 public:
52  {
55  SelectColumns
56  };
57 
59  {
62  ExtendedSelection
63  };
64 
70  pqFlatTreeView(QWidget* parent = nullptr);
71  ~pqFlatTreeView() override;
72 
83  bool eventFilter(QObject* object, QEvent* e) override;
84 
88  QAbstractItemModel* getModel() const { return this->Model; }
90  void setModel(QAbstractItemModel* model);
91 
92  QModelIndex getRootIndex() const;
93  void setRootIndex(const QModelIndex& index);
95 
99  QItemSelectionModel* getSelectionModel() const { return this->Selection; }
101  void setSelectionModel(QItemSelectionModel* selectionModel);
102 
103  SelectionBehavior getSelectionBehavior() const { return this->Behavior; }
104  void setSelectionBehavior(SelectionBehavior behavior);
105 
106  SelectionMode getSelectionMode() const { return this->Mode; }
107  void setSelectionMode(SelectionMode mode);
109 
113  QHeaderView* getHeader() const { return this->HeaderView; }
115  void setHeader(QHeaderView* headerView);
116 
128  bool isColumnSizeManaged() const { return this->ManageSizes; }
129 
137  void setColumnSizeManaged(bool managed);
139 
143  int getIconSize() const;
145  void setIconSize(int iconSize);
147 
151  bool isIndexHidden(const QModelIndex& index) const;
153  void getVisibleRect(const QModelIndex& index, QRect& area) const;
154  QModelIndex getIndexVisibleAt(const QPoint& point) const;
155  QModelIndex getIndexCellAt(const QPoint& point) const;
156  void getSelectionIn(const QRect& rect, QItemSelection& items) const;
158 
162  bool isIndexExpanded(const QModelIndex& index) const;
164  QModelIndex getNextVisibleIndex(
165  const QModelIndex& index, const QModelIndex& root = QModelIndex()) const;
166  QModelIndex getRelativeIndex(const QString& id, const QModelIndex& root = QModelIndex()) const;
167  void getRelativeIndexId(
168  const QModelIndex& index, QString& id, const QModelIndex& root = QModelIndex()) const;
170 
174  bool startEditing(const QModelIndex& index);
176  void finishEditing();
177  void cancelEditing();
179 
180 Q_SIGNALS:
181  void activated(const QModelIndex& index);
182  void clicked(const QModelIndex& index);
183 
184 public Q_SLOTS:
185  void reset();
186  void selectAll();
187  void setCurrentIndex(const QModelIndex& index);
188  void expandAll();
189  void expand(const QModelIndex& index);
190  void collapse(const QModelIndex& index);
191  void scrollTo(const QModelIndex& index);
192 
193 protected Q_SLOTS:
197  void insertRows(const QModelIndex& parent, int start, int end);
199  void startRowRemoval(const QModelIndex& parent, int start, int end);
200  void finishRowRemoval(const QModelIndex& parent, int start, int end);
201  void insertColumns(const QModelIndex& parent, int start, int end);
202  void startColumnRemoval(const QModelIndex& parent, int start, int end);
203  void finishColumnRemoval(const QModelIndex& parent, int start, int end);
204  void updateData(const QModelIndex& topLeft, const QModelIndex& bottomRight);
206 
207 protected: // NOLINT(readability-redundant-access-specifiers)
211  void keyPressEvent(QKeyEvent* e) override;
213  void keyboardSearch(const QString& search);
215 
219  void mousePressEvent(QMouseEvent* e) override;
221  void mouseMoveEvent(QMouseEvent* e) override;
222  void mouseReleaseEvent(QMouseEvent* e) override;
223  void mouseDoubleClickEvent(QMouseEvent* e) override;
225 
229  bool event(QEvent* e) override;
232 
233  int horizontalOffset() const;
234  int verticalOffset() const;
235 
236  void resizeEvent(QResizeEvent* e) override;
237  bool viewportEvent(QEvent* e) override;
238  void paintEvent(QPaintEvent* e) override;
239  QStyleOptionViewItem getViewOptions() const;
240 
241 private Q_SLOTS:
245  void handleSectionResized(int index, int oldSize, int newSize);
247  void handleSectionMoved(int index, int oldVisual, int newVisual);
249 
253  void changeCurrent(const QModelIndex& current, const QModelIndex& previous);
255  void changeCurrentRow(const QModelIndex& current, const QModelIndex& previous);
256  void changeCurrentColumn(const QModelIndex& current, const QModelIndex& previous);
257  void changeSelection(const QItemSelection& selected, const QItemSelection& deselected);
259 
260 private: // NOLINT(readability-redundant-access-specifiers)
261  void resetRoot();
262  void resetPreferredSizes();
263 
267  void layoutEditor();
269  void layoutItems();
270  void layoutItem(pqFlatTreeViewItem* item, int& point, const QFontMetrics& fm);
271  int getDataWidth(const QModelIndex& index, const QFontMetrics& fm) const;
272  int getWidthSum(pqFlatTreeViewItem* item, int column) const;
273  bool updateContentsWidth();
274  void updateScrollBars();
275  void addChildItems(pqFlatTreeViewItem* item, int parentChildCount);
277 
281  bool getIndexRowList(const QModelIndex& index, pqFlatTreeViewItemRows& rowList) const;
283  pqFlatTreeViewItem* getItem(const pqFlatTreeViewItemRows& rowList) const;
284  pqFlatTreeViewItem* getItem(const QModelIndex& index) const;
285  pqFlatTreeViewItem* getItemAt(int contentsY) const;
286  pqFlatTreeViewItem* getNextItem(pqFlatTreeViewItem* item) const;
287  pqFlatTreeViewItem* getNextVisibleItem(pqFlatTreeViewItem* item) const;
288  pqFlatTreeViewItem* getPreviousVisibleItem(pqFlatTreeViewItem* item) const;
289  pqFlatTreeViewItem* getLastVisibleItem() const;
291 
292  void expandItem(pqFlatTreeViewItem* item);
293 
294  void getSelectionIn(
295  const QModelIndex& topLeft, const QModelIndex& bottomRight, QItemSelection& items) const;
296 
297  void drawBranches(QPainter& painter, pqFlatTreeViewItem* item, int halfIndent,
298  const QColor& branchColor, const QColor& expandColor, const QStyleOptionViewItem& options);
299  bool drawDecoration(QPainter& painter, int px, int py, const QModelIndex& index,
300  const QStyleOptionViewItem& options, int itemHeight);
301  void drawData(QPainter& painter, int px, int py, const QModelIndex& index,
302  const QStyleOptionViewItem& options, int itemHeight, int itemWidth, int columnWidth,
303  bool selected);
304  void drawFocus(
305  QPainter& painter, const QRect& cell, const QStyleOptionViewItem& options, bool selected);
306 
307  QAbstractItemModel* Model;
308  QItemSelectionModel* Selection;
309  SelectionBehavior Behavior;
310  SelectionMode Mode;
311  QHeaderView* HeaderView;
312  pqFlatTreeViewItem* Root;
313  pqFlatTreeViewInternal* Internal;
314  int IconSize;
315  int IndentWidth;
316  int ContentsWidth;
317  int ContentsHeight;
318  int TextMargin;
319  int DoubleTextMargin;
320  bool FontChanged;
321  bool ManageSizes;
322  bool InUpdateWidth;
323  bool HeaderOwned;
324  bool SelectionOwned;
325 
326  static int PipeLength;
327 };
328 
329 #endif
previous
SelectionMode getSelectionMode() const
The pqFlatTreeView class is used to display a flattened tree view of a hierarchical model...
#define PQWIDGETS_EXPORT
mode
SelectionBehavior getSelectionBehavior() const
bool isColumnSizeManaged() const
Gets whether or not the column size is managed by the view.
point
index