]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
32779ffce8eb6ffd4a6b23c9d57f467149bc943b
[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 "draganddrophelper.h"
29 #include "folderexpander.h"
30 #include "renamedialog.h"
31 #include "sidebartreeview.h"
32 #include "treeviewcontextmenu.h"
33
34 #include <kfileplacesmodel.h>
35 #include <kdirlister.h>
36 #include <kfileitem.h>
37 #include <konq_operations.h>
38
39 #include <QApplication>
40 #include <QItemSelection>
41 #include <QTreeView>
42 #include <QBoxLayout>
43 #include <QModelIndex>
44 #include <QScrollBar>
45 #include <QTimer>
46
47 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
48 SidebarPage(parent),
49 m_setLeafVisible(false),
50 m_mouseButtons(Qt::NoButton),
51 m_dirLister(0),
52 m_dolphinModel(0),
53 m_proxyModel(0),
54 m_treeView(0),
55 m_leafDir()
56 {
57 setLayoutDirection(Qt::LeftToRight);
58 }
59
60 TreeViewSidebarPage::~TreeViewSidebarPage()
61 {
62 FoldersPanelSettings::self()->writeConfig();
63
64 delete m_proxyModel;
65 m_proxyModel = 0;
66 delete m_dolphinModel;
67 m_dolphinModel = 0;
68 m_dirLister = 0; // deleted by m_dolphinModel
69 }
70
71 QSize TreeViewSidebarPage::sizeHint() const
72 {
73 return QSize(200, 400);
74 }
75
76 void TreeViewSidebarPage::setShowHiddenFiles(bool show)
77 {
78 FoldersPanelSettings::setShowHiddenFiles(show);
79 if (m_dirLister != 0) {
80 m_dirLister->setShowingDotFiles(show);
81 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
82 }
83 }
84
85 bool TreeViewSidebarPage::showHiddenFiles() const
86 {
87 return FoldersPanelSettings::showHiddenFiles();
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 Q_ASSERT(m_dolphinModel == 0);
144 m_dolphinModel = new DolphinModel(this);
145 m_dolphinModel->setDirLister(m_dirLister);
146 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
147 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
148 this, SLOT(expandToDir(const QModelIndex&)));
149
150 Q_ASSERT(m_proxyModel == 0);
151 m_proxyModel = new DolphinSortFilterProxyModel(this);
152 m_proxyModel->setSourceModel(m_dolphinModel);
153
154 Q_ASSERT(m_treeView == 0);
155 m_treeView = new SidebarTreeView(this);
156 m_treeView->setModel(m_proxyModel);
157 m_proxyModel->setSorting(DolphinView::SortByName);
158 m_proxyModel->setSortOrder(Qt::AscendingOrder);
159
160 new FolderExpander(m_treeView, m_proxyModel);
161
162 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
163 this, SLOT(updateActiveView(const QModelIndex&)));
164 connect(m_treeView, SIGNAL(urlsDropped(const QModelIndex&, QDropEvent*)),
165 this, SLOT(dropUrls(const QModelIndex&, QDropEvent*)));
166 connect(m_treeView, SIGNAL(pressed(const QModelIndex&)),
167 this, SLOT(updateMouseButtons()));
168
169 QVBoxLayout* layout = new QVBoxLayout(this);
170 layout->setMargin(0);
171 layout->addWidget(m_treeView);
172 }
173
174 loadTree(url());
175 SidebarPage::showEvent(event);
176 }
177
178 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
179 {
180 SidebarPage::contextMenuEvent(event);
181
182 KFileItem item;
183 const QModelIndex index = m_treeView->indexAt(event->pos());
184 if (index.isValid()) {
185 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
186 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
187 emit changeSelection(KFileItemList());
188 }
189
190 TreeViewContextMenu contextMenu(this, item);
191 contextMenu.open();
192 }
193
194 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
195 {
196 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
197 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
198 if (!item.isNull()) {
199 emit changeUrl(item.url(), m_mouseButtons);
200 }
201 }
202
203 void TreeViewSidebarPage::dropUrls(const QModelIndex& index, QDropEvent* event)
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 DragAndDropHelper::dropUrls(item, item.url(), event, this);
211 }
212 }
213 }
214
215 void TreeViewSidebarPage::expandToDir(const QModelIndex& index)
216 {
217 m_treeView->setExpanded(index, true);
218 selectLeafDirectory();
219 }
220
221 void TreeViewSidebarPage::scrollToLeaf()
222 {
223 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
224 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
225 if (proxyIndex.isValid()) {
226 m_treeView->scrollTo(proxyIndex);
227 }
228 }
229
230 void TreeViewSidebarPage::updateMouseButtons()
231 {
232 m_mouseButtons = QApplication::mouseButtons();
233 }
234
235 void TreeViewSidebarPage::loadTree(const KUrl& url)
236 {
237 Q_ASSERT(m_dirLister != 0);
238 m_leafDir = url;
239
240 KUrl baseUrl = url;
241 if (url.isLocalFile()) {
242 // use the root directory as base for local URLs
243 baseUrl = QDir::rootPath();
244 } else {
245 // clear the path for non-local URLs and use it as base
246 baseUrl = url;
247 baseUrl.setPath(QString());
248 }
249
250 if (m_dirLister->url() != baseUrl) {
251 m_dirLister->stop();
252 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
253 }
254 m_dolphinModel->expandToUrl(m_leafDir);
255 }
256
257 void TreeViewSidebarPage::selectLeafDirectory()
258 {
259 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
260 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
261 if (!proxyIndex.isValid()) {
262 return;
263 }
264
265 if (m_setLeafVisible) {
266 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
267 // scrollToLeaf(). This assures that the scrolling is done after
268 // the horizontal scrollbar gets visible (otherwise the scrollbar
269 // might hide the leaf).
270 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
271 m_setLeafVisible = false;
272 }
273
274 QItemSelectionModel* selModel = m_treeView->selectionModel();
275 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
276 }
277
278 #include "treeviewsidebarpage.moc"