]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
use KGlobal::config() instead of parsing kdeglobals each time (thanks to David for...
[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_dirLister;
52 m_dirLister = 0;
53 }
54
55 QSize TreeViewSidebarPage::sizeHint() const
56 {
57 QSize size = SidebarPage::sizeHint();
58 size.setWidth(200);
59 return size;
60 }
61
62 void TreeViewSidebarPage::setUrl(const KUrl& url)
63 {
64 if (!url.isValid() || (url == SidebarPage::url())) {
65 return;
66 }
67
68 SidebarPage::setUrl(url);
69 if (m_dirLister != 0) {
70 loadTree(url);
71 }
72 }
73
74 void TreeViewSidebarPage::showEvent(QShowEvent* event)
75 {
76 if (event->spontaneous()) {
77 SidebarPage::showEvent(event);
78 return;
79 }
80
81 if (m_dirLister == 0) {
82 // Postpone the creating of the dir lister to the first show event.
83 // This assures that no performance and memory overhead is given when the TreeView is not
84 // used at all (see TreeViewSidebarPage::setUrl()).
85 m_dirLister = new KDirLister();
86 m_dirLister->setDirOnlyMode(true);
87 m_dirLister->setAutoUpdate(true);
88 m_dirLister->setMainWindow(this);
89 m_dirLister->setDelayedMimeTypes(true);
90 m_dirLister->setAutoErrorHandlingEnabled(false, this);
91
92 connect(m_dirLister, SIGNAL(started(const KUrl&)),
93 this, SLOT(slotDirListerStarted(const KUrl&)));
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
190 // assure that m_leafDir gets selected
191 dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
192 proxyIndex = m_proxyModel->mapFromSource(dirIndex);
193 m_treeView->scrollTo(proxyIndex);
194
195 QItemSelectionModel* selModel = m_treeView->selectionModel();
196 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
197 }
198
199
200 void TreeViewSidebarPage::loadSubTree()
201 {
202 QItemSelectionModel* selModel = m_treeView->selectionModel();
203 selModel->clearSelection();
204
205 if (m_leafDir.isParentOf(m_dirLister->url())) {
206 // The leaf directory is not a child of the base URL, hence
207 // no sub directory must be loaded or selected.
208 return;
209 }
210
211 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
212 if (index.isValid()) {
213 // the item with the given URL is already part of the model
214 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
215 m_treeView->scrollTo(proxyIndex);
216 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
217 } else {
218 // Load all sub directories that need to get expanded for making
219 // the leaf directory visible. The slot triggerExpanding() will
220 // get invoked if the expanding has been finished.
221 m_dolphinModel->expandToUrl(m_leafDir);
222 }
223 }
224
225 void TreeViewSidebarPage::loadTree(const KUrl& url)
226 {
227 Q_ASSERT(m_dirLister != 0);
228 m_leafDir = url;
229
230 // adjust the root of the tree to the base place
231 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
232 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
233 if (!baseUrl.isValid()) {
234 // it's possible that no closest item is available and hence an
235 // empty URL is returned
236 baseUrl = url;
237 }
238
239 if (m_dirLister->url() != baseUrl) {
240 m_dirLister->stop();
241 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
242 } else {
243 loadSubTree();
244 }
245 }
246
247 #include "treeviewsidebarpage.moc"