]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewextensionsfactory.cpp
allow filtering for filenames having a +
[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 // Give the view the ability to auto-expand its directories on hovering
116 // (the column view takes care about this itself). If the details view
117 // uses expandable folders, the auto-expanding should be used always.
118 m_folderExpander = new FolderExpander(view, proxyModel());
119 m_folderExpander->setEnabled(settings->autoExpandFolders());
120 connect(m_folderExpander, SIGNAL(enterDir(const QModelIndex&)),
121 controller, SLOT(triggerItem(const QModelIndex&)));
122
123 // react on namefilter changes
124 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
125 this, SLOT(slotNameFilterChanged(const QString&)));
126
127 view->viewport()->installEventFilter(this);
128 }
129
130 ViewExtensionsFactory::~ViewExtensionsFactory()
131 {
132 }
133
134 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
135 {
136 m_autoScroller->handleCurrentIndexChange(current, previous);
137 }
138
139 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
140 {
141 return m_fileItemDelegate;
142 }
143
144 void ViewExtensionsFactory::setAutoFolderExpandingEnabled(bool enabled)
145 {
146 m_folderExpander->setEnabled(enabled);
147 }
148
149 bool ViewExtensionsFactory::autoFolderExpandingEnabled() const
150 {
151 return m_folderExpander->enabled();
152 }
153
154 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
155 {
156 Q_UNUSED(watched);
157 if ((event->type() == QEvent::Wheel) && (m_selectionManager != 0)) {
158 m_selectionManager->reset();
159 }
160 return false;
161 }
162
163 void ViewExtensionsFactory::slotZoomLevelChanged()
164 {
165 m_previewGenerator->updateIcons();
166 if (m_selectionManager != 0) {
167 m_selectionManager->reset();
168 }
169 }
170
171 void ViewExtensionsFactory::cancelPreviews()
172 {
173 m_previewGenerator->cancelPreviews();
174 }
175
176 void ViewExtensionsFactory::slotShowPreviewChanged()
177 {
178 const bool show = m_controller->dolphinView()->showPreview();
179 m_previewGenerator->setPreviewShown(show);
180 }
181
182 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
183 {
184 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
185 KDirLister* dirLister = dirModel->dirLister();
186
187 dirLister->stop();
188
189 const bool show = m_controller->dolphinView()->showHiddenFiles();
190 dirLister->setShowingDotFiles(show);
191
192 const KUrl url = dirLister->url();
193 if (url.isValid()) {
194 dirLister->openUrl(url, KDirLister::NoFlags);
195 }
196 }
197
198 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
199 {
200 proxyModel()->setSorting(sorting);
201 }
202
203 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
204 {
205 proxyModel()->setSortOrder(order);
206 }
207
208 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
209 {
210 proxyModel()->setSortFoldersFirst(foldersFirst);
211 }
212
213 void ViewExtensionsFactory::slotNameFilterChanged(const QString& nameFilter)
214 {
215 proxyModel()->setFilterFixedString(nameFilter);
216 }
217
218 void ViewExtensionsFactory::slotRequestVersionControlActions(const KFileItemList& items)
219 {
220 QList<QAction*> actions;
221 if (items.isEmpty()) {
222 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel()->sourceModel());
223 const KUrl url = dirModel->dirLister()->url();
224 actions = m_versionControlObserver->contextMenuActions(url.path(KUrl::AddTrailingSlash));
225 } else {
226 actions = m_versionControlObserver->contextMenuActions(items);
227 }
228 m_controller->setVersionControlActions(actions);
229 }
230
231 void ViewExtensionsFactory::requestActivation()
232 {
233 m_controller->requestActivation();
234 }
235
236 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
237 {
238 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
239 }
240
241 #include "viewextensionsfactory.moc"
242