]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewextensionsfactory.cpp
Move the VersionControlObserver from the DolphinView to the ViewExtensionsFactory...
[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 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
114 this, SLOT(slotNameFilterChanged(const QString&)));
115
116 view->viewport()->installEventFilter(this);
117 }
118
119 ViewExtensionsFactory::~ViewExtensionsFactory()
120 {
121 }
122
123 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
124 {
125 m_autoScroller->handleCurrentIndexChange(current, previous);
126 }
127
128 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
129 {
130 return m_fileItemDelegate;
131 }
132
133 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
134 {
135 Q_UNUSED(watched);
136 if ((event->type() == QEvent::Wheel) && (m_selectionManager != 0)) {
137 m_selectionManager->reset();
138 }
139 return false;
140 }
141
142 void ViewExtensionsFactory::slotZoomLevelChanged()
143 {
144 m_previewGenerator->updateIcons();
145 if (m_selectionManager != 0) {
146 m_selectionManager->reset();
147 }
148 }
149
150 void ViewExtensionsFactory::cancelPreviews()
151 {
152 m_previewGenerator->cancelPreviews();
153 }
154
155 void ViewExtensionsFactory::slotShowPreviewChanged()
156 {
157 const bool show = m_controller->dolphinView()->showPreview();
158 m_previewGenerator->setPreviewShown(show);
159 }
160
161 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
162 {
163 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
164 KDirLister* dirLister = dirModel->dirLister();
165
166 dirLister->stop();
167
168 const bool show = m_controller->dolphinView()->showHiddenFiles();
169 dirLister->setShowingDotFiles(show);
170
171 const KUrl url = dirLister->url();
172 if (url.isValid()) {
173 dirLister->openUrl(url, KDirLister::NoFlags);
174 }
175 }
176
177 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
178 {
179 proxyModel()->setSorting(sorting);
180 }
181
182 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
183 {
184 proxyModel()->setSortOrder(order);
185 }
186
187 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
188 {
189 proxyModel()->setSortFoldersFirst(foldersFirst);
190 }
191
192 void ViewExtensionsFactory::slotNameFilterChanged(const QString& nameFilter)
193 {
194 proxyModel()->setFilterRegExp(nameFilter);
195 }
196
197 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList& items)
198 {
199 QList<QAction*> actions;
200 if (items.isEmpty()) {
201 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel()->sourceModel());
202 const KUrl url = dirModel->dirLister()->url();
203 actions = m_versionControlObserver->contextMenuActions(url.path(KUrl::AddTrailingSlash));
204 } else {
205 actions = m_versionControlObserver->contextMenuActions(items);
206 }
207 m_controller->setVersionControlActions(actions);
208 }
209
210 void ViewExtensionsFactory::requestActivation()
211 {
212 m_controller->requestActivation();
213 }
214
215 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
216 {
217 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
218 }
219
220 #include "viewextensionsfactory.moc"
221