ParaView
pqFlatTreeView.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: pqFlatTreeView.h
5 
6  Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
7  All rights reserved.
8 
9  ParaView is a free software; you can redistribute it and/or modify it
10  under the terms of the ParaView license version 1.2.
11 
12  See License_v1.2.txt for the full ParaView license.
13  A copy of this license can be obtained by contacting
14  Kitware Inc.
15  28 Corporate Drive
16  Clifton Park, NY 12065
17  USA
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 =========================================================================*/
32 
38 #ifndef _pqFlatTreeView_h
39 #define _pqFlatTreeView_h
40 
41 #include "pqWidgetsModule.h"
42 #include <QAbstractScrollArea>
43 #include <QModelIndex> // Needed for return type
44 #include <QStyleOptionViewItem> // Needed for return type
45 
46 class pqFlatTreeViewItem;
47 class pqFlatTreeViewItemRows;
48 class pqFlatTreeViewInternal;
49 
50 class QAbstractItemModel;
51 class QColor;
52 class QFontMetrics;
53 class QHeaderView;
54 class QItemSelection;
55 class QItemSelectionModel;
56 class QPoint;
57 class QRect;
58 
74 class PQWIDGETS_EXPORT pqFlatTreeView : public QAbstractScrollArea
75 {
76  Q_OBJECT
77 
78 public:
80  {
83  SelectColumns
84  };
85 
87  {
90  ExtendedSelection
91  };
92 
93 public:
99  pqFlatTreeView(QWidget* parent = 0);
100  virtual ~pqFlatTreeView();
101 
112  virtual bool eventFilter(QObject* object, QEvent* e);
113 
118  QAbstractItemModel* getModel() const { return this->Model; }
119  void setModel(QAbstractItemModel* model);
120 
121  QModelIndex getRootIndex() const;
122  void setRootIndex(const QModelIndex& index);
124 
129  QItemSelectionModel* getSelectionModel() const { return this->Selection; }
130  void setSelectionModel(QItemSelectionModel* selectionModel);
131 
132  SelectionBehavior getSelectionBehavior() const { return this->Behavior; }
133  void setSelectionBehavior(SelectionBehavior behavior);
134 
135  SelectionMode getSelectionMode() const { return this->Mode; }
136  void setSelectionMode(SelectionMode mode);
138 
143  QHeaderView* getHeader() const { return this->HeaderView; }
144  void setHeader(QHeaderView* headerView);
145 
157  bool isColumnSizeManaged() const { return this->ManageSizes; }
158 
166  void setColumnSizeManaged(bool managed);
168 
173  int getIconSize() const;
174  void setIconSize(int iconSize);
176 
181  bool isIndexHidden(const QModelIndex& index) const;
182  void getVisibleRect(const QModelIndex& index, QRect& area) const;
183  QModelIndex getIndexVisibleAt(const QPoint& point) const;
184  QModelIndex getIndexCellAt(const QPoint& point) const;
185  void getSelectionIn(const QRect& rect, QItemSelection& items) const;
187 
192  bool isIndexExpanded(const QModelIndex& index) const;
193  QModelIndex getNextVisibleIndex(
194  const QModelIndex& index, const QModelIndex& root = QModelIndex()) const;
195  QModelIndex getRelativeIndex(const QString& id, const QModelIndex& root = QModelIndex()) const;
196  void getRelativeIndexId(
197  const QModelIndex& index, QString& id, const QModelIndex& root = QModelIndex()) const;
199 
204  bool startEditing(const QModelIndex& index);
205  void finishEditing();
206  void cancelEditing();
208 
209 signals:
210  void activated(const QModelIndex& index);
211  void clicked(const QModelIndex& index);
212 
213 public slots:
214  void reset();
215  void selectAll();
216  void setCurrentIndex(const QModelIndex& index);
217  void expandAll();
218  void expand(const QModelIndex& index);
219  void collapse(const QModelIndex& index);
220  void scrollTo(const QModelIndex& index);
221 
222 protected slots:
227  void insertRows(const QModelIndex& parent, int start, int end);
228  void startRowRemoval(const QModelIndex& parent, int start, int end);
229  void finishRowRemoval(const QModelIndex& parent, int start, int end);
230  void insertColumns(const QModelIndex& parent, int start, int end);
231  void startColumnRemoval(const QModelIndex& parent, int start, int end);
232  void finishColumnRemoval(const QModelIndex& parent, int start, int end);
233  void updateData(const QModelIndex& topLeft, const QModelIndex& bottomRight);
235 
236 protected:
241  virtual void keyPressEvent(QKeyEvent* e);
242  void keyboardSearch(const QString& search);
244 
249  virtual void mousePressEvent(QMouseEvent* e);
250  virtual void mouseMoveEvent(QMouseEvent* e);
251  virtual void mouseReleaseEvent(QMouseEvent* e);
252  virtual void mouseDoubleClickEvent(QMouseEvent* e);
253  virtual void wheelEvent(QWheelEvent* e);
255 
260  virtual bool event(QEvent* e);
262 
263  int horizontalOffset() const;
264  int verticalOffset() const;
265 
266  virtual void resizeEvent(QResizeEvent* e);
267  virtual bool viewportEvent(QEvent* e);
268  virtual void paintEvent(QPaintEvent* e);
269  QStyleOptionViewItem getViewOptions() const;
270 
271 private slots:
276  void handleSectionResized(int index, int oldSize, int newSize);
277  void handleSectionMoved(int index, int oldVisual, int newVisual);
279 
284  void changeCurrent(const QModelIndex& current, const QModelIndex& previous);
285  void changeCurrentRow(const QModelIndex& current, const QModelIndex& previous);
286  void changeCurrentColumn(const QModelIndex& current, const QModelIndex& previous);
287  void changeSelection(const QItemSelection& selected, const QItemSelection& deselected);
289 
290 private:
291  void resetRoot();
292  void resetPreferredSizes();
293 
298  void layoutEditor();
299  void layoutItems();
300  void layoutItem(pqFlatTreeViewItem* item, int& point, const QFontMetrics& fm);
301  int getDataWidth(const QModelIndex& index, const QFontMetrics& fm) const;
302  int getWidthSum(pqFlatTreeViewItem* item, int column) const;
303  bool updateContentsWidth();
304  void updateScrollBars();
305  void addChildItems(pqFlatTreeViewItem* item, int parentChildCount);
307 
312  bool getIndexRowList(const QModelIndex& index, pqFlatTreeViewItemRows& rowList) const;
313  pqFlatTreeViewItem* getItem(const pqFlatTreeViewItemRows& rowList) const;
314  pqFlatTreeViewItem* getItem(const QModelIndex& index) const;
315  pqFlatTreeViewItem* getItemAt(int contentsY) const;
316  pqFlatTreeViewItem* getNextItem(pqFlatTreeViewItem* item) const;
317  pqFlatTreeViewItem* getNextVisibleItem(pqFlatTreeViewItem* item) const;
318  pqFlatTreeViewItem* getPreviousVisibleItem(pqFlatTreeViewItem* item) const;
319  pqFlatTreeViewItem* getLastVisibleItem() const;
321 
322  void expandItem(pqFlatTreeViewItem* item);
323 
324  void getSelectionIn(
325  const QModelIndex& topLeft, const QModelIndex& bottomRight, QItemSelection& items) const;
326 
327  void drawBranches(QPainter& painter, pqFlatTreeViewItem* item, int halfIndent,
328  const QColor& branchColor, const QColor& expandColor, const QStyleOptionViewItem& options);
329  bool drawDecoration(QPainter& painter, int px, int py, const QModelIndex& index,
330  const QStyleOptionViewItem& options, int itemHeight);
331  void drawData(QPainter& painter, int px, int py, const QModelIndex& index,
332  const QStyleOptionViewItem& options, int itemHeight, int itemWidth, int columnWidth,
333  bool selected);
334  void drawFocus(
335  QPainter& painter, const QRect& cell, const QStyleOptionViewItem& options, bool selected);
336 
337 private:
338  QAbstractItemModel* Model;
339  QItemSelectionModel* Selection;
340  SelectionBehavior Behavior;
341  SelectionMode Mode;
342  QHeaderView* HeaderView;
343  pqFlatTreeViewItem* Root;
344  pqFlatTreeViewInternal* Internal;
345  int IconSize;
346  int IndentWidth;
347  int ContentsWidth;
348  int ContentsHeight;
349  int TextMargin;
350  int DoubleTextMargin;
351  bool FontChanged;
352  bool ManageSizes;
353  bool InUpdateWidth;
354  bool HeaderOwned;
355  bool SelectionOwned;
356 
357  static int PipeLength;
358 };
359 
360 #endif
QItemSelectionModel * getSelectionModel() const
QHeaderView * getHeader() const
Gets whether or not the column size is managed by the view.
SelectionMode getSelectionMode() const
QAbstractItemModel * getModel() const
The pqFlatTreeView class is used to display a flattened tree view of a hierarchical model...
SelectionBehavior getSelectionBehavior() const
bool isColumnSizeManaged() const
Gets whether or not the column size is managed by the view.