]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
lessons learned from the column view: never invoke KDirModel::expandToUrl() when...
[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 "dolphinmainwindow.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinsettings.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 <QItemSelection>
35 #include <QTreeView>
36 #include <QBoxLayout>
37 #include <QModelIndex>
38
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
40 SidebarPage(parent),
41 m_dirListerCompleted(false),
42 m_dirLister(0),
43 m_dolphinModel(0),
44 m_proxyModel(0),
45 m_treeView(0),
46 m_leafDir()
47 {
48 }
49
50 TreeViewSidebarPage::~TreeViewSidebarPage()
51 {
52 delete m_dirLister;
53 m_dirLister = 0;
54 }
55
56 QSize TreeViewSidebarPage::sizeHint() const
57 {
58 QSize size = SidebarPage::sizeHint();
59 size.setWidth(200);
60 return size;
61 }
62
63 void TreeViewSidebarPage::setUrl(const KUrl& url)
64 {
65 if (!url.isValid() || (url == SidebarPage::url())) {
66 return;
67 }
68
69 SidebarPage::setUrl(url);
70 if (m_dirLister != 0) {
71 loadTree(url);
72 }
73 }
74
75 void TreeViewSidebarPage::showEvent(QShowEvent* event)
76 {
77 if (event->spontaneous()) {
78 SidebarPage::showEvent(event);
79 return;
80 }
81
82 if (m_dirLister == 0) {
83 // Postpone the creating of the dir lister to the first show event.
84 // This assures that no performance and memory overhead is given when the TreeView is not
85 // used at all (see TreeViewSidebarPage::setUrl()).
86 m_dirLister = new KDirLister();
87 m_dirLister->setDirOnlyMode(true);
88 m_dirLister->setAutoUpdate(true);
89 m_dirLister->setMainWindow(this);
90 m_dirLister->setDelayedMimeTypes(true);
91 m_dirLister->setAutoErrorHandlingEnabled(false, this);
92
93 m_dirListerCompleted = true;
94 connect(m_dirLister, SIGNAL(started(const KUrl&)),
95 this, SLOT(slotDirListerStarted(const KUrl&)));
96 connect(m_dirLister, SIGNAL(completed()),
97 this, SLOT(slotDirListerCompleted()));
98
99 Q_ASSERT(m_dolphinModel == 0);
100 m_dolphinModel = new DolphinModel(this);
101 m_dolphinModel->setDirLister(m_dirLister);
102 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
103 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
104 this, SLOT(triggerExpanding(const QModelIndex&)));
105
106 Q_ASSERT(m_proxyModel == 0);
107 m_proxyModel = new DolphinSortFilterProxyModel(this);
108 m_proxyModel->setSourceModel(m_dolphinModel);
109
110 Q_ASSERT(m_treeView == 0);
111 m_treeView = new SidebarTreeView(this);
112 m_treeView->setModel(m_proxyModel);
113 m_proxyModel->setSorting(DolphinView::SortByName);
114 m_proxyModel->setSortOrder(Qt::AscendingOrder);
115
116 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
117 this, SLOT(updateActiveView(const QModelIndex&)));
118 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
119 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
120
121 QVBoxLayout* layout = new QVBoxLayout(this);
122 layout->setMargin(0);
123 layout->addWidget(m_treeView);
124 }
125
126 loadTree(url());
127 SidebarPage::showEvent(event);
128 }
129
130 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
131 {
132 SidebarPage::contextMenuEvent(event);
133
134 const QModelIndex index = m_treeView->indexAt(event->pos());
135 if (!index.isValid()) {
136 // only open a context menu above a directory item
137 return;
138 }
139
140 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
141 KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
142
143 emit changeSelection(QList<KFileItem>());
144 TreeViewContextMenu contextMenu(this, item);
145 contextMenu.open();
146 }
147
148 void TreeViewSidebarPage::expandSelectionParent()
149 {
150 disconnect(m_dirLister, SIGNAL(completed()),
151 this, SLOT(expandSelectionParent()));
152
153 // expand the parent folder of the selected item
154 KUrl parentUrl = url().upUrl();
155 if (!m_dirLister->url().isParentOf(parentUrl)) {
156 return;
157 }
158
159 QModelIndex index = m_dolphinModel->indexForUrl(parentUrl);
160 if (index.isValid()) {
161 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
162 m_treeView->setExpanded(proxyIndex, true);
163
164 // select the item and assure that the item is visible
165 index = m_dolphinModel->indexForUrl(url());
166 if (index.isValid()) {
167 proxyIndex = m_proxyModel->mapFromSource(index);
168 m_treeView->scrollTo(proxyIndex);
169
170 QItemSelectionModel* selModel = m_treeView->selectionModel();
171 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
172 }
173 }
174 }
175
176 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
177 {
178 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
179 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
180 if (!item.isNull()) {
181 emit changeUrl(item.url());
182 }
183 }
184
185 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
186 const QModelIndex& index)
187 {
188 if (index.isValid()) {
189 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
190 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
191 Q_ASSERT(!item.isNull());
192 if (item.isDir()) {
193 emit urlsDropped(urls, item.url());
194 }
195 }
196 }
197
198 void TreeViewSidebarPage::triggerExpanding(const QModelIndex& index)
199 {
200 Q_UNUSED(index);
201 // the expanding of the folders may not be done in the context
202 // of this slot
203 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection);
204 }
205
206 void TreeViewSidebarPage::expandToLeafDir()
207 {
208 // expand all directories until the parent directory of m_leafDir
209 const KUrl parentUrl = m_leafDir.upUrl();
210 QModelIndex dirIndex = m_dolphinModel->indexForUrl(parentUrl);
211 QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
212 m_treeView->setExpanded(proxyIndex, true);
213
214 // assure that m_leafDir gets selected
215 dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
216 proxyIndex = m_proxyModel->mapFromSource(dirIndex);
217 m_treeView->scrollTo(proxyIndex);
218
219 QItemSelectionModel* selModel = m_treeView->selectionModel();
220 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
221 }
222
223 void TreeViewSidebarPage::loadSubTree()
224 {
225 disconnect(m_dirLister, SIGNAL(completed()),
226 this, SLOT(loadSubTree()));
227
228 QItemSelectionModel* selModel = m_treeView->selectionModel();
229 selModel->clearSelection();
230
231 if (m_leafDir.isParentOf(m_dirLister->url())) {
232 // The leaf directory is not a child of the base URL, hence
233 // no sub directory must be loaded or selected.
234 return;
235 }
236
237 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
238 if (index.isValid()) {
239 // the item with the given URL is already part of the model
240 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
241 m_treeView->scrollTo(proxyIndex);
242 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
243 } else {
244 // Load all sub directories that need to get expanded for making
245 // the leaf directory visible. The slot triggerExpanding() will
246 // get invoked if the expanding has been finished.
247 Q_ASSERT(m_dirListerCompleted);
248 m_dolphinModel->expandToUrl(m_leafDir);
249 }
250 }
251
252 void TreeViewSidebarPage::slotDirListerStarted(const KUrl& url)
253 {
254 Q_UNUSED(url);
255 m_dirListerCompleted = false;
256 }
257
258 void TreeViewSidebarPage::slotDirListerCompleted()
259 {
260 m_dirListerCompleted = true;
261 }
262
263
264 void TreeViewSidebarPage::loadTree(const KUrl& url)
265 {
266 Q_ASSERT(m_dirLister != 0);
267 m_leafDir = url;
268
269 // adjust the root of the tree to the base place
270 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
271 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
272 if (!baseUrl.isValid()) {
273 // it's possible that no closest item is available and hence an
274 // empty URL is returned
275 baseUrl = url;
276 }
277
278 connect(m_dirLister, SIGNAL(completed()),
279 this, SLOT(loadSubTree()));
280 if ((m_dirLister->url() != baseUrl) || !m_dirListerCompleted) {
281 m_dirLister->stop();
282 m_dirLister->openUrl(baseUrl);
283 } else {
284 loadSubTree();
285 }
286 }
287
288 #include "treeviewsidebarpage.moc"