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