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