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
12 #include "dolphin_export.h"
21 class KItemListKeyboardSearchManager
;
22 class KItemListSelectionManager
;
24 class KItemListWidget
;
26 class QGraphicsSceneHoverEvent
;
27 class QGraphicsSceneDragDropEvent
;
28 class QGraphicsSceneMouseEvent
;
29 class QGraphicsSceneResizeEvent
;
30 class QGraphicsSceneWheelEvent
;
31 class QInputMethodEvent
;
37 * @brief Controls the view, model and selection of an item-list.
39 * For a working item-list it is mandatory to set a compatible view and model
40 * with KItemListController::setView() and KItemListController::setModel().
44 * @see KItemListSelectionManager
46 class DOLPHIN_EXPORT KItemListController
: public QObject
49 Q_PROPERTY(KItemModelBase
* model READ model WRITE setModel
)
50 Q_PROPERTY(KItemListView
*view READ view WRITE setView
)
51 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior
)
52 Q_PROPERTY(AutoActivationBehavior autoActivationBehavior READ autoActivationBehavior WRITE setAutoActivationBehavior
)
53 Q_PROPERTY(MouseDoubleClickAction mouseDoubleClickAction READ mouseDoubleClickAction WRITE setMouseDoubleClickAction
)
56 enum SelectionBehavior
{
61 Q_ENUM(SelectionBehavior
)
63 enum AutoActivationBehavior
{
64 ActivationAndExpansion
,
68 enum MouseDoubleClickAction
{
69 ActivateAndExpandItem
,
74 * @param model Model of the controller. The ownership is passed to the controller.
75 * @param view View of the controller. The ownership is passed to the controller.
76 * @param parent Optional parent object.
78 KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
= nullptr);
79 ~KItemListController() override
;
81 void setModel(KItemModelBase
* model
);
82 KItemModelBase
* model() const;
84 void setView(KItemListView
* view
);
85 KItemListView
* view() const;
87 KItemListSelectionManager
* selectionManager() const;
89 void setSelectionBehavior(SelectionBehavior behavior
);
90 SelectionBehavior
selectionBehavior() const;
92 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
93 AutoActivationBehavior
autoActivationBehavior() const;
95 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
96 MouseDoubleClickAction
mouseDoubleClickAction() const;
98 int indexCloseToMousePressedPosition() const;
101 * Sets the delay in milliseconds when dragging an object above an item
102 * until the item gets activated automatically. A value of -1 indicates
103 * that no automatic activation will be done at all (= default value).
105 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
106 * otherwise the automatic activation is not available.
108 * After activating the item the signal itemActivated() will be
109 * emitted. If the view supports the expanding of items
110 * (KItemListView::supportsItemExpanding() returns true) and the item
111 * itself is expandable (see KItemModelBase::isExpandable()) then instead
112 * of activating the item it gets expanded instead (see
113 * KItemModelBase::setExpanded()).
115 void setAutoActivationDelay(int delay
);
116 int autoActivationDelay() const;
119 * If set to true, the signals itemActivated() and itemsActivated() are emitted
120 * after a single-click of the left mouse button. If set to false (the default),
121 * the setting from style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) is used.
123 void setSingleClickActivationEnforced(bool singleClick
);
124 bool singleClickActivationEnforced() const;
126 bool processEvent(QEvent
* event
, const QTransform
& transform
);
130 * Is emitted if exactly one item has been activated by e.g. a mouse-click
131 * or by pressing Return/Enter.
133 void itemActivated(int index
);
136 * Is emitted if more than one item has been activated by pressing Return/Enter
137 * when having a selection.
139 void itemsActivated(const KItemSet
& indexes
);
141 void itemMiddleClicked(int index
);
144 * Emitted if a context-menu is requested for the item with
145 * the index \a index. It is assured that the index is valid.
147 void itemContextMenuRequested(int index
, const QPointF
& pos
);
150 * Emitted if a context-menu is requested for the KItemListView.
152 void viewContextMenuRequested(const QPointF
& pos
);
155 * Emitted if a context-menu is requested for the header of the KItemListView.
157 void headerContextMenuRequested(const QPointF
& pos
);
160 * Is emitted if the item with the index \p index gets hovered.
162 void itemHovered(int index
);
165 * Is emitted if the item with the index \p index gets unhovered.
166 * It is assured that the signal itemHovered() for this index
167 * has been emitted before.
169 void itemUnhovered(int index
);
172 * Is emitted if a mouse-button has been pressed above an item.
173 * If the index is smaller than 0, the mouse-button has been pressed
174 * above the viewport.
176 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
179 * Is emitted if a mouse-button has been released above an item.
180 * It is assured that the signal mouseButtonPressed() has been emitted before.
181 * If the index is smaller than 0, the mouse-button has been pressed
182 * above the viewport.
184 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
186 void itemExpansionToggleClicked(int index
);
189 * Is emitted if a drop event is done above the item with the index
190 * \a index. If \a index is < 0 the drop event is done above an
191 * empty area of the view.
192 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
193 * which is emitted if the drop event occurs on an empty area in
194 * the view, and make sure that index is always >= 0 in itemDropEvent().
196 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
199 * Is emitted if a drop event is done between the item with the index
200 * \a index and the previous item.
202 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
205 * Is emitted if the Escape key is pressed.
207 void escapePressed();
209 void modelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
210 void viewChanged(KItemListView
* current
, KItemListView
* previous
);
212 void selectedItemTextPressed(int index
);
220 void slotStateChanged(QScroller::State newState
);
223 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
226 * Is invoked when the rubberband boundaries have been changed and will select
227 * all items that are touched by the rubberband.
229 void slotRubberBandChanged();
231 void slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
);
233 void slotAutoActivationTimeout();
237 * Creates a QDrag object and initiates a drag-operation.
239 void startDragging();
242 * @return Widget that is currently in the hovered state. 0 is returned
243 * if no widget is marked as hovered.
245 KItemListWidget
* hoveredWidget() const;
248 * @return Widget that is below the position \a pos. 0 is returned
249 * if no widget is below the position.
251 KItemListWidget
* widgetForPos(const QPointF
& pos
) const;
254 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
255 * set, it will be adjusted to the current item. If it is set it will be
256 * checked whether it is still valid, otherwise it will be reset to the
259 void updateKeyboardAnchor();
262 * @return Index for the next row based on \a index.
263 * If there is no next row \a index will be returned.
265 int nextRowIndex(int index
) const;
268 * @return Index for the previous row based on \a index.
269 * If there is no previous row \a index will be returned.
271 int previousRowIndex(int index
) const;
274 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
275 * @return The position of the keyboard anchor for the item with the index \a index.
276 * If a horizontal scrolling is used the y-position of the item will be returned,
277 * for the vertical scrolling the x-position will be returned.
279 qreal
keyboardAnchorPos(int index
) const;
282 * Dependent on the selection-behavior the extendedSelectionRegion-property
283 * of the KItemListStyleOption from the view should be adjusted: If no
284 * rubberband selection is used the property should be enabled.
286 void updateExtendedSelectionRegion();
288 bool keyPressEvent(QKeyEvent
* event
);
289 bool inputMethodEvent(QInputMethodEvent
* event
);
290 bool mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
291 bool mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
292 bool mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
293 bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
294 bool dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
295 bool dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
296 bool dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
297 bool dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
298 bool hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
299 bool hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
300 bool hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
301 bool wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
);
302 bool resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
);
303 bool gestureEvent(QGestureEvent
* event
, const QTransform
& transform
);
304 void tapTriggered(QTapGesture
* tap
, const QTransform
& transform
);
305 void tapAndHoldTriggered(QGestureEvent
* event
, const QTransform
& transform
);
306 void pinchTriggered(QGestureEvent
* event
, const QTransform
& transform
);
307 void swipeTriggered(QGestureEvent
* event
, const QTransform
& transform
);
308 void twoFingerTapTriggered(QGestureEvent
* event
, const QTransform
& transform
);
309 bool onPress(const QPoint
& screenPos
, const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
);
310 bool onRelease(const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
);
311 void startRubberBand();
314 bool m_singleClickActivationEnforced
;
315 bool m_selectionTogglePressed
;
316 bool m_clearSelectionIfItemsAreNotDragged
;
317 bool m_isSwipeGesture
;
318 bool m_dragActionOrRightClick
;
319 bool m_scrollerIsScrolling
;
320 bool m_pinchGestureInProgress
;
322 SelectionBehavior m_selectionBehavior
;
323 AutoActivationBehavior m_autoActivationBehavior
;
324 MouseDoubleClickAction m_mouseDoubleClickAction
;
325 KItemModelBase
* m_model
;
326 KItemListView
* m_view
;
327 KItemListSelectionManager
* m_selectionManager
;
328 KItemListKeyboardSearchManager
* m_keyboardManager
;
330 QPointF m_pressedMousePos
;
332 QTimer
* m_autoActivationTimer
;
334 Qt::GestureType m_swipeGesture
;
335 Qt::GestureType m_twoFingerTapGesture
;
336 Qt::MouseEventSource m_lastSource
;
339 * When starting a rubberband selection during a Shift- or Control-key has been
340 * pressed the current selection should never be deleted. To be able to restore
341 * the current selection it is remembered in m_oldSelection before the
342 * rubberband gets activated.
344 KItemSet m_oldSelection
;
347 * Assuming a view is given with a vertical scroll-orientation, grouped items and
348 * a maximum of 4 columns:
355 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
356 * item. Now when pressing key-down again item 11 should get the current item and not
357 * item 10. This makes it necessary to keep track of the requested column to have a
358 * similar behavior like in a text editor:
360 int m_keyboardAnchorIndex
;
361 qreal m_keyboardAnchorPos
;