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