]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
SVN_SILENT made messages (.desktop file)
[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(KFileItemList());
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 != 0) {
171 const KUrl& url = item->url();
172 emit changeUrl(url);
173 }
174 }
175
176 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
177 const QModelIndex& index)
178 {
179 if (index.isValid()) {
180 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
181 KFileItem* item = m_dirModel->itemForIndex(dirIndex);
182 Q_ASSERT(item != 0);
183 if (item->isDir()) {
184 emit urlsDropped(urls, item->url());
185 }
186 }
187 }
188
189 void TreeViewSidebarPage::loadTree(const KUrl& url)
190 {
191 Q_ASSERT(m_dirLister != 0);
192
193 // adjust the root of the tree to the base bookmark
194 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
195 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
196 if (!baseUrl.isValid()) {
197 // it's possible that no closest item is available and hence an
198 // empty URL is returned
199 baseUrl = url;
200 }
201
202 if (m_dirLister->url() != baseUrl) {
203 m_dirLister->stop();
204 m_dirLister->openUrl(baseUrl);
205 }
206
207 // select the folder which contains the given URL
208 QItemSelectionModel* selModel = m_treeView->selectionModel();
209 selModel->clearSelection();
210
211 const QModelIndex index = m_dirModel->indexForUrl(url);
212 if (index.isValid()) {
213 // the item with the given URL is already part of the model
214 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
215 m_treeView->scrollTo(proxyIndex);
216 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
217 } else {
218 // The item with the given URL is not loaded by the model yet. Iterate
219 // backward to the base URL and trigger the loading of the items for
220 // each hierarchy level.
221 connect(m_dirLister, SIGNAL(completed()),
222 this, SLOT(expandSelectionParent()));
223
224 // Implementation note: It is important to remove the trailing slash from
225 // the parent URL, as the directories from the dir lister (KDirLister::directories())
226 // don't have a trailing slash and hence KUrl::List::contains() would fail...
227 KUrl parentUrl = url.upUrl();
228 parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
229 while (!parentUrl.isParentOf(baseUrl)) {
230 if (m_dirLister->directories().contains(parentUrl)) {
231 m_dirLister->updateDirectory(parentUrl);
232 } else {
233 m_dirLister->openUrl(parentUrl, true, false);
234 }
235 parentUrl = parentUrl.upUrl();
236 parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
237 }
238 }
239 }
240
241 #include "treeviewsidebarpage.moc"