]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewextensionsfactory.cpp
Move the selection listener from DolphinView to ViewExtensionsFactory. The statusbar...
[dolphin.git] / src / 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 "dolphincontroller.h"
23 #include "dolphinfileitemdelegate.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinviewautoscroller.h"
27 #include "selectionmanager.h"
28 #include "settings/dolphinsettings.h"
29 #include "tooltips/tooltipmanager.h"
30 #include "versioncontrolobserver.h"
31
32 #include "dolphin_generalsettings.h"
33
34 #include <kdirlister.h>
35 #include <kdirmodel.h>
36 #include <kfilepreviewgenerator.h>
37 #include <QAbstractItemView>
38
39 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView* view,
40 DolphinController* controller) :
41 QObject(view),
42 m_view(view),
43 m_controller(controller),
44 m_toolTipManager(0),
45 m_previewGenerator(0),
46 m_selectionManager(0),
47 m_autoScroller(0),
48 m_fileItemDelegate(0),
49 m_versionControlObserver(0)
50 {
51 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
52
53 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
54
55 // initialize tooltips
56 if (settings->showToolTips()) {
57 DolphinSortFilterProxyModel* proxyModel = static_cast<DolphinSortFilterProxyModel*>(view->model());
58 m_toolTipManager = new ToolTipManager(view, proxyModel);
59
60 connect(controller, SIGNAL(hideToolTip()),
61 m_toolTipManager, SLOT(hideTip()));
62 }
63
64 // initialize preview generator
65 m_previewGenerator = new KFilePreviewGenerator(view);
66 m_previewGenerator->setPreviewShown(controller->dolphinView()->showPreview());
67 connect(controller, SIGNAL(zoomLevelChanged(int)),
68 this, SLOT(slotZoomLevelChanged()));
69 connect(controller, SIGNAL(cancelPreviews()),
70 this, SLOT(cancelPreviews()));
71 connect(controller->dolphinView(), SIGNAL(showPreviewChanged()),
72 this, SLOT(slotShowPreviewChanged()));
73
74 // initialize selection manager
75 if (settings->showSelectionToggle()) {
76 m_selectionManager = new SelectionManager(view);
77 connect(m_selectionManager, SIGNAL(selectionChanged()),
78 this, SLOT(requestActivation()));
79 connect(controller, SIGNAL(urlChanged(const KUrl&)),
80 m_selectionManager, SLOT(reset()));
81 }
82
83 // initialize auto scroller
84 m_autoScroller = new DolphinViewAutoScroller(view);
85
86 // initialize file item delegate
87 m_fileItemDelegate = new DolphinFileItemDelegate(view);
88 m_fileItemDelegate->setShowToolTipWhenElided(false);
89 view->setItemDelegate(m_fileItemDelegate);
90
91 // initialize version control observer
92 const DolphinView* dolphinView = controller->dolphinView();
93 m_versionControlObserver = new VersionControlObserver(view);
94 connect(m_versionControlObserver, SIGNAL(infoMessage(const QString&)),
95 dolphinView, SIGNAL(infoMessage(const QString&)));
96 connect(m_versionControlObserver, SIGNAL(errorMessage(const QString&)),
97 dolphinView, SIGNAL(errorMessage(const QString&)));
98 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(const QString&)),
99 dolphinView, SIGNAL(operationCompletedMessage(const QString&)));
100 connect(controller, SIGNAL(requestVersionControlActions(const KFileItemList&)),
101 this, SLOT(slotRequestVersionControlActions(const KFileItemList&)));
102
103 // react on view property changes
104 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
105 this, SLOT(slotShowHiddenFilesChanged()));
106 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
107 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
108 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
109 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
110 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
111 this, SLOT(slotSortFoldersFirstChanged(bool)));
112
113 // inform the controller about selection changes
114 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
115 controller, SLOT(emitSelectionChanged()));
116
117 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
118 this, SLOT(slotNameFilterChanged(const QString&)));
119
120 view->viewport()->installEventFilter(this);
121 }
122
123 ViewExtensionsFactory::~ViewExtensionsFactory()
124 {
125 }
126
127 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
128 {
129 m_autoScroller->handleCurrentIndexChange(current, previous);
130 }
131
132 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
133 {
134 return m_fileItemDelegate;
135 }
136
137 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
138 {
139 Q_UNUSED(watched);
140 if ((event->type() == QEvent::Wheel) && (m_selectionManager != 0)) {
141 m_selectionManager->reset();
142 }
143 return false;
144 }
145
146 void ViewExtensionsFactory::slotZoomLevelChanged()
147 {
148 m_previewGenerator->updateIcons();
149 if (m_selectionManager != 0) {
150 m_selectionManager->reset();
151 }
152 }
153
154 void ViewExtensionsFactory::cancelPreviews()
155 {
156 m_previewGenerator->cancelPreviews();
157 }
158
159 void ViewExtensionsFactory::slotShowPreviewChanged()
160 {
161 const bool show = m_controller->dolphinView()->showPreview();
162 m_previewGenerator->setPreviewShown(show);
163 }
164
165 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
166 {
167 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
168 KDirLister* dirLister = dirModel->dirLister();
169
170 dirLister->stop();
171
172 const bool show = m_controller->dolphinView()->showHiddenFiles();
173 dirLister->setShowingDotFiles(show);
174
175 const KUrl url = dirLister->url();
176 if (url.isValid()) {
177 dirLister->openUrl(url, KDirLister::NoFlags);
178 }
179 }
180
181 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
182 {
183 proxyModel()->setSorting(sorting);
184 }
185
186 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
187 {
188 proxyModel()->setSortOrder(order);
189 }
190
191 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
192 {
193 proxyModel()->setSortFoldersFirst(foldersFirst);
194 }
195
196 void ViewExtensionsFactory::slotNameFilterChanged(const QString& nameFilter)
197 {
198 proxyModel()->setFilterRegExp(nameFilter);
199 }
200
201 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList& items)
202 {
203 QList<QAction*> actions;
204 if (items.isEmpty()) {
205 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel()->sourceModel());
206 const KUrl url = dirModel->dirLister()->url();
207 actions = m_versionControlObserver->contextMenuActions(url.path(KUrl::AddTrailingSlash));
208 } else {
209 actions = m_versionControlObserver->contextMenuActions(items);
210 }
211 m_controller->setVersionControlActions(actions);
212 }
213
214 void ViewExtensionsFactory::requestActivation()
215 {
216 m_controller->requestActivation();
217 }
218
219 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
220 {
221 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
222 }
223
224 #include "viewextensionsfactory.moc"
225