]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
port Dolphin from KUrl to QUrl
[dolphin.git] / src / panels / folders / folderspanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphin_folderspanelsettings.h"
23 #include "dolphin_generalsettings.h"
24 #include "treeviewcontextmenu.h"
25 #include "foldersitemlistwidget.h"
26
27 #include <views/renamedialog.h>
28 #include <kitemviews/kitemlistselectionmanager.h>
29 #include <kitemviews/kfileitemlistview.h>
30 #include <kitemviews/kfileitemlistwidget.h>
31 #include <kitemviews/kitemlistcontainer.h>
32 #include <kitemviews/kitemlistcontroller.h>
33 #include <kitemviews/kfileitemmodel.h>
34
35 #include <KFileItem>
36 #include <KJobWidgets>
37 #include <KJobUiDelegate>
38 #include <KIO/CopyJob>
39 #include <KIO/FileUndoManager>
40
41 #include <QApplication>
42 #include <QBoxLayout>
43 #include <QDropEvent>
44 #include <QGraphicsSceneDragDropEvent>
45 #include <QGraphicsView>
46 #include <QPropertyAnimation>
47 #include <QTimer>
48
49 #include <views/draganddrophelper.h>
50
51 #include <KDebug>
52
53 FoldersPanel::FoldersPanel(QWidget* parent) :
54 Panel(parent),
55 m_updateCurrentItem(false),
56 m_controller(0),
57 m_model(0)
58 {
59 setLayoutDirection(Qt::LeftToRight);
60 }
61
62 FoldersPanel::~FoldersPanel()
63 {
64 FoldersPanelSettings::self()->save();
65
66 if (m_controller) {
67 KItemListView* view = m_controller->view();
68 m_controller->setView(0);
69 delete view;
70 }
71 }
72
73 void FoldersPanel::setShowHiddenFiles(bool show)
74 {
75 FoldersPanelSettings::setHiddenFilesShown(show);
76 m_model->setShowHiddenFiles(show);
77 }
78
79 bool FoldersPanel::showHiddenFiles() const
80 {
81 return FoldersPanelSettings::hiddenFilesShown();
82 }
83
84 void FoldersPanel::setAutoScrolling(bool enable)
85 {
86 // TODO: Not supported yet in Dolphin 2.0
87 FoldersPanelSettings::setAutoScrolling(enable);
88 }
89
90 bool FoldersPanel::autoScrolling() const
91 {
92 return FoldersPanelSettings::autoScrolling();
93 }
94
95 void FoldersPanel::rename(const KFileItem& item)
96 {
97 if (GeneralSettings::renameInline()) {
98 const int index = m_model->index(item);
99 m_controller->view()->editRole(index, "text");
100 } else {
101 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
102 dialog->setAttribute(Qt::WA_DeleteOnClose);
103 dialog->show();
104 dialog->raise();
105 dialog->activateWindow();
106 }
107 }
108
109 bool FoldersPanel::urlChanged()
110 {
111 if (!url().isValid() || url().scheme().contains("search")) {
112 // Skip results shown by a search, as possible identical
113 // directory names are useless without parent-path information.
114 return false;
115 }
116
117 if (m_controller) {
118 loadTree(url());
119 }
120
121 return true;
122 }
123
124 void FoldersPanel::showEvent(QShowEvent* event)
125 {
126 if (event->spontaneous()) {
127 Panel::showEvent(event);
128 return;
129 }
130
131 if (!m_controller) {
132 // Postpone the creating of the controller to the first show event.
133 // This assures that no performance and memory overhead is given when the folders panel is not
134 // used at all and stays invisible.
135 KFileItemListView* view = new KFileItemListView();
136 view->setWidgetCreator(new KItemListWidgetCreator<FoldersItemListWidget>());
137 view->setSupportsItemExpanding(true);
138 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
139 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
140 // opening the folders panel.
141 view->setOpacity(0);
142
143 connect(view, &KFileItemListView::roleEditingFinished,
144 this, &FoldersPanel::slotRoleEditingFinished);
145
146 m_model = new KFileItemModel(this);
147 m_model->setShowDirectoriesOnly(true);
148 m_model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
149 // Use a QueuedConnection to give the view the possibility to react first on the
150 // finished loading.
151 connect(m_model, &KFileItemModel::directoryLoadingCompleted, this, &FoldersPanel::slotLoadingCompleted, Qt::QueuedConnection);
152
153 m_controller = new KItemListController(m_model, view, this);
154 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
155 m_controller->setAutoActivationBehavior(KItemListController::ExpansionOnly);
156 m_controller->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem);
157 m_controller->setAutoActivationDelay(750);
158 m_controller->setSingleClickActivationEnforced(true);
159
160 connect(m_controller, &KItemListController::itemActivated, this, &FoldersPanel::slotItemActivated);
161 connect(m_controller, &KItemListController::itemMiddleClicked, this, &FoldersPanel::slotItemMiddleClicked);
162 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &FoldersPanel::slotItemContextMenuRequested);
163 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &FoldersPanel::slotViewContextMenuRequested);
164 connect(m_controller, &KItemListController::itemDropEvent, this, &FoldersPanel::slotItemDropEvent);
165
166 KItemListContainer* container = new KItemListContainer(m_controller, this);
167 container->setEnabledFrame(false);
168
169 QVBoxLayout* layout = new QVBoxLayout(this);
170 layout->setMargin(0);
171 layout->addWidget(container);
172 }
173
174 loadTree(url());
175 Panel::showEvent(event);
176 }
177
178 void FoldersPanel::keyPressEvent(QKeyEvent* event)
179 {
180 const int key = event->key();
181 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
182 event->accept();
183 } else {
184 Panel::keyPressEvent(event);
185 }
186 }
187
188 void FoldersPanel::slotItemActivated(int index)
189 {
190 const KFileItem item = m_model->fileItem(index);
191 if (!item.isNull()) {
192 emit folderActivated(item.url());
193 }
194 }
195
196 void FoldersPanel::slotItemMiddleClicked(int index)
197 {
198 const KFileItem item = m_model->fileItem(index);
199 if (!item.isNull()) {
200 emit folderMiddleClicked(item.url());
201 }
202 }
203
204 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
205 {
206 Q_UNUSED(pos);
207
208 const KFileItem fileItem = m_model->fileItem(index);
209
210 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
211 contextMenu.data()->open();
212 if (contextMenu.data()) {
213 delete contextMenu.data();
214 }
215 }
216
217 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
218 {
219 Q_UNUSED(pos);
220
221 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
222 contextMenu.data()->open();
223 if (contextMenu.data()) {
224 delete contextMenu.data();
225 }
226 }
227
228 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
229 {
230 if (index >= 0) {
231 KFileItem destItem = m_model->fileItem(index);
232 if (destItem.isNull()) {
233 return;
234 }
235
236 QDropEvent dropEvent(event->pos().toPoint(),
237 event->possibleActions(),
238 event->mimeData(),
239 event->buttons(),
240 event->modifiers());
241
242 QString error;
243 DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent, error);
244 if (!error.isEmpty()) {
245 emit errorMessage(error);
246 }
247 }
248 }
249
250 void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
251 {
252 if (role == "text") {
253 const KFileItem item = m_model->fileItem(index);
254 const QString newName = value.toString();
255 if (!newName.isEmpty() && newName != item.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
256 const QUrl oldUrl = item.url();
257 QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename);
258 newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName));
259
260 KIO::Job* job = KIO::moveAs(oldUrl, newUrl);
261 KJobWidgets::setWindow(job, this);
262 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, QList<QUrl>() << oldUrl, newUrl, job);
263 job->ui()->setAutoErrorHandlingEnabled(true);
264 }
265 }
266 }
267
268 void FoldersPanel::slotLoadingCompleted()
269 {
270 if (m_controller->view()->opacity() == 0) {
271 // The loading of the initial tree after opening the Folders panel
272 // has been finished. Trigger the increasing of the opacity after
273 // a short delay to give the view the chance to finish its internal
274 // animations.
275 // TODO: Check whether it makes sense to allow accessing the
276 // view-internal delay for usecases like this.
277 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
278 }
279
280 if (!m_updateCurrentItem) {
281 return;
282 }
283
284 const int index = m_model->index(url());
285 updateCurrentItem(index);
286 m_updateCurrentItem = false;
287 }
288
289 void FoldersPanel::startFadeInAnimation()
290 {
291 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
292 anim->setStartValue(0);
293 anim->setEndValue(1);
294 anim->setEasingCurve(QEasingCurve::InOutQuad);
295 anim->start(QAbstractAnimation::DeleteWhenStopped);
296 anim->setDuration(200);
297 }
298
299 void FoldersPanel::loadTree(const QUrl& url)
300 {
301 Q_ASSERT(m_controller);
302
303 m_updateCurrentItem = false;
304
305 QUrl baseUrl;
306 if (url.isLocalFile()) {
307 // Use the root directory as base for local URLs (#150941)
308 baseUrl = QDir::rootPath();
309 } else {
310 // Clear the path for non-local URLs and use it as base
311 baseUrl = url;
312 baseUrl.setPath(QString('/'));
313 }
314
315 if (m_model->directory() != baseUrl) {
316 m_updateCurrentItem = true;
317 m_model->refreshDirectory(baseUrl);
318 }
319
320 const int index = m_model->index(url);
321 if (index >= 0) {
322 updateCurrentItem(index);
323 } else {
324 m_updateCurrentItem = true;
325 m_model->expandParentDirectories(url);
326 // slotLoadingCompleted() will be invoked after the model has
327 // expanded the url
328 }
329 }
330
331 void FoldersPanel::updateCurrentItem(int index)
332 {
333 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
334 selectionManager->setCurrentItem(index);
335 selectionManager->clearSelection();
336 selectionManager->setSelected(index);
337
338 m_controller->view()->scrollToItem(index);
339 }
340