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