]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
allow Konqueror to open also files inside a new tab, not only directories
[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 "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_folderspanelsettings.h"
27 #include "sidebartreeview.h"
28 #include "treeviewcontextmenu.h"
29
30 #include <kfileplacesmodel.h>
31 #include <kdirlister.h>
32 #include <kfileitem.h>
33
34 #include <QItemSelection>
35 #include <QTreeView>
36 #include <QBoxLayout>
37 #include <QModelIndex>
38 #include <QScrollBar>
39 #include <QTimer>
40
41 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
42 SidebarPage(parent),
43 m_setLeafVisible(false),
44 m_horizontalPos(0),
45 m_dirLister(0),
46 m_dolphinModel(0),
47 m_proxyModel(0),
48 m_treeView(0),
49 m_leafDir()
50 {
51 }
52
53 TreeViewSidebarPage::~TreeViewSidebarPage()
54 {
55 FoldersPanelSettings::self()->writeConfig();
56
57 delete m_proxyModel;
58 m_proxyModel = 0;
59 delete m_dolphinModel;
60 m_dolphinModel = 0;
61 m_dirLister = 0; // deleted by m_dolphinModel
62 }
63
64 QSize TreeViewSidebarPage::sizeHint() const
65 {
66 return QSize(200, 400);
67 }
68
69 void TreeViewSidebarPage::setShowHiddenFiles(bool show)
70 {
71 FoldersPanelSettings::setShowHiddenFiles(show);
72 if (m_dirLister != 0) {
73 m_dirLister->setShowingDotFiles(show);
74 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
75 }
76 }
77
78 bool TreeViewSidebarPage::showHiddenFiles() const
79 {
80 return FoldersPanelSettings::showHiddenFiles();
81 }
82
83 void TreeViewSidebarPage::setUrl(const KUrl& url)
84 {
85 if (!url.isValid() || (url == SidebarPage::url())) {
86 return;
87 }
88
89 SidebarPage::setUrl(url);
90 if (m_dirLister != 0) {
91 m_setLeafVisible = true;
92 loadTree(url);
93 }
94 }
95
96 void TreeViewSidebarPage::showEvent(QShowEvent* event)
97 {
98 if (event->spontaneous()) {
99 SidebarPage::showEvent(event);
100 return;
101 }
102
103 if (m_dirLister == 0) {
104 // Postpone the creating of the dir lister to the first show event.
105 // This assures that no performance and memory overhead is given when the TreeView is not
106 // used at all (see TreeViewSidebarPage::setUrl()).
107 m_dirLister = new KDirLister();
108 m_dirLister->setDirOnlyMode(true);
109 m_dirLister->setAutoUpdate(true);
110 m_dirLister->setMainWindow(window());
111 m_dirLister->setDelayedMimeTypes(true);
112 m_dirLister->setAutoErrorHandlingEnabled(false, this);
113 m_dirLister->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
114
115 connect(m_dirLister, SIGNAL(completed()),
116 this, SLOT(triggerLoadSubTree()));
117
118 Q_ASSERT(m_dolphinModel == 0);
119 m_dolphinModel = new DolphinModel(this);
120 m_dolphinModel->setDirLister(m_dirLister);
121 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
122 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
123 this, SLOT(triggerExpanding()));
124
125 Q_ASSERT(m_proxyModel == 0);
126 m_proxyModel = new DolphinSortFilterProxyModel(this);
127 m_proxyModel->setSourceModel(m_dolphinModel);
128
129 Q_ASSERT(m_treeView == 0);
130 m_treeView = new SidebarTreeView(this);
131 m_treeView->setModel(m_proxyModel);
132 m_proxyModel->setSorting(DolphinView::SortByName);
133 m_proxyModel->setSortOrder(Qt::AscendingOrder);
134
135 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
136 this, SLOT(updateActiveView(const QModelIndex&)));
137 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
138 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
139
140 QVBoxLayout* layout = new QVBoxLayout(this);
141 layout->setMargin(0);
142 layout->addWidget(m_treeView);
143 }
144
145 loadTree(url());
146 SidebarPage::showEvent(event);
147 }
148
149 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
150 {
151 SidebarPage::contextMenuEvent(event);
152
153 KFileItem item;
154 const QModelIndex index = m_treeView->indexAt(event->pos());
155 if (index.isValid()) {
156 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
157 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
158 emit changeSelection(KFileItemList());
159 }
160
161 TreeViewContextMenu contextMenu(this, item);
162 contextMenu.open();
163 }
164
165 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
166 {
167 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
168 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
169 if (!item.isNull()) {
170 emit changeUrl(item.url());
171 }
172 }
173
174 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
175 const QModelIndex& index)
176 {
177 if (index.isValid()) {
178 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
179 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
180 Q_ASSERT(!item.isNull());
181 if (item.isDir()) {
182 emit urlsDropped(urls, item.url());
183 }
184 }
185 }
186
187 void TreeViewSidebarPage::triggerExpanding()
188 {
189 // the expanding of the folders may not be done in the context
190 // of this slot
191 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection);
192 }
193
194 void TreeViewSidebarPage::triggerLoadSubTree()
195 {
196 // the loading of the sub tree may not be done in the context
197 // of this slot
198 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection);
199 }
200
201 void TreeViewSidebarPage::expandToLeafDir()
202 {
203 // expand all directories until the parent directory of m_leafDir
204 const KUrl parentUrl = m_leafDir.upUrl();
205 QModelIndex dirIndex = m_dolphinModel->indexForUrl(parentUrl);
206 QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
207 m_treeView->setExpanded(proxyIndex, true);
208 selectLeafDirectory();
209 }
210
211
212 void TreeViewSidebarPage::loadSubTree()
213 {
214 m_treeView->selectionModel()->clearSelection();
215
216 if (m_leafDir.isParentOf(m_dirLister->url())) {
217 // The leaf directory is not a child of the base URL, hence
218 // no sub directory must be loaded or selected.
219 return;
220 }
221
222 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
223 if (index.isValid()) {
224 selectLeafDirectory();
225 } else {
226 // Load all sub directories that need to get expanded for making
227 // the leaf directory visible. The slot triggerExpanding() will
228 // get invoked if the expanding has been finished.
229 m_dolphinModel->expandToUrl(m_leafDir);
230 }
231 }
232
233 void TreeViewSidebarPage::scrollToLeaf()
234 {
235 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
236 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
237 if (proxyIndex.isValid()) {
238 m_treeView->scrollTo(proxyIndex);
239 }
240 }
241
242 void TreeViewSidebarPage::loadTree(const KUrl& url)
243 {
244 Q_ASSERT(m_dirLister != 0);
245 m_leafDir = url;
246
247 m_horizontalPos = m_treeView->horizontalScrollBar()->value();
248
249 KUrl baseUrl = url;
250 if (url.isLocalFile()) {
251 // use the root directory as base for local URLs
252 baseUrl = QDir::rootPath();
253 } else {
254 // clear the path for non-local URLs and use it as base
255 baseUrl = url;
256 baseUrl.setPath(QString());
257 }
258
259 if (m_dirLister->url() != baseUrl) {
260 m_dirLister->stop();
261 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
262 } else {
263 loadSubTree();
264 }
265 }
266
267 void TreeViewSidebarPage::selectLeafDirectory()
268 {
269 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
270 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
271 if (!proxyIndex.isValid()) {
272 return;
273 }
274
275 if (m_setLeafVisible) {
276 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
277 // scrollToLeaf(). This assures that the scrolling is done after
278 // the horizontal scrollbar gets visible (otherwise the scrollbar
279 // might hide the leaf).
280 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
281 m_setLeafVisible = false;
282 }
283
284 QItemSelectionModel* selModel = m_treeView->selectionModel();
285 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
286
287 m_treeView->horizontalScrollBar()->setValue(m_horizontalPos);
288 }
289
290 #include "treeviewsidebarpage.moc"