]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Remove unused #include
[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->setAttribute(Qt::WA_DeleteOnClose);
109 dialog->show();
110 dialog->raise();
111 dialog->activateWindow();
112 }
113 }
114
115 bool FoldersPanel::urlChanged()
116 {
117 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
118 // Skip results shown by a search, as possible identical
119 // directory names are useless without parent-path information.
120 return false;
121 }
122
123 if (m_controller) {
124 loadTree(url());
125 }
126
127 return true;
128 }
129
130 void FoldersPanel::reloadTree()
131 {
132 if (m_controller) {
133 loadTree(url(), AllowJumpHome);
134 }
135 }
136
137
138 void FoldersPanel::showEvent(QShowEvent* event)
139 {
140 if (event->spontaneous()) {
141 Panel::showEvent(event);
142 return;
143 }
144
145 if (!m_controller) {
146 // Postpone the creating of the controller to the first show event.
147 // This assures that no performance and memory overhead is given when the folders panel is not
148 // used at all and stays invisible.
149 KFileItemListView* view = new KFileItemListView();
150 view->setWidgetCreator(new KItemListWidgetCreator<FoldersItemListWidget>());
151 view->setSupportsItemExpanding(true);
152 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
153 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
154 // opening the folders panel.
155 view->setOpacity(0);
156
157 connect(view, &KFileItemListView::roleEditingFinished,
158 this, &FoldersPanel::slotRoleEditingFinished);
159
160 m_model = new KFileItemModel(this);
161 m_model->setShowDirectoriesOnly(true);
162 m_model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
163 // Use a QueuedConnection to give the view the possibility to react first on the
164 // finished loading.
165 connect(m_model, &KFileItemModel::directoryLoadingCompleted, this, &FoldersPanel::slotLoadingCompleted, Qt::QueuedConnection);
166
167 m_controller = new KItemListController(m_model, view, this);
168 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
169 m_controller->setAutoActivationBehavior(KItemListController::ExpansionOnly);
170 m_controller->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem);
171 m_controller->setAutoActivationDelay(750);
172 m_controller->setSingleClickActivationEnforced(true);
173
174 connect(m_controller, &KItemListController::itemActivated, this, &FoldersPanel::slotItemActivated);
175 connect(m_controller, &KItemListController::itemMiddleClicked, this, &FoldersPanel::slotItemMiddleClicked);
176 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &FoldersPanel::slotItemContextMenuRequested);
177 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &FoldersPanel::slotViewContextMenuRequested);
178 connect(m_controller, &KItemListController::itemDropEvent, this, &FoldersPanel::slotItemDropEvent);
179
180 KItemListContainer* container = new KItemListContainer(m_controller, this);
181 container->setEnabledFrame(false);
182
183 QVBoxLayout* layout = new QVBoxLayout(this);
184 layout->setMargin(0);
185 layout->addWidget(container);
186 }
187
188 loadTree(url());
189 Panel::showEvent(event);
190 }
191
192 void FoldersPanel::keyPressEvent(QKeyEvent* event)
193 {
194 const int key = event->key();
195 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
196 event->accept();
197 } else {
198 Panel::keyPressEvent(event);
199 }
200 }
201
202 void FoldersPanel::slotItemActivated(int index)
203 {
204 const KFileItem item = m_model->fileItem(index);
205 if (!item.isNull()) {
206 emit folderActivated(item.url());
207 }
208 }
209
210 void FoldersPanel::slotItemMiddleClicked(int index)
211 {
212 const KFileItem item = m_model->fileItem(index);
213 if (!item.isNull()) {
214 emit folderMiddleClicked(item.url());
215 }
216 }
217
218 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
219 {
220 Q_UNUSED(pos);
221
222 const KFileItem fileItem = m_model->fileItem(index);
223
224 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
225 contextMenu.data()->open();
226 if (contextMenu.data()) {
227 delete contextMenu.data();
228 }
229 }
230
231 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
232 {
233 Q_UNUSED(pos);
234
235 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
236 contextMenu.data()->open();
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(),
251 event->possibleActions(),
252 event->mimeData(),
253 event->buttons(),
254 event->modifiers());
255
256 KIO::DropJob *job = DragAndDropHelper::dropUrls(destItem.mostLocalUrl(), &dropEvent, this);
257 if (job) {
258 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
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 QString newName = value.toString();
268 if (!newName.isEmpty() && newName != item.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
269 const QUrl oldUrl = item.url();
270 QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename);
271 newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName));
272
273 KIO::Job* job = KIO::moveAs(oldUrl, newUrl);
274 KJobWidgets::setWindow(job, this);
275 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job);
276 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
277 }
278 }
279 }
280
281 void FoldersPanel::slotLoadingCompleted()
282 {
283 if (m_controller->view()->opacity() == 0) {
284 // The loading of the initial tree after opening the Folders panel
285 // has been finished. Trigger the increasing of the opacity after
286 // a short delay to give the view the chance to finish its internal
287 // animations.
288 // TODO: Check whether it makes sense to allow accessing the
289 // view-internal delay for usecases like this.
290 QTimer::singleShot(250, this, &FoldersPanel::startFadeInAnimation);
291 }
292
293 if (!m_updateCurrentItem) {
294 return;
295 }
296
297 const int index = m_model->index(url());
298 updateCurrentItem(index);
299 m_updateCurrentItem = false;
300 }
301
302 void FoldersPanel::startFadeInAnimation()
303 {
304 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
305 anim->setStartValue(0);
306 anim->setEndValue(1);
307 anim->setEasingCurve(QEasingCurve::InOutQuad);
308 anim->start(QAbstractAnimation::DeleteWhenStopped);
309 anim->setDuration(200);
310 }
311
312 void FoldersPanel::loadTree(const QUrl& url, FoldersPanel::NavigationBehaviour navigationBehaviour)
313 {
314 Q_ASSERT(m_controller);
315
316 m_updateCurrentItem = false;
317 bool jumpHome = false;
318
319 QUrl baseUrl;
320 if (!url.isLocalFile()) {
321 // Clear the path for non-local URLs and use it as base
322 baseUrl = url;
323 baseUrl.setPath(QStringLiteral("/"));
324 } else if (Dolphin::homeUrl().isParentOf(url) || (Dolphin::homeUrl() == url)) {
325 if (FoldersPanelSettings::limitFoldersPanelToHome() ) {
326 baseUrl = Dolphin::homeUrl();
327 } else {
328 // Use the root directory as base for local URLs (#150941)
329 baseUrl = QUrl::fromLocalFile(QDir::rootPath());
330 }
331 } else if (FoldersPanelSettings::limitFoldersPanelToHome() && navigationBehaviour == AllowJumpHome) {
332 baseUrl = Dolphin::homeUrl();
333 jumpHome = true;
334 } else {
335 // Use the root directory as base for local URLs (#150941)
336 baseUrl = QUrl::fromLocalFile(QDir::rootPath());
337 }
338
339 if (m_model->directory() != baseUrl && !jumpHome) {
340 m_updateCurrentItem = true;
341 m_model->refreshDirectory(baseUrl);
342 }
343
344 const int index = m_model->index(url);
345 if (jumpHome) {
346 emit folderActivated(baseUrl);
347 } else if (index >= 0) {
348 updateCurrentItem(index);
349 } else if (url == baseUrl) {
350 // clear the selection when visiting the base url
351 updateCurrentItem(-1);
352 } else {
353 m_updateCurrentItem = true;
354 m_model->expandParentDirectories(url);
355
356 // slotLoadingCompleted() will be invoked after the model has
357 // expanded the url
358 }
359 }
360
361 void FoldersPanel::updateCurrentItem(int index)
362 {
363 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
364 selectionManager->setCurrentItem(index);
365 selectionManager->clearSelection();
366 selectionManager->setSelected(index);
367
368 m_controller->view()->scrollToItem(index);
369 }
370