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