]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewextensionsfactory.cpp
restore sorting functionality in a generic way which also works for the column view
[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
31 #include "dolphin_generalsettings.h"
32
33 #include <kdirlister.h>
34 #include <kdirmodel.h>
35 #include <kfilepreviewgenerator.h>
36 #include <QAbstractItemView>
37
38 ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView* view,
39 DolphinController* controller) :
40 QObject(view),
41 m_view(view),
42 m_controller(controller),
43 m_toolTipManager(0),
44 m_previewGenerator(0),
45 m_selectionManager(0),
46 m_autoScroller(0),
47 m_fileItemDelegate(0)
48 {
49 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
50
51 // initialize tooltips
52 if (settings->showToolTips()) {
53 DolphinSortFilterProxyModel* proxyModel = static_cast<DolphinSortFilterProxyModel*>(view->model());
54 m_toolTipManager = new ToolTipManager(view, proxyModel);
55
56 connect(controller, SIGNAL(hideToolTip()),
57 m_toolTipManager, SLOT(hideTip()));
58 }
59
60 // initialize preview generator
61 m_previewGenerator = new KFilePreviewGenerator(view);
62 m_previewGenerator->setPreviewShown(controller->dolphinView()->showPreview());
63 connect(controller, SIGNAL(zoomLevelChanged(int)),
64 this, SLOT(slotZoomLevelChanged()));
65 connect(controller, SIGNAL(cancelPreviews()),
66 this, SLOT(cancelPreviews()));
67 connect(controller->dolphinView(), SIGNAL(showPreviewChanged()),
68 this, SLOT(slotShowPreviewChanged()));
69
70 // initialize selection manager
71 if (settings->showSelectionToggle()) {
72 m_selectionManager = new SelectionManager(view);
73 connect(m_selectionManager, SIGNAL(selectionChanged()),
74 this, SLOT(requestActivation()));
75 connect(controller, SIGNAL(urlChanged(const KUrl&)),
76 m_selectionManager, SLOT(reset()));
77 }
78
79 // initialize auto scroller
80 m_autoScroller = new DolphinViewAutoScroller(view);
81
82 // initialize file item delegate
83 m_fileItemDelegate = new DolphinFileItemDelegate(view);
84 m_fileItemDelegate->setShowToolTipWhenElided(false);
85 view->setItemDelegate(m_fileItemDelegate);
86
87 // react on view property changes
88 const DolphinView* dolphinView = controller->dolphinView();
89 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
90 this, SLOT(slotShowHiddenFilesChanged()));
91 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
92 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
93 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
94 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
95 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
96 this, SLOT(slotSortFoldersFirstChanged(bool)));
97 view->viewport()->installEventFilter(this);
98 }
99
100 ViewExtensionsFactory::~ViewExtensionsFactory()
101 {
102 }
103
104 void ViewExtensionsFactory::handleCurrentIndexChange(const QModelIndex& current, const QModelIndex& previous)
105 {
106 m_autoScroller->handleCurrentIndexChange(current, previous);
107 }
108
109 DolphinFileItemDelegate* ViewExtensionsFactory::fileItemDelegate() const
110 {
111 return m_fileItemDelegate;
112 }
113
114 bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event)
115 {
116 Q_UNUSED(watched);
117 if ((event->type() == QEvent::Wheel) && (m_selectionManager != 0)) {
118 m_selectionManager->reset();
119 }
120 return false;
121 }
122
123 void ViewExtensionsFactory::slotZoomLevelChanged()
124 {
125 m_previewGenerator->updateIcons();
126 if (m_selectionManager != 0) {
127 m_selectionManager->reset();
128 }
129 }
130
131 void ViewExtensionsFactory::cancelPreviews()
132 {
133 m_previewGenerator->cancelPreviews();
134 }
135
136 void ViewExtensionsFactory::slotShowPreviewChanged()
137 {
138 const bool show = m_controller->dolphinView()->showPreview();
139 m_previewGenerator->setPreviewShown(show);
140 }
141
142 void ViewExtensionsFactory::slotShowHiddenFilesChanged()
143 {
144 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel()->sourceModel());
145 KDirLister* dirLister = dirModel->dirLister();
146
147 dirLister->stop();
148
149 const bool show = m_controller->dolphinView()->showHiddenFiles();
150 dirLister->setShowingDotFiles(show);
151
152 const KUrl url = dirLister->url();
153 if (url.isValid()) {
154 dirLister->openUrl(url, KDirLister::NoFlags);
155 }
156 }
157
158 void ViewExtensionsFactory::slotSortingChanged(DolphinView::Sorting sorting)
159 {
160 proxyModel()->setSorting(sorting);
161 }
162
163 void ViewExtensionsFactory::slotSortOrderChanged(Qt::SortOrder order)
164 {
165 proxyModel()->setSortOrder(order);
166 }
167
168 void ViewExtensionsFactory::slotSortFoldersFirstChanged(bool foldersFirst)
169 {
170 proxyModel()->setSortFoldersFirst(foldersFirst);
171 }
172
173 void ViewExtensionsFactory::requestActivation()
174 {
175 m_controller->requestActivation();
176 }
177
178 DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const
179 {
180 return static_cast<DolphinSortFilterProxyModel*>(m_view->model());
181 }
182
183 #include "viewextensionsfactory.moc"
184