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
{ NoSelection
, SingleSelection
, MultiSelection
};
60 Q_ENUM(SelectionBehavior
)
62 enum AutoActivationBehavior
{ ActivationAndExpansion
, ExpansionOnly
};
64 enum MouseDoubleClickAction
{ ActivateAndExpandItem
, ActivateItemOnly
};
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.
71 KItemListController(KItemModelBase
*model
, KItemListView
*view
, QObject
*parent
= nullptr);
72 ~KItemListController() override
;
74 void setModel(KItemModelBase
*model
);
75 KItemModelBase
*model() const;
77 void setView(KItemListView
*view
);
78 KItemListView
*view() const;
80 KItemListSelectionManager
*selectionManager() const;
82 void setSelectionBehavior(SelectionBehavior behavior
);
83 SelectionBehavior
selectionBehavior() const;
85 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
86 AutoActivationBehavior
autoActivationBehavior() const;
88 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
89 MouseDoubleClickAction
mouseDoubleClickAction() const;
91 int indexCloseToMousePressedPosition() const;
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).
98 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
99 * otherwise the automatic activation is not available.
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()).
108 void setAutoActivationDelay(int delay
);
109 int autoActivationDelay() const;
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.
116 void setSingleClickActivationEnforced(bool singleClick
);
117 bool singleClickActivationEnforced() const;
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.
123 void setSelectionModeEnabled(bool enabled
);
124 bool selectionMode() const;
127 * @return \c true if search as you type is active, or \c false otherwise.
129 bool isSearchAsYouTypeActive() const;
131 bool processEvent(QEvent
*event
, const QTransform
&transform
);
135 * Is emitted if exactly one item has been activated by e.g. a mouse-click
136 * or by pressing Return/Enter.
138 void itemActivated(int index
);
141 * Is emitted if more than one item has been activated by pressing Return/Enter
142 * when having a selection.
144 void itemsActivated(const KItemSet
&indexes
);
146 void itemMiddleClicked(int index
);
149 * Emitted if a context-menu is requested for the item with
150 * the index \a index. It is assured that the index is valid.
152 void itemContextMenuRequested(int index
, const QPointF
&pos
);
155 * Emitted if a context-menu is requested for the KItemListView.
157 void viewContextMenuRequested(const QPointF
&pos
);
160 * Emitted if a context-menu is requested for the header of the KItemListView.
162 void headerContextMenuRequested(const QPointF
&pos
);
165 * Is emitted if the item with the index \p index gets hovered.
167 void itemHovered(int index
);
170 * Is emitted if the item with the index \p index gets unhovered.
171 * It is assured that the signal itemHovered() for this index
172 * has been emitted before.
174 void itemUnhovered(int index
);
177 * Is emitted if a mouse-button has been pressed above an item.
178 * If the index is smaller than 0, the mouse-button has been pressed
179 * above the viewport.
181 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
184 * Is emitted if a mouse-button has been released above an item.
185 * It is assured that the signal mouseButtonPressed() has been emitted before.
186 * If the index is smaller than 0, the mouse-button has been pressed
187 * above the viewport.
189 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
191 void itemExpansionToggleClicked(int index
);
194 * Is emitted if a drop event is done above the item with the index
195 * \a index. If \a index is < 0 the drop event is done above an
196 * empty area of the view.
197 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
198 * which is emitted if the drop event occurs on an empty area in
199 * the view, and make sure that index is always >= 0 in itemDropEvent().
201 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
);
204 * Is emitted if a drop event is done between the item with the index
205 * \a index and the previous item.
207 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
);
210 * Is emitted if the Escape key is pressed.
212 void escapePressed();
215 * Used to request either entering or leaving of selection mode
216 * Leaving is requested by pressing Escape when no item is selected.
218 * Entering is requested if left click is pressed down for a long time without moving the cursor too much.
219 * Moving the cursor would either trigger an item drag if the click was initiated on top of an item
220 * or a selection rectangle if the click was not initiated on top of an item.
221 * So long press is only emitted if there wasn't a lot of cursor movement.
223 void selectionModeChangeRequested(bool enabled
);
225 void modelChanged(KItemModelBase
*current
, KItemModelBase
*previous
);
226 void viewChanged(KItemListView
*current
, KItemListView
*previous
);
228 void selectedItemTextPressed(int index
);
236 void slotStateChanged(QScroller::State newState
);
239 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
242 * Is invoked when the rubberband boundaries have been changed and will select
243 * all items that are touched by the rubberband.
245 void slotRubberBandChanged();
247 void slotChangeCurrentItem(const QString
&text
, bool searchFromNextItem
);
249 void slotAutoActivationTimeout();
253 * Creates a QDrag object and initiates a drag-operation.
255 void startDragging();
258 * @return Widget that is currently in the hovered state. 0 is returned
259 * if no widget is marked as hovered.
261 KItemListWidget
*hoveredWidget() const;
264 * @return Widget that is below the position \a pos. 0 is returned
265 * if no widget is below the position.
267 KItemListWidget
*widgetForPos(const QPointF
&pos
) const;
270 * @return Widget that should receive a drop event if an item is dropped at \a pos. 0 is returned
271 * if no widget should receive a drop event at the position.
273 * While widgetForPos() returns a widget if \a pos is anywhere inside the hover highlight area of the widget,
274 * widgetForDropPos() only returns a widget if \a pos is directly above the widget (widget->contains(pos) == true).
276 KItemListWidget
*widgetForDropPos(const QPointF
&pos
) const;
279 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
280 * set, it will be adjusted to the current item. If it is set it will be
281 * checked whether it is still valid, otherwise it will be reset to the
284 void updateKeyboardAnchor();
287 * @return Index for the next row based on \a index.
288 * If there is no next row \a index will be returned.
290 int nextRowIndex(int index
) const;
293 * @return Index for the previous row based on \a index.
294 * If there is no previous row \a index will be returned.
296 int previousRowIndex(int index
) const;
299 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
300 * @return The position of the keyboard anchor for the item with the index \a index.
301 * If a horizontal scrolling is used the y-position of the item will be returned,
302 * for the vertical scrolling the x-position will be returned.
304 qreal
keyboardAnchorPos(int index
) const;
307 * Dependent on the selection-behavior the extendedSelectionRegion-property
308 * of the KItemListStyleOption from the view should be adjusted: If no
309 * rubberband selection is used the property should be enabled.
311 void updateExtendedSelectionRegion();
313 bool keyPressEvent(QKeyEvent
*event
);
314 bool inputMethodEvent(QInputMethodEvent
*event
);
315 bool mousePressEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
316 bool mouseMoveEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
317 bool mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
318 bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
319 bool dragEnterEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
320 bool dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
321 bool dragMoveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
322 bool dropEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
323 bool hoverEnterEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
);
324 bool hoverMoveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
);
325 bool hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
);
326 bool wheelEvent(QGraphicsSceneWheelEvent
*event
, const QTransform
&transform
);
327 bool resizeEvent(QGraphicsSceneResizeEvent
*event
, const QTransform
&transform
);
328 bool gestureEvent(QGestureEvent
*event
, const QTransform
&transform
);
329 bool touchBeginEvent(QTouchEvent
*event
, const QTransform
&transform
);
330 void tapTriggered(QTapGesture
*tap
, const QTransform
&transform
);
331 void tapAndHoldTriggered(QGestureEvent
*event
, const QTransform
&transform
);
332 void pinchTriggered(QGestureEvent
*event
, const QTransform
&transform
);
333 void swipeTriggered(QGestureEvent
*event
, const QTransform
&transform
);
334 void twoFingerTapTriggered(QGestureEvent
*event
, const QTransform
&transform
);
335 bool onPress(const QPoint
&screenPos
, const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
);
336 bool onRelease(const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
);
337 void startRubberBand();
340 bool m_singleClickActivationEnforced
;
341 bool m_selectionMode
;
342 bool m_selectionTogglePressed
;
343 bool m_clearSelectionIfItemsAreNotDragged
;
344 bool m_isSwipeGesture
;
345 bool m_dragActionOrRightClick
;
346 bool m_scrollerIsScrolling
;
347 bool m_pinchGestureInProgress
;
350 SelectionBehavior m_selectionBehavior
;
351 AutoActivationBehavior m_autoActivationBehavior
;
352 MouseDoubleClickAction m_mouseDoubleClickAction
;
353 KItemModelBase
*m_model
;
354 KItemListView
*m_view
;
355 KItemListSelectionManager
*m_selectionManager
;
356 KItemListKeyboardSearchManager
*m_keyboardManager
;
357 std::optional
<int> m_pressedIndex
;
358 QPointF m_pressedMousePos
;
360 QTimer
*m_autoActivationTimer
;
362 Qt::GestureType m_swipeGesture
;
363 Qt::GestureType m_twoFingerTapGesture
;
366 * When starting a rubberband selection during a Shift- or Control-key has been
367 * pressed the current selection should never be deleted. To be able to restore
368 * the current selection it is remembered in m_oldSelection before the
369 * rubberband gets activated.
371 KItemSet m_oldSelection
;
374 * Assuming a view is given with a vertical scroll-orientation, grouped items and
375 * a maximum of 4 columns:
382 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
383 * item. Now when pressing key-down again item 11 should get the current item and not
384 * item 10. This makes it necessary to keep track of the requested column to have a
385 * similar behavior like in a text editor:
387 int m_keyboardAnchorIndex
;
388 qreal m_keyboardAnchorPos
;