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 "foldersitemlistwidget.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"
35 #include <KJobWidgets>
36 #include <KJobUiDelegate>
37 #include <KIO/CopyJob>
38 #include <KIO/DropJob>
39 #include <KIO/FileUndoManager>
41 #include <QApplication>
43 #include <QGraphicsSceneDragDropEvent>
44 #include <QGraphicsView>
45 #include <QPropertyAnimation>
48 FoldersPanel::FoldersPanel(QWidget
* parent
) :
50 m_updateCurrentItem(false),
51 m_controller(nullptr),
54 setLayoutDirection(Qt::LeftToRight
);
57 FoldersPanel::~FoldersPanel()
59 FoldersPanelSettings::self()->save();
62 KItemListView
* view
= m_controller
->view();
63 m_controller
->setView(nullptr);
68 void FoldersPanel::setShowHiddenFiles(bool show
)
70 FoldersPanelSettings::setHiddenFilesShown(show
);
71 m_model
->setShowHiddenFiles(show
);
74 bool FoldersPanel::showHiddenFiles() const
76 return FoldersPanelSettings::hiddenFilesShown();
79 void FoldersPanel::setLimitFoldersPanelToHome(bool enable
)
81 FoldersPanelSettings::setLimitFoldersPanelToHome(enable
);
85 bool FoldersPanel::limitFoldersPanelToHome() const
87 return FoldersPanelSettings::limitFoldersPanelToHome();
90 void FoldersPanel::setAutoScrolling(bool enable
)
92 // TODO: Not supported yet in Dolphin 2.0
93 FoldersPanelSettings::setAutoScrolling(enable
);
96 bool FoldersPanel::autoScrolling() const
98 return FoldersPanelSettings::autoScrolling();
101 void FoldersPanel::rename(const KFileItem
& item
)
103 if (GeneralSettings::renameInline()) {
104 const int index
= m_model
->index(item
);
105 m_controller
->view()->editRole(index
, "text");
107 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
112 bool FoldersPanel::urlChanged()
114 if (!url().isValid() || url().scheme().contains(QLatin1String("search"))) {
115 // Skip results shown by a search, as possible identical
116 // directory names are useless without parent-path information.
127 void FoldersPanel::reloadTree()
130 loadTree(url(), AllowJumpHome
);
135 void FoldersPanel::showEvent(QShowEvent
* event
)
137 if (event
->spontaneous()) {
138 Panel::showEvent(event
);
143 // Postpone the creating of the controller to the first show event.
144 // This assures that no performance and memory overhead is given when the folders panel is not
145 // used at all and stays invisible.
146 KFileItemListView
* view
= new KFileItemListView();
147 view
->setWidgetCreator(new KItemListWidgetCreator
<FoldersItemListWidget
>());
148 view
->setSupportsItemExpanding(true);
149 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
150 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
151 // opening the folders panel.
154 connect(view
, &KFileItemListView::roleEditingFinished
,
155 this, &FoldersPanel::slotRoleEditingFinished
);
157 m_model
= new KFileItemModel(this);
158 m_model
->setShowDirectoriesOnly(true);
159 m_model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
160 // Use a QueuedConnection to give the view the possibility to react first on the
162 connect(m_model
, &KFileItemModel::directoryLoadingCompleted
, this, &FoldersPanel::slotLoadingCompleted
, Qt::QueuedConnection
);
164 m_controller
= new KItemListController(m_model
, view
, this);
165 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
166 m_controller
->setAutoActivationBehavior(KItemListController::ExpansionOnly
);
167 m_controller
->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem
);
168 m_controller
->setAutoActivationDelay(750);
169 m_controller
->setSingleClickActivationEnforced(true);
171 connect(m_controller
, &KItemListController::itemActivated
, this, &FoldersPanel::slotItemActivated
);
172 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &FoldersPanel::slotItemMiddleClicked
);
173 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &FoldersPanel::slotItemContextMenuRequested
);
174 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &FoldersPanel::slotViewContextMenuRequested
);
175 connect(m_controller
, &KItemListController::itemDropEvent
, this, &FoldersPanel::slotItemDropEvent
);
177 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
178 container
->setEnabledFrame(false);
180 QVBoxLayout
* layout
= new QVBoxLayout(this);
181 layout
->setContentsMargins(0, 0, 0, 0);
182 layout
->addWidget(container
);
186 Panel::showEvent(event
);
189 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
191 const int key
= event
->key();
192 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
195 Panel::keyPressEvent(event
);
199 void FoldersPanel::slotItemActivated(int index
)
201 const KFileItem item
= m_model
->fileItem(index
);
202 if (!item
.isNull()) {
203 emit
folderActivated(item
.url());
207 void FoldersPanel::slotItemMiddleClicked(int index
)
209 const KFileItem item
= m_model
->fileItem(index
);
210 if (!item
.isNull()) {
211 emit
folderMiddleClicked(item
.url());
215 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
217 const KFileItem fileItem
= m_model
->fileItem(index
);
219 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
220 contextMenu
.data()->open(pos
.toPoint());
221 if (contextMenu
.data()) {
222 delete contextMenu
.data();
226 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
228 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
229 contextMenu
.data()->open(pos
.toPoint());
230 if (contextMenu
.data()) {
231 delete contextMenu
.data();
235 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
238 KFileItem destItem
= m_model
->fileItem(index
);
239 if (destItem
.isNull()) {
243 QDropEvent
dropEvent(event
->pos().toPoint(),
244 event
->possibleActions(),
249 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(destItem
.mostLocalUrl(), &dropEvent
, this);
251 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
256 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
258 if (role
== "text") {
259 const KFileItem item
= m_model
->fileItem(index
);
260 const QString newName
= value
.toString();
261 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1Char('.') && newName
!= QLatin1String("..")) {
262 const QUrl oldUrl
= item
.url();
263 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
264 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
266 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
267 KJobWidgets::setWindow(job
, this);
268 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, {oldUrl
}, newUrl
, job
);
269 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
274 void FoldersPanel::slotLoadingCompleted()
276 if (m_controller
->view()->opacity() == 0) {
277 // The loading of the initial tree after opening the Folders panel
278 // has been finished. Trigger the increasing of the opacity after
279 // a short delay to give the view the chance to finish its internal
281 // TODO: Check whether it makes sense to allow accessing the
282 // view-internal delay for usecases like this.
283 QTimer::singleShot(250, this, &FoldersPanel::startFadeInAnimation
);
286 if (!m_updateCurrentItem
) {
290 const int index
= m_model
->index(url());
291 updateCurrentItem(index
);
292 m_updateCurrentItem
= false;
295 void FoldersPanel::startFadeInAnimation()
297 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
298 anim
->setStartValue(0);
299 anim
->setEndValue(1);
300 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
301 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
302 anim
->setDuration(200);
305 void FoldersPanel::loadTree(const QUrl
& url
, FoldersPanel::NavigationBehaviour navigationBehaviour
)
307 Q_ASSERT(m_controller
);
309 m_updateCurrentItem
= false;
310 bool jumpHome
= false;
313 if (!url
.isLocalFile()) {
314 // Clear the path for non-local URLs and use it as base
316 baseUrl
.setPath(QStringLiteral("/"));
317 } else if (Dolphin::homeUrl().isParentOf(url
) || (Dolphin::homeUrl() == url
)) {
318 if (FoldersPanelSettings::limitFoldersPanelToHome() ) {
319 baseUrl
= Dolphin::homeUrl();
321 // Use the root directory as base for local URLs (#150941)
322 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
324 } else if (FoldersPanelSettings::limitFoldersPanelToHome() && navigationBehaviour
== AllowJumpHome
) {
325 baseUrl
= Dolphin::homeUrl();
328 // Use the root directory as base for local URLs (#150941)
329 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
332 if (m_model
->directory() != baseUrl
&& !jumpHome
) {
333 m_updateCurrentItem
= true;
334 m_model
->refreshDirectory(baseUrl
);
337 const int index
= m_model
->index(url
);
339 emit
folderActivated(baseUrl
);
340 } else if (index
>= 0) {
341 updateCurrentItem(index
);
342 } else if (url
== baseUrl
) {
343 // clear the selection when visiting the base url
344 updateCurrentItem(-1);
346 m_updateCurrentItem
= true;
347 m_model
->expandParentDirectories(url
);
349 // slotLoadingCompleted() will be invoked after the model has
354 void FoldersPanel::updateCurrentItem(int index
)
356 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
357 selectionManager
->setCurrentItem(index
);
358 selectionManager
->clearSelection();
359 selectionManager
->setSelected(index
);
361 m_controller
->view()->scrollToItem(index
);