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