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