]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
Fixed some drag & drop issues:
[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->addWidget(m_treeView);
86 }
87
88 TreeViewSidebarPage::~TreeViewSidebarPage()
89 {
90 delete m_dirLister;
91 m_dirLister = 0;
92 }
93
94 void TreeViewSidebarPage::activeViewChanged()
95 {
96 connectToActiveView();
97 }
98
99 void TreeViewSidebarPage::showEvent(QShowEvent* event)
100 {
101 SidebarPage::showEvent(event);
102 connectToActiveView();
103 }
104
105 void TreeViewSidebarPage::updateSelection(const KUrl& url)
106 {
107 if (!url.isValid() || (url == m_selectedUrl)) {
108 return;
109 }
110
111 m_selectedUrl = url;
112
113 // adjust the root of the tree to the base bookmark
114 const KUrl baseUrl = BookmarkSelector::baseBookmark(url).url();
115 if (m_dirLister->url() != baseUrl) {
116 m_dirLister->stop();
117 m_dirLister->openUrl(baseUrl);
118 }
119
120 // select the folder which contains the given URL
121 QItemSelectionModel* selModel = m_treeView->selectionModel();
122 selModel->clearSelection();
123
124 const KFileItem item(S_IFDIR, KFileItem::Unknown, url);
125
126 const QModelIndex index = m_dirModel->indexForItem(item);
127 if (index.isValid()) {
128 #if defined(USE_PROXY_MODEL)
129 // the item with the given URL is already part of the model
130 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
131 m_treeView->scrollTo(proxyIndex);
132 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
133 #else
134 m_treeView->scrollTo(index);
135 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
136 #endif
137 }
138 else {
139 // The item with the given URL is not loaded by the model yet. Iterate
140 // backward to the base URL and trigger the loading of the items for
141 // each hierarchy level.
142 connect(m_dirLister, SIGNAL(completed()),
143 this, SLOT(expandSelectionParent()));
144
145 KUrl parentUrl = url.upUrl();
146 while (!parentUrl.isParentOf(baseUrl)) {
147 m_dirLister->openUrl(parentUrl, true, false);
148 parentUrl = parentUrl.upUrl();
149 }
150 }
151 }
152
153 void TreeViewSidebarPage::expandSelectionParent()
154 {
155 disconnect(m_dirLister, SIGNAL(completed()),
156 this, SLOT(expandSelectionParent()));
157
158 // expand the parent folder of the selected item
159 const KFileItem parentItem(S_IFDIR, KFileItem::Unknown, m_selectedUrl.upUrl());
160 QModelIndex index = m_dirModel->indexForItem(parentItem);
161 if (index.isValid()) {
162 #if defined(USE_PROXY_MODEL)
163 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
164 m_treeView->setExpanded(proxyIndex, true);
165 #else
166 m_treeView->setExpanded(index, true);
167 #endif
168
169 // select the item and assure that the item is visible
170 const KFileItem selectedItem(S_IFDIR, KFileItem::Unknown, m_selectedUrl);
171 index = m_dirModel->indexForItem(selectedItem);
172 if (index.isValid()) {
173 #if defined(USE_PROXY_MODEL)
174 proxyIndex = m_proxyModel->mapFromSource(index);
175 m_treeView->scrollTo(proxyIndex);
176
177 QItemSelectionModel* selModel = m_treeView->selectionModel();
178 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
179 #else
180 m_treeView->scrollTo(index);
181
182 QItemSelectionModel* selModel = m_treeView->selectionModel();
183 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
184 #endif
185
186 }
187 }
188 }
189
190 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
191 {
192 #if defined(USE_PROXY_MODEL)
193 const QModelIndex& dirIndex = m_proxyModel->mapToSource(index);
194 const KFileItem* item = m_dirModel->itemForIndex(dirIndex);
195 #else
196 const KFileItem* item = m_dirModel->itemForIndex(index);
197 #endif
198 if (item != 0) {
199 const KUrl& url = item->url();
200 mainWindow()->activeView()->setUrl(url);
201 }
202 }
203
204 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
205 const QModelIndex& index)
206 {
207 if (index.isValid()) {
208 #if defined(USE_PROXY_MODEL)
209 const QModelIndex& dirIndex = m_proxyModel->mapToSource(index);
210 KFileItem* item = m_dirModel->itemForIndex(dirIndex);
211 #else
212 KFileItem* item = m_dirModel->itemForIndex(index);
213 #endif
214 Q_ASSERT(item != 0);
215 if (item->isDir()) {
216 mainWindow()->dropUrls(urls, item->url());
217 }
218 }
219 }
220
221 void TreeViewSidebarPage::connectToActiveView()
222 {
223 const QWidget* parent = parentWidget();
224 if ((parent == 0) || parent->isHidden()) {
225 return;
226 }
227
228 const DolphinView* view = mainWindow()->activeView();
229 const KUrl& url = view->url();
230
231 connect(view, SIGNAL(urlChanged(const KUrl&)),
232 this, SLOT(updateSelection(const KUrl&)));
233
234 updateSelection(url);
235 }
236
237 #include "treeviewsidebarpage.moc"