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