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