2 * SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "folderspanel.h"
9 #include "dolphin_folderspanelsettings.h"
10 #include "dolphin_generalsettings.h"
11 #include "foldersitemlistwidget.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"
22 #include <KJobWidgets>
23 #include <KJobUiDelegate>
24 #include <KIO/CopyJob>
25 #include <KIO/DropJob>
26 #include <KIO/FileUndoManager>
27 #include <KIO/RenameFileDialog>
29 #include <QApplication>
31 #include <QGraphicsSceneDragDropEvent>
32 #include <QGraphicsView>
33 #include <QPropertyAnimation>
36 FoldersPanel::FoldersPanel(QWidget
* parent
) :
38 m_updateCurrentItem(false),
39 m_controller(nullptr),
42 setLayoutDirection(Qt::LeftToRight
);
45 FoldersPanel::~FoldersPanel()
47 FoldersPanelSettings::self()->save();
50 KItemListView
* view
= m_controller
->view();
51 m_controller
->setView(nullptr);
56 void FoldersPanel::setShowHiddenFiles(bool show
)
58 FoldersPanelSettings::setHiddenFilesShown(show
);
59 m_model
->setShowHiddenFiles(show
);
62 bool FoldersPanel::showHiddenFiles() const
64 return FoldersPanelSettings::hiddenFilesShown();
67 void FoldersPanel::setLimitFoldersPanelToHome(bool enable
)
69 FoldersPanelSettings::setLimitFoldersPanelToHome(enable
);
73 bool FoldersPanel::limitFoldersPanelToHome() const
75 return FoldersPanelSettings::limitFoldersPanelToHome();
78 void FoldersPanel::setAutoScrolling(bool enable
)
80 // TODO: Not supported yet in Dolphin 2.0
81 FoldersPanelSettings::setAutoScrolling(enable
);
84 bool FoldersPanel::autoScrolling() const
86 return FoldersPanelSettings::autoScrolling();
89 void FoldersPanel::rename(const KFileItem
& item
)
91 if (GeneralSettings::renameInline()) {
92 const int index
= m_model
->index(item
);
93 m_controller
->view()->editRole(index
, "text");
95 KIO::RenameFileDialog
* dialog
= new KIO::RenameFileDialog(KFileItemList({item
}), this);
100 bool FoldersPanel::urlChanged()
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.
115 void FoldersPanel::reloadTree()
118 loadTree(url(), AllowJumpHome
);
123 void FoldersPanel::showEvent(QShowEvent
* event
)
125 if (event
->spontaneous()) {
126 Panel::showEvent(event
);
131 // Postpone the creating of the controller to the first show event.
132 // This assures that no performance and memory overhead is given when the folders panel is not
133 // used at all and stays invisible.
134 KFileItemListView
* view
= new KFileItemListView();
135 view
->setScanDirectories(false);
136 view
->setWidgetCreator(new KItemListWidgetCreator
<FoldersItemListWidget
>());
137 view
->setSupportsItemExpanding(true);
138 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
139 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
140 // opening the folders panel.
143 connect(view
, &KFileItemListView::roleEditingFinished
,
144 this, &FoldersPanel::slotRoleEditingFinished
);
146 m_model
= new KFileItemModel(this);
147 m_model
->setShowDirectoriesOnly(true);
148 m_model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
149 // Use a QueuedConnection to give the view the possibility to react first on the
151 connect(m_model
, &KFileItemModel::directoryLoadingCompleted
, this, &FoldersPanel::slotLoadingCompleted
, Qt::QueuedConnection
);
153 m_controller
= new KItemListController(m_model
, view
, this);
154 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
155 m_controller
->setAutoActivationBehavior(KItemListController::ExpansionOnly
);
156 m_controller
->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem
);
157 m_controller
->setAutoActivationDelay(750);
158 m_controller
->setSingleClickActivationEnforced(true);
160 connect(m_controller
, &KItemListController::itemActivated
, this, &FoldersPanel::slotItemActivated
);
161 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &FoldersPanel::slotItemMiddleClicked
);
162 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &FoldersPanel::slotItemContextMenuRequested
);
163 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &FoldersPanel::slotViewContextMenuRequested
);
164 connect(m_controller
, &KItemListController::itemDropEvent
, this, &FoldersPanel::slotItemDropEvent
);
166 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
167 container
->setEnabledFrame(false);
169 QVBoxLayout
* layout
= new QVBoxLayout(this);
170 layout
->setContentsMargins(0, 0, 0, 0);
171 layout
->addWidget(container
);
175 Panel::showEvent(event
);
178 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
180 const int key
= event
->key();
181 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
184 Panel::keyPressEvent(event
);
188 void FoldersPanel::slotItemActivated(int index
)
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
203 Q_EMIT
folderActivated(item
.url());
208 void FoldersPanel::slotItemMiddleClicked(int index
)
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());
217 Q_EMIT
folderInNewTab(item
.url());
222 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
224 const KFileItem fileItem
= m_model
->fileItem(index
);
226 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
227 contextMenu
.data()->open(pos
.toPoint());
228 if (contextMenu
.data()) {
229 delete contextMenu
.data();
233 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
235 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
236 contextMenu
.data()->open(pos
.toPoint());
237 if (contextMenu
.data()) {
238 delete contextMenu
.data();
242 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
245 KFileItem destItem
= m_model
->fileItem(index
);
246 if (destItem
.isNull()) {
250 QDropEvent
dropEvent(event
->pos().toPoint(),
251 event
->possibleActions(),
256 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(destItem
.mostLocalUrl(), &dropEvent
, this);
258 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) {
259 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
260 Q_EMIT
errorMessage(job
->errorString());
267 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
269 if (role
== "text") {
270 const KFileItem item
= m_model
->fileItem(index
);
271 const EditResult retVal
= value
.value
<EditResult
>();
272 const QString newName
= retVal
.newName
;
273 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1Char('.') && newName
!= QLatin1String("..")) {
274 const QUrl oldUrl
= item
.url();
275 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
276 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
278 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
279 KJobWidgets::setWindow(job
, this);
280 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, {oldUrl
}, newUrl
, job
);
281 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
286 void FoldersPanel::slotLoadingCompleted()
288 if (m_controller
->view()->opacity() == 0) {
289 // The loading of the initial tree after opening the Folders panel
290 // has been finished. Trigger the increasing of the opacity after
291 // a short delay to give the view the chance to finish its internal
293 // TODO: Check whether it makes sense to allow accessing the
294 // view-internal delay for usecases like this.
295 QTimer::singleShot(250, this, &FoldersPanel::startFadeInAnimation
);
298 if (!m_updateCurrentItem
) {
302 const int index
= m_model
->index(url());
303 updateCurrentItem(index
);
304 m_updateCurrentItem
= false;
307 void FoldersPanel::startFadeInAnimation()
309 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
310 anim
->setStartValue(0);
311 anim
->setEndValue(1);
312 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
313 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
314 anim
->setDuration(200);
317 void FoldersPanel::loadTree(const QUrl
& url
, FoldersPanel::NavigationBehaviour navigationBehaviour
)
319 Q_ASSERT(m_controller
);
321 m_updateCurrentItem
= false;
322 bool jumpHome
= false;
325 if (!url
.isLocalFile()) {
326 // Clear the path for non-local URLs and use it as base
328 baseUrl
.setPath(QStringLiteral("/"));
329 } else if (Dolphin::homeUrl().isParentOf(url
) || (Dolphin::homeUrl() == url
)) {
330 if (FoldersPanelSettings::limitFoldersPanelToHome() ) {
331 baseUrl
= Dolphin::homeUrl();
333 // Use the root directory as base for local URLs (#150941)
334 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
336 } else if (FoldersPanelSettings::limitFoldersPanelToHome() && navigationBehaviour
== AllowJumpHome
) {
337 baseUrl
= Dolphin::homeUrl();
340 // Use the root directory as base for local URLs (#150941)
341 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
344 if (m_model
->directory() != baseUrl
&& !jumpHome
) {
345 m_updateCurrentItem
= true;
346 m_model
->refreshDirectory(baseUrl
);
349 const int index
= m_model
->index(url
);
351 Q_EMIT
folderActivated(baseUrl
);
352 } else if (index
>= 0) {
353 updateCurrentItem(index
);
354 } else if (url
== baseUrl
) {
355 // clear the selection when visiting the base url
356 updateCurrentItem(-1);
358 m_updateCurrentItem
= true;
359 m_model
->expandParentDirectories(url
);
361 // slotLoadingCompleted() will be invoked after the model has
366 void FoldersPanel::updateCurrentItem(int index
)
368 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
369 selectionManager
->setCurrentItem(index
);
370 selectionManager
->clearSelection();
371 selectionManager
->setSelected(index
);
373 m_controller
->view()->scrollToItem(index
);