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