]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
There are some extractable strings in subdirs too.
[dolphin.git] / src / treeviewsidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 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 "treeviewsidebarpage.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_folderspanelsettings.h"
27 #include "sidebartreeview.h"
28 #include "treeviewcontextmenu.h"
29
30 #include <kfileplacesmodel.h>
31 #include <kdirlister.h>
32 #include <kfileitem.h>
33
34 #include <QApplication>
35 #include <QItemSelection>
36 #include <QTreeView>
37 #include <QBoxLayout>
38 #include <QModelIndex>
39 #include <QScrollBar>
40 #include <QTimer>
41
42 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
43 SidebarPage(parent),
44 m_setLeafVisible(false),
45 m_mouseButtons(Qt::NoButton),
46 m_dirLister(0),
47 m_dolphinModel(0),
48 m_proxyModel(0),
49 m_treeView(0),
50 m_leafDir()
51 {
52 }
53
54 TreeViewSidebarPage::~TreeViewSidebarPage()
55 {
56 FoldersPanelSettings::self()->writeConfig();
57
58 delete m_proxyModel;
59 m_proxyModel = 0;
60 delete m_dolphinModel;
61 m_dolphinModel = 0;
62 m_dirLister = 0; // deleted by m_dolphinModel
63 }
64
65 QSize TreeViewSidebarPage::sizeHint() const
66 {
67 return QSize(200, 400);
68 }
69
70 void TreeViewSidebarPage::setShowHiddenFiles(bool show)
71 {
72 FoldersPanelSettings::setShowHiddenFiles(show);
73 if (m_dirLister != 0) {
74 m_dirLister->setShowingDotFiles(show);
75 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
76 }
77 }
78
79 bool TreeViewSidebarPage::showHiddenFiles() const
80 {
81 return FoldersPanelSettings::showHiddenFiles();
82 }
83
84 void TreeViewSidebarPage::setUrl(const KUrl& url)
85 {
86 if (!url.isValid() || (url == SidebarPage::url())) {
87 return;
88 }
89
90 SidebarPage::setUrl(url);
91 if (m_dirLister != 0) {
92 m_setLeafVisible = true;
93 loadTree(url);
94 }
95 }
96
97 void TreeViewSidebarPage::showEvent(QShowEvent* event)
98 {
99 if (event->spontaneous()) {
100 SidebarPage::showEvent(event);
101 return;
102 }
103
104 if (m_dirLister == 0) {
105 // Postpone the creating of the dir lister to the first show event.
106 // This assures that no performance and memory overhead is given when the TreeView is not
107 // used at all (see TreeViewSidebarPage::setUrl()).
108 m_dirLister = new KDirLister();
109 m_dirLister->setDirOnlyMode(true);
110 m_dirLister->setAutoUpdate(true);
111 m_dirLister->setMainWindow(window());
112 m_dirLister->setDelayedMimeTypes(true);
113 m_dirLister->setAutoErrorHandlingEnabled(false, this);
114 m_dirLister->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
115
116 connect(m_dirLister, SIGNAL(completed()),
117 this, SLOT(triggerLoadSubTree()));
118
119 Q_ASSERT(m_dolphinModel == 0);
120 m_dolphinModel = new DolphinModel(this);
121 m_dolphinModel->setDirLister(m_dirLister);
122 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
123 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
124 this, SLOT(triggerExpanding()));
125
126 Q_ASSERT(m_proxyModel == 0);
127 m_proxyModel = new DolphinSortFilterProxyModel(this);
128 m_proxyModel->setSourceModel(m_dolphinModel);
129
130 Q_ASSERT(m_treeView == 0);
131 m_treeView = new SidebarTreeView(this);
132 m_treeView->setModel(m_proxyModel);
133 m_proxyModel->setSorting(DolphinView::SortByName);
134 m_proxyModel->setSortOrder(Qt::AscendingOrder);
135
136 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
137 this, SLOT(updateActiveView(const QModelIndex&)));
138 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
139 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
140 connect(m_treeView, SIGNAL(pressed(const QModelIndex&)),
141 this, SLOT(updateMouseButtons()));
142
143 QVBoxLayout* layout = new QVBoxLayout(this);
144 layout->setMargin(0);
145 layout->addWidget(m_treeView);
146 }
147
148 loadTree(url());
149 SidebarPage::showEvent(event);
150 }
151
152 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
153 {
154 SidebarPage::contextMenuEvent(event);
155
156 KFileItem item;
157 const QModelIndex index = m_treeView->indexAt(event->pos());
158 if (index.isValid()) {
159 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
160 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
161 emit changeSelection(KFileItemList());
162 }
163
164 TreeViewContextMenu contextMenu(this, item);
165 contextMenu.open();
166 }
167
168 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
169 {
170 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
171 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
172 if (!item.isNull()) {
173 emit changeUrl(item.url(), m_mouseButtons);
174 }
175 }
176
177 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
178 const QModelIndex& index)
179 {
180 if (index.isValid()) {
181 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
182 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
183 Q_ASSERT(!item.isNull());
184 if (item.isDir()) {
185 emit urlsDropped(urls, item.url());
186 }
187 }
188 }
189
190 void TreeViewSidebarPage::triggerExpanding()
191 {
192 // the expanding of the folders may not be done in the context
193 // of this slot
194 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection);
195 }
196
197 void TreeViewSidebarPage::triggerLoadSubTree()
198 {
199 // the loading of the sub tree may not be done in the context
200 // of this slot
201 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection);
202 }
203
204 void TreeViewSidebarPage::expandToLeafDir()
205 {
206 // expand all directories until the parent directory of m_leafDir
207 const KUrl parentUrl = m_leafDir.upUrl();
208 QModelIndex dirIndex = m_dolphinModel->indexForUrl(parentUrl);
209 QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
210 m_treeView->setExpanded(proxyIndex, true);
211 selectLeafDirectory();
212 }
213
214
215 void TreeViewSidebarPage::loadSubTree()
216 {
217 m_treeView->selectionModel()->clearSelection();
218
219 if (m_leafDir.isParentOf(m_dirLister->url())) {
220 // The leaf directory is not a child of the base URL, hence
221 // no sub directory must be loaded or selected.
222 return;
223 }
224
225 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
226 if (index.isValid()) {
227 selectLeafDirectory();
228 } else {
229 // Load all sub directories that need to get expanded for making
230 // the leaf directory visible. The slot triggerExpanding() will
231 // get invoked if the expanding has been finished.
232 m_dolphinModel->expandToUrl(m_leafDir);
233 }
234 }
235
236 void TreeViewSidebarPage::scrollToLeaf()
237 {
238 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
239 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
240 if (proxyIndex.isValid()) {
241 m_treeView->scrollTo(proxyIndex);
242 }
243 }
244
245 void TreeViewSidebarPage::updateMouseButtons()
246 {
247 m_mouseButtons = QApplication::mouseButtons();
248 }
249
250 void TreeViewSidebarPage::loadTree(const KUrl& url)
251 {
252 Q_ASSERT(m_dirLister != 0);
253 m_leafDir = url;
254
255 KUrl baseUrl = url;
256 if (url.isLocalFile()) {
257 // use the root directory as base for local URLs
258 baseUrl = QDir::rootPath();
259 } else {
260 // clear the path for non-local URLs and use it as base
261 baseUrl = url;
262 baseUrl.setPath(QString());
263 }
264
265 if (m_dirLister->url() != baseUrl) {
266 m_dirLister->stop();
267 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
268 } else {
269 loadSubTree();
270 }
271 }
272
273 void TreeViewSidebarPage::selectLeafDirectory()
274 {
275 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
276 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
277 if (!proxyIndex.isValid()) {
278 return;
279 }
280
281 if (m_setLeafVisible) {
282 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
283 // scrollToLeaf(). This assures that the scrolling is done after
284 // the horizontal scrollbar gets visible (otherwise the scrollbar
285 // might hide the leaf).
286 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
287 m_setLeafVisible = false;
288 }
289
290 QItemSelectionModel* selModel = m_treeView->selectionModel();
291 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
292 }
293
294 #include "treeviewsidebarpage.moc"