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 NOTIFY modelChanged
)
53 Q_PROPERTY(KItemListView
*view READ view WRITE setView NOTIFY viewChanged
)
56 enum SelectionBehavior
{ NoSelection
, SingleSelection
, MultiSelection
};
57 Q_ENUM(SelectionBehavior
)
59 enum AutoActivationBehavior
{ ActivationAndExpansion
, ExpansionOnly
};
61 enum MouseDoubleClickAction
{ ActivateAndExpandItem
, ActivateItemOnly
};
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.
68 KItemListController(KItemModelBase
*model
, KItemListView
*view
, QObject
*parent
= nullptr);
69 ~KItemListController() override
;
71 void setModel(KItemModelBase
*model
);
72 KItemModelBase
*model() const;
74 void setView(KItemListView
*view
);
75 KItemListView
*view() const;
77 KItemListSelectionManager
*selectionManager() const;
79 void setSelectionBehavior(SelectionBehavior behavior
);
80 SelectionBehavior
selectionBehavior() const;
82 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
83 AutoActivationBehavior
autoActivationBehavior() const;
85 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
86 MouseDoubleClickAction
mouseDoubleClickAction() const;
88 int indexCloseToMousePressedPosition() const;
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).
95 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
96 * otherwise the automatic activation is not available.
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()).
105 void setAutoActivationDelay(int delay
);
106 int autoActivationDelay() const;
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.
113 void setSingleClickActivationEnforced(bool singleClick
);
114 bool singleClickActivationEnforced() const;
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.
120 void setSelectionModeEnabled(bool enabled
);
121 bool selectionMode() const;
124 * @return \c true if search as you type is active, or \c false otherwise.
126 bool isSearchAsYouTypeActive() const;
128 bool processEvent(QEvent
*event
, const QTransform
&transform
);
132 * Is emitted if exactly one item has been activated by e.g. a mouse-click
133 * or by pressing Return/Enter.
135 void itemActivated(int index
);
138 * Is emitted if more than one item has been activated by pressing Return/Enter
139 * when having a selection.
141 void itemsActivated(const KItemSet
&indexes
);
143 void itemMiddleClicked(int index
);
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.
149 void itemContextMenuRequested(int index
, const QPointF
&pos
);
152 * Emitted if a context-menu is requested for the KItemListView.
154 void viewContextMenuRequested(const QPointF
&pos
);
157 * Emitted if a context-menu is requested for the header of the KItemListView.
159 void headerContextMenuRequested(const QPointF
&pos
);
162 * Is emitted if the item with the index \p index gets hovered.
164 void itemHovered(int index
);
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.
171 void itemUnhovered(int index
);
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.
178 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
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.
186 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
188 void itemExpansionToggleClicked(int index
);
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().
198 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
);
201 * Is emitted if a drop event is done between the item with the index
202 * \a index and the previous item.
204 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
);
207 * Is emitted if the Escape key is pressed.
209 void escapePressed();
212 * Used to request either entering or leaving of selection mode
213 * Leaving is requested by pressing Escape when no item is selected.
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.
220 void selectionModeChangeRequested(bool enabled
);
222 void modelChanged(KItemModelBase
*current
, KItemModelBase
*previous
);
223 void viewChanged(KItemListView
*current
, KItemListView
*previous
);
225 void selectedItemTextPressed(int index
);
233 void slotStateChanged(QScroller::State newState
);
236 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
239 * Is invoked when the rubberband boundaries have been changed and will select
240 * all items that are touched by the rubberband.
242 void slotRubberBandChanged();
244 void slotChangeCurrentItem(const QString
&text
, bool searchFromNextItem
);
246 void slotAutoActivationTimeout();
250 * Creates a QDrag object and initiates a drag-operation.
252 void startDragging();
255 * @return Widget that is currently in the hovered state. 0 is returned
256 * if no widget is marked as hovered.
258 KItemListWidget
*hoveredWidget() const;
261 * @return Widget that is below the position \a pos. 0 is returned
262 * if no widget is below the position.
264 KItemListWidget
*widgetForPos(const QPointF
&pos
) const;
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.
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).
273 KItemListWidget
*widgetForDropPos(const QPointF
&pos
) const;
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
281 void updateKeyboardAnchor();
284 * @return Index for the next row based on \a index.
285 * If there is no next row \a index will be returned.
287 int nextRowIndex(int index
) const;
290 * @return Index for the previous row based on \a index.
291 * If there is no previous row \a index will be returned.
293 int previousRowIndex(int index
) const;
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.
301 qreal
keyboardAnchorPos(int index
) const;
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.
308 void updateExtendedSelectionRegion();
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();
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
;
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
;
357 QTimer
*m_autoActivationTimer
;
359 Qt::GestureType m_swipeGesture
;
360 Qt::GestureType m_twoFingerTapGesture
;
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.
368 KItemSet m_oldSelection
;
371 * Assuming a view is given with a vertical scroll-orientation, grouped items and
372 * a maximum of 4 columns:
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:
384 int m_keyboardAnchorIndex
;
385 qreal m_keyboardAnchorPos
;