]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphintreeview.h
Add unit test for bug 217447.
[dolphin.git] / src / views / dolphintreeview.h
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2008 by Simon St. James <kdedevel@etotheipiplusone.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #ifndef DOLPHINTREEVIEW_H
22 #define DOLPHINTREEVIEW_H
23
24 #include <QTreeView>
25 #include <libdolphin_export.h>
26
27 /**
28 * @brief Extends QTreeView by a custom selection- and hover-mechanism.
29 *
30 * QTreeView does not respect the width of a cell for the hover-feedback
31 * and when selecting items. DolphinTreeView improves this by respecting the
32 * content-width of the first column. The selection-mechanism also
33 * respects the content-width.
34 */
35 class LIBDOLPHINPRIVATE_EXPORT DolphinTreeView : public QTreeView
36 {
37 Q_OBJECT
38
39 public:
40 explicit DolphinTreeView(QWidget* parent = 0);
41 virtual ~DolphinTreeView();
42
43 virtual QModelIndex indexAt (const QPoint& point) const;
44 virtual QRegion visualRegionForSelection(const QItemSelection& selection) const;
45
46 protected:
47 /**
48 * @return True, if the item with the index \p index accepts a drop. In this
49 * case a visual feedback for the user is given during dragging. Per
50 * default false is returned.
51 */
52 virtual bool acceptsDrop(const QModelIndex& index) const;
53
54 virtual bool event(QEvent* event);
55 virtual void mousePressEvent(QMouseEvent* event);
56 virtual void mouseMoveEvent(QMouseEvent* event);
57 virtual void mouseReleaseEvent(QMouseEvent* event);
58 virtual void startDrag(Qt::DropActions supportedActions);
59 virtual void dragEnterEvent(QDragEnterEvent* event);
60 virtual void dragMoveEvent(QDragMoveEvent* event);
61 virtual void dragLeaveEvent(QDragLeaveEvent* event);
62 virtual void paintEvent(QPaintEvent* event);
63 virtual void keyPressEvent(QKeyEvent* event);
64 virtual void keyReleaseEvent(QKeyEvent* event);
65 virtual void currentChanged(const QModelIndex& current, const QModelIndex& previous);
66 virtual void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command);
67 virtual void scrollTo(const QModelIndex& index, ScrollHint hint = EnsureVisible);
68
69 private slots:
70 /**
71 * If the elastic band is currently shown, update the elastic band based on
72 * the current mouse position and ensure that the selection is the set of items
73 * intersecting it.
74 */
75 void updateElasticBandSelection();
76
77 /**
78 * Updates the destination \a destination from
79 * the elastic band to the current mouse position and triggers
80 * an update.
81 */
82 void updateElasticBand();
83
84 /**
85 * Returns the rectangle for the elastic band dependent from the
86 * origin \a origin, the current destination
87 * \a destination and the viewport position.
88 */
89 QRect elasticBandRect() const;
90
91 private:
92 /**
93 * Returns true, if \a pos is within the expanding toggle of a tree.
94 */
95 bool isAboveExpandingToggle(const QPoint& pos) const;
96
97 private:
98 bool m_keyPressed; // true if a key is pressed currently; info used by currentChanged()
99 bool m_expandingTogglePressed;
100 bool m_useDefaultIndexAt; // true, if QTreeView::indexAt() should be used
101 bool m_ignoreScrollTo; // true if calls to scrollTo(...) should do nothing.
102
103 QRect m_dropRect;
104
105 struct ElasticBand
106 {
107 ElasticBand();
108
109 // Elastic band origin and destination coordinates are relative to t
110 // he origin of the view, not the viewport.
111 bool show;
112 QPoint origin;
113 QPoint destination;
114
115 // Optimization mechanisms for use with elastic band selection.
116 // Basically, allow "incremental" updates to the selection based
117 // on the last elastic band shape.
118 QPoint lastSelectionOrigin;
119 QPoint lastSelectionDestination;
120
121 // If true, compute the set of selected elements from scratch (slower)
122 bool ignoreOldInfo;
123
124 // Edges of the filenames that are closest to the edges of oldSelectionRect.
125 // Used to decide whether horizontal changes in the elastic band are likely
126 // to require us to re-check which items are selected.
127 int outsideNearestLeftEdge;
128 int outsideNearestRightEdge;
129 int insideNearestLeftEdge;
130 int insideNearestRightEdge;
131 // The set of items that were selected at the time this band was shown.
132 // NOTE: Unless CTRL was pressed when the band was created, this is always empty.
133 QItemSelection originalSelection;
134 } m_band;
135 };
136
137 #endif