]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.h
Places Panel: Prepare code to save state of bookmarks
[dolphin.git] / src / kitemviews / kitemlistcontroller.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #ifndef KITEMLISTCONTROLLER_H
24 #define KITEMLISTCONTROLLER_H
25
26 #include <libdolphin_export.h>
27
28 #include <QObject>
29 #include <QPixmap>
30 #include <QPointF>
31 #include <QSet>
32
33 class KItemModelBase;
34 class KItemListKeyboardSearchManager;
35 class KItemListSelectionManager;
36 class KItemListView;
37 class KItemListWidget;
38 class QGraphicsSceneHoverEvent;
39 class QGraphicsSceneDragDropEvent;
40 class QGraphicsSceneMouseEvent;
41 class QGraphicsSceneResizeEvent;
42 class QGraphicsSceneWheelEvent;
43 class QHideEvent;
44 class QInputMethodEvent;
45 class QKeyEvent;
46 class QMimeData;
47 class QShowEvent;
48 class QTransform;
49
50 /**
51 * @brief Controls the view, model and selection of an item-list.
52 *
53 * For a working item-list it is mandatory to set a compatible view and model
54 * with KItemListController::setView() and KItemListController::setModel().
55 *
56 * @see KItemListView
57 * @see KItemModelBase
58 * @see KItemListSelectionManager
59 */
60 class LIBDOLPHINPRIVATE_EXPORT KItemListController : public QObject
61 {
62 Q_OBJECT
63 Q_ENUMS(SelectionBehavior)
64 Q_PROPERTY(KItemModelBase* model READ model WRITE setModel)
65 Q_PROPERTY(KItemListView *view READ view WRITE setView)
66 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior)
67
68 public:
69 enum SelectionBehavior {
70 NoSelection,
71 SingleSelection,
72 MultiSelection
73 };
74
75 /**
76 * @param model Model of the controller. The ownership is passed to the controller.
77 * @param view View of the controller. The ownership is passed to the controller.
78 * @param parent Optional parent object.
79 */
80 KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent = 0);
81 virtual ~KItemListController();
82
83 void setModel(KItemModelBase* model);
84 KItemModelBase* model() const;
85
86 void setView(KItemListView* view);
87 KItemListView* view() const;
88
89 KItemListSelectionManager* selectionManager() const;
90
91 void setSelectionBehavior(SelectionBehavior behavior);
92 SelectionBehavior selectionBehavior() const;
93
94 /**
95 * Sets the delay in milliseconds when dragging an object above an item
96 * until the item gets activated automatically. A value of -1 indicates
97 * that no automatic activation will be done at all (= default value).
98 *
99 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
100 * otherwise the automatic activation is not available.
101 *
102 * After activating the item the signal itemActivated() will be
103 * emitted. If the view supports the expanding of items
104 * (KItemListView::supportsItemExpanding() returns true) and the item
105 * itself is expandable (see KItemModelBase::isExpandable()) then instead
106 * of activating the item it gets expanded instead (see
107 * KItemModelBase::setExpanded()).
108 */
109 void setAutoActivationDelay(int delay);
110 int autoActivationDelay() const;
111
112 /**
113 * If set to true, the signals itemActivated() and itemsActivated() are emitted
114 * after a single-click of the left mouse button. If set to false, a double-click
115 * is required. Per default the setting from KGlobalSettings::singleClick() is
116 * used.
117 */
118 void setSingleClickActivation(bool singleClick);
119 bool singleClickActivation() const;
120
121 virtual bool showEvent(QShowEvent* event);
122 virtual bool hideEvent(QHideEvent* event);
123 virtual bool keyPressEvent(QKeyEvent* event);
124 virtual bool inputMethodEvent(QInputMethodEvent* event);
125 virtual bool mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
126 virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
127 virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
128 virtual bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
129 virtual bool dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
130 virtual bool dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
131 virtual bool dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
132 virtual bool dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
133 virtual bool hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
134 virtual bool hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
135 virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
136 virtual bool wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform);
137 virtual bool resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform);
138 virtual bool processEvent(QEvent* event, const QTransform& transform);
139
140 signals:
141 /**
142 * Is emitted if exactly one item has been activated by e.g. a mouse-click
143 * or by pressing Return/Enter.
144 */
145 void itemActivated(int index);
146
147 /**
148 * Is emitted if more than one item has been activated by pressing Return/Enter
149 * when having a selection.
150 */
151 void itemsActivated(const QSet<int>& indexes);
152
153 void itemMiddleClicked(int index);
154
155 /**
156 * Emitted if a context-menu is requested for the item with
157 * the index \a index. It is assured that the index is valid.
158 */
159 void itemContextMenuRequested(int index, const QPointF& pos);
160
161 /**
162 * Emitted if a context-menu is requested for the KItemListView.
163 */
164 void viewContextMenuRequested(const QPointF& pos);
165
166 /**
167 * Emitted if a context-menu is requested for the header of the KItemListView.
168 */
169 void headerContextMenuRequested(const QPointF& pos);
170
171 /**
172 * Is emitted if the item with the index \p index gets hovered.
173 */
174 void itemHovered(int index);
175
176 /**
177 * Is emitted if the item with the index \p index gets unhovered.
178 * It is assured that the signal itemHovered() for this index
179 * has been emitted before.
180 */
181 void itemUnhovered(int index);
182
183 /**
184 * Is emitted if a mouse-button has been pressed above an item.
185 * If the index is smaller than 0, the mouse-button has been pressed
186 * above the viewport.
187 */
188 void mouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
189
190 /**
191 * Is emitted if a mouse-button has been released above an item.
192 * It is assured that the signal mouseButtonPressed() has been emitted before.
193 * If the index is smaller than 0, the mouse-button has been pressed
194 * above the viewport.
195 */
196 void mouseButtonReleased(int itemIndex, Qt::MouseButtons buttons);
197
198 void itemExpansionToggleClicked(int index);
199
200 /**
201 * Is emitted if a drop event is done above the item with the index
202 * \a index. If \a index is < 0 the drop event is done above an
203 * empty area of the view.
204 */
205 void itemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
206
207 void modelChanged(KItemModelBase* current, KItemModelBase* previous);
208 void viewChanged(KItemListView* current, KItemListView* previous);
209
210 private slots:
211 void slotViewScrollOffsetChanged(qreal current, qreal previous);
212
213 /**
214 * Is invoked when the rubberband boundaries have been changed and will select
215 * all items that are touched by the rubberband.
216 */
217 void slotRubberBandChanged();
218
219 void slotChangeCurrentItem(const QString& text, bool searchFromNextItem);
220
221 void slotAutoActivationTimeout();
222
223 private:
224 /**
225 * Creates a QDrag object and initiates a drag-operation.
226 */
227 void startDragging();
228
229 /**
230 * @return Widget that is currently in the hovered state. 0 is returned
231 * if no widget is marked as hovered.
232 */
233 KItemListWidget* hoveredWidget() const;
234
235 /**
236 * @return Widget that is below the position \a pos. 0 is returned
237 * if no widget is below the position.
238 */
239 KItemListWidget* widgetForPos(const QPointF& pos) const;
240
241 /**
242 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
243 * set, it will be adjusted to the current item. If it is set it will be
244 * checked whether it is still valid, otherwise it will be reset to the
245 * current item.
246 */
247 void updateKeyboardAnchor();
248
249 /**
250 * @return Index for the next row based on \a index.
251 * If there is no next row \a index will be returned.
252 */
253 int nextRowIndex(int index) const;
254
255 /**
256 * @return Index for the previous row based on \a index.
257 * If there is no previous row \a index will be returned.
258 */
259 int previousRowIndex(int index) const;
260
261 /**
262 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
263 * @return The position of the keyboard anchor for the item with the index \a index.
264 * If a horizontal scrolling is used the y-position of the item will be returned,
265 * for the vertical scrolling the x-position will be returned.
266 */
267 qreal keyboardAnchorPos(int index) const;
268
269 /**
270 * Dependent on the selection-behavior the extendedSelectionRegion-property
271 * of the KItemListStyleOption from the view should be adjusted: If no
272 * rubberband selection is used the property should be enabled.
273 */
274 void updateExtendedSelectionRegion();
275
276 private:
277 bool m_singleClickActivation;
278 bool m_selectionTogglePressed;
279 bool m_clearSelectionIfItemsAreNotDragged;
280 SelectionBehavior m_selectionBehavior;
281 KItemModelBase* m_model;
282 KItemListView* m_view;
283 KItemListSelectionManager* m_selectionManager;
284 KItemListKeyboardSearchManager* m_keyboardManager;
285 int m_pressedIndex;
286 QPointF m_pressedMousePos;
287
288 QTimer* m_autoActivationTimer;
289
290 /**
291 * When starting a rubberband selection during a Shift- or Control-key has been
292 * pressed the current selection should never be deleted. To be able to restore
293 * the current selection it is remembered in m_oldSelection before the
294 * rubberband gets activated.
295 */
296 QSet<int> m_oldSelection;
297
298 /**
299 * Assuming a view is given with a vertical scroll-orientation, grouped items and
300 * a maximum of 4 columns:
301 *
302 * 1 2 3 4
303 * 5 6 7
304 * 8 9 10 11
305 * 12 13 14
306 *
307 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
308 * item. Now when pressing key-down again item 11 should get the current item and not
309 * item 10. This makes it necessary to keep track of the requested column to have a
310 * similar behavior like in a text editor:
311 */
312 int m_keyboardAnchorIndex;
313 qreal m_keyboardAnchorPos;
314 };
315
316 #endif
317
318