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