]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
Forwardport 773570:
[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 "dolphinmodel.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 <kfileitem.h>
32
33 #include <QItemSelection>
34 #include <QTreeView>
35 #include <QBoxLayout>
36 #include <QModelIndex>
37 #include <QScrollBar>
38 #include <QTimer>
39
40 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
41 SidebarPage(parent),
42 m_setLeafVisible(false),
43 m_horizontalPos(0),
44 m_dirLister(0),
45 m_dolphinModel(0),
46 m_proxyModel(0),
47 m_treeView(0),
48 m_leafDir()
49 {
50 }
51
52 TreeViewSidebarPage::~TreeViewSidebarPage()
53 {
54 delete m_proxyModel;
55 m_proxyModel = 0;
56 delete m_dolphinModel;
57 m_dolphinModel = 0;
58 m_dirLister = 0; // deleted by m_dolphinModel
59 }
60
61 QSize TreeViewSidebarPage::sizeHint() const
62 {
63 return QSize(200, 400);
64 }
65
66 void TreeViewSidebarPage::setUrl(const KUrl& url)
67 {
68 if (!url.isValid() || (url == SidebarPage::url())) {
69 return;
70 }
71
72 SidebarPage::setUrl(url);
73 if (m_dirLister != 0) {
74 m_setLeafVisible = true;
75 loadTree(url);
76 }
77 }
78
79 void TreeViewSidebarPage::showEvent(QShowEvent* event)
80 {
81 if (event->spontaneous()) {
82 SidebarPage::showEvent(event);
83 return;
84 }
85
86 if (m_dirLister == 0) {
87 // Postpone the creating of the dir lister to the first show event.
88 // This assures that no performance and memory overhead is given when the TreeView is not
89 // used at all (see TreeViewSidebarPage::setUrl()).
90 m_dirLister = new KDirLister();
91 m_dirLister->setDirOnlyMode(true);
92 m_dirLister->setAutoUpdate(true);
93 m_dirLister->setMainWindow(this);
94 m_dirLister->setDelayedMimeTypes(true);
95 m_dirLister->setAutoErrorHandlingEnabled(false, this);
96
97 connect(m_dirLister, SIGNAL(completed()),
98 this, SLOT(triggerLoadSubTree()));
99
100 Q_ASSERT(m_dolphinModel == 0);
101 m_dolphinModel = new DolphinModel(this);
102 m_dolphinModel->setDirLister(m_dirLister);
103 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
104 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
105 this, SLOT(triggerExpanding()));
106
107 Q_ASSERT(m_proxyModel == 0);
108 m_proxyModel = new DolphinSortFilterProxyModel(this);
109 m_proxyModel->setSourceModel(m_dolphinModel);
110
111 Q_ASSERT(m_treeView == 0);
112 m_treeView = new SidebarTreeView(this);
113 m_treeView->setModel(m_proxyModel);
114 m_proxyModel->setSorting(DolphinView::SortByName);
115 m_proxyModel->setSortOrder(Qt::AscendingOrder);
116
117 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
118 this, SLOT(updateActiveView(const QModelIndex&)));
119 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
120 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
121
122 QVBoxLayout* layout = new QVBoxLayout(this);
123 layout->setMargin(0);
124 layout->addWidget(m_treeView);
125 }
126
127 loadTree(url());
128 SidebarPage::showEvent(event);
129 }
130
131 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
132 {
133 SidebarPage::contextMenuEvent(event);
134
135 const QModelIndex index = m_treeView->indexAt(event->pos());
136 if (!index.isValid()) {
137 // only open a context menu above a directory item
138 return;
139 }
140
141 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
142 KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
143
144 emit changeSelection(KFileItemList());
145 TreeViewContextMenu contextMenu(this, item);
146 contextMenu.open();
147 }
148
149 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
150 {
151 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
152 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
153 if (!item.isNull()) {
154 emit changeUrl(item.url());
155 }
156 }
157
158 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
159 const QModelIndex& index)
160 {
161 if (index.isValid()) {
162 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
163 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
164 Q_ASSERT(!item.isNull());
165 if (item.isDir()) {
166 emit urlsDropped(urls, item.url());
167 }
168 }
169 }
170
171 void TreeViewSidebarPage::triggerExpanding()
172 {
173 // the expanding of the folders may not be done in the context
174 // of this slot
175 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection);
176 }
177
178 void TreeViewSidebarPage::triggerLoadSubTree()
179 {
180 // the loading of the sub tree may not be done in the context
181 // of this slot
182 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection);
183 }
184
185 void TreeViewSidebarPage::expandToLeafDir()
186 {
187 // expand all directories until the parent directory of m_leafDir
188 const KUrl parentUrl = m_leafDir.upUrl();
189 QModelIndex dirIndex = m_dolphinModel->indexForUrl(parentUrl);
190 QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
191 m_treeView->setExpanded(proxyIndex, true);
192 selectLeafDirectory();
193 }
194
195
196 void TreeViewSidebarPage::loadSubTree()
197 {
198 m_treeView->selectionModel()->clearSelection();
199
200 if (m_leafDir.isParentOf(m_dirLister->url())) {
201 // The leaf directory is not a child of the base URL, hence
202 // no sub directory must be loaded or selected.
203 return;
204 }
205
206 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
207 if (index.isValid()) {
208 selectLeafDirectory();
209 } else {
210 // Load all sub directories that need to get expanded for making
211 // the leaf directory visible. The slot triggerExpanding() will
212 // get invoked if the expanding has been finished.
213 m_dolphinModel->expandToUrl(m_leafDir);
214 }
215 }
216
217 void TreeViewSidebarPage::scrollToLeaf()
218 {
219 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
220 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
221 if (proxyIndex.isValid()) {
222 m_treeView->scrollTo(proxyIndex);
223 }
224 }
225
226 void TreeViewSidebarPage::loadTree(const KUrl& url)
227 {
228 Q_ASSERT(m_dirLister != 0);
229 m_leafDir = url;
230
231 m_horizontalPos = m_treeView->horizontalScrollBar()->value();
232
233 KUrl baseUrl = url;
234 if (url.isLocalFile()) {
235 // use the root directory as base for local URLs
236 baseUrl = KUrl("file:///");
237 } else {
238 // clear the path for non-local URLs and use it as base
239 baseUrl = url;
240 baseUrl.setPath(QString());
241 }
242
243 if (m_dirLister->url() != baseUrl) {
244 m_dirLister->stop();
245 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
246 } else {
247 loadSubTree();
248 }
249 }
250
251 void TreeViewSidebarPage::selectLeafDirectory()
252 {
253 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
254 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
255 if (!proxyIndex.isValid()) {
256 return;
257 }
258
259 if (m_setLeafVisible) {
260 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
261 // scrollToLeaf(). This assures that the scrolling is done after
262 // the horizontal scrollbar gets visible (otherwise the scrollbar
263 // might hide the leaf).
264 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
265 m_setLeafVisible = false;
266 }
267
268 QItemSelectionModel* selModel = m_treeView->selectionModel();
269 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
270
271 m_treeView->horizontalScrollBar()->setValue(m_horizontalPos);
272 }
273
274 #include "treeviewsidebarpage.moc"