1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #ifndef KITEMLISTCONTROLLER_H
24 #define KITEMLISTCONTROLLER_H
26 #include <libdolphin_export.h>
34 class KItemListKeyboardSearchManager
;
35 class KItemListSelectionManager
;
37 class KItemListWidget
;
38 class QGraphicsSceneHoverEvent
;
39 class QGraphicsSceneDragDropEvent
;
40 class QGraphicsSceneMouseEvent
;
41 class QGraphicsSceneResizeEvent
;
42 class QGraphicsSceneWheelEvent
;
44 class QInputMethodEvent
;
51 * @brief Controls the view, model and selection of an item-list.
53 * For a working item-list it is mandatory to set a compatible view and model
54 * with KItemListController::setView() and KItemListController::setModel().
58 * @see KItemListSelectionManager
60 class LIBDOLPHINPRIVATE_EXPORT KItemListController
: public QObject
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
)
69 enum SelectionBehavior
{
75 KItemListController(QObject
* parent
= 0);
76 virtual ~KItemListController();
78 void setModel(KItemModelBase
* model
);
79 KItemModelBase
* model() const;
81 void setView(KItemListView
* view
);
82 KItemListView
* view() const;
84 KItemListSelectionManager
* selectionManager() const;
86 void setSelectionBehavior(SelectionBehavior behavior
);
87 SelectionBehavior
selectionBehavior() const;
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).
94 * The hovered item must support dropping (see KItemModelBase::supportsDropping()),
95 * otherwise the automatic activation is not available.
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()).
104 void setAutoActivationDelay(int delay
);
105 int autoActivationDelay() const;
108 * If set to true, the signals itemActivated() and itemsActivated() are emitted
109 * after a single-click of the left mouse button. If set to false, a double-click
110 * is required. Per default the setting from KGlobalSettings::singleClick() is
113 void setSingleClickActivation(bool singleClick
);
114 bool singleClickActivation() const;
116 virtual bool showEvent(QShowEvent
* event
);
117 virtual bool hideEvent(QHideEvent
* event
);
118 virtual bool keyPressEvent(QKeyEvent
* event
);
119 virtual bool inputMethodEvent(QInputMethodEvent
* event
);
120 virtual bool mousePressEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
121 virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
122 virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
123 virtual bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent
* event
, const QTransform
& transform
);
124 virtual bool dragEnterEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
125 virtual bool dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
126 virtual bool dragMoveEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
127 virtual bool dropEvent(QGraphicsSceneDragDropEvent
* event
, const QTransform
& transform
);
128 virtual bool hoverEnterEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
129 virtual bool hoverMoveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
130 virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent
* event
, const QTransform
& transform
);
131 virtual bool wheelEvent(QGraphicsSceneWheelEvent
* event
, const QTransform
& transform
);
132 virtual bool resizeEvent(QGraphicsSceneResizeEvent
* event
, const QTransform
& transform
);
133 virtual bool processEvent(QEvent
* event
, const QTransform
& transform
);
137 * Is emitted if exactly one item has been activated by e.g. a mouse-click
138 * or by pressing Return/Enter.
140 void itemActivated(int index
);
143 * Is emitted if more than one item has been activated by pressing Return/Enter
144 * when having a selection.
146 void itemsActivated(const QSet
<int>& indexes
);
148 void itemMiddleClicked(int index
);
151 * Emitted if a context-menu is requested for the item with
152 * the index \a index. It is assured that the index is valid.
154 void itemContextMenuRequested(int index
, const QPointF
& pos
);
157 * Emitted if a context-menu is requested for the KItemListView.
159 void viewContextMenuRequested(const QPointF
& pos
);
162 * Emitted if a context-menu is requested for the header of the KItemListView.
164 void headerContextMenuRequested(const QPointF
& pos
);
167 * Is emitted if the item with the index \p index gets hovered.
169 void itemHovered(int index
);
172 * Is emitted if the item with the index \p index gets unhovered.
173 * It is assured that the signal itemHovered() for this index
174 * has been emitted before.
176 void itemUnhovered(int index
);
179 * Is emitted if a mouse-button has been pressed above an item.
181 void itemPressed(int index
, Qt::MouseButton button
);
184 * Is emitted if a mouse-button has been released above an item.
185 * It is assured that the signal itemPressed() has been emitted before.
187 void itemReleased(int index
, Qt::MouseButton button
);
189 void itemExpansionToggleClicked(int index
);
192 * Is emitted if a drop event is done above the item with the index
193 * \a index. If \a index is < 0 the drop event is done above an
194 * empty area of the view.
196 void itemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
198 void modelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
199 void viewChanged(KItemListView
* current
, KItemListView
* previous
);
202 void slotViewScrollOffsetChanged(qreal current
, qreal previous
);
205 * Is invoked when the rubberband boundaries have been changed and will select
206 * all items that are touched by the rubberband.
208 void slotRubberBandChanged();
210 void slotChangeCurrentItem(const QString
& text
, bool searchFromNextItem
);
212 void slotAutoActivationTimeout();
216 * Creates a QDrag object and initiates a drag-operation.
218 void startDragging();
221 * @return Widget that is currently in the hovered state. 0 is returned
222 * if no widget is marked as hovered.
224 KItemListWidget
* hoveredWidget() const;
227 * @return Widget that is below the position \a pos. 0 is returned
228 * if no widget is below the position.
230 KItemListWidget
* widgetForPos(const QPointF
& pos
) const;
233 * Updates m_keyboardAnchorIndex and m_keyboardAnchorPos. If no anchor is
234 * set, it will be adjusted to the current item. If it is set it will be
235 * checked whether it is still valid, otherwise it will be reset to the
238 void updateKeyboardAnchor();
241 * @return Index for the next row based on \a index.
242 * If there is no next row \a index will be returned.
244 int nextRowIndex(int index
) const;
247 * @return Index for the previous row based on \a index.
248 * If there is no previous row \a index will be returned.
250 int previousRowIndex(int index
) const;
253 * Helper method for updateKeyboardAnchor(), previousRowIndex() and nextRowIndex().
254 * @return The position of the keyboard anchor for the item with the index \a index.
255 * If a horizontal scrolling is used the y-position of the item will be returned,
256 * for the vertical scrolling the x-position will be returned.
258 qreal
keyboardAnchorPos(int index
) const;
261 bool m_singleClickActivation
;
262 bool m_selectionTogglePressed
;
263 bool m_clearSelectionIfItemsAreNotDragged
;
264 SelectionBehavior m_selectionBehavior
;
265 KItemModelBase
* m_model
;
266 KItemListView
* m_view
;
267 KItemListSelectionManager
* m_selectionManager
;
268 KItemListKeyboardSearchManager
* m_keyboardManager
;
270 QPointF m_pressedMousePos
;
272 QTimer
* m_autoActivationTimer
;
275 * When starting a rubberband selection during a Shift- or Control-key has been
276 * pressed the current selection should never be deleted. To be able to restore
277 * the current selection it is remembered in m_oldSelection before the
278 * rubberband gets activated.
280 QSet
<int> m_oldSelection
;
283 * Assuming a view is given with a vertical scroll-orientation, grouped items and
284 * a maximum of 4 columns:
291 * If the current index is on 4 and key-down is pressed, then item 7 gets the current
292 * item. Now when pressing key-down again item 11 should get the current item and not
293 * item 10. This makes it necessary to keep track of the requested column to have a
294 * similar behavior like in a text editor:
296 int m_keyboardAnchorIndex
;
297 qreal m_keyboardAnchorPos
;