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