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