]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewextensionsfactory.cpp
a52871ff447afe1d62c2a1cdc10eed9a51bc82dc
[dolphin.git] / src / views / viewextensionsfactory.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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>
37 #include <KDirModel>
38 #include <KFilePreviewGenerator>
39 #include <QAbstractItemView>
40 #include <QApplication>
41
42 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView* view,
43 DolphinViewController* dolphinViewController,
44 const ViewModeController* viewModeController) :
45 QObject(view),
46 m_view(view),
47 m_dolphinViewController(dolphinViewController),
48 m_toolTipManager(0),
49 m_previewGenerator(0),
50 m_selectionManager(0),
51 m_autoScroller(0),
52 m_fileItemDelegate(0),
53 m_versionControlObserver(0)
54 {
55 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
56
57 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
58
59 // initialize tooltips
60 if (settings->showToolTips()) {
61 DolphinSortFilterProxyModel* proxyModel = static_cast<DolphinSortFilterProxyModel*>(view->model());
62 m_toolTipManager = new ToolTipManager(view, proxyModel);
63
64 connect(dolphinViewController, SIGNAL(hideToolTip()),
65 m_toolTipManager, SLOT(hideToolTip()));
66 }
67
68 // initialize preview generator
69 Q_ASSERT(view->iconSize().isValid());
70 m_previewGenerator = new KFilePreviewGenerator(view);
71 m_previewGenerator->setPreviewShown(dolphinViewController->view()->showPreview());
72 connect(viewModeController, SIGNAL(zoomLevelChanged(int)),
73 this, SLOT(slotZoomLevelChanged()));
74 connect(viewModeController, SIGNAL(cancelPreviews()),
75 this, SLOT(cancelPreviews()));
76
77 // slotPreviewChanged() is connected as Qt::QueuedConnection to prevent performance
78 // issues when the directory lister changes its URL after the preview-changes have
79 // been applied. Usecase: Switch from directory A having no previews to
80 // directory B with previews (see sequence in DolphinView::setUrl()).
81 connect(dolphinViewController->view(), SIGNAL(showPreviewChanged()),
82 this, SLOT(slotShowPreviewChanged()),
83 Qt::QueuedConnection);
84
85 // initialize selection manager
86 m_selectionManager = new SelectionManager(view);
87 connect(m_selectionManager, SIGNAL(selectionChanged()),
88 this, SLOT(requestActivation()));
89 connect(viewModeController, SIGNAL(urlChanged(const KUrl&)),
90 m_selectionManager, SLOT(reset()));
91
92 // initialize auto scroller
93 m_autoScroller = new DolphinViewAutoScroller(view);
94
95 // initialize file item delegate
96 m_fileItemDelegate = new DolphinFileItemDelegate(view);
97 m_fileItemDelegate->setShowToolTipWhenElided(false);
98 view->setItemDelegate(m_fileItemDelegate);
99
100 // initialize version control observer
101 const DolphinView* dolphinView = dolphinViewController->view();
102 m_versionControlObserver = new VersionControlObserver(view);
103 connect(m_versionControlObserver, SIGNAL(infoMessage(const QString&)),
104 dolphinView, SIGNAL(infoMessage(const QString&)));
105 connect(m_versionControlObserver, SIGNAL(errorMessage(const QString&)),
106 dolphinView, SIGNAL(errorMessage(const QString&)));
107 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(const QString&)),
108 dolphinView, SIGNAL(operationCompletedMessage(const QString&)));
109 connect(dolphinViewController, SIGNAL(requestVersionControlActions(const KFileItemList&)),
110 this, SLOT(slotRequestVersionControlActions(const KFileItemList&)));
111
112 // react on view property changes
113 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
114 this, SLOT(slotShowHiddenFilesChanged()));
115 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
116 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
117 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
118 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
119 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
120 this, SLOT(slotSortFoldersFirstChanged(bool)));
121
122 // Give the view the ability to auto-expand its directories on hovering
123 // (the column view takes care about this itself). If the details view
124 // uses expandable folders, the auto-expanding should be used always.
125 m_folderExpander = new FolderExpander(view, proxyModel());
126 m_folderExpander->setEnabled(settings->autoExpandFolders());
127 connect(m_folderExpander, SIGNAL(enterDir(const QModelIndex&)),
128 dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
129
130 // react on namefilter changes
131 connect(viewModeController, SIGNAL(nameFilterChanged(const QString&)),
132 this, SLOT(slotNameFilterChanged(const QString&)));
133
134 view->viewport()->installEventFilter(this);
135 }
136
137 ViewExtensionsFactory::~ViewExtensionsFactory()
138 {
139 }
140
141 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
142 {
143 m_autoScroller->handleCurrentIndexChange(current, previous);
144 }
145
146 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
147 {
148 return m_fileItemDelegate;
149 }
150
151 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled)
152 {
153 m_folderExpander->setEnabled(enabled);
154 }
155
156 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
157 {
158 return m_folderExpander->enabled();
159 }
160
161 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
162 {
163 Q_UNUSED(watched);
164 if ((event->type() == QEvent::Wheel) && m_selectionManager) {
165 m_selectionManager->reset();
166 }
167 return false;
168 }
169
170 void ViewExtensionsFactory::slotZoomLevelChanged()
171 {
172 m_previewGenerator->updateIcons();
173 if (m_selectionManager) {
174 m_selectionManager->reset();
175 }
176 }
177
178 void ViewExtensionsFactory::cancelPreviews()
179 {
180 m_previewGenerator->cancelPreviews();
181 }
182
183 void ViewExtensionsFactory::slotShowPreviewChanged()
184 {
185 const bool show = m_dolphinViewController->view()->showPreview();
186 m_previewGenerator->setPreviewShown(show);
187 }
188
189 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
190 {
191 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
192 KDirLister* dirLister = dirModel->dirLister();
193
194 dirLister->stop();
195
196 const bool show = m_dolphinViewController->view()->showHiddenFiles();
197 dirLister->setShowingDotFiles(show);
198
199 const KUrl url = dirLister->url();
200 if (url.isValid()) {
201 dirLister->openUrl(url, KDirLister::NoFlags);
202 }
203 }
204
205 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
206 {
207 proxyModel()->setSorting(sorting);
208 }
209
210 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
211 {
212 proxyModel()->setSortOrder(order);
213 }
214
215 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
216 {
217 proxyModel()->setSortFoldersFirst(foldersFirst);
218 }
219
220 void ViewExtensionsFactory::slotNameFilterChanged(const QString& nameFilter)
221 {
222 proxyModel()->setFilterFixedString(nameFilter);
223 }
224
225 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList& items)
226 {
227 QList<QAction*> actions;
228 if (items.isEmpty()) {
229 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel()->sourceModel());
230 const KUrl url = dirModel->dirLister()->url();
231 actions = m_versionControlObserver->contextMenuActions(url.path(KUrl::AddTrailingSlash));
232 } else {
233 actions = m_versionControlObserver->contextMenuActions(items);
234 }
235 m_dolphinViewController->setVersionControlActions(actions);
236 }
237
238 void ViewExtensionsFactory::requestActivation()
239 {
240 m_dolphinViewController->requestActivation();
241 }
242
243 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
244 {
245 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
246 }
247
248 #include "viewextensionsfactory.moc"
249