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