1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "folderspanel.h"
22 #include "dolphin_folderspanelsettings.h"
23 #include "dolphin_generalsettings.h"
24 #include "treeviewcontextmenu.h"
25 #include "foldersitemlistwidget.h"
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>
36 #include <KJobWidgets>
37 #include <KJobUiDelegate>
38 #include <KIO/CopyJob>
39 #include <KIO/DropJob>
40 #include <KIO/FileUndoManager>
42 #include <QApplication>
45 #include <QGraphicsSceneDragDropEvent>
46 #include <QGraphicsView>
47 #include <QPropertyAnimation>
50 #include <views/draganddrophelper.h>
52 #include "dolphindebug.h"
55 FoldersPanel::FoldersPanel(QWidget
* parent
) :
57 m_updateCurrentItem(false),
58 m_controller(nullptr),
61 setLayoutDirection(Qt::LeftToRight
);
64 FoldersPanel::~FoldersPanel()
66 FoldersPanelSettings::self()->save();
69 KItemListView
* view
= m_controller
->view();
70 m_controller
->setView(nullptr);
75 void FoldersPanel::setShowHiddenFiles(bool show
)
77 FoldersPanelSettings::setHiddenFilesShown(show
);
78 m_model
->setShowHiddenFiles(show
);
81 bool FoldersPanel::showHiddenFiles() const
83 return FoldersPanelSettings::hiddenFilesShown();
86 void FoldersPanel::setLimitFoldersPanelToHome(bool enable
)
88 FoldersPanelSettings::setLimitFoldersPanelToHome(enable
);
92 bool FoldersPanel::limitFoldersPanelToHome() const
94 return limitFoldersPanelToHome();
97 void FoldersPanel::setAutoScrolling(bool enable
)
99 // TODO: Not supported yet in Dolphin 2.0
100 FoldersPanelSettings::setAutoScrolling(enable
);
103 bool FoldersPanel::autoScrolling() const
105 return FoldersPanelSettings::autoScrolling();
108 void FoldersPanel::rename(const KFileItem
& item
)
110 if (GeneralSettings::renameInline()) {
111 const int index
= m_model
->index(item
);
112 m_controller
->view()->editRole(index
, "text");
114 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
115 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
118 dialog
->activateWindow();
122 bool FoldersPanel::urlChanged()
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.
137 void FoldersPanel::reloadTree()
145 void FoldersPanel::showEvent(QShowEvent
* event
)
147 if (event
->spontaneous()) {
148 Panel::showEvent(event
);
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.
164 connect(view
, &KFileItemListView::roleEditingFinished
,
165 this, &FoldersPanel::slotRoleEditingFinished
);
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
172 connect(m_model
, &KFileItemModel::directoryLoadingCompleted
, this, &FoldersPanel::slotLoadingCompleted
, Qt::QueuedConnection
);
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);
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
);
187 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
188 container
->setEnabledFrame(false);
190 QVBoxLayout
* layout
= new QVBoxLayout(this);
191 layout
->setMargin(0);
192 layout
->addWidget(container
);
196 Panel::showEvent(event
);
199 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
201 const int key
= event
->key();
202 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
205 Panel::keyPressEvent(event
);
209 void FoldersPanel::slotItemActivated(int index
)
211 const KFileItem item
= m_model
->fileItem(index
);
212 if (!item
.isNull()) {
213 emit
folderActivated(item
.url());
217 void FoldersPanel::slotItemMiddleClicked(int index
)
219 const KFileItem item
= m_model
->fileItem(index
);
220 if (!item
.isNull()) {
221 emit
folderMiddleClicked(item
.url());
225 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
229 const KFileItem fileItem
= m_model
->fileItem(index
);
231 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
232 contextMenu
.data()->open();
233 if (contextMenu
.data()) {
234 delete contextMenu
.data();
238 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
242 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
243 contextMenu
.data()->open();
244 if (contextMenu
.data()) {
245 delete contextMenu
.data();
249 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
252 KFileItem destItem
= m_model
->fileItem(index
);
253 if (destItem
.isNull()) {
257 QDropEvent
dropEvent(event
->pos().toPoint(),
258 event
->possibleActions(),
263 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(destItem
.mostLocalUrl(), &dropEvent
, this);
265 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
270 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
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
));
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);
288 void FoldersPanel::slotLoadingCompleted()
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
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
);
300 if (!m_updateCurrentItem
) {
304 const int index
= m_model
->index(url());
305 updateCurrentItem(index
);
306 m_updateCurrentItem
= false;
309 void FoldersPanel::startFadeInAnimation()
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);
319 void FoldersPanel::loadTree(const QUrl
& url
)
321 Q_ASSERT(m_controller
);
323 m_updateCurrentItem
= false;
326 if (url
.isLocalFile()) {
327 const bool isInHomeFolder
= Dolphin::homeUrl().isParentOf(url
) || (Dolphin::homeUrl() == url
);
328 if (limitFoldersPanelToHome() && isInHomeFolder
) {
329 baseUrl
= Dolphin::homeUrl();
331 // Use the root directory as base for local URLs (#150941)
332 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
335 // Clear the path for non-local URLs and use it as base
337 baseUrl
.setPath(QString('/'));
340 if (m_model
->directory() != baseUrl
) {
341 m_updateCurrentItem
= true;
342 m_model
->refreshDirectory(baseUrl
);
345 const int index
= m_model
->index(url
);
347 updateCurrentItem(index
);
348 } else if (url
== baseUrl
) {
349 // clear the selection when visiting the base url
350 updateCurrentItem(-1);
352 m_updateCurrentItem
= true;
353 m_model
->expandParentDirectories(url
);
355 // slotLoadingCompleted() will be invoked after the model has
360 void FoldersPanel::updateCurrentItem(int index
)
362 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
363 selectionManager
->setCurrentItem(index
);
364 selectionManager
->clearSelection();
365 selectionManager
->setSelected(index
);
367 m_controller
->view()->scrollToItem(index
);