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