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