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