]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
ignore spontaneous show events (thanks to Dominik Haumann for pointing this out)
[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 "dolphinmainwindow.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "sidebartreeview.h"
27 #include "treeviewcontextmenu.h"
28
29 #include <kfileplacesmodel.h>
30 #include <kdirlister.h>
31 #include <kdirmodel.h>
32 #include <kfileitem.h>
33
34 #include <QtGui/QHeaderView>
35 #include <QtGui/QItemSelection>
36 #include <QtGui/QTreeView>
37 #include <QtGui/QBoxLayout>
38
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
40 SidebarPage(parent),
41 m_dirLister(0),
42 m_dirModel(0),
43 m_proxyModel(0),
44 m_treeView(0)
45 {
46 }
47
48 TreeViewSidebarPage::~TreeViewSidebarPage()
49 {
50 delete m_dirLister;
51 m_dirLister = 0;
52 }
53
54 void TreeViewSidebarPage::setUrl(const KUrl& url)
55 {
56 if (!url.isValid() || (url == SidebarPage::url())) {
57 return;
58 }
59
60 SidebarPage::setUrl(url);
61 if (m_dirLister != 0) {
62 loadTree(url);
63 }
64 }
65
66 void TreeViewSidebarPage::showEvent(QShowEvent* event)
67 {
68 if (event->spontaneous()) {
69 SidebarPage::showEvent(event);
70 return;
71 }
72
73 if (m_dirLister == 0) {
74 // Postpone the creating of the dir lister to the first show event.
75 // This assures that no performance and memory overhead is given when the TreeView is not
76 // used at all (see TreeViewSidebarPage::setUrl()).
77 m_dirLister = new KDirLister();
78 m_dirLister->setDirOnlyMode(true);
79 m_dirLister->setAutoUpdate(true);
80 m_dirLister->setMainWindow(this);
81 m_dirLister->setDelayedMimeTypes(true);
82 m_dirLister->setAutoErrorHandlingEnabled(false, this);
83
84 Q_ASSERT(m_dirModel == 0);
85 m_dirModel = new KDirModel();
86 m_dirModel->setDirLister(m_dirLister);
87 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
88
89 Q_ASSERT(m_proxyModel == 0);
90 m_proxyModel = new DolphinSortFilterProxyModel(this);
91 m_proxyModel->setSourceModel(m_dirModel);
92
93 Q_ASSERT(m_treeView == 0);
94 m_treeView = new SidebarTreeView(this);
95 m_treeView->setModel(m_proxyModel);
96 m_proxyModel->setSorting(DolphinView::SortByName);
97 m_proxyModel->setSortOrder(Qt::AscendingOrder);
98
99 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
100 this, SLOT(updateActiveView(const QModelIndex&)));
101 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
102 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
103
104 QVBoxLayout* layout = new QVBoxLayout(this);
105 layout->setMargin(0);
106 layout->addWidget(m_treeView);
107 }
108
109 loadTree(url());
110 SidebarPage::showEvent(event);
111 }
112
113 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
114 {
115 SidebarPage::contextMenuEvent(event);
116
117 const QModelIndex index = m_treeView->indexAt(event->pos());
118 if (!index.isValid()) {
119 // only open a context menu above a directory item
120 return;
121 }
122
123 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
124 KFileItem* item = m_dirModel->itemForIndex(dirModelIndex);
125
126 emit changeSelection(KFileItemList());
127 TreeViewContextMenu contextMenu(this, item);
128 contextMenu.open();
129 }
130
131 void TreeViewSidebarPage::expandSelectionParent()
132 {
133 disconnect(m_dirLister, SIGNAL(completed()),
134 this, SLOT(expandSelectionParent()));
135
136 // expand the parent folder of the selected item
137 KUrl parentUrl = url().upUrl();
138 if (!m_dirLister->url().isParentOf(parentUrl)) {
139 return;
140 }
141
142 QModelIndex index = m_dirModel->indexForUrl(parentUrl);
143 if (index.isValid()) {
144 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
145 m_treeView->setExpanded(proxyIndex, true);
146
147 // select the item and assure that the item is visible
148 index = m_dirModel->indexForUrl(url());
149 if (index.isValid()) {
150 proxyIndex = m_proxyModel->mapFromSource(index);
151 m_treeView->scrollTo(proxyIndex);
152
153 QItemSelectionModel* selModel = m_treeView->selectionModel();
154 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
155 }
156 }
157 }
158
159 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
160 {
161 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
162 const KFileItem* item = m_dirModel->itemForIndex(dirIndex);
163 if (item != 0) {
164 const KUrl& url = item->url();
165 emit changeUrl(url);
166 }
167 }
168
169 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
170 const QModelIndex& index)
171 {
172 if (index.isValid()) {
173 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
174 KFileItem* item = m_dirModel->itemForIndex(dirIndex);
175 Q_ASSERT(item != 0);
176 if (item->isDir()) {
177 emit urlsDropped(urls, item->url());
178 }
179 }
180 }
181
182 void TreeViewSidebarPage::loadTree(const KUrl& url)
183 {
184 Q_ASSERT(m_dirLister != 0);
185
186 // adjust the root of the tree to the base bookmark
187 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
188 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
189 if (!baseUrl.isValid()) {
190 // it's possible that no closest item is available and hence an
191 // empty URL is returned
192 baseUrl = url;
193 }
194
195 if (m_dirLister->url() != baseUrl) {
196 m_dirLister->stop();
197 m_dirLister->openUrl(baseUrl);
198 }
199
200 // select the folder which contains the given URL
201 QItemSelectionModel* selModel = m_treeView->selectionModel();
202 selModel->clearSelection();
203
204 const QModelIndex index = m_dirModel->indexForUrl(url);
205 if (index.isValid()) {
206 // the item with the given URL is already part of the model
207 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
208 m_treeView->scrollTo(proxyIndex);
209 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
210 } else {
211 // The item with the given URL is not loaded by the model yet. Iterate
212 // backward to the base URL and trigger the loading of the items for
213 // each hierarchy level.
214 connect(m_dirLister, SIGNAL(completed()),
215 this, SLOT(expandSelectionParent()));
216
217 // Implementation note: It is important to remove the trailing slash from
218 // the parent URL, as the directories from the dir lister (KDirLister::directories())
219 // don't have a trailing slash and hence KUrl::List::contains() would fail...
220 KUrl parentUrl = url.upUrl();
221 parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
222 while (!parentUrl.isParentOf(baseUrl)) {
223 if (m_dirLister->directories().contains(parentUrl)) {
224 m_dirLister->updateDirectory(parentUrl);
225 } else {
226 m_dirLister->openUrl(parentUrl, true, false);
227 }
228 parentUrl = parentUrl.upUrl();
229 parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
230 }
231 }
232 }
233
234 #include "treeviewsidebarpage.moc"