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