]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.h
Fix includes
[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 "kitemset.h"
29
30 #include <QObject>
31 #include <QPixmap>
32 #include <QPointF>
33
34 class KItemModelBase;
35 class KItemListKeyboardSearchManager;
36 class KItemListSelectionManager;
37 class KItemListView;
38 class KItemListWidget;
39 class QGraphicsSceneHoverEvent;
40 class QGraphicsSceneDragDropEvent;
41 class QGraphicsSceneMouseEvent;
42 class QGraphicsSceneResizeEvent;
43 class QGraphicsSceneWheelEvent;
44 class QHideEvent;
45 class QInputMethodEvent;
46 class QKeyEvent;
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 Q_PROPERTY(AutoActivationBehavior autoActivationBehavior READ autoActivationBehavior WRITE setAutoActivationBehavior)
68 Q_PROPERTY(MouseDoubleClickAction mouseDoubleClickAction READ mouseDoubleClickAction WRITE setMouseDoubleClickAction)
69
70 public:
71 enum SelectionBehavior {
72 NoSelection,
73 SingleSelection,
74 MultiSelection
75 };
76
77 enum AutoActivationBehavior {
78 ActivationAndExpansion,
79 ExpansionOnly
80 };
81
82 enum MouseDoubleClickAction {
83 ActivateAndExpandItem,
84 ActivateItemOnly
85 };
86
87 /**
88 * @param model Model of the controller. The ownership is passed to the controller.
89 * @param view View of the controller. The ownership is passed to the controller.
90 * @param parent Optional parent object.
91 */
92 KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent = 0);
93 virtual ~KItemListController();
94
95 void setModel(KItemModelBase* model);
96 KItemModelBase* model() const;
97
98 void setView(KItemListView* view);
99 KItemListView* view() const;
100
101 KItemListSelectionManager* selectionManager() const;
102
103 void setSelectionBehavior(SelectionBehavior behavior);
104 SelectionBehavior selectionBehavior() const;
105
106 void setAutoActivationBehavior(AutoActivationBehavior behavior);
107 AutoActivationBehavior autoActivationBehavior() const;
108
109 void setMouseDoubleClickAction(MouseDoubleClickAction action);
110 MouseDoubleClickAction mouseDoubleClickAction() const;
111
112 /**
113 * Sets the delay in milliseconds when dragging an object above an item
114 * until the item gets activated automatically. A value of -1 indicates
115 * that no automatic activation will be done at all (= default value).
116 *
117 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
118 * otherwise the automatic activation is not available.
119 *
120 * After activating the item the signal itemActivated() will be
121 * emitted. If the view supports the expanding of items
122 * (KItemListView::supportsItemExpanding() returns true) and the item
123 * itself is expandable (see KItemModelBase::isExpandable()) then instead
124 * of activating the item it gets expanded instead (see
125 * KItemModelBase::setExpanded()).
126 */
127 void setAutoActivationDelay(int delay);
128 int autoActivationDelay() const;
129
130 /**
131 * If set to true, the signals itemActivated() and itemsActivated() are emitted
132 * after a single-click of the left mouse button. If set to false (the default),
133 * the setting from KGlobalSettings::singleClick() is used.
134 */
135 void setSingleClickActivationEnforced(bool singleClick);
136 bool singleClickActivationEnforced() const;
137
138 virtual bool showEvent(QShowEvent* event);
139 virtual bool hideEvent(QHideEvent* event);
140 virtual bool keyPressEvent(QKeyEvent* event);
141 virtual bool inputMethodEvent(QInputMethodEvent* event);
142 virtual bool mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
143 virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
144 virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
145 virtual bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
146 virtual bool dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
147 virtual bool dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
148 virtual bool dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
149 virtual bool dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
150 virtual bool hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
151 virtual bool hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
152 virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
153 virtual bool wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform);
154 virtual bool resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform);
155 virtual bool processEvent(QEvent* event, const QTransform& transform);
156
157 signals:
158 /**
159 * Is emitted if exactly one item has been activated by e.g. a mouse-click
160 * or by pressing Return/Enter.
161 */
162 void itemActivated(int index);
163
164 /**
165 * Is emitted if more than one item has been activated by pressing Return/Enter
166 * when having a selection.
167 */
168 void itemsActivated(const KItemSet& indexes);
169
170 void itemMiddleClicked(int index);
171
172 /**
173 * Emitted if a context-menu is requested for the item with
174 * the index \a index. It is assured that the index is valid.
175 */
176 void itemContextMenuRequested(int index, const QPointF& pos);
177
178 /**
179 * Emitted if a context-menu is requested for the KItemListView.
180 */
181 void viewContextMenuRequested(const QPointF& pos);
182
183 /**
184 * Emitted if a context-menu is requested for the header of the KItemListView.
185 */
186 void headerContextMenuRequested(const QPointF& pos);
187
188 /**
189 * Is emitted if the item with the index \p index gets hovered.
190 */
191 void itemHovered(int index);
192
193 /**
194 * Is emitted if the item with the index \p index gets unhovered.
195 * It is assured that the signal itemHovered() for this index
196 * has been emitted before.
197 */
198 void itemUnhovered(int index);
199
200 /**
201 * Is emitted if a mouse-button has been pressed above an item.
202 * If the index is smaller than 0, the mouse-button has been pressed
203 * above the viewport.
204 */
205 void mouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
206
207 /**
208 * Is emitted if a mouse-button has been released above an item.
209 * It is assured that the signal mouseButtonPressed() has been emitted before.
210 * If the index is smaller than 0, the mouse-button has been pressed
211 * above the viewport.
212 */
213 void mouseButtonReleased(int itemIndex, Qt::MouseButtons buttons);
214
215 void itemExpansionToggleClicked(int index);
216
217 /**
218 * Is emitted if a drop event is done above the item with the index
219 * \a index. If \a index is < 0 the drop event is done above an
220 * empty area of the view.
221 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
222 * which is emitted if the drop event occurs on an empty area in
223 * the view, and make sure that index is always >= 0 in itemDropEvent().
224 */
225 void itemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
226
227 /**
228 * Is emitted if a drop event is done between the item with the index
229 * \a index and the previous item.
230 */
231 void aboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
232
233 /**
234 * Is emitted if the Escape key is pressed.
235 */
236 void escapePressed();
237
238 void modelChanged(KItemModelBase* current, KItemModelBase* previous);
239 void viewChanged(KItemListView* current, KItemListView* previous);
240
241 private slots:
242 void slotViewScrollOffsetChanged(qreal current, qreal previous);
243
244 /**
245 * Is invoked when the rubberband boundaries have been changed and will select
246 * all items that are touched by the rubberband.
247 */
248 void slotRubberBandChanged();
249
250 void slotChangeCurrentItem(const QString& text, bool searchFromNextItem);
251
252 void slotAutoActivationTimeout();
253
254 private:
255 /**
256 * Creates a QDrag object and initiates a drag-operation.
257 */
258 void startDragging();
259
260 /**
261 * @return Widget that is currently in the hovered state. 0 is returned
262 * if no widget is marked as hovered.
263 */
264 KItemListWidget* hoveredWidget() const;
265
266 /**
267 * @return Widget that is below the position \a pos. 0 is returned
268 * if no widget is below the position.
269 */
270 KItemListWidget* widgetForPos(const QPointF& pos) const;
271
272 /**
273 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
274 * set, it will be adjusted to the current item. If it is set it will be
275 * checked whether it is still valid, otherwise it will be reset to the
276 * current item.
277 */
278 void updateKeyboardAnchor();
279
280 /**
281 * @return Index for the next row based on \a index.
282 * If there is no next row \a index will be returned.
283 */
284 int nextRowIndex(int index) const;
285
286 /**
287 * @return Index for the previous row based on \a index.
288 * If there is no previous row \a index will be returned.
289 */
290 int previousRowIndex(int index) const;
291
292 /**
293 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
294 * @return The position of the keyboard anchor for the item with the index \a index.
295 * If a horizontal scrolling is used the y-position of the item will be returned,
296 * for the vertical scrolling the x-position will be returned.
297 */
298 qreal keyboardAnchorPos(int index) const;
299
300 /**
301 * Dependent on the selection-behavior the extendedSelectionRegion-property
302 * of the KItemListStyleOption from the view should be adjusted: If no
303 * rubberband selection is used the property should be enabled.
304 */
305 void updateExtendedSelectionRegion();
306
307 private:
308 bool m_singleClickActivationEnforced;
309 bool m_selectionTogglePressed;
310 bool m_clearSelectionIfItemsAreNotDragged;
311 SelectionBehavior m_selectionBehavior;
312 AutoActivationBehavior m_autoActivationBehavior;
313 MouseDoubleClickAction m_mouseDoubleClickAction;
314 KItemModelBase* m_model;
315 KItemListView* m_view;
316 KItemListSelectionManager* m_selectionManager;
317 KItemListKeyboardSearchManager* m_keyboardManager;
318 int m_pressedIndex;
319 QPointF m_pressedMousePos;
320
321 QTimer* m_autoActivationTimer;
322
323 /**
324 * When starting a rubberband selection during a Shift- or Control-key has been
325 * pressed the current selection should never be deleted. To be able to restore
326 * the current selection it is remembered in m_oldSelection before the
327 * rubberband gets activated.
328 */
329 KItemSet m_oldSelection;
330
331 /**
332 * Assuming a view is given with a vertical scroll-orientation, grouped items and
333 * a maximum of 4 columns:
334 *
335 * 1 2 3 4
336 * 5 6 7
337 * 8 9 10 11
338 * 12 13 14
339 *
340 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
341 * item. Now when pressing key-down again item 11 should get the current item and not
342 * item 10. This makes it necessary to keep track of the requested column to have a
343 * similar behavior like in a text editor:
344 */
345 int m_keyboardAnchorIndex;
346 qreal m_keyboardAnchorPos;
347 };
348
349 #endif
350
351