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"
20 class KItemListKeyboardSearchManager
;
21 class KItemListSelectionManager
;
23 class KItemListWidget
;
24 class QGraphicsSceneHoverEvent
;
25 class QGraphicsSceneDragDropEvent
;
26 class QGraphicsSceneMouseEvent
;
27 class QGraphicsSceneResizeEvent
;
28 class QGraphicsSceneWheelEvent
;
29 class QInputMethodEvent
;
34 * @brief Controls the view, model and selection of an item-list.
36 * For a working item-list it is mandatory to set a compatible view and model
37 * with KItemListController::setView() and KItemListController::setModel().
41 * @see KItemListSelectionManager
43 class DOLPHIN_EXPORT KItemListController
: public QObject
46 Q_PROPERTY(KItemModelBase
* model READ model WRITE setModel
)
47 Q_PROPERTY(KItemListView
*view READ view WRITE setView
)
48 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior
)
49 Q_PROPERTY(AutoActivationBehavior autoActivationBehavior READ autoActivationBehavior WRITE setAutoActivationBehavior
)
50 Q_PROPERTY(MouseDoubleClickAction mouseDoubleClickAction READ mouseDoubleClickAction WRITE setMouseDoubleClickAction
)
53 enum SelectionBehavior
{
58 Q_ENUM(SelectionBehavior
)
60 enum AutoActivationBehavior
{
61 ActivationAndExpansion
,
65 enum MouseDoubleClickAction
{
66 ActivateAndExpandItem
,
71 * @param model Model of the controller. The ownership is passed to the controller.
72 * @param view View of the controller. The ownership is passed to the controller.
73 * @param parent Optional parent object.
75 KItemListController(KItemModelBase
* model
, KItemListView
* view
, QObject
* parent
= nullptr);
76 ~KItemListController() override
;
78 void setModel(KItemModelBase
* model
);
79 KItemModelBase
* model() const;
81 void setView(KItemListView
* view
);
82 KItemListView
* view() const;
84 KItemListSelectionManager
* selectionManager() const;
86 void setSelectionBehavior(SelectionBehavior behavior
);
87 SelectionBehavior
selectionBehavior() const;
89 void setAutoActivationBehavior(AutoActivationBehavior behavior
);
90 AutoActivationBehavior
autoActivationBehavior() const;
92 void setMouseDoubleClickAction(MouseDoubleClickAction action
);
93 MouseDoubleClickAction
mouseDoubleClickAction() const;
95 int indexCloseToMousePressedPosition() const;
98 * Sets the delay in milliseconds when dragging an object above an item
99 * until the item gets activated automatically. A value of -1 indicates
100 * that no automatic activation will be done at all (= default value).
102 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
103 * otherwise the automatic activation is not available.
105 * After activating the item the signal itemActivated() will be
106 * emitted. If the view supports the expanding of items
107 * (KItemListView::supportsItemExpanding() returns true) and the item
108 * itself is expandable (see KItemModelBase::isExpandable()) then instead
109 * of activating the item it gets expanded instead (see
110 * KItemModelBase::setExpanded()).
112 void setAutoActivationDelay(int delay
);
113 int autoActivationDelay() const;
116 * If set to true, the signals itemActivated() and itemsActivated() are emitted
117 * after a single-click of the left mouse button. If set to false (the default),
118 * the setting from style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) is used.
120 void setSingleClickActivationEnforced(bool singleClick
);
121 bool singleClickActivationEnforced() const;
123 bool processEvent(QEvent
* event
, const QTransform
& transform
);
127 * Is emitted if exactly one item has been activated by e.g. a mouse-click
128 * or by pressing Return/Enter.
130 void itemActivated(int index
);
133 * Is emitted if more than one item has been activated by pressing Return/Enter
134 * when having a selection.
136 void itemsActivated(const KItemSet
& indexes
);
138 void itemMiddleClicked(int index
);
141 * Emitted if a context-menu is requested for the item with
142 * the index \a index. It is assured that the index is valid.
144 void itemContextMenuRequested(int index
, const QPointF
& pos
);
147 * Emitted if a context-menu is requested for the KItemListView.
149 void viewContextMenuRequested(const QPointF
& pos
);
152 * Emitted if a context-menu is requested for the header of the KItemListView.
154 void headerContextMenuRequested(const QPointF
& pos
);
157 * Is emitted if the item with the index \p index gets hovered.
159 void itemHovered(int index
);
162 * Is emitted if the item with the index \p index gets unhovered.
163 * It is assured that the signal itemHovered() for this index
164 * has been emitted before.
166 void itemUnhovered(int index
);
169 * Is emitted if a mouse-button has been pressed above an item.
170 * If the index is smaller than 0, the mouse-button has been pressed
171 * above the viewport.
173 void mouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
176 * Is emitted if a mouse-button has been released above an item.
177 * It is assured that the signal mouseButtonPressed() has been emitted before.
178 * If the index is smaller than 0, the mouse-button has been pressed
179 * above the viewport.
181 void mouseButtonReleased(int itemIndex
, Qt::MouseButtons buttons
);
183 void itemExpansionToggleClicked(int index
);
186 * Is emitted if a drop event is done above the item with the index
187 * \a index. If \a index is < 0 the drop event is done above an
188 * empty area of the view.
189 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
190 * which is emitted if the drop event occurs on an empty area in
191 * the view, and make sure that index is always >= 0 in itemDropEvent().
193 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
196 * Is emitted if a drop event is done between the item with the index
197 * \a index and the previous item.
199 void aboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
202 * Is emitted if the Escape key is pressed.
204 void escapePressed();
206 void modelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
207 void viewChanged(KItemListView
* current
, KItemListView
* previous
);
209 void selectedItemTextPressed(int index
);
212 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
215 * Is invoked when the rubberband boundaries have been changed and will select
216 * all items that are touched by the rubberband.
218 void slotRubberBandChanged();
220 void slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
);
222 void slotAutoActivationTimeout();
226 * Creates a QDrag object and initiates a drag-operation.
228 void startDragging();
231 * @return Widget that is currently in the hovered state. 0 is returned
232 * if no widget is marked as hovered.
234 KItemListWidget
* hoveredWidget() const;
237 * @return Widget that is below the position \a pos. 0 is returned
238 * if no widget is below the position.
240 KItemListWidget
* widgetForPos(const QPointF
& pos
) const;
243 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
244 * set, it will be adjusted to the current item. If it is set it will be
245 * checked whether it is still valid, otherwise it will be reset to the
248 void updateKeyboardAnchor();
251 * @return Index for the next row based on \a index.
252 * If there is no next row \a index will be returned.
254 int nextRowIndex(int index
) const;
257 * @return Index for the previous row based on \a index.
258 * If there is no previous row \a index will be returned.
260 int previousRowIndex(int index
) const;
263 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
264 * @return The position of the keyboard anchor for the item with the index \a index.
265 * If a horizontal scrolling is used the y-position of the item will be returned,
266 * for the vertical scrolling the x-position will be returned.
268 qreal
keyboardAnchorPos(int index
) const;
271 * Dependent on the selection-behavior the extendedSelectionRegion-property
272 * of the KItemListStyleOption from the view should be adjusted: If no
273 * rubberband selection is used the property should be enabled.
275 void updateExtendedSelectionRegion();
277 bool keyPressEvent(QKeyEvent
* event
);
278 bool inputMethodEvent(QInputMethodEvent
* event
);
279 bool mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
280 bool mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
281 bool mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
282 bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
283 bool dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
284 bool dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
285 bool dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
286 bool dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
287 bool hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
288 bool hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
289 bool hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
290 bool wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
);
291 bool resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
);
294 bool m_singleClickActivationEnforced
;
295 bool m_selectionTogglePressed
;
296 bool m_clearSelectionIfItemsAreNotDragged
;
297 SelectionBehavior m_selectionBehavior
;
298 AutoActivationBehavior m_autoActivationBehavior
;
299 MouseDoubleClickAction m_mouseDoubleClickAction
;
300 KItemModelBase
* m_model
;
301 KItemListView
* m_view
;
302 KItemListSelectionManager
* m_selectionManager
;
303 KItemListKeyboardSearchManager
* m_keyboardManager
;
305 QPointF m_pressedMousePos
;
307 QTimer
* m_autoActivationTimer
;
310 * When starting a rubberband selection during a Shift- or Control-key has been
311 * pressed the current selection should never be deleted. To be able to restore
312 * the current selection it is remembered in m_oldSelection before the
313 * rubberband gets activated.
315 KItemSet m_oldSelection
;
318 * Assuming a view is given with a vertical scroll-orientation, grouped items and
319 * a maximum of 4 columns:
326 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
327 * item. Now when pressing key-down again item 11 should get the current item and not
328 * item 10. This makes it necessary to keep track of the requested column to have a
329 * similar behavior like in a text editor:
331 int m_keyboardAnchorIndex
;
332 qreal m_keyboardAnchorPos
;