]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontroller.h
Fix keyboard issues when groups are enabled
[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 <libdolphin_export.h>
27
28 #include <QObject>
29 #include <QPixmap>
30 #include <QPointF>
31 #include <QSet>
32
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 QMimeData;
47 class QShowEvent;
48 class QTransform;
49
50 /**
51 * @brief Controls the view, model and selection of an item-list.
52 *
53 * For a working item-list it is mandatory to set a compatible view and model
54 * with KItemListController::setView() and KItemListController::setModel().
55 *
56 * @see KItemListView
57 * @see KItemModelBase
58 * @see KItemListSelectionManager
59 */
60 class LIBDOLPHINPRIVATE_EXPORT KItemListController : public QObject
61 {
62 Q_OBJECT
63 Q_ENUMS(SelectionBehavior)
64 Q_PROPERTY(KItemModelBase* model READ model WRITE setModel)
65 Q_PROPERTY(KItemListView *view READ view WRITE setView)
66 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior)
67
68 public:
69 enum SelectionBehavior {
70 NoSelection,
71 SingleSelection,
72 MultiSelection
73 };
74
75 KItemListController(QObject* parent = 0);
76 virtual ~KItemListController();
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 /**
90 * Sets the delay in milliseconds when dragging an object above an item
91 * until the item gets activated automatically. A value of -1 indicates
92 * that no automatic activation will be done at all (= default value).
93 *
94 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
95 * otherwise the automatic activation is not available.
96 *
97 * After activating the item the signal itemActivated() will be
98 * emitted. If the view supports the expanding of items
99 * (KItemListView::supportsItemExpanding() returns true) and the item
100 * itself is expandable (see KItemModelBase::isExpandable()) then instead
101 * of activating the item it gets expanded instead (see
102 * KItemModelBase::setExpanded()).
103 */
104 void setAutoActivationDelay(int delay);
105 int autoActivationDelay() const;
106
107 virtual bool showEvent(QShowEvent* event);
108 virtual bool hideEvent(QHideEvent* event);
109 virtual bool keyPressEvent(QKeyEvent* event);
110 virtual bool inputMethodEvent(QInputMethodEvent* event);
111 virtual bool mousePressEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
112 virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
113 virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
114 virtual bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, const QTransform& transform);
115 virtual bool dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
116 virtual bool dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
117 virtual bool dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
118 virtual bool dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform);
119 virtual bool hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
120 virtual bool hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
121 virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform);
122 virtual bool wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform);
123 virtual bool resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform);
124 virtual bool processEvent(QEvent* event, const QTransform& transform);
125
126 signals:
127 /**
128 * Is emitted if exactly one item has been activated by e.g. a mouse-click
129 * or by pressing Return/Enter.
130 */
131 void itemActivated(int index);
132
133 /**
134 * Is emitted if more than one item has been activated by pressing Return/Enter
135 * when having a selection.
136 */
137 void itemsActivated(const QSet<int>& indexes);
138
139 void itemMiddleClicked(int index);
140
141 /**
142 * Emitted if a context-menu is requested for the item with
143 * the index \a index. It is assured that the index is valid.
144 */
145 void itemContextMenuRequested(int index, const QPointF& pos);
146
147 /**
148 * Emitted if a context-menu is requested for the KItemListView.
149 */
150 void viewContextMenuRequested(const QPointF& pos);
151
152 /**
153 * Emitted if a context-menu is requested for the header of the KItemListView.
154 */
155 void headerContextMenuRequested(const QPointF& pos);
156
157 /**
158 * Is emitted if the item with the index \p index gets hovered.
159 */
160 void itemHovered(int index);
161
162 /**
163 * Is emitted if the item with the index \p index gets unhovered.
164 * It is assured that the signal itemHovered() for this index
165 * has been emitted before.
166 */
167 void itemUnhovered(int index);
168
169 /**
170 * Is emitted if a mouse-button has been pressed above an item.
171 */
172 void itemPressed(int index, Qt::MouseButton button);
173
174 /**
175 * Is emitted if a mouse-button has been released above an item.
176 * It is assured that the signal itemPressed() has been emitted before.
177 */
178 void itemReleased(int index, Qt::MouseButton button);
179
180 void itemExpansionToggleClicked(int index);
181
182 /**
183 * Is emitted if a drop event is done above the item with the index
184 * \a index. If \a index is < 0 the drop event is done above an
185 * empty area of the view.
186 */
187 void itemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
188
189 void modelChanged(KItemModelBase* current, KItemModelBase* previous);
190 void viewChanged(KItemListView* current, KItemListView* previous);
191
192 private slots:
193 void slotViewScrollOffsetChanged(qreal current, qreal previous);
194
195 /**
196 * Is invoked when the rubberband boundaries have been changed and will select
197 * all items that are touched by the rubberband.
198 */
199 void slotRubberBandChanged();
200
201 void slotChangeCurrentItem(const QString& text, bool searchFromNextItem);
202
203 void slotAutoActivationTimeout();
204
205 private:
206 /**
207 * Creates a QDrag object and initiates a drag-operation.
208 */
209 void startDragging();
210
211 /**
212 * @return Widget that is currently in the hovered state. 0 is returned
213 * if no widget is marked as hovered.
214 */
215 KItemListWidget* hoveredWidget() const;
216
217 /**
218 * @return Widget that is below the position \a pos. 0 is returned
219 * if no widget is below the position.
220 */
221 KItemListWidget* widgetForPos(const QPointF& pos) const;
222
223 /**
224 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
225 * set, it will be adjusted to the current item. If it is set it will be
226 * checked whether it is still valid, otherwise it will be reset to the
227 * current item.
228 */
229 void updateKeyboardAnchor();
230
231 /**
232 * @return Index for the next row based on the current index.
233 * If there is no next row the current index will be returned.
234 */
235 int nextRowIndex() const;
236
237 /**
238 * @return Index for the previous row based on the current index.
239 * If there is no previous row the current index will be returned.
240 */
241 int previousRowIndex() const;
242
243 /**
244 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
245 * @return The position of the keyboard anchor for the item with the index \a index.
246 * If a horizontal scrolling is used the y-position of the item will be returned,
247 * for the vertical scrolling the x-position will be returned.
248 */
249 qreal keyboardAnchorPos(int index) const;
250
251 private:
252 bool m_selectionTogglePressed;
253 SelectionBehavior m_selectionBehavior;
254 KItemModelBase* m_model;
255 KItemListView* m_view;
256 KItemListSelectionManager* m_selectionManager;
257 KItemListKeyboardSearchManager* m_keyboardManager;
258 int m_pressedIndex;
259 QPointF m_pressedMousePos;
260
261 QTimer* m_autoActivationTimer;
262
263 /**
264 * When starting a rubberband selection during a Shift- or Control-key has been
265 * pressed the current selection should never be deleted. To be able to restore
266 * the current selection it is remembered in m_oldSelection before the
267 * rubberband gets activated.
268 */
269 QSet<int> m_oldSelection;
270
271 /**
272 * Assuming a view is given with a vertical scroll-orientation, grouped items and
273 * a maximum of 4 columns:
274 *
275 * 1 2 3 4
276 * 5 6 7
277 * 8 9 10 11
278 * 12 13 14
279 *
280 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
281 * item. Now when pressing key-down again item 11 should get the current item and not
282 * item 10. This makes it necessary to keep track of the requested column to have a
283 * similar behavior like in a text editor:
284 */
285 int m_keyboardAnchorIndex;
286 qreal m_keyboardAnchorXPos;
287 };
288
289 #endif
290
291