]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
* Fixed execution of links in the metadata widget.
[dolphin.git] / src / panels / folders / folderspanel.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 "folderspanel.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "settings/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 "paneltreeview.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 FoldersPanel::FoldersPanel(QWidget* parent) :
48 Panel(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 FoldersPanel::~FoldersPanel()
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 FoldersPanel::sizeHint() const
72 {
73 return QSize(200, 400);
74 }
75
76 void FoldersPanel::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 FoldersPanel::showHiddenFiles() const
86 {
87 return FoldersPanelSettings::showHiddenFiles();
88 }
89
90 void FoldersPanel::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 QPointer<RenameDialog> dialog = new RenameDialog(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 delete dialog;
109 }
110 }
111
112 void FoldersPanel::setUrl(const KUrl& url)
113 {
114 if (!url.isValid() || (url == Panel::url())) {
115 return;
116 }
117
118 Panel::setUrl(url);
119 if (m_dirLister != 0) {
120 m_setLeafVisible = true;
121 loadTree(url);
122 }
123 }
124
125 void FoldersPanel::showEvent(QShowEvent* event)
126 {
127 if (event->spontaneous()) {
128 Panel::showEvent(event);
129 return;
130 }
131
132 if (m_dirLister == 0) {
133 // Postpone the creating of the dir lister to the first show event.
134 // This assures that no performance and memory overhead is given when the TreeView is not
135 // used at all (see FoldersPanel::setUrl()).
136 m_dirLister = new KDirLister();
137 m_dirLister->setDirOnlyMode(true);
138 m_dirLister->setAutoUpdate(true);
139 m_dirLister->setMainWindow(window());
140 m_dirLister->setDelayedMimeTypes(true);
141 m_dirLister->setAutoErrorHandlingEnabled(false, this);
142 m_dirLister->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
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(expandToDir(const QModelIndex&)));
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 PanelTreeView(this);
157 m_treeView->setModel(m_proxyModel);
158 m_proxyModel->setSorting(DolphinView::SortByName);
159 m_proxyModel->setSortOrder(Qt::AscendingOrder);
160
161 new FolderExpander(m_treeView, m_proxyModel);
162
163 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
164 this, SLOT(updateActiveView(const QModelIndex&)));
165 connect(m_treeView, SIGNAL(urlsDropped(const QModelIndex&, QDropEvent*)),
166 this, SLOT(dropUrls(const QModelIndex&, QDropEvent*)));
167 connect(m_treeView, SIGNAL(pressed(const QModelIndex&)),
168 this, SLOT(updateMouseButtons()));
169
170 QVBoxLayout* layout = new QVBoxLayout(this);
171 layout->setMargin(0);
172 layout->addWidget(m_treeView);
173 }
174
175 loadTree(url());
176 Panel::showEvent(event);
177 }
178
179 void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
180 {
181 Panel::contextMenuEvent(event);
182
183 KFileItem item;
184 const QModelIndex index = m_treeView->indexAt(event->pos());
185 if (index.isValid()) {
186 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
187 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
188 emit changeSelection(KFileItemList());
189 }
190
191 TreeViewContextMenu contextMenu(this, item);
192 contextMenu.open();
193 }
194
195 void FoldersPanel::keyPressEvent(QKeyEvent* event)
196 {
197 const int key = event->key();
198 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
199 event->accept();
200 updateActiveView(m_treeView->currentIndex());
201 } else {
202 Panel::keyPressEvent(event);
203 }
204 }
205
206 void FoldersPanel::updateActiveView(const QModelIndex& index)
207 {
208 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
209 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
210 if (!item.isNull()) {
211 emit changeUrl(item.url(), m_mouseButtons);
212 }
213 }
214
215 void FoldersPanel::dropUrls(const QModelIndex& index, QDropEvent* event)
216 {
217 if (index.isValid()) {
218 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
219 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
220 Q_ASSERT(!item.isNull());
221 if (item.isDir()) {
222 DragAndDropHelper::instance().dropUrls(item, item.url(), event, this);
223 }
224 }
225 }
226
227 void FoldersPanel::expandToDir(const QModelIndex& index)
228 {
229 m_treeView->setExpanded(index, true);
230 selectLeafDirectory();
231 m_treeView->resizeColumnToContents(DolphinModel::Name);
232 }
233
234 void FoldersPanel::scrollToLeaf()
235 {
236 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
237 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
238 if (proxyIndex.isValid()) {
239 m_treeView->scrollTo(proxyIndex);
240 }
241 }
242
243 void FoldersPanel::updateMouseButtons()
244 {
245 m_mouseButtons = QApplication::mouseButtons();
246 }
247
248 void FoldersPanel::loadTree(const KUrl& url)
249 {
250 Q_ASSERT(m_dirLister != 0);
251 m_leafDir = url;
252
253 KUrl baseUrl;
254 if (url.isLocalFile()) {
255 // use the root directory as base for local URLs (#150941)
256 baseUrl = QDir::rootPath();
257 } else {
258 // clear the path for non-local URLs and use it as base
259 baseUrl = url;
260 baseUrl.setPath(QString('/'));
261 }
262
263 if (m_dirLister->url() != baseUrl) {
264 m_dirLister->stop();
265 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
266 }
267 m_dolphinModel->expandToUrl(m_leafDir);
268 }
269
270 void FoldersPanel::selectLeafDirectory()
271 {
272 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
273 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
274 if (!proxyIndex.isValid()) {
275 return;
276 }
277
278 if (m_setLeafVisible) {
279 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
280 // scrollToLeaf(). This assures that the scrolling is done after
281 // the horizontal scrollbar gets visible (otherwise the scrollbar
282 // might hide the leaf).
283 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
284 m_setLeafVisible = false;
285 }
286
287 QItemSelectionModel* selModel = m_treeView->selectionModel();
288 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
289 }
290
291 #include "folderspanel.moc"