]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.h
[KItemListView] Check if KItemListStyleOption actually changed before emitting a...
[dolphin.git] / src / kitemviews / kitemlistcontroller.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #ifndef KITEMLISTCONTROLLER_H
24 #define KITEMLISTCONTROLLER_H
25
26 #include "dolphin_export.h"
27 #include "kitemset.h"
28
29 #include <QObject>
30 #include <QPointF>
31
32 class QTimer;
33 class KItemModelBase;
34 class KItemListKeyboardSearchManager;
35 class KItemListSelectionManager;
36 class KItemListView;
37 class KItemListWidget;
38 class QGraphicsSceneHoverEvent;
39 class QGraphicsSceneDragDropEvent;
40 class QGraphicsSceneMouseEvent;
41 class QGraphicsSceneResizeEvent;
42 class QGraphicsSceneWheelEvent;
43 class QHideEvent;
44 class QInputMethodEvent;
45 class QKeyEvent;
46 class QShowEvent;
47 class QTransform;
48
49 /**
50 * @brief Controls the view, model and selection of an item-list.
51 *
52 * For a working item-list it is mandatory to set a compatible view and model
53 * with KItemListController::setView() and KItemListController::setModel().
54 *
55 * @see KItemListView
56 * @see KItemModelBase
57 * @see KItemListSelectionManager
58 */
59 class DOLPHIN_EXPORT KItemListController : public QObject
60 {
61 Q_OBJECT
62 Q_PROPERTY(KItemModelBase* model READ model WRITE setModel)
63 Q_PROPERTY(KItemListView *view READ view WRITE setView)
64 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior)
65 Q_PROPERTY(AutoActivationBehavior autoActivationBehavior READ autoActivationBehavior WRITE setAutoActivationBehavior)
66 Q_PROPERTY(MouseDoubleClickAction mouseDoubleClickAction READ mouseDoubleClickAction WRITE setMouseDoubleClickAction)
67
68 public:
69 enum SelectionBehavior {
70 NoSelection,
71 SingleSelection,
72 MultiSelection
73 };
74 Q_ENUM(SelectionBehavior)
75
76 enum AutoActivationBehavior {
77 ActivationAndExpansion,
78 ExpansionOnly
79 };
80
81 enum MouseDoubleClickAction {
82 ActivateAndExpandItem,
83 ActivateItemOnly
84 };
85
86 /**
87 * @param model Model of the controller. The ownership is passed to the controller.
88 * @param view View of the controller. The ownership is passed to the controller.
89 * @param parent Optional parent object.
90 */
91 KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent = nullptr);
92 ~KItemListController() override;
93
94 void setModel(KItemModelBase* model);
95 KItemModelBase* model() const;
96
97 void setView(KItemListView* view);
98 KItemListView* view() const;
99
100 KItemListSelectionManager* selectionManager() const;
101
102 void setSelectionBehavior(SelectionBehavior behavior);
103 SelectionBehavior selectionBehavior() const;
104
105 void setAutoActivationBehavior(AutoActivationBehavior behavior);
106 AutoActivationBehavior autoActivationBehavior() const;
107
108 void setMouseDoubleClickAction(MouseDoubleClickAction action);
109 MouseDoubleClickAction mouseDoubleClickAction() const;
110
111 int indexCloseToMousePressedPosition() const;
112
113 /**
114 * Sets the delay in milliseconds when dragging an object above an item
115 * until the item gets activated automatically. A value of -1 indicates
116 * that no automatic activation will be done at all (= default value).
117 *
118 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
119 * otherwise the automatic activation is not available.
120 *
121 * After activating the item the signal itemActivated() will be
122 * emitted. If the view supports the expanding of items
123 * (KItemListView::supportsItemExpanding() returns true) and the item
124 * itself is expandable (see KItemModelBase::isExpandable()) then instead
125 * of activating the item it gets expanded instead (see
126 * KItemModelBase::setExpanded()).
127 */
128 void setAutoActivationDelay(int delay);
129 int autoActivationDelay() const;
130
131 /**
132 * If set to true, the signals itemActivated() and itemsActivated() are emitted
133 * after a single-click of the left mouse button. If set to false (the default),
134 * the setting from style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) is used.
135 */
136 void setSingleClickActivationEnforced(bool singleClick);
137 bool singleClickActivationEnforced() const;
138
139 virtual bool showEvent(QShowEvent* event);
140 virtual bool hideEvent(QHideEvent* event);
141 virtual bool keyPressEvent(QKeyEvent* event);
142 virtual bool inputMethodEvent(QInputMethodEvent* event);
143 virtual bool mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
144 virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
145 virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
146 virtual bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
147 virtual bool dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
148 virtual bool dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
149 virtual bool dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
150 virtual bool dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
151 virtual bool hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
152 virtual bool hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
153 virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
154 virtual bool wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform);
155 virtual bool resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform);
156 virtual bool processEvent(QEvent* event, const QTransform& transform);
157
158 signals:
159 /**
160 * Is emitted if exactly one item has been activated by e.g. a mouse-click
161 * or by pressing Return/Enter.
162 */
163 void itemActivated(int index);
164
165 /**
166 * Is emitted if more than one item has been activated by pressing Return/Enter
167 * when having a selection.
168 */
169 void itemsActivated(const KItemSet& indexes);
170
171 void itemMiddleClicked(int index);
172
173 /**
174 * Emitted if a context-menu is requested for the item with
175 * the index \a index. It is assured that the index is valid.
176 */
177 void itemContextMenuRequested(int index, const QPointF& pos);
178
179 /**
180 * Emitted if a context-menu is requested for the KItemListView.
181 */
182 void viewContextMenuRequested(const QPointF& pos);
183
184 /**
185 * Emitted if a context-menu is requested for the header of the KItemListView.
186 */
187 void headerContextMenuRequested(const QPointF& pos);
188
189 /**
190 * Is emitted if the item with the index \p index gets hovered.
191 */
192 void itemHovered(int index);
193
194 /**
195 * Is emitted if the item with the index \p index gets unhovered.
196 * It is assured that the signal itemHovered() for this index
197 * has been emitted before.
198 */
199 void itemUnhovered(int index);
200
201 /**
202 * Is emitted if a mouse-button has been pressed above an item.
203 * If the index is smaller than 0, the mouse-button has been pressed
204 * above the viewport.
205 */
206 void mouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
207
208 /**
209 * Is emitted if a mouse-button has been released above an item.
210 * It is assured that the signal mouseButtonPressed() has been emitted before.
211 * If the index is smaller than 0, the mouse-button has been pressed
212 * above the viewport.
213 */
214 void mouseButtonReleased(int itemIndex, Qt::MouseButtons buttons);
215
216 void itemExpansionToggleClicked(int index);
217
218 /**
219 * Is emitted if a drop event is done above the item with the index
220 * \a index. If \a index is < 0 the drop event is done above an
221 * empty area of the view.
222 * TODO: Introduce a new signal viewDropEvent(QGraphicsSceneDragDropEvent),
223 * which is emitted if the drop event occurs on an empty area in
224 * the view, and make sure that index is always >= 0 in itemDropEvent().
225 */
226 void itemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
227
228 /**
229 * Is emitted if a drop event is done between the item with the index
230 * \a index and the previous item.
231 */
232 void aboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
233
234 /**
235 * Is emitted if the Escape key is pressed.
236 */
237 void escapePressed();
238
239 void modelChanged(KItemModelBase* current, KItemModelBase* previous);
240 void viewChanged(KItemListView* current, KItemListView* previous);
241
242 void selectedItemTextPressed(int index);
243
244 private slots:
245 void slotViewScrollOffsetChanged(qreal current, qreal previous);
246
247 /**
248 * Is invoked when the rubberband boundaries have been changed and will select
249 * all items that are touched by the rubberband.
250 */
251 void slotRubberBandChanged();
252
253 void slotChangeCurrentItem(const QString& text, bool searchFromNextItem);
254
255 void slotAutoActivationTimeout();
256
257 private:
258 /**
259 * Creates a QDrag object and initiates a drag-operation.
260 */
261 void startDragging();
262
263 /**
264 * @return Widget that is currently in the hovered state. 0 is returned
265 * if no widget is marked as hovered.
266 */
267 KItemListWidget* hoveredWidget() const;
268
269 /**
270 * @return Widget that is below the position \a pos. 0 is returned
271 * if no widget is below the position.
272 */
273 KItemListWidget* widgetForPos(const QPointF& pos) const;
274
275 /**
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
279 * current item.
280 */
281 void updateKeyboardAnchor();
282
283 /**
284 * @return Index for the next row based on \a index.
285 * If there is no next row \a index will be returned.
286 */
287 int nextRowIndex(int index) const;
288
289 /**
290 * @return Index for the previous row based on \a index.
291 * If there is no previous row \a index will be returned.
292 */
293 int previousRowIndex(int index) const;
294
295 /**
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.
300 */
301 qreal keyboardAnchorPos(int index) const;
302
303 /**
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.
307 */
308 void updateExtendedSelectionRegion();
309
310 private:
311 bool m_singleClickActivationEnforced;
312 bool m_selectionTogglePressed;
313 bool m_clearSelectionIfItemsAreNotDragged;
314 SelectionBehavior m_selectionBehavior;
315 AutoActivationBehavior m_autoActivationBehavior;
316 MouseDoubleClickAction m_mouseDoubleClickAction;
317 KItemModelBase* m_model;
318 KItemListView* m_view;
319 KItemListSelectionManager* m_selectionManager;
320 KItemListKeyboardSearchManager* m_keyboardManager;
321 int m_pressedIndex;
322 QPointF m_pressedMousePos;
323
324 QTimer* m_autoActivationTimer;
325
326 /**
327 * When starting a rubberband selection during a Shift- or Control-key has been
328 * pressed the current selection should never be deleted. To be able to restore
329 * the current selection it is remembered in m_oldSelection before the
330 * rubberband gets activated.
331 */
332 KItemSet m_oldSelection;
333
334 /**
335 * Assuming a view is given with a vertical scroll-orientation, grouped items and
336 * a maximum of 4 columns:
337 *
338 * 1 2 3 4
339 * 5 6 7
340 * 8 9 10 11
341 * 12 13 14
342 *
343 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
344 * item. Now when pressing key-down again item 11 should get the current item and not
345 * item 10. This makes it necessary to keep track of the requested column to have a
346 * similar behavior like in a text editor:
347 */
348 int m_keyboardAnchorIndex;
349 qreal m_keyboardAnchorPos;
350 };
351
352 #endif
353
354