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