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