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