]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
cleanup of unused forward declarations
[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 "bookmarkselector.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "sidebartreeview.h"
27
28 #include <kdirlister.h>
29 #include <kdirmodel.h>
30 #include <kfileitem.h>
31
32 #include <QHeaderView>
33 #include <QItemSelectionModel>
34 #include <QTreeView>
35 #include <QVBoxLayout>
36
37 // TODO: currently when using a proxy model the strange effect occurs
38 // that items get duplicated. Activate the following define to have the proxy
39 // model:
40 //#define USE_PROXY_MODEL
41
42 TreeViewSidebarPage::TreeViewSidebarPage(DolphinMainWindow* mainWindow,
43 QWidget* parent) :
44 SidebarPage(mainWindow, parent),
45 m_dirLister(0),
46 m_dirModel(0),
47 m_proxyModel(0),
48 m_treeView(0),
49 m_selectedUrl()
50 {
51 Q_ASSERT(mainWindow != 0);
52
53 m_dirLister = new KDirLister();
54 m_dirLister->setDirOnlyMode(true);
55 m_dirLister->setAutoUpdate(true);
56 m_dirLister->setMainWindow(this);
57 m_dirLister->setDelayedMimeTypes(true);
58 m_dirLister->setAutoErrorHandlingEnabled(false, this);
59
60 m_dirModel = new KDirModel();
61 m_dirModel->setDirLister(m_dirLister);
62 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
63
64
65 #if defined(USE_PROXY_MODEL)
66 m_proxyModel = new DolphinSortFilterProxyModel(this);
67 m_proxyModel->setSourceModel(m_dirModel);
68
69 m_treeView = new SidebarTreeView(mainWindow, this);
70 m_treeView->setModel(m_proxyModel);
71
72 m_proxyModel->setSorting(DolphinView::SortByName);
73 m_proxyModel->setSortOrder(Qt::Ascending);
74 #else
75 m_treeView = new SidebarTreeView(mainWindow, this);
76 m_treeView->setModel(m_dirModel);
77 #endif
78
79 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
80 this, SLOT(updateActiveView(const QModelIndex&)));
81 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
82 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
83
84 QVBoxLayout* layout = new QVBoxLayout(this);
85 layout->setMargin(0);
86 layout->addWidget(m_treeView);
87 }
88
89 TreeViewSidebarPage::~TreeViewSidebarPage()
90 {
91 delete m_dirLister;
92 m_dirLister = 0;
93 }
94
95 void TreeViewSidebarPage::activeViewChanged()
96 {
97 connectToActiveView();
98 }
99
100 void TreeViewSidebarPage::showEvent(QShowEvent* event)
101 {
102 SidebarPage::showEvent(event);
103 connectToActiveView();
104 }
105
106 void TreeViewSidebarPage::updateSelection(const KUrl& url)
107 {
108 if (!url.isValid() || (url == m_selectedUrl)) {
109 return;
110 }
111
112 m_selectedUrl = url;
113
114 // adjust the root of the tree to the base bookmark
115 const KUrl baseUrl = BookmarkSelector::baseBookmark(url).url();
116 if (m_dirLister->url() != baseUrl) {
117 m_dirLister->stop();
118 m_dirLister->openUrl(baseUrl);
119 }
120
121 // select the folder which contains the given URL
122 QItemSelectionModel* selModel = m_treeView->selectionModel();
123 selModel->clearSelection();
124
125 const KFileItem item(S_IFDIR, KFileItem::Unknown, url);
126
127 const QModelIndex index = m_dirModel->indexForItem(item);
128 if (index.isValid()) {
129 #if defined(USE_PROXY_MODEL)
130 // the item with the given URL is already part of the model
131 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
132 m_treeView->scrollTo(proxyIndex);
133 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
134 #else
135 m_treeView->scrollTo(index);
136 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
137 #endif
138 }
139 else {
140 // The item with the given URL is not loaded by the model yet. Iterate
141 // backward to the base URL and trigger the loading of the items for
142 // each hierarchy level.
143 connect(m_dirLister, SIGNAL(completed()),
144 this, SLOT(expandSelectionParent()));
145
146 KUrl parentUrl = url.upUrl();
147 while (!parentUrl.isParentOf(baseUrl)) {
148 m_dirLister->openUrl(parentUrl, true, false);
149 parentUrl = parentUrl.upUrl();
150 }
151 }
152 }
153
154 void TreeViewSidebarPage::expandSelectionParent()
155 {
156 disconnect(m_dirLister, SIGNAL(completed()),
157 this, SLOT(expandSelectionParent()));
158
159 // expand the parent folder of the selected item
160 const KFileItem parentItem(S_IFDIR, KFileItem::Unknown, m_selectedUrl.upUrl());
161 QModelIndex index = m_dirModel->indexForItem(parentItem);
162 if (index.isValid()) {
163 #if defined(USE_PROXY_MODEL)
164 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
165 m_treeView->setExpanded(proxyIndex, true);
166 #else
167 m_treeView->setExpanded(index, true);
168 #endif
169
170 // select the item and assure that the item is visible
171 const KFileItem selectedItem(S_IFDIR, KFileItem::Unknown, m_selectedUrl);
172 index = m_dirModel->indexForItem(selectedItem);
173 if (index.isValid()) {
174 #if defined(USE_PROXY_MODEL)
175 proxyIndex = m_proxyModel->mapFromSource(index);
176 m_treeView->scrollTo(proxyIndex);
177
178 QItemSelectionModel* selModel = m_treeView->selectionModel();
179 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
180 #else
181 m_treeView->scrollTo(index);
182
183 QItemSelectionModel* selModel = m_treeView->selectionModel();
184 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
185 #endif
186
187 }
188 }
189 }
190
191 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
192 {
193 #if defined(USE_PROXY_MODEL)
194 const QModelIndex& dirIndex = m_proxyModel->mapToSource(index);
195 const KFileItem* item = m_dirModel->itemForIndex(dirIndex);
196 #else
197 const KFileItem* item = m_dirModel->itemForIndex(index);
198 #endif
199 if (item != 0) {
200 const KUrl& url = item->url();
201 mainWindow()->activeView()->setUrl(url);
202 }
203 }
204
205 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
206 const QModelIndex& index)
207 {
208 if (index.isValid()) {
209 #if defined(USE_PROXY_MODEL)
210 const QModelIndex& dirIndex = m_proxyModel->mapToSource(index);
211 KFileItem* item = m_dirModel->itemForIndex(dirIndex);
212 #else
213 KFileItem* item = m_dirModel->itemForIndex(index);
214 #endif
215 Q_ASSERT(item != 0);
216 if (item->isDir()) {
217 mainWindow()->dropUrls(urls, item->url());
218 }
219 }
220 }
221
222 void TreeViewSidebarPage::connectToActiveView()
223 {
224 const QWidget* parent = parentWidget();
225 if ((parent == 0) || parent->isHidden()) {
226 return;
227 }
228
229 const DolphinView* view = mainWindow()->activeView();
230 const KUrl& url = view->url();
231
232 connect(view, SIGNAL(urlChanged(const KUrl&)),
233 this, SLOT(updateSelection(const KUrl&)));
234
235 updateSelection(url);
236 }
237
238 #include "treeviewsidebarpage.moc"