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
;
38 * @brief Controls the view, model and selection of an item-list.
40 * For a working item-list it is mandatory to set a compatible view and model
41 * with KItemListController::setView() and KItemListController::setModel().
45 * @see KItemListSelectionManager
47 class DOLPHIN_EXPORT KItemListController
: public QObject
50 Q_PROPERTY(KItemModelBase
* model READ model WRITE setModel
)
51 Q_PROPERTY(KItemListView
*view READ view WRITE setView
)
52 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior
)
53 Q_PROPERTY(AutoActivationBehavior autoActivationBehavior READ autoActivationBehavior WRITE setAutoActivationBehavior
)
54 Q_PROPERTY(MouseDoubleClickAction mouseDoubleClickAction READ mouseDoubleClickAction WRITE setMouseDoubleClickAction
)
57 enum SelectionBehavior
{
62 Q_ENUM(SelectionBehavior
)
64 enum AutoActivationBehavior
{
65 ActivationAndExpansion
,
69 enum MouseDoubleClickAction
{
70 ActivateAndExpandItem
,
75 * @param model Model of the controller. The ownership is passed to the controller.
76 * @param view View of the controller. The ownership is passed to the controller.
77 * @param parent Optional parent object.
79 KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
= nullptr);
80 ~KItemListController() override
;
82 void setModel(KItemModelBase
* model
);
83 KItemModelBase
* model() const;
85 void setView(KItemListView
* view
);
86 KItemListView
* view() const;
88 KItemListSelectionManager
* selectionManager() const;
90 void setSelectionBehavior(SelectionBehavior behavior
);
91 SelectionBehavior
selectionBehavior() const;
93 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
94 AutoActivationBehavior
autoActivationBehavior() const;
96 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
97 MouseDoubleClickAction
mouseDoubleClickAction() const;
99 int indexCloseToMousePressedPosition() const;
102 * Sets the delay in milliseconds when dragging an object above an item
103 * until the item gets activated automatically. A value of -1 indicates
104 * that no automatic activation will be done at all (= default value).
106 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
107 * otherwise the automatic activation is not available.
109 * After activating the item the signal itemActivated() will be
110 * emitted. If the view supports the expanding of items
111 * (KItemListView::supportsItemExpanding() returns true) and the item
112 * itself is expandable (see KItemModelBase::isExpandable()) then instead
113 * of activating the item it gets expanded instead (see
114 * KItemModelBase::setExpanded()).
116 void setAutoActivationDelay(int delay
);
117 int autoActivationDelay() const;
120 * If set to true, the signals itemActivated() and itemsActivated() are emitted
121 * after a single-click of the left mouse button. If set to false (the default),
122 * the setting from style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) is used.
124 void setSingleClickActivationEnforced(bool singleClick
);
125 bool singleClickActivationEnforced() const;
127 bool processEvent(QEvent
* event
, const QTransform
& transform
);
131 * Is emitted if exactly one item has been activated by e.g. a mouse-click
132 * or by pressing Return/Enter.
134 void itemActivated(int index
);
137 * Is emitted if more than one item has been activated by pressing Return/Enter
138 * when having a selection.
140 void itemsActivated(const KItemSet
& indexes
);
142 void itemMiddleClicked(int index
);
145 * Emitted if a context-menu is requested for the item with
146 * the index \a index. It is assured that the index is valid.
148 void itemContextMenuRequested(int index
, const QPointF
& pos
);
151 * Emitted if a context-menu is requested for the KItemListView.
153 void viewContextMenuRequested(const QPointF
& pos
);
156 * Emitted if a context-menu is requested for the header of the KItemListView.
158 void headerContextMenuRequested(const QPointF
& pos
);
161 * Is emitted if the item with the index \p index gets hovered.
163 void itemHovered(int index
);
166 * Is emitted if the item with the index \p index gets unhovered.
167 * It is assured that the signal itemHovered() for this index
168 * has been emitted before.
170 void itemUnhovered(int index
);
173 * Is emitted if a mouse-button has been pressed above an item.
174 * If the index is smaller than 0, the mouse-button has been pressed
175 * above the viewport.
177 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
180 * Is emitted if a mouse-button has been released above an item.
181 * It is assured that the signal mouseButtonPressed() has been emitted before.
182 * If the index is smaller than 0, the mouse-button has been pressed
183 * above the viewport.
185 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
187 void itemExpansionToggleClicked(int index
);
190 * Is emitted if a drop event is done above the item with the index
191 * \a index. If \a index is < 0 the drop event is done above an
192 * empty area of the view.
193 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
194 * which is emitted if the drop event occurs on an empty area in
195 * the view, and make sure that index is always >= 0 in itemDropEvent().
197 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
200 * Is emitted if a drop event is done between the item with the index
201 * \a index and the previous item.
203 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
206 * Is emitted if the Escape key is pressed.
208 void escapePressed();
210 void modelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
211 void viewChanged(KItemListView
* current
, KItemListView
* previous
);
213 void selectedItemTextPressed(int index
);
221 void slotStateChanged(QScroller::State newState
);
224 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
227 * Is invoked when the rubberband boundaries have been changed and will select
228 * all items that are touched by the rubberband.
230 void slotRubberBandChanged();
232 void slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
);
234 void slotAutoActivationTimeout();
238 * Creates a QDrag object and initiates a drag-operation.
240 void startDragging();
243 * @return Widget that is currently in the hovered state. 0 is returned
244 * if no widget is marked as hovered.
246 KItemListWidget
* hoveredWidget() const;
249 * @return Widget that is below the position \a pos. 0 is returned
250 * if no widget is below the position.
252 KItemListWidget
* widgetForPos(const QPointF
& pos
) const;
255 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
256 * set, it will be adjusted to the current item. If it is set it will be
257 * checked whether it is still valid, otherwise it will be reset to the
260 void updateKeyboardAnchor();
263 * @return Index for the next row based on \a index.
264 * If there is no next row \a index will be returned.
266 int nextRowIndex(int index
) const;
269 * @return Index for the previous row based on \a index.
270 * If there is no previous row \a index will be returned.
272 int previousRowIndex(int index
) const;
275 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
276 * @return The position of the keyboard anchor for the item with the index \a index.
277 * If a horizontal scrolling is used the y-position of the item will be returned,
278 * for the vertical scrolling the x-position will be returned.
280 qreal
keyboardAnchorPos(int index
) const;
283 * Dependent on the selection-behavior the extendedSelectionRegion-property
284 * of the KItemListStyleOption from the view should be adjusted: If no
285 * rubberband selection is used the property should be enabled.
287 void updateExtendedSelectionRegion();
289 bool keyPressEvent(QKeyEvent
* event
);
290 bool inputMethodEvent(QInputMethodEvent
* event
);
291 bool mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
292 bool mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
293 bool mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
294 bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
295 bool dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
296 bool dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
297 bool dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
298 bool dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
299 bool hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
300 bool hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
301 bool hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
302 bool wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
);
303 bool resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
);
304 bool gestureEvent(QGestureEvent
* event
, const QTransform
& transform
);
305 bool touchBeginEvent(QTouchEvent
* event
, const QTransform
& transform
);
306 void tapTriggered(QTapGesture
* tap
, const QTransform
& transform
);
307 void tapAndHoldTriggered(QGestureEvent
* event
, const QTransform
& transform
);
308 void pinchTriggered(QGestureEvent
* event
, const QTransform
& transform
);
309 void swipeTriggered(QGestureEvent
* event
, const QTransform
& transform
);
310 void twoFingerTapTriggered(QGestureEvent
* event
, const QTransform
& transform
);
311 bool onPress(const QPoint
& screenPos
, const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
);
312 bool onRelease(const QPointF
& pos
, const Qt::KeyboardModifiers modifiers
, const Qt::MouseButtons buttons
, bool touch
);
313 void startRubberBand();
316 bool m_singleClickActivationEnforced
;
317 bool m_selectionTogglePressed
;
318 bool m_clearSelectionIfItemsAreNotDragged
;
319 bool m_isSwipeGesture
;
320 bool m_dragActionOrRightClick
;
321 bool m_scrollerIsScrolling
;
322 bool m_pinchGestureInProgress
;
325 SelectionBehavior m_selectionBehavior
;
326 AutoActivationBehavior m_autoActivationBehavior
;
327 MouseDoubleClickAction m_mouseDoubleClickAction
;
328 KItemModelBase
* m_model
;
329 KItemListView
* m_view
;
330 KItemListSelectionManager
* m_selectionManager
;
331 KItemListKeyboardSearchManager
* m_keyboardManager
;
333 QPointF m_pressedMousePos
;
335 QTimer
* m_autoActivationTimer
;
337 Qt::GestureType m_swipeGesture
;
338 Qt::GestureType m_twoFingerTapGesture
;
341 * When starting a rubberband selection during a Shift- or Control-key has been
342 * pressed the current selection should never be deleted. To be able to restore
343 * the current selection it is remembered in m_oldSelection before the
344 * rubberband gets activated.
346 KItemSet m_oldSelection
;
349 * Assuming a view is given with a vertical scroll-orientation, grouped items and
350 * a maximum of 4 columns:
357 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
358 * item. Now when pressing key-down again item 11 should get the current item and not
359 * item 10. This makes it necessary to keep track of the requested column to have a
360 * similar behavior like in a text editor:
362 int m_keyboardAnchorIndex
;
363 qreal m_keyboardAnchorPos
;