]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
Let's keep it simple: only offer a basic context menu for the treeview sidebar.
[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 const QModelIndex index = m_treeView->indexAt(event->pos());
112 if (!index.isValid()) {
113 // only open a context menu above a directory item
114 return;
115 }
116
117 #if defined(USE_PROXY_MODEL)
118 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
119 KFileItem* item = m_dirModel->itemForIndex(dirModelIndex);
120 #else
121 KFileItem* item = m_dirModel->itemForIndex(index);
122 #endif
123
124 DolphinContextMenu contextMenu(mainWindow(),
125 item,
126 m_dirLister->url(),
127 DolphinContextMenu::SidebarView);
128 contextMenu.open();
129 }
130
131 void TreeViewSidebarPage::updateSelection(const KUrl& url)
132 {
133 if (!url.isValid() || (url == m_selectedUrl)) {
134 return;
135 }
136
137 m_selectedUrl = url;
138
139 // adjust the root of the tree to the base bookmark
140 const KUrl baseUrl = BookmarkSelector::baseBookmark(url).url();
141 if (m_dirLister->url() != baseUrl) {
142 m_dirLister->stop();
143 m_dirLister->openUrl(baseUrl);
144 }
145
146 // select the folder which contains the given URL
147 QItemSelectionModel* selModel = m_treeView->selectionModel();
148 selModel->clearSelection();
149
150 const KFileItem item(S_IFDIR, KFileItem::Unknown, url);
151
152 const QModelIndex index = m_dirModel->indexForItem(item);
153 if (index.isValid()) {
154 #if defined(USE_PROXY_MODEL)
155 // the item with the given URL is already part of the model
156 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
157 m_treeView->scrollTo(proxyIndex);
158 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
159 #else
160 m_treeView->scrollTo(index);
161 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
162 #endif
163 }
164 else {
165 // The item with the given URL is not loaded by the model yet. Iterate
166 // backward to the base URL and trigger the loading of the items for
167 // each hierarchy level.
168 connect(m_dirLister, SIGNAL(completed()),
169 this, SLOT(expandSelectionParent()));
170
171 KUrl parentUrl = url.upUrl();
172 while (!parentUrl.isParentOf(baseUrl)) {
173 m_dirLister->openUrl(parentUrl, true, false);
174 parentUrl = parentUrl.upUrl();
175 }
176 }
177 }
178
179 void TreeViewSidebarPage::expandSelectionParent()
180 {
181 disconnect(m_dirLister, SIGNAL(completed()),
182 this, SLOT(expandSelectionParent()));
183
184 // expand the parent folder of the selected item
185 const KFileItem parentItem(S_IFDIR, KFileItem::Unknown, m_selectedUrl.upUrl());
186 QModelIndex index = m_dirModel->indexForItem(parentItem);
187 if (index.isValid()) {
188 #if defined(USE_PROXY_MODEL)
189 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
190 m_treeView->setExpanded(proxyIndex, true);
191 #else
192 m_treeView->setExpanded(index, true);
193 #endif
194
195 // select the item and assure that the item is visible
196 const KFileItem selectedItem(S_IFDIR, KFileItem::Unknown, m_selectedUrl);
197 index = m_dirModel->indexForItem(selectedItem);
198 if (index.isValid()) {
199 #if defined(USE_PROXY_MODEL)
200 proxyIndex = m_proxyModel->mapFromSource(index);
201 m_treeView->scrollTo(proxyIndex);
202
203 QItemSelectionModel* selModel = m_treeView->selectionModel();
204 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
205 #else
206 m_treeView->scrollTo(index);
207
208 QItemSelectionModel* selModel = m_treeView->selectionModel();
209 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
210 #endif
211
212 }
213 }
214 }
215
216 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
217 {
218 #if defined(USE_PROXY_MODEL)
219 const QModelIndex& dirIndex = m_proxyModel->mapToSource(index);
220 const KFileItem* item = m_dirModel->itemForIndex(dirIndex);
221 #else
222 const KFileItem* item = m_dirModel->itemForIndex(index);
223 #endif
224 if (item != 0) {
225 const KUrl& url = item->url();
226 mainWindow()->activeView()->setUrl(url);
227 }
228 }
229
230 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
231 const QModelIndex& index)
232 {
233 if (index.isValid()) {
234 #if defined(USE_PROXY_MODEL)
235 const QModelIndex& dirIndex = m_proxyModel->mapToSource(index);
236 KFileItem* item = m_dirModel->itemForIndex(dirIndex);
237 #else
238 KFileItem* item = m_dirModel->itemForIndex(index);
239 #endif
240 Q_ASSERT(item != 0);
241 if (item->isDir()) {
242 mainWindow()->dropUrls(urls, item->url());
243 }
244 }
245 }
246
247 void TreeViewSidebarPage::connectToActiveView()
248 {
249 const QWidget* parent = parentWidget();
250 if ((parent == 0) || parent->isHidden()) {
251 return;
252 }
253
254 const DolphinView* view = mainWindow()->activeView();
255 const KUrl& url = view->url();
256
257 connect(view, SIGNAL(urlChanged(const KUrl&)),
258 this, SLOT(updateSelection(const KUrl&)));
259
260 updateSelection(url);
261 }
262
263 #include "treeviewsidebarpage.moc"