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