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