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