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