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>
54 FoldersPanel::FoldersPanel(QWidget
* parent
) :
56 m_updateCurrentItem(false),
60 setLayoutDirection(Qt::LeftToRight
);
63 FoldersPanel::~FoldersPanel()
65 FoldersPanelSettings::self()->save();
68 KItemListView
* view
= m_controller
->view();
69 m_controller
->setView(0);
74 void FoldersPanel::setShowHiddenFiles(bool show
)
76 FoldersPanelSettings::setHiddenFilesShown(show
);
77 m_model
->setShowHiddenFiles(show
);
80 bool FoldersPanel::showHiddenFiles() const
82 return FoldersPanelSettings::hiddenFilesShown();
85 void FoldersPanel::setAutoScrolling(bool enable
)
87 // TODO: Not supported yet in Dolphin 2.0
88 FoldersPanelSettings::setAutoScrolling(enable
);
91 bool FoldersPanel::autoScrolling() const
93 return FoldersPanelSettings::autoScrolling();
96 void FoldersPanel::rename(const KFileItem
& item
)
98 if (GeneralSettings::renameInline()) {
99 const int index
= m_model
->index(item
);
100 m_controller
->view()->editRole(index
, "text");
102 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
103 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
106 dialog
->activateWindow();
110 bool FoldersPanel::urlChanged()
112 if (!url().isValid() || url().scheme().contains("search")) {
113 // Skip results shown by a search, as possible identical
114 // directory names are useless without parent-path information.
125 void FoldersPanel::showEvent(QShowEvent
* event
)
127 if (event
->spontaneous()) {
128 Panel::showEvent(event
);
133 // Postpone the creating of the controller to the first show event.
134 // This assures that no performance and memory overhead is given when the folders panel is not
135 // used at all and stays invisible.
136 KFileItemListView
* view
= new KFileItemListView();
137 view
->setWidgetCreator(new KItemListWidgetCreator
<FoldersItemListWidget
>());
138 view
->setSupportsItemExpanding(true);
139 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
140 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
141 // opening the folders panel.
144 connect(view
, &KFileItemListView::roleEditingFinished
,
145 this, &FoldersPanel::slotRoleEditingFinished
);
147 m_model
= new KFileItemModel(this);
148 m_model
->setShowDirectoriesOnly(true);
149 m_model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
150 // Use a QueuedConnection to give the view the possibility to react first on the
152 connect(m_model
, &KFileItemModel::directoryLoadingCompleted
, this, &FoldersPanel::slotLoadingCompleted
, Qt::QueuedConnection
);
154 m_controller
= new KItemListController(m_model
, view
, this);
155 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
156 m_controller
->setAutoActivationBehavior(KItemListController::ExpansionOnly
);
157 m_controller
->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem
);
158 m_controller
->setAutoActivationDelay(750);
159 m_controller
->setSingleClickActivationEnforced(true);
161 connect(m_controller
, &KItemListController::itemActivated
, this, &FoldersPanel::slotItemActivated
);
162 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &FoldersPanel::slotItemMiddleClicked
);
163 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &FoldersPanel::slotItemContextMenuRequested
);
164 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &FoldersPanel::slotViewContextMenuRequested
);
165 connect(m_controller
, &KItemListController::itemDropEvent
, this, &FoldersPanel::slotItemDropEvent
);
167 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
168 container
->setEnabledFrame(false);
170 QVBoxLayout
* layout
= new QVBoxLayout(this);
171 layout
->setMargin(0);
172 layout
->addWidget(container
);
176 Panel::showEvent(event
);
179 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
181 const int key
= event
->key();
182 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
185 Panel::keyPressEvent(event
);
189 void FoldersPanel::slotItemActivated(int index
)
191 const KFileItem item
= m_model
->fileItem(index
);
192 if (!item
.isNull()) {
193 emit
folderActivated(item
.url());
197 void FoldersPanel::slotItemMiddleClicked(int index
)
199 const KFileItem item
= m_model
->fileItem(index
);
200 if (!item
.isNull()) {
201 emit
folderMiddleClicked(item
.url());
205 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
209 const KFileItem fileItem
= m_model
->fileItem(index
);
211 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
212 contextMenu
.data()->open();
213 if (contextMenu
.data()) {
214 delete contextMenu
.data();
218 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
222 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
223 contextMenu
.data()->open();
224 if (contextMenu
.data()) {
225 delete contextMenu
.data();
229 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
232 KFileItem destItem
= m_model
->fileItem(index
);
233 if (destItem
.isNull()) {
237 QDropEvent
dropEvent(event
->pos().toPoint(),
238 event
->possibleActions(),
243 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(destItem
.url(), &dropEvent
, this);
245 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
250 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
252 if (role
== "text") {
253 const KFileItem item
= m_model
->fileItem(index
);
254 const QString newName
= value
.toString();
255 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
256 const QUrl oldUrl
= item
.url();
257 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
258 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
260 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
261 KJobWidgets::setWindow(job
, this);
262 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, {oldUrl
}, newUrl
, job
);
263 job
->ui()->setAutoErrorHandlingEnabled(true);
268 void FoldersPanel::slotLoadingCompleted()
270 if (m_controller
->view()->opacity() == 0) {
271 // The loading of the initial tree after opening the Folders panel
272 // has been finished. Trigger the increasing of the opacity after
273 // a short delay to give the view the chance to finish its internal
275 // TODO: Check whether it makes sense to allow accessing the
276 // view-internal delay for usecases like this.
277 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
280 if (!m_updateCurrentItem
) {
284 const int index
= m_model
->index(url());
285 updateCurrentItem(index
);
286 m_updateCurrentItem
= false;
289 void FoldersPanel::startFadeInAnimation()
291 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
292 anim
->setStartValue(0);
293 anim
->setEndValue(1);
294 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
295 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
296 anim
->setDuration(200);
299 void FoldersPanel::loadTree(const QUrl
& url
)
301 Q_ASSERT(m_controller
);
303 m_updateCurrentItem
= false;
306 if (url
.isLocalFile()) {
307 // Use the root directory as base for local URLs (#150941)
308 baseUrl
= QUrl::fromLocalFile(QDir::rootPath());
310 // Clear the path for non-local URLs and use it as base
312 baseUrl
.setPath(QString('/'));
315 if (m_model
->directory() != baseUrl
) {
316 m_updateCurrentItem
= true;
317 m_model
->refreshDirectory(baseUrl
);
320 const int index
= m_model
->index(url
);
322 updateCurrentItem(index
);
324 m_updateCurrentItem
= true;
325 m_model
->expandParentDirectories(url
);
326 // slotLoadingCompleted() will be invoked after the model has
331 void FoldersPanel::updateCurrentItem(int index
)
333 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
334 selectionManager
->setCurrentItem(index
);
335 selectionManager
->clearSelection();
336 selectionManager
->setSelected(index
);
338 m_controller
->view()->scrollToItem(index
);