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