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