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