]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
3793d1da83d4fd0c7704a1756a6d7981f22c9176
[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 <QItemSelection>
35 #include <QTreeView>
36 #include <QBoxLayout>
37 #include <QModelIndex>
38 #include <QTimer>
39
40 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
41 SidebarPage(parent),
42 m_dirLister(0),
43 m_dirModel(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 Q_ASSERT(m_dirModel == 0);
94 m_dirModel = new KDirModel(this);
95 m_dirModel->setDirLister(m_dirLister);
96 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
97 connect(m_dirModel, SIGNAL(expand(const QModelIndex&)),
98 this, SLOT(triggerExpanding(const QModelIndex&)));
99
100 Q_ASSERT(m_proxyModel == 0);
101 m_proxyModel = new DolphinSortFilterProxyModel(this);
102 m_proxyModel->setSourceModel(m_dirModel);
103
104 Q_ASSERT(m_treeView == 0);
105 m_treeView = new SidebarTreeView(this);
106 m_treeView->setModel(m_proxyModel);
107 m_proxyModel->setSorting(DolphinView::SortByName);
108 m_proxyModel->setSortOrder(Qt::AscendingOrder);
109
110 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
111 this, SLOT(updateActiveView(const QModelIndex&)));
112 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
113 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
114
115 QVBoxLayout* layout = new QVBoxLayout(this);
116 layout->setMargin(0);
117 layout->addWidget(m_treeView);
118 }
119
120 loadTree(url());
121 SidebarPage::showEvent(event);
122 }
123
124 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
125 {
126 SidebarPage::contextMenuEvent(event);
127
128 const QModelIndex index = m_treeView->indexAt(event->pos());
129 if (!index.isValid()) {
130 // only open a context menu above a directory item
131 return;
132 }
133
134 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
135 KFileItem item = m_dirModel->itemForIndex(dirModelIndex);
136
137 emit changeSelection(QList<KFileItem>());
138 TreeViewContextMenu contextMenu(this, item);
139 contextMenu.open();
140 }
141
142 void TreeViewSidebarPage::expandSelectionParent()
143 {
144 disconnect(m_dirLister, SIGNAL(completed()),
145 this, SLOT(expandSelectionParent()));
146
147 // expand the parent folder of the selected item
148 KUrl parentUrl = url().upUrl();
149 if (!m_dirLister->url().isParentOf(parentUrl)) {
150 return;
151 }
152
153 QModelIndex index = m_dirModel->indexForUrl(parentUrl);
154 if (index.isValid()) {
155 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
156 m_treeView->setExpanded(proxyIndex, true);
157
158 // select the item and assure that the item is visible
159 index = m_dirModel->indexForUrl(url());
160 if (index.isValid()) {
161 proxyIndex = m_proxyModel->mapFromSource(index);
162 m_treeView->scrollTo(proxyIndex);
163
164 QItemSelectionModel* selModel = m_treeView->selectionModel();
165 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
166 }
167 }
168 }
169
170 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
171 {
172 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
173 const KFileItem item = m_dirModel->itemForIndex(dirIndex);
174 if (!item.isNull()) {
175 emit changeUrl(item.url());
176 }
177 }
178
179 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
180 const QModelIndex& index)
181 {
182 if (index.isValid()) {
183 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
184 KFileItem item = m_dirModel->itemForIndex(dirIndex);
185 Q_ASSERT(!item.isNull());
186 if (item.isDir()) {
187 emit urlsDropped(urls, item.url());
188 }
189 }
190 }
191
192 void TreeViewSidebarPage::triggerExpanding(const QModelIndex& index)
193 {
194 Q_UNUSED(index);
195 // the expanding of the folders may not be done in the context
196 // of this slot
197 QTimer::singleShot(0, this, SLOT(expandToLeafDir()));
198 }
199
200 void TreeViewSidebarPage::expandToLeafDir()
201 {
202 // expand all directories until the parent directory of m_leafDir
203 const KUrl parentUrl = m_leafDir.upUrl();
204 QModelIndex dirIndex = m_dirModel->indexForUrl(parentUrl);
205 QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
206 m_treeView->setExpanded(proxyIndex, true);
207
208 // assure that m_leafDir gets selected
209 dirIndex = m_dirModel->indexForUrl(m_leafDir);
210 proxyIndex = m_proxyModel->mapFromSource(dirIndex);
211 m_treeView->scrollTo(proxyIndex);
212
213 QItemSelectionModel* selModel = m_treeView->selectionModel();
214 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
215 }
216
217 void TreeViewSidebarPage::loadSubTree()
218 {
219 disconnect(m_dirLister, SIGNAL(completed()),
220 this, SLOT(loadSubTree()));
221
222 QItemSelectionModel* selModel = m_treeView->selectionModel();
223 selModel->clearSelection();
224
225 if (m_leafDir.isParentOf(m_dirLister->url())) {
226 // The leaf directory is not a child of the base URL, hence
227 // no sub directory must be loaded or selected.
228 return;
229 }
230
231 const QModelIndex index = m_dirModel->indexForUrl(m_leafDir);
232 if (index.isValid()) {
233 // the item with the given URL is already part of the model
234 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
235 m_treeView->scrollTo(proxyIndex);
236 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
237 } else {
238 // Load all sub directories that need to get expanded for making
239 // the leaf directory visible. The slot triggerExpanding() will
240 // get invoked if the expanding has been finished.
241 m_dirModel->expandToUrl(m_leafDir);
242 }
243 }
244
245 void TreeViewSidebarPage::loadTree(const KUrl& url)
246 {
247 Q_ASSERT(m_dirLister != 0);
248 m_leafDir = url;
249
250 // adjust the root of the tree to the base place
251 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
252 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
253 if (!baseUrl.isValid()) {
254 // it's possible that no closest item is available and hence an
255 // empty URL is returned
256 baseUrl = url;
257 }
258
259 connect(m_dirLister, SIGNAL(completed()),
260 this, SLOT(loadSubTree()));
261 if (m_dirLister->url() != baseUrl) {
262 m_dirLister->stop();
263 m_dirLister->openUrl(baseUrl);
264 } else {
265 loadSubTree();
266 }
267 }
268
269 #include "treeviewsidebarpage.moc"