]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewextensionsfactory.cpp
Use the actions "new_tab" and "new_window" for the viewport-contextmenu instead of...
[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 connect(viewModeController, SIGNAL(urlChanged(const KUrl&)), SLOT(restoreCursor()));
137 }
138
139 ViewExtensionsFactory::~ViewExtensionsFactory()
140 {
141 }
142
143 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
144 {
145 m_autoScroller->handleCurrentIndexChange(current, previous);
146 }
147
148 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
149 {
150 return m_fileItemDelegate;
151 }
152
153 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled)
154 {
155 m_folderExpander->setEnabled(enabled);
156 }
157
158 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
159 {
160 return m_folderExpander->enabled();
161 }
162
163 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
164 {
165 Q_UNUSED(watched);
166
167 switch (event->type()) {
168 case QEvent::Wheel:
169 if (m_selectionManager != 0) {
170 m_selectionManager->reset();
171 }
172 break;
173
174 case QEvent::Leave:
175 restoreCursor();
176 break;
177
178 default: break;
179 }
180
181 return false;
182 }
183
184 void ViewExtensionsFactory::slotZoomLevelChanged()
185 {
186 m_previewGenerator->updateIcons();
187 if (m_selectionManager != 0) {
188 m_selectionManager->reset();
189 }
190 }
191
192 void ViewExtensionsFactory::cancelPreviews()
193 {
194 m_previewGenerator->cancelPreviews();
195 }
196
197 void ViewExtensionsFactory::slotShowPreviewChanged()
198 {
199 const bool show = m_dolphinViewController->view()->showPreview();
200 m_previewGenerator->setPreviewShown(show);
201 }
202
203 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
204 {
205 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
206 KDirLister* dirLister = dirModel->dirLister();
207
208 dirLister->stop();
209
210 const bool show = m_dolphinViewController->view()->showHiddenFiles();
211 dirLister->setShowingDotFiles(show);
212
213 const KUrl url = dirLister->url();
214 if (url.isValid()) {
215 dirLister->openUrl(url, KDirLister::NoFlags);
216 }
217 }
218
219 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
220 {
221 proxyModel()->setSorting(sorting);
222 }
223
224 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
225 {
226 proxyModel()->setSortOrder(order);
227 }
228
229 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
230 {
231 proxyModel()->setSortFoldersFirst(foldersFirst);
232 }
233
234 void ViewExtensionsFactory::slotNameFilterChanged(const QString& nameFilter)
235 {
236 proxyModel()->setFilterFixedString(nameFilter);
237 }
238
239 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList& items)
240 {
241 QList<QAction*> actions;
242 if (items.isEmpty()) {
243 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel()->sourceModel());
244 const KUrl url = dirModel->dirLister()->url();
245 actions = m_versionControlObserver->contextMenuActions(url.path(KUrl::AddTrailingSlash));
246 } else {
247 actions = m_versionControlObserver->contextMenuActions(items);
248 }
249 m_dolphinViewController->setVersionControlActions(actions);
250 }
251
252 void ViewExtensionsFactory::requestActivation()
253 {
254 m_dolphinViewController->requestActivation();
255 }
256
257 void ViewExtensionsFactory::applyPointingHandCursor()
258 {
259 if (!m_appliedPointingHandCursor && !(QApplication::mouseButtons() & Qt::LeftButton)) {
260 QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
261 m_appliedPointingHandCursor = true;
262 }
263 }
264
265 void ViewExtensionsFactory::restoreCursor()
266 {
267 if (m_appliedPointingHandCursor) {
268 QApplication::restoreOverrideCursor();
269 m_appliedPointingHandCursor = false;
270 }
271 }
272
273 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
274 {
275 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
276 }
277
278 #include "viewextensionsfactory.moc"
279