]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewextensionsfactory.cpp
Use a pointing-hand cursor when hovering items as discussed on http://lists.kde.org...
[dolphin.git] / src / views / viewextensionsfactory.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "viewextensionsfactory.h"
21
22 #include "dolphinfileitemdelegate.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinviewcontroller.h"
26 #include "dolphinviewautoscroller.h"
27 #include "folderexpander.h"
28 #include "selectionmanager.h"
29 #include "settings/dolphinsettings.h"
30 #include "tooltips/tooltipmanager.h"
31 #include "versioncontrol/versioncontrolobserver.h"
32 #include "viewmodecontroller.h"
33
34 #include "dolphin_generalsettings.h"
35
36 #include <kdirlister.h>
37 #include <kdirmodel.h>
38 #include <kfilepreviewgenerator.h>
39 #include <QAbstractItemView>
40 #include <QApplication>
41
42 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView* view,
43 DolphinViewController* dolphinViewController,
44 const ViewModeController* viewModeController) :
45 QObject(view),
46 m_appliedPointingHandCursor(false),
47 m_view(view),
48 m_dolphinViewController(dolphinViewController),
49 m_toolTipManager(0),
50 m_previewGenerator(0),
51 m_selectionManager(0),
52 m_autoScroller(0),
53 m_fileItemDelegate(0),
54 m_versionControlObserver(0)
55 {
56 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
57
58 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
59
60 // initialize tooltips
61 if (settings->showToolTips()) {
62 DolphinSortFilterProxyModel* proxyModel = static_cast<DolphinSortFilterProxyModel*>(view->model());
63 m_toolTipManager = new ToolTipManager(view, proxyModel);
64
65 connect(dolphinViewController, SIGNAL(hideToolTip()),
66 m_toolTipManager, SLOT(hideToolTip()));
67 }
68
69 // initialize preview generator
70 Q_ASSERT(view->iconSize().isValid());
71 m_previewGenerator = new KFilePreviewGenerator(view);
72 m_previewGenerator->setPreviewShown(dolphinViewController->view()->showPreview());
73 connect(viewModeController, SIGNAL(zoomLevelChanged(int)),
74 this, SLOT(slotZoomLevelChanged()));
75 connect(viewModeController, SIGNAL(cancelPreviews()),
76 this, SLOT(cancelPreviews()));
77 connect(dolphinViewController->view(), SIGNAL(showPreviewChanged()),
78 this, SLOT(slotShowPreviewChanged()));
79
80 // initialize selection manager
81 if (settings->showSelectionToggle()) {
82 m_selectionManager = new SelectionManager(view);
83 connect(m_selectionManager, SIGNAL(selectionChanged()),
84 this, SLOT(requestActivation()));
85 connect(viewModeController, SIGNAL(urlChanged(const KUrl&)),
86 m_selectionManager, SLOT(reset()));
87 }
88
89 // initialize auto scroller
90 m_autoScroller = new DolphinViewAutoScroller(view);
91
92 // initialize file item delegate
93 m_fileItemDelegate = new DolphinFileItemDelegate(view);
94 m_fileItemDelegate->setShowToolTipWhenElided(false);
95 view->setItemDelegate(m_fileItemDelegate);
96
97 // initialize version control observer
98 const DolphinView* dolphinView = dolphinViewController->view();
99 m_versionControlObserver = new VersionControlObserver(view);
100 connect(m_versionControlObserver, SIGNAL(infoMessage(const QString&)),
101 dolphinView, SIGNAL(infoMessage(const QString&)));
102 connect(m_versionControlObserver, SIGNAL(errorMessage(const QString&)),
103 dolphinView, SIGNAL(errorMessage(const QString&)));
104 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(const QString&)),
105 dolphinView, SIGNAL(operationCompletedMessage(const QString&)));
106 connect(dolphinViewController, SIGNAL(requestVersionControlActions(const KFileItemList&)),
107 this, SLOT(slotRequestVersionControlActions(const KFileItemList&)));
108
109 // react on view property changes
110 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
111 this, SLOT(slotShowHiddenFilesChanged()));
112 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
113 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
114 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
115 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
116 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
117 this, SLOT(slotSortFoldersFirstChanged(bool)));
118
119 // Give the view the ability to auto-expand its directories on hovering
120 // (the column view takes care about this itself). If the details view
121 // uses expandable folders, the auto-expanding should be used always.
122 m_folderExpander = new FolderExpander(view, proxyModel());
123 m_folderExpander->setEnabled(settings->autoExpandFolders());
124 connect(m_folderExpander, SIGNAL(enterDir(const QModelIndex&)),
125 dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
126
127 // react on namefilter changes
128 connect(viewModeController, SIGNAL(nameFilterChanged(const QString&)),
129 this, SLOT(slotNameFilterChanged(const QString&)));
130
131 view->viewport()->installEventFilter(this);
132
133 // Apply a pointing-hand cursor when hovering files
134 connect(view, SIGNAL(entered(const QModelIndex&)), SLOT(applyPointingHandCursor()));
135 connect(view, SIGNAL(viewportEntered()), SLOT(restoreCursor()));
136 }
137
138 ViewExtensionsFactory::~ViewExtensionsFactory()
139 {
140 }
141
142 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
143 {
144 m_autoScroller->handleCurrentIndexChange(current, previous);
145 }
146
147 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
148 {
149 return m_fileItemDelegate;
150 }
151
152 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled)
153 {
154 m_folderExpander->setEnabled(enabled);
155 }
156
157 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
158 {
159 return m_folderExpander->enabled();
160 }
161
162 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
163 {
164 Q_UNUSED(watched);
165
166 switch (event->type()) {
167 case QEvent::Wheel:
168 if (m_selectionManager != 0) {
169 m_selectionManager->reset();
170 }
171 break;
172
173 case QEvent::Leave:
174 restoreCursor();
175 break;
176
177 default: break;
178 }
179
180 return false;
181 }
182
183 void ViewExtensionsFactory::slotZoomLevelChanged()
184 {
185 m_previewGenerator->updateIcons();
186 if (m_selectionManager != 0) {
187 m_selectionManager->reset();
188 }
189 }
190
191 void ViewExtensionsFactory::cancelPreviews()
192 {
193 m_previewGenerator->cancelPreviews();
194 }
195
196 void ViewExtensionsFactory::slotShowPreviewChanged()
197 {
198 const bool show = m_dolphinViewController->view()->showPreview();
199 m_previewGenerator->setPreviewShown(show);
200 }
201
202 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
203 {
204 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
205 KDirLister* dirLister = dirModel->dirLister();
206
207 dirLister->stop();
208
209 const bool show = m_dolphinViewController->view()->showHiddenFiles();
210 dirLister->setShowingDotFiles(show);
211
212 const KUrl url = dirLister->url();
213 if (url.isValid()) {
214 dirLister->openUrl(url, KDirLister::NoFlags);
215 }
216 }
217
218 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
219 {
220 proxyModel()->setSorting(sorting);
221 }
222
223 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
224 {
225 proxyModel()->setSortOrder(order);
226 }
227
228 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
229 {
230 proxyModel()->setSortFoldersFirst(foldersFirst);
231 }
232
233 void ViewExtensionsFactory::slotNameFilterChanged(const QString& nameFilter)
234 {
235 proxyModel()->setFilterFixedString(nameFilter);
236 }
237
238 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList& items)
239 {
240 QList<QAction*> actions;
241 if (items.isEmpty()) {
242 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel()->sourceModel());
243 const KUrl url = dirModel->dirLister()->url();
244 actions = m_versionControlObserver->contextMenuActions(url.path(KUrl::AddTrailingSlash));
245 } else {
246 actions = m_versionControlObserver->contextMenuActions(items);
247 }
248 m_dolphinViewController->setVersionControlActions(actions);
249 }
250
251 void ViewExtensionsFactory::requestActivation()
252 {
253 m_dolphinViewController->requestActivation();
254 }
255
256 void ViewExtensionsFactory::applyPointingHandCursor()
257 {
258 if (!m_appliedPointingHandCursor && !(QApplication::mouseButtons() & Qt::LeftButton)) {
259 QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
260 m_appliedPointingHandCursor = true;
261 }
262 }
263
264 void ViewExtensionsFactory::restoreCursor()
265 {
266 if (m_appliedPointingHandCursor) {
267 QApplication::restoreOverrideCursor();
268 m_appliedPointingHandCursor = false;
269 }
270 }
271
272 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
273 {
274 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
275 }
276
277 #include "viewextensionsfactory.moc"
278