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
;
27 class QContextMenuEvent
;
29 class QGraphicsSceneHoverEvent
;
30 class QGraphicsSceneDragDropEvent
;
31 class QGraphicsSceneMouseEvent
;
32 class QGraphicsSceneResizeEvent
;
33 class QGraphicsSceneWheelEvent
;
34 class QInputMethodEvent
;
41 * @brief Controls the view, model and selection of an item-list.
43 * For a working item-list it is mandatory to set a compatible view and model
44 * with KItemListController::setView() and KItemListController::setModel().
48 * @see KItemListSelectionManager
50 class DOLPHIN_EXPORT KItemListController
: public QObject
53 Q_PROPERTY(KItemModelBase
*model READ model WRITE setModel NOTIFY modelChanged
)
54 Q_PROPERTY(KItemListView
*view READ view WRITE setView NOTIFY viewChanged
)
57 enum SelectionBehavior
{ NoSelection
, SingleSelection
, MultiSelection
};
58 Q_ENUM(SelectionBehavior
)
60 enum AutoActivationBehavior
{ ActivationAndExpansion
, ExpansionOnly
};
62 enum MouseDoubleClickAction
{ ActivateAndExpandItem
, ActivateItemOnly
};
65 * @param model Model of the controller. The ownership is passed to the controller.
66 * @param view View of the controller. The ownership is passed to the controller.
67 * @param parent Optional parent object.
69 KItemListController(KItemModelBase
*model
, KItemListView
*view
, QObject
*parent
= nullptr);
70 ~KItemListController() override
;
72 void setModel(KItemModelBase
*model
);
73 KItemModelBase
*model() const;
75 void setView(KItemListView
*view
);
76 KItemListView
*view() const;
78 KItemListSelectionManager
*selectionManager() const;
80 void setSelectionBehavior(SelectionBehavior behavior
);
81 SelectionBehavior
selectionBehavior() const;
83 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
84 AutoActivationBehavior
autoActivationBehavior() const;
86 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
87 MouseDoubleClickAction
mouseDoubleClickAction() const;
89 int indexCloseToMousePressedPosition() const;
91 void setAutoActivationEnabled(bool enabled
);
92 bool isAutoActivationEnabled() const;
95 * If set to true, the signals itemActivated() and itemsActivated() are emitted
96 * after a single-click of the left mouse button. If set to false (the default),
97 * the setting from style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) is used.
99 void setSingleClickActivationEnforced(bool singleClick
);
100 bool singleClickActivationEnforced() const;
103 * Setting the selection mode to enabled will make selecting and deselecting easier by acting
104 * kind of similar to when the Control Key is held down.
106 void setSelectionModeEnabled(bool enabled
);
107 bool selectionMode() const;
110 * @return \c true if search as you type is active, or \c false otherwise.
112 bool isSearchAsYouTypeActive() const;
114 bool processEvent(QEvent
*event
, const QTransform
&transform
);
118 * Is emitted if exactly one item has been activated by e.g. a mouse-click
119 * or by pressing Return/Enter.
121 void itemActivated(int index
);
124 * Is emitted if more than one item has been activated by pressing Return/Enter
125 * when having a selection.
127 void itemsActivated(const KItemSet
&indexes
);
129 void itemMiddleClicked(int index
);
132 * Emitted if a context-menu is requested for the item with
133 * the index \a index. It is assured that the index is valid.
135 void itemContextMenuRequested(int index
, const QPointF
&pos
);
138 * Emitted if a context-menu is requested for the KItemListView.
140 void viewContextMenuRequested(const QPointF
&pos
);
143 * Emitted if a context-menu is requested for the header of the KItemListView.
145 void headerContextMenuRequested(const QPointF
&pos
);
148 * Is emitted if the item with the index \p index gets hovered.
150 void itemHovered(int index
);
153 * Is emitted if the item with the index \p index gets unhovered.
154 * It is assured that the signal itemHovered() for this index
155 * has been emitted before.
157 void itemUnhovered(int index
);
160 * Is emitted if a mouse-button has been pressed above an item.
161 * If the index is smaller than 0, the mouse-button has been pressed
162 * above the viewport.
164 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
167 * Is emitted if a mouse-button has been released above an item.
168 * It is assured that the signal mouseButtonPressed() has been emitted before.
169 * If the index is smaller than 0, the mouse-button has been pressed
170 * above the viewport.
172 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
174 void itemExpansionToggleClicked(int index
);
177 * Is emitted if a drop event is done above the item with the index
178 * \a index. If \a index is < 0 the drop event is done above an
179 * empty area of the view.
180 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
181 * which is emitted if the drop event occurs on an empty area in
182 * the view, and make sure that index is always >= 0 in itemDropEvent().
184 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
);
187 * Is emitted if a drop event is done between the item with the index
188 * \a index and the previous item.
190 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
);
193 * Is emitted if the Escape key is pressed.
195 void escapePressed();
198 * Used to request either entering or leaving of selection mode
199 * Leaving is requested by pressing Escape when no item is selected.
201 * Entering is requested if left click is pressed down for a long time without moving the cursor too much.
202 * Moving the cursor would either trigger an item drag if the click was initiated on top of an item
203 * or a selection rectangle if the click was not initiated on top of an item.
204 * So long press is only emitted if there wasn't a lot of cursor movement.
206 void selectionModeChangeRequested(bool enabled
);
208 void modelChanged(KItemModelBase
*current
, KItemModelBase
*previous
);
209 void viewChanged(KItemListView
*current
, KItemListView
*previous
);
211 void selectedItemTextPressed(int index
);
219 * Emitted when the view's background is double-clicked.
220 * Used to trigger an user configured action.
222 void doubleClickViewBackground(Qt::MouseButton button
);
225 void slotStateChanged(QScroller::State newState
);
228 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
231 * Is invoked when the rubberband boundaries have been changed and will select
232 * all items that are touched by the rubberband.
234 void slotRubberBandChanged();
236 void slotChangeCurrentItem(const QString
&text
, bool searchFromNextItem
);
238 void slotAutoActivationTimeout();
242 * Creates a QDrag object and initiates a drag-operation.
244 void startDragging();
247 * @return Widget that is currently in the hovered state. 0 is returned
248 * if no widget is marked as hovered.
250 KItemListWidget
*hoveredWidget() const;
253 * @return Widget that is below the position \a pos. 0 is returned
254 * if no widget is below the position.
256 KItemListWidget
*widgetForPos(const QPointF
&pos
) const;
259 * @return Widget that should receive a drop event if an item is dropped at \a pos. 0 is returned
260 * if no widget should receive a drop event at the position.
262 * While widgetForPos() returns a widget if \a pos is anywhere inside the hover highlight area of the widget,
263 * widgetForDropPos() only returns a widget if \a pos is directly above the widget (widget->contains(pos) == true).
265 KItemListWidget
*widgetForDropPos(const QPointF
&pos
) const;
268 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
269 * set, it will be adjusted to the current item. If it is set it will be
270 * checked whether it is still valid, otherwise it will be reset to the
273 void updateKeyboardAnchor();
276 * @return Index for the next row based on \a index.
277 * If there is no next row \a index will be returned.
279 int nextRowIndex(int index
) const;
282 * @return Index for the previous row based on \a index.
283 * If there is no previous row \a index will be returned.
285 int previousRowIndex(int index
) const;
288 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
289 * @return The position of the keyboard anchor for the item with the index \a index.
290 * If a horizontal scrolling is used the y-position of the item will be returned,
291 * for the vertical scrolling the x-position will be returned.
293 qreal
keyboardAnchorPos(int index
) const;
296 * Dependent on the selection-behavior the extendedSelectionRegion-property
297 * of the KItemListStyleOption from the view should be adjusted: If no
298 * rubberband selection is used the property should be enabled.
300 void updateExtendedSelectionRegion();
302 bool keyPressEvent(QKeyEvent
*event
);
303 bool inputMethodEvent(QInputMethodEvent
*event
);
304 bool mousePressEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
305 bool mouseMoveEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
306 bool mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
307 bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
*event
, const QTransform
&transform
);
308 bool contextMenuEvent(QContextMenuEvent
*event
);
309 bool dragEnterEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
310 bool dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
311 bool dragMoveEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
312 bool dropEvent(QGraphicsSceneDragDropEvent
*event
, const QTransform
&transform
);
313 bool hoverEnterEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
);
314 bool hoverMoveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
);
315 bool hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
, const QTransform
&transform
);
316 bool wheelEvent(QGraphicsSceneWheelEvent
*event
, const QTransform
&transform
);
317 bool resizeEvent(QGraphicsSceneResizeEvent
*event
, const QTransform
&transform
);
318 bool gestureEvent(QGestureEvent
*event
, const QTransform
&transform
);
319 bool touchBeginEvent(QTouchEvent
*event
, const QTransform
&transform
);
320 void tapTriggered(QTapGesture
*tap
, const QTransform
&transform
);
321 void tapAndHoldTriggered(QGestureEvent
*event
, const QTransform
&transform
);
322 void pinchTriggered(QGestureEvent
*event
, const QTransform
&transform
);
323 void swipeTriggered(QGestureEvent
*event
, const QTransform
&transform
);
324 void twoFingerTapTriggered(QGestureEvent
*event
, const QTransform
&transform
);
325 bool onPress(const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
);
326 bool onRelease(const QPointF
&pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
);
327 void startRubberBand();
330 bool m_singleClickActivationEnforced
;
331 bool m_selectionMode
;
332 bool m_selectionTogglePressed
;
333 bool m_clearSelectionIfItemsAreNotDragged
;
334 bool m_isSwipeGesture
;
335 bool m_dragActionOrRightClick
;
336 bool m_scrollerIsScrolling
;
337 bool m_pinchGestureInProgress
;
340 SelectionBehavior m_selectionBehavior
;
341 AutoActivationBehavior m_autoActivationBehavior
;
342 MouseDoubleClickAction m_mouseDoubleClickAction
;
343 KItemModelBase
*m_model
;
344 KItemListView
*m_view
;
345 KItemListSelectionManager
*m_selectionManager
;
346 KItemListKeyboardSearchManager
*m_keyboardManager
;
347 std::optional
<int> m_pressedIndex
;
348 QPointF m_pressedMouseGlobalPos
;
350 bool m_autoActivationEnabled
= false;
351 QTimer
*m_autoActivationTimer
;
353 Qt::GestureType m_swipeGesture
;
354 Qt::GestureType m_twoFingerTapGesture
;
357 * When starting a rubberband selection during a Shift- or Control-key has been
358 * pressed the current selection should never be deleted. To be able to restore
359 * the current selection it is remembered in m_oldSelection before the
360 * rubberband gets activated.
362 KItemSet m_oldSelection
;
365 * Assuming a view is given with a vertical scroll-orientation, grouped items and
366 * a maximum of 4 columns:
373 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
374 * item. Now when pressing key-down again item 11 should get the current item and not
375 * item 10. This makes it necessary to keep track of the requested column to have a
376 * similar behavior like in a text editor:
378 int m_keyboardAnchorIndex
;
379 qreal m_keyboardAnchorPos
;