2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * Based on the Itemviews NG project from Trolltech Labs
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #ifndef KITEMLISTCONTROLLER_H
10 #define KITEMLISTCONTROLLER_H
14 #include "dolphin_export.h"
23 class KItemListKeyboardSearchManager
;
24 class KItemListSelectionManager
;
26 class KItemListWidget
;
28 class QGraphicsSceneHoverEvent
;
29 class QGraphicsSceneDragDropEvent
;
30 class QGraphicsSceneMouseEvent
;
31 class QGraphicsSceneResizeEvent
;
32 class QGraphicsSceneWheelEvent
;
33 class QInputMethodEvent
;
40 * @brief Controls the view, model and selection of an item-list.
42 * For a working item-list it is mandatory to set a compatible view and model
43 * with KItemListController::setView() and KItemListController::setModel().
47 * @see KItemListSelectionManager
49 class DOLPHIN_EXPORT KItemListController
: public QObject
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
)
59 enum SelectionBehavior
{
64 Q_ENUM(SelectionBehavior
)
66 enum AutoActivationBehavior
{
67 ActivationAndExpansion
,
71 enum MouseDoubleClickAction
{
72 ActivateAndExpandItem
,
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.
81 KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
= nullptr);
82 ~KItemListController() override
;
84 void setModel(KItemModelBase
* model
);
85 KItemModelBase
* model() const;
87 void setView(KItemListView
* view
);
88 KItemListView
* view() const;
90 KItemListSelectionManager
* selectionManager() const;
92 void setSelectionBehavior(SelectionBehavior behavior
);
93 SelectionBehavior
selectionBehavior() const;
95 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
96 AutoActivationBehavior
autoActivationBehavior() const;
98 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
99 MouseDoubleClickAction
mouseDoubleClickAction() const;
101 int indexCloseToMousePressedPosition() const;
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).
108 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
109 * otherwise the automatic activation is not available.
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()).
118 void setAutoActivationDelay(int delay
);
119 int autoActivationDelay() const;
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.
126 void setSingleClickActivationEnforced(bool singleClick
);
127 bool singleClickActivationEnforced() const;
129 void setSelectionMode(bool enabled
);
130 bool selectionMode() const;
132 bool processEvent(QEvent
* event
, const QTransform
& transform
);
136 * Is emitted if exactly one item has been activated by e.g. a mouse-click
137 * or by pressing Return/Enter.
139 void itemActivated(int index
);
142 * Is emitted if more than one item has been activated by pressing Return/Enter
143 * when having a selection.
145 void itemsActivated(const KItemSet
&indexes
);
147 void itemMiddleClicked(int index
);
150 * Emitted if a context-menu is requested for the item with
151 * the index \a index. It is assured that the index is valid.
153 void itemContextMenuRequested(int index
, const QPointF
& pos
);
156 * Emitted if a context-menu is requested for the KItemListView.
158 void viewContextMenuRequested(const QPointF
& pos
);
161 * Emitted if a context-menu is requested for the header of the KItemListView.
163 void headerContextMenuRequested(const QPointF
& pos
);
166 * Is emitted if the item with the index \p index gets hovered.
168 void itemHovered(int index
);
171 * Is emitted if the item with the index \p index gets unhovered.
172 * It is assured that the signal itemHovered() for this index
173 * has been emitted before.
175 void itemUnhovered(int index
);
178 * Is emitted if a mouse-button has been pressed above an item.
179 * If the index is smaller than 0, the mouse-button has been pressed
180 * above the viewport.
182 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
185 * Is emitted if a mouse-button has been released above an item.
186 * It is assured that the signal mouseButtonPressed() has been emitted before.
187 * If the index is smaller than 0, the mouse-button has been pressed
188 * above the viewport.
190 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
192 void itemExpansionToggleClicked(int index
);
195 * Is emitted if a drop event is done above the item with the index
196 * \a index. If \a index is < 0 the drop event is done above an
197 * empty area of the view.
198 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
199 * which is emitted if the drop event occurs on an empty area in
200 * the view, and make sure that index is always >= 0 in itemDropEvent().
202 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
205 * Is emitted if a drop event is done between the item with the index
206 * \a index and the previous item.
208 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
211 * Is emitted if the Escape key is pressed.
213 void escapePressed();
216 * Is emitted 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.
221 void selectionModeRequested();
223 void modelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
224 void viewChanged(KItemListView
* current
, KItemListView
* previous
);
226 void selectedItemTextPressed(int index
);
234 void slotStateChanged(QScroller::State newState
);
237 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
240 * Is invoked when the rubberband boundaries have been changed and will select
241 * all items that are touched by the rubberband.
243 void slotRubberBandChanged();
245 void slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
);
247 void slotAutoActivationTimeout();
251 * Creates a QDrag object and initiates a drag-operation.
253 void startDragging();
256 * @return Widget that is currently in the hovered state. 0 is returned
257 * if no widget is marked as hovered.
259 KItemListWidget
* hoveredWidget() const;
262 * @return Widget that is below the position \a pos. 0 is returned
263 * if no widget is below the position.
265 KItemListWidget
* widgetForPos(const QPointF
& pos
) const;
268 * @return Widget that should receive a drop event if an item is dropped at \a pos. 0 is returned
269 * if no widget should receive a drop event at the position.
271 * While widgetForPos() returns a widget if \a pos is anywhere inside the hover highlight area of the widget,
272 * widgetForDropPos() only returns a widget if \a pos is directly above the widget (widget->contains(pos) == true).
274 KItemListWidget
* widgetForDropPos(const QPointF
& pos
) const;
277 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
278 * set, it will be adjusted to the current item. If it is set it will be
279 * checked whether it is still valid, otherwise it will be reset to the
282 void updateKeyboardAnchor();
285 * @return Index for the next row based on \a index.
286 * If there is no next row \a index will be returned.
288 int nextRowIndex(int index
) const;
291 * @return Index for the previous row based on \a index.
292 * If there is no previous row \a index will be returned.
294 int previousRowIndex(int index
) const;
297 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
298 * @return The position of the keyboard anchor for the item with the index \a index.
299 * If a horizontal scrolling is used the y-position of the item will be returned,
300 * for the vertical scrolling the x-position will be returned.
302 qreal
keyboardAnchorPos(int index
) const;
305 * Dependent on the selection-behavior the extendedSelectionRegion-property
306 * of the KItemListStyleOption from the view should be adjusted: If no
307 * rubberband selection is used the property should be enabled.
309 void updateExtendedSelectionRegion();
311 bool keyPressEvent(QKeyEvent
* event
);
312 bool inputMethodEvent(QInputMethodEvent
* event
);
313 bool mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
314 bool mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
315 bool mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
316 bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
317 bool dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
318 bool dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
319 bool dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
320 bool dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
321 bool hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
322 bool hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
323 bool hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
324 bool wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
);
325 bool resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
);
326 bool gestureEvent(QGestureEvent
* event
, const QTransform
& transform
);
327 bool touchBeginEvent(QTouchEvent
* event
, const QTransform
& transform
);
328 void tapTriggered(QTapGesture
* tap
, const QTransform
& transform
);
329 void tapAndHoldTriggered(QGestureEvent
* event
, const QTransform
& transform
);
330 void pinchTriggered(QGestureEvent
* event
, const QTransform
& transform
);
331 void swipeTriggered(QGestureEvent
* event
, const QTransform
& transform
);
332 void twoFingerTapTriggered(QGestureEvent
* event
, const QTransform
& transform
);
333 bool onPress(const QPoint
& screenPos
, const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
);
334 bool onRelease(const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
);
335 void startRubberBand();
338 bool m_singleClickActivationEnforced
;
339 bool m_selectionMode
;
340 bool m_selectionTogglePressed
;
341 bool m_clearSelectionIfItemsAreNotDragged
;
342 bool m_isSwipeGesture
;
343 bool m_dragActionOrRightClick
;
344 bool m_scrollerIsScrolling
;
345 bool m_pinchGestureInProgress
;
348 SelectionBehavior m_selectionBehavior
;
349 AutoActivationBehavior m_autoActivationBehavior
;
350 MouseDoubleClickAction m_mouseDoubleClickAction
;
351 KItemModelBase
* m_model
;
352 KItemListView
* m_view
;
353 KItemListSelectionManager
* m_selectionManager
;
354 KItemListKeyboardSearchManager
* m_keyboardManager
;
355 std::optional
<int> m_pressedIndex
;
356 QPointF m_pressedMousePos
;
358 QTimer
* m_autoActivationTimer
;
359 QTimer
* m_longPressDetectionTimer
;
361 Qt::GestureType m_swipeGesture
;
362 Qt::GestureType m_twoFingerTapGesture
;
365 * When starting a rubberband selection during a Shift- or Control-key has been
366 * pressed the current selection should never be deleted. To be able to restore
367 * the current selection it is remembered in m_oldSelection before the
368 * rubberband gets activated.
370 KItemSet m_oldSelection
;
373 * Assuming a view is given with a vertical scroll-orientation, grouped items and
374 * a maximum of 4 columns:
381 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
382 * item. Now when pressing key-down again item 11 should get the current item and not
383 * item 10. This makes it necessary to keep track of the requested column to have a
384 * similar behavior like in a text editor:
386 int m_keyboardAnchorIndex
;
387 qreal m_keyboardAnchorPos
;