]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphintreeview.h
DolphinTreeView contains some code to update the selection after a
[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 void keyboardSearch(const QString & search);
45 virtual QRegion visualRegionForSelection(const QItemSelection& selection) const;
46
47 protected:
48 /**
49 * @return True, if the item with the index \p index accepts a drop. In this
50 * case a visual feedback for the user is given during dragging. Per
51 * default false is returned.
52 */
53 virtual bool acceptsDrop(const QModelIndex& index) const;
54
55 virtual bool event(QEvent* event);
56 virtual void mousePressEvent(QMouseEvent* event);
57 virtual void mouseMoveEvent(QMouseEvent* event);
58 virtual void mouseReleaseEvent(QMouseEvent* event);
59 virtual void startDrag(Qt::DropActions supportedActions);
60 virtual void dragEnterEvent(QDragEnterEvent* event);
61 virtual void dragMoveEvent(QDragMoveEvent* event);
62 virtual void dragLeaveEvent(QDragLeaveEvent* event);
63 virtual void paintEvent(QPaintEvent* event);
64 virtual void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command);
65 virtual void scrollTo(const QModelIndex& index, ScrollHint hint = EnsureVisible);
66
67 private slots:
68 /**
69 * If the elastic band is currently shown, update the elastic band based on
70 * the current mouse position and ensure that the selection is the set of items
71 * intersecting it.
72 */
73 void updateElasticBandSelection();
74
75 /**
76 * Updates the destination \a destination from
77 * the elastic band to the current mouse position and triggers
78 * an update.
79 */
80 void updateElasticBand();
81
82 /**
83 * Returns the rectangle for the elastic band dependent from the
84 * origin \a origin, the current destination
85 * \a destination and the viewport position.
86 */
87 QRect elasticBandRect() const;
88
89 private:
90 /**
91 * Returns true, if \a pos is within the expanding toggle of a tree.
92 */
93 bool isAboveExpandingToggle(const QPoint& pos) const;
94
95 private:
96 bool m_expandingTogglePressed;
97 bool m_useDefaultIndexAt; // true, if QTreeView::indexAt() should be used
98 bool m_ignoreScrollTo; // true if calls to scrollTo(...) should do nothing.
99
100 QRect m_dropRect;
101
102 struct ElasticBand
103 {
104 ElasticBand();
105
106 // Elastic band origin and destination coordinates are relative to t
107 // he origin of the view, not the viewport.
108 bool show;
109 QPoint origin;
110 QPoint destination;
111
112 // Optimization mechanisms for use with elastic band selection.
113 // Basically, allow "incremental" updates to the selection based
114 // on the last elastic band shape.
115 QPoint lastSelectionOrigin;
116 QPoint lastSelectionDestination;
117
118 // If true, compute the set of selected elements from scratch (slower)
119 bool ignoreOldInfo;
120
121 // Edges of the filenames that are closest to the edges of oldSelectionRect.
122 // Used to decide whether horizontal changes in the elastic band are likely
123 // to require us to re-check which items are selected.
124 int outsideNearestLeftEdge;
125 int outsideNearestRightEdge;
126 int insideNearestLeftEdge;
127 int insideNearestRightEdge;
128 // The set of items that were selected at the time this band was shown.
129 // NOTE: Unless CTRL was pressed when the band was created, this is always empty.
130 QItemSelection originalSelection;
131 } m_band;
132 };
133
134 #endif