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 <KIO/CopyJob>
23 #include <KIO/DropJob>
24 #include <KIO/FileUndoManager>
25 #include <KIO/RenameFileDialog>
26 #include <KJobUiDelegate>
27 #include <KJobWidgets>
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
);
122 void FoldersPanel::showEvent(QShowEvent
*event
)
124 if (event
->spontaneous()) {
125 Panel::showEvent(event
);
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.
141 connect(view
, &KFileItemListView::roleEditingFinished
, this, &FoldersPanel::slotRoleEditingFinished
);
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
148 connect(m_model
, &KFileItemModel::directoryLoadingCompleted
, this, &FoldersPanel::slotLoadingCompleted
, Qt::QueuedConnection
);
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
->setSingleClickActivationEnforced(true);
156 connect(m_controller
, &KItemListController::itemActivated
, this, &FoldersPanel::slotItemActivated
);
157 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &FoldersPanel::slotItemMiddleClicked
);
158 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &FoldersPanel::slotItemContextMenuRequested
);
159 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &FoldersPanel::slotViewContextMenuRequested
);
160 connect(m_controller
, &KItemListController::itemDropEvent
, this, &FoldersPanel::slotItemDropEvent
);
162 KItemListContainer
*container
= new KItemListContainer(m_controller
, this);
163 #ifndef QT_NO_ACCESSIBILITY
164 view
->setAccessibleParentsObject(container
);
166 container
->setEnabledFrame(false);
168 QVBoxLayout
*layout
= new QVBoxLayout(this);
169 layout
->setContentsMargins(0, 0, 0, 0);
170 layout
->addWidget(container
);
174 Panel::showEvent(event
);
177 void FoldersPanel::keyPressEvent(QKeyEvent
*event
)
179 const int key
= event
->key();
180 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
183 Panel::keyPressEvent(event
);
187 void FoldersPanel::slotItemActivated(int index
)
189 const KFileItem item
= m_model
->fileItem(index
);
190 if (!item
.isNull()) {
191 const auto modifiers
= QGuiApplication::keyboardModifiers();
192 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
193 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
194 Q_EMIT
folderInNewActiveTab(item
.url());
195 } else if (modifiers
& Qt::ControlModifier
) {
196 Q_EMIT
folderInNewTab(item
.url());
197 } else if (modifiers
& Qt::ShiftModifier
) {
198 // The shift modifier is not considered because it is used to expand the tree view without actually
199 // opening the folder
202 Q_EMIT
folderActivated(item
.url());
207 void FoldersPanel::slotItemMiddleClicked(int index
)
209 const KFileItem item
= m_model
->fileItem(index
);
210 if (!item
.isNull()) {
211 const auto modifiers
= QGuiApplication::keyboardModifiers();
212 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
213 if (modifiers
& Qt::ShiftModifier
) {
214 Q_EMIT
folderInNewActiveTab(item
.url());
216 Q_EMIT
folderInNewTab(item
.url());
221 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
&pos
)
223 const KFileItem fileItem
= m_model
->fileItem(index
);
225 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
226 contextMenu
.data()->open(pos
.toPoint());
227 if (contextMenu
.data()) {
228 delete contextMenu
.data();
232 void FoldersPanel::slotViewContextMenuRequested(const QPointF
&pos
)
234 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
235 contextMenu
.data()->open(pos
.toPoint());
236 if (contextMenu
.data()) {
237 delete contextMenu
.data();
241 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
*event
)
244 KFileItem destItem
= m_model
->fileItem(index
);
245 if (destItem
.isNull()) {
249 QDropEvent
dropEvent(event
->pos().toPoint(), event
->possibleActions(), event
->mimeData(), event
->buttons(), event
->modifiers());
251 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(destItem
.mostLocalUrl(), &dropEvent
, this);
253 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) {
254 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
255 Q_EMIT
errorMessage(job
->errorString());
262 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
&role
, const QVariant
&value
)
264 if (role
== "text") {
265 const KFileItem item
= m_model
->fileItem(index
);
266 const EditResult retVal
= value
.value
<EditResult
>();
267 const QString newName
= retVal
.newName
;
268 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1Char('.') && newName
!= QLatin1String("..")) {
269 const QUrl oldUrl
= item
.url();
270 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
271 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
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);
281 void FoldersPanel::slotLoadingCompleted()
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
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
);
293 if (!m_updateCurrentItem
) {
297 const int index
= m_model
->index(url());
298 updateCurrentItem(index
);
299 m_updateCurrentItem
= false;
302 void FoldersPanel::startFadeInAnimation()
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);
312 void FoldersPanel::loadTree(const QUrl
&url
, FoldersPanel::NavigationBehaviour navigationBehaviour
)
314 Q_ASSERT(m_controller
);
316 m_updateCurrentItem
= false;
317 bool jumpHome
= false;
320 if (!url
.isLocalFile()) {
321 // Clear the path for non-local URLs and use it as base
323 baseUrl
.setPath(QStringLiteral("/"));
324 } else if (Dolphin::homeUrl().isParentOf(url
) || (Dolphin::homeUrl() == url
)) {
325 if (FoldersPanelSettings::limitFoldersPanelToHome()) {
326 baseUrl
= Dolphin::homeUrl();
328 // Use the root directory as base for local URLs (#150941)
329 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
331 } else if (FoldersPanelSettings::limitFoldersPanelToHome() && navigationBehaviour
== AllowJumpHome
) {
332 baseUrl
= Dolphin::homeUrl();
335 // Use the root directory as base for local URLs (#150941)
336 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
339 if (m_model
->directory() != baseUrl
&& !jumpHome
) {
340 m_updateCurrentItem
= true;
341 m_model
->refreshDirectory(baseUrl
);
344 const int index
= m_model
->index(url
);
346 Q_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);
353 m_updateCurrentItem
= true;
354 m_model
->expandParentDirectories(url
);
356 // slotLoadingCompleted() will be invoked after the model has
361 void FoldersPanel::updateCurrentItem(int index
)
363 KItemListSelectionManager
*selectionManager
= m_controller
->selectionManager();
364 selectionManager
->setCurrentItem(index
);
365 selectionManager
->clearSelection();
366 selectionManager
->setSelected(index
);
368 m_controller
->view()->scrollToItem(index
);
371 #include "moc_folderspanel.cpp"