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