]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.h
Output of licensedigger + manual cleanup afterwards.
[dolphin.git] / src / kitemviews / kitemlistcontroller.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #ifndef KITEMLISTCONTROLLER_H
10 #define KITEMLISTCONTROLLER_H
11
12 #include "dolphin_export.h"
13 #include "kitemset.h"
14
15 #include <QObject>
16 #include <QPointF>
17
18 class QTimer;
19 class KItemModelBase;
20 class KItemListKeyboardSearchManager;
21 class KItemListSelectionManager;
22 class KItemListView;
23 class KItemListWidget;
24 class QGraphicsSceneHoverEvent;
25 class QGraphicsSceneDragDropEvent;
26 class QGraphicsSceneMouseEvent;
27 class QGraphicsSceneResizeEvent;
28 class QGraphicsSceneWheelEvent;
29 class QInputMethodEvent;
30 class QKeyEvent;
31 class QTransform;
32
33 /**
34 * @brief Controls the view, model and selection of an item-list.
35 *
36 * For a working item-list it is mandatory to set a compatible view and model
37 * with KItemListController::setView() and KItemListController::setModel().
38 *
39 * @see KItemListView
40 * @see KItemModelBase
41 * @see KItemListSelectionManager
42 */
43 class DOLPHIN_EXPORT KItemListController : public QObject
44 {
45 Q_OBJECT
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)
51
52 public:
53 enum SelectionBehavior {
54 NoSelection,
55 SingleSelection,
56 MultiSelection
57 };
58 Q_ENUM(SelectionBehavior)
59
60 enum AutoActivationBehavior {
61 ActivationAndExpansion,
62 ExpansionOnly
63 };
64
65 enum MouseDoubleClickAction {
66 ActivateAndExpandItem,
67 ActivateItemOnly
68 };
69
70 /**
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.
74 */
75 KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent = nullptr);
76 ~KItemListController() override;
77
78 void setModel(KItemModelBase* model);
79 KItemModelBase* model() const;
80
81 void setView(KItemListView* view);
82 KItemListView* view() const;
83
84 KItemListSelectionManager* selectionManager() const;
85
86 void setSelectionBehavior(SelectionBehavior behavior);
87 SelectionBehavior selectionBehavior() const;
88
89 void setAutoActivationBehavior(AutoActivationBehavior behavior);
90 AutoActivationBehavior autoActivationBehavior() const;
91
92 void setMouseDoubleClickAction(MouseDoubleClickAction action);
93 MouseDoubleClickAction mouseDoubleClickAction() const;
94
95 int indexCloseToMousePressedPosition() const;
96
97 /**
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).
101 *
102 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
103 * otherwise the automatic activation is not available.
104 *
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()).
111 */
112 void setAutoActivationDelay(int delay);
113 int autoActivationDelay() const;
114
115 /**
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.
119 */
120 void setSingleClickActivationEnforced(bool singleClick);
121 bool singleClickActivationEnforced() const;
122
123 bool processEvent(QEvent* event, const QTransform& transform);
124
125 signals:
126 /**
127 * Is emitted if exactly one item has been activated by e.g. a mouse-click
128 * or by pressing Return/Enter.
129 */
130 void itemActivated(int index);
131
132 /**
133 * Is emitted if more than one item has been activated by pressing Return/Enter
134 * when having a selection.
135 */
136 void itemsActivated(const KItemSet& indexes);
137
138 void itemMiddleClicked(int index);
139
140 /**
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.
143 */
144 void itemContextMenuRequested(int index, const QPointF& pos);
145
146 /**
147 * Emitted if a context-menu is requested for the KItemListView.
148 */
149 void viewContextMenuRequested(const QPointF& pos);
150
151 /**
152 * Emitted if a context-menu is requested for the header of the KItemListView.
153 */
154 void headerContextMenuRequested(const QPointF& pos);
155
156 /**
157 * Is emitted if the item with the index \p index gets hovered.
158 */
159 void itemHovered(int index);
160
161 /**
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.
165 */
166 void itemUnhovered(int index);
167
168 /**
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.
172 */
173 void mouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
174
175 /**
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.
180 */
181 void mouseButtonReleased(int itemIndex, Qt::MouseButtons buttons);
182
183 void itemExpansionToggleClicked(int index);
184
185 /**
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().
192 */
193 void itemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
194
195 /**
196 * Is emitted if a drop event is done between the item with the index
197 * \a index and the previous item.
198 */
199 void aboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
200
201 /**
202 * Is emitted if the Escape key is pressed.
203 */
204 void escapePressed();
205
206 void modelChanged(KItemModelBase* current, KItemModelBase* previous);
207 void viewChanged(KItemListView* current, KItemListView* previous);
208
209 void selectedItemTextPressed(int index);
210
211 private slots:
212 void slotViewScrollOffsetChanged(qreal current, qreal previous);
213
214 /**
215 * Is invoked when the rubberband boundaries have been changed and will select
216 * all items that are touched by the rubberband.
217 */
218 void slotRubberBandChanged();
219
220 void slotChangeCurrentItem(const QString& text, bool searchFromNextItem);
221
222 void slotAutoActivationTimeout();
223
224 private:
225 /**
226 * Creates a QDrag object and initiates a drag-operation.
227 */
228 void startDragging();
229
230 /**
231 * @return Widget that is currently in the hovered state. 0 is returned
232 * if no widget is marked as hovered.
233 */
234 KItemListWidget* hoveredWidget() const;
235
236 /**
237 * @return Widget that is below the position \a pos. 0 is returned
238 * if no widget is below the position.
239 */
240 KItemListWidget* widgetForPos(const QPointF& pos) const;
241
242 /**
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
246 * current item.
247 */
248 void updateKeyboardAnchor();
249
250 /**
251 * @return Index for the next row based on \a index.
252 * If there is no next row \a index will be returned.
253 */
254 int nextRowIndex(int index) const;
255
256 /**
257 * @return Index for the previous row based on \a index.
258 * If there is no previous row \a index will be returned.
259 */
260 int previousRowIndex(int index) const;
261
262 /**
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.
267 */
268 qreal keyboardAnchorPos(int index) const;
269
270 /**
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.
274 */
275 void updateExtendedSelectionRegion();
276
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);
292
293 private:
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;
304 int m_pressedIndex;
305 QPointF m_pressedMousePos;
306
307 QTimer* m_autoActivationTimer;
308
309 /**
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.
314 */
315 KItemSet m_oldSelection;
316
317 /**
318 * Assuming a view is given with a vertical scroll-orientation, grouped items and
319 * a maximum of 4 columns:
320 *
321 * 1 2 3 4
322 * 5 6 7
323 * 8 9 10 11
324 * 12 13 14
325 *
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:
330 */
331 int m_keyboardAnchorIndex;
332 qreal m_keyboardAnchorPos;
333 };
334
335 #endif
336
337