1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #ifndef DOLPHINCONTROLLER_H
21 #define DOLPHINCONTROLLER_H
23 #include <dolphinview.h>
25 #include <QtCore/QObject>
26 #include <libdolphin_export.h>
28 class QAbstractItemView
;
36 // TODO: get rid of all the state duplications in the controller and allow read access
37 // to the Dolphin view for all view implementations
40 * @brief Acts as mediator between the abstract Dolphin view and the view
43 * The abstract Dolphin view (see DolphinView) represents the parent of the controller.
44 * The lifetime of the controller is equal to the lifetime of the Dolphin view.
45 * The controller is passed to the current view implementation
46 * (see DolphinIconsView, DolphinDetailsView and DolphinColumnView)
47 * by passing it in the constructor and informing the controller about the change
48 * of the view implementation:
51 * QAbstractItemView* view = new DolphinIconsView(parent, controller);
52 * controller->setItemView(view);
55 * The communication of the view implementations to the abstract view is done by:
56 * - triggerContextMenuRequest()
57 * - requestActivation()
58 * - triggerUrlChangeRequest()
59 * - indicateDroppedUrls()
60 * - indicateSortingChange()
61 * - indicateSortOrderChanged()
62 * - indicateSortFoldersFirstChanged()
65 * - handleKeyPressEvent()
67 * - emitViewportEntered()
68 * - replaceUrlByClipboard()
71 * The communication of the abstract view to the view implementations is done by:
73 * - setShowHiddenFiles()
75 * - indicateActivationChange()
78 class LIBDOLPHINPRIVATE_EXPORT DolphinController
: public QObject
83 explicit DolphinController(DolphinView
* dolphinView
);
84 virtual ~DolphinController();
87 * Allows read access for the view implementation to the abstract
90 const DolphinView
* dolphinView() const;
93 * Sets the URL to \a url and emits the signal urlChanged() if
94 * \a url is different for the current URL. This method should
95 * be invoked by the abstract Dolphin view whenever the current
96 * URL has been changed.
98 void setUrl(const KUrl
& url
);
99 const KUrl
& url() const;
102 * Changes the current item view where the controller is working. This
103 * is only necessary for views like the tree view, where internally
104 * several QAbstractItemView instances are used.
106 void setItemView(QAbstractItemView
* view
);
108 QAbstractItemView
* itemView() const;
111 * Allows a view implementation to request an URL change to \a url.
112 * The signal requestUrlChange() is emitted and the abstract Dolphin view
113 * will assure that the URL of the Dolphin Controller will be updated
114 * later. Invoking this method makes only sense if the view implementation
115 * shows a hierarchy of URLs and allows to change the URL within
116 * the view (e. g. this is the case in the column view).
118 void triggerUrlChangeRequest(const KUrl
& url
);
121 * Requests a context menu for the position \a pos. This method
122 * should be invoked by the view implementation when a context
123 * menu should be opened. The abstract Dolphin view itself
124 * takes care itself to get the selected items depending from
125 * \a pos. It is possible to define a custom list of actions for
126 * the context menu by \a customActions.
128 void triggerContextMenuRequest(const QPoint
& pos
,
129 const QList
<QAction
*>& customActions
= QList
<QAction
*>());
132 * Requests an activation of the view and emits the signal
133 * activated(). This method should be invoked by the view implementation
134 * if e. g. a mouse click on the view has been done.
135 * After the activation has been changed, the view implementation
136 * might listen to the activationChanged() signal.
138 void requestActivation();
141 * Indicates that URLs are dropped above a destination. This method
142 * should be invoked by the view implementation. The abstract Dolphin view
143 * will start the corresponding action (copy, move, link).
144 * @param destItem Item of the destination (can be null, see KFileItem::isNull()).
145 * @param destPath Path of the destination.
146 * @param event Drop event
148 void indicateDroppedUrls(const KFileItem
& destItem
,
149 const KUrl
& destPath
,
153 * Informs the abstract Dolphin view about a sorting change done inside
154 * the view implementation. This method should be invoked by the view
155 * implementation (e. g. the details view uses this method in combination
156 * with the details header).
158 void indicateSortingChange(DolphinView::Sorting sorting
);
161 * Informs the abstract Dolphin view about a sort order change done inside
162 * the view implementation. This method should be invoked by the view
163 * implementation (e. g. the details view uses this method in combination
164 * with the details header).
166 void indicateSortOrderChange(Qt::SortOrder order
);
169 * Informs the abstract Dolphin view about a change between separate sorting
170 * (with folders first) and mixed sorting of files and folders done inside
171 * the view implementation. This method should be invoked by the view
172 * implementation (e. g. the details view uses this method in combination
173 * with the details header).
175 void indicateSortFoldersFirstChange(bool foldersFirst
);
178 * Informs the abstract Dolphin view about an additional information change
179 * done inside the view implementation. This method should be invoked by the
180 * view implementation (e. g. the details view uses this method in combination
181 * with the details header).
183 void indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
);
186 * Informs the view implementation about a change of the activation
187 * state and is invoked by the abstract Dolphin view. The signal
188 * activationChanged() is emitted.
190 void indicateActivationChange(bool active
);
193 * Sets the zoom level to \a level and emits the signal zoomLevelChanged().
194 * It must be assured that the used level is inside the range
195 * DolphinController::zoomLevelMinimum() and
196 * DolphinController::zoomLevelMaximum().
197 * Is invoked by the abstract Dolphin view.
199 void setZoomLevel(int level
);
200 int zoomLevel() const;
203 * Tells the view implementation to zoom out by emitting the signal zoomOut()
204 * and is invoked by the abstract Dolphin view.
206 void triggerZoomOut();
209 * Should be invoked in each view implementation whenever a key has been
210 * pressed. If the selection model of \a view is not empty and
211 * the return key has been pressed, the selected items will get triggered.
213 void handleKeyPressEvent(QKeyEvent
* event
);
216 * Replaces the URL of the abstract Dolphin view with the content
217 * of the clipboard as URL. If the clipboard contains no text,
218 * nothing will be done.
220 void replaceUrlByClipboard();
222 /** Emits the signal hideToolTip(). */
223 void emitHideToolTip();
226 * Emits the signal itemTriggered() for the item \a item.
227 * The method can be used by the view implementations to
228 * trigger an item directly without mouse interaction.
229 * If the item triggering is done by the mouse, it is recommended
230 * to use QAbstractItemView::triggerItem(), as this will check
231 * the used mouse buttons to execute the correct action.
233 void emitItemTriggered(const KFileItem
& item
);
236 * Returns the file item for the proxy index \a index of the view \a view.
238 KFileItem
itemForIndex(const QModelIndex
& index
) const;
242 * Emits the signal itemTriggered() if the file item for the index \a index
243 * is not null and the left mouse button has been pressed. If the item is
244 * null, the signal itemEntered() is emitted.
245 * The method should be invoked by the view implementations whenever the
246 * user has triggered an item with the mouse (see
247 * QAbstractItemView::clicked() or QAbstractItemView::doubleClicked()).
249 void triggerItem(const QModelIndex
& index
);
252 * Emits the signal tabRequested(), if the file item for the index \a index
253 * represents a directory and when the middle mouse button has been pressed.
254 * The method should be invoked by the controller parent.
256 void requestTab(const QModelIndex
& index
);
259 * Emits the signal itemEntered() if the file item for the index \a index
260 * is not null. The method should be invoked by the controller parent
261 * whenever the mouse cursor is above an item.
263 void emitItemEntered(const QModelIndex
& index
);
266 * Emits the signal viewportEntered(). The method should be invoked by
267 * the controller parent whenever the mouse cursor is above the viewport.
269 void emitViewportEntered();
273 * Is emitted if the URL for the Dolphin controller has been changed
276 void urlChanged(const KUrl
& url
);
279 * Is emitted if the view implementation requests a changing of the current
280 * URL to \a url (see triggerUrlChangeRequest()).
282 void requestUrlChange(const KUrl
& url
);
285 * Is emitted if a context menu should be opened (see triggerContextMenuRequest()).
286 * The abstract Dolphin view connects to this signal and will open the context menu.
287 * @param pos Position relative to the view widget where the
288 * context menu should be opened. It is recommended
289 * to get the corresponding model index from
291 * @param customActions List of actions that is added to the context menu when
292 * the menu is opened above the viewport.
294 void requestContextMenu(const QPoint
& pos
, QList
<QAction
*> customActions
);
297 * Is emitted if the view has been activated by e. g. a mouse click.
298 * The abstract Dolphin view connects to this signal to know the
299 * destination view for the menu actions.
304 * Is emitted if URLs have been dropped to the destination
305 * path \a destPath. If the URLs have been dropped above an item of
306 * the destination path, the item is indicated by \a destItem
307 * (can be null, see KFileItem::isNull()).
309 void urlsDropped(const KFileItem
& destItem
,
310 const KUrl
& destPath
,
314 * Is emitted if the sorting has been changed to \a sorting by
315 * the view implementation (see indicateSortingChanged().
316 * The abstract Dolphin view connects to
317 * this signal to update its menu action.
319 void sortingChanged(DolphinView::Sorting sorting
);
322 * Is emitted if the sort order has been changed to \a order
323 * by the view implementation (see indicateSortOrderChanged().
324 * The abstract Dolphin view connects
325 * to this signal to update its menu actions.
327 void sortOrderChanged(Qt::SortOrder order
);
330 * Is emitted if 'sort folders first' has been changed to \a foldersFirst
331 * by the view implementation (see indicateSortOrderChanged().
332 * The abstract Dolphin view connects
333 * to this signal to update its menu actions.
335 void sortFoldersFirstChanged(bool foldersFirst
);
338 * Is emitted if the additional info has been changed to \a info
339 * by the view implementation. The abstract Dolphin view connects
340 * to this signal to update its menu actions.
342 void additionalInfoChanged(const KFileItemDelegate::InformationList
& info
);
345 * Is emitted if the activation state has been changed to \a active
346 * by the abstract Dolphin view.
347 * The view implementation might connect to this signal if custom
348 * updates are required in this case.
350 void activationChanged(bool active
);
353 * Is emitted if the item \a item should be triggered. The abstract
354 * Dolphin view connects to this signal. If the item represents a directory,
355 * the directory is opened. On a file the corresponding application is opened.
356 * The item is null (see KFileItem::isNull()), when clicking on the viewport itself.
358 void itemTriggered(const KFileItem
& item
);
361 * Is emitted if the mouse cursor has entered the item
362 * given by \a index (see emitItemEntered()).
363 * The abstract Dolphin view connects to this signal.
365 void itemEntered(const KFileItem
& item
);
368 * Is emitted if a new tab should be opened for the URL \a url.
370 void tabRequested(const KUrl
& url
);
373 * Is emitted if the mouse cursor has entered
374 * the viewport (see emitViewportEntered().
375 * The abstract Dolphin view connects to this signal.
377 void viewportEntered();
380 * Is emitted if the view should change the zoom to \a level. The view implementation
381 * must connect to this signal if it supports zooming.
383 void zoomLevelChanged(int level
);
386 * Is emitted if the abstract view should hide an open tooltip.
391 void updateMouseButtonState();
395 static Qt::MouseButtons m_mouseButtons
; // TODO: this is a workaround until Qt-issue 176832 has been fixed
397 DolphinView
* m_dolphinView
;
398 QAbstractItemView
* m_itemView
;
401 inline const DolphinView
* DolphinController::dolphinView() const
403 return m_dolphinView
;
406 inline const KUrl
& DolphinController::url() const
411 inline QAbstractItemView
* DolphinController::itemView() const
416 inline int DolphinController::zoomLevel() const