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/FileUndoManager>
41 #include <QApplication>
44 #include <QGraphicsSceneDragDropEvent>
45 #include <QGraphicsView>
46 #include <QPropertyAnimation>
49 #include <views/draganddrophelper.h>
53 FoldersPanel::FoldersPanel(QWidget
* parent
) :
55 m_updateCurrentItem(false),
59 setLayoutDirection(Qt::LeftToRight
);
62 FoldersPanel::~FoldersPanel()
64 FoldersPanelSettings::self()->save();
67 KItemListView
* view
= m_controller
->view();
68 m_controller
->setView(0);
73 void FoldersPanel::setShowHiddenFiles(bool show
)
75 FoldersPanelSettings::setHiddenFilesShown(show
);
76 m_model
->setShowHiddenFiles(show
);
79 bool FoldersPanel::showHiddenFiles() const
81 return FoldersPanelSettings::hiddenFilesShown();
84 void FoldersPanel::setAutoScrolling(bool enable
)
86 // TODO: Not supported yet in Dolphin 2.0
87 FoldersPanelSettings::setAutoScrolling(enable
);
90 bool FoldersPanel::autoScrolling() const
92 return FoldersPanelSettings::autoScrolling();
95 void FoldersPanel::rename(const KFileItem
& item
)
97 if (GeneralSettings::renameInline()) {
98 const int index
= m_model
->index(item
);
99 m_controller
->view()->editRole(index
, "text");
101 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
102 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
105 dialog
->activateWindow();
109 bool FoldersPanel::urlChanged()
111 if (!url().isValid() || url().protocol().contains("search")) {
112 // Skip results shown by a search, as possible identical
113 // directory names are useless without parent-path information.
124 void FoldersPanel::showEvent(QShowEvent
* event
)
126 if (event
->spontaneous()) {
127 Panel::showEvent(event
);
132 // Postpone the creating of the controller to the first show event.
133 // This assures that no performance and memory overhead is given when the folders panel is not
134 // used at all and stays invisible.
135 KFileItemListView
* view
= new KFileItemListView();
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
->setMargin(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 emit
folderActivated(item
.url());
196 void FoldersPanel::slotItemMiddleClicked(int index
)
198 const KFileItem item
= m_model
->fileItem(index
);
199 if (!item
.isNull()) {
200 emit
folderMiddleClicked(item
.url());
204 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
208 const KFileItem fileItem
= m_model
->fileItem(index
);
210 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
211 contextMenu
.data()->open();
212 if (contextMenu
.data()) {
213 delete contextMenu
.data();
217 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
221 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
222 contextMenu
.data()->open();
223 if (contextMenu
.data()) {
224 delete contextMenu
.data();
228 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
231 KFileItem destItem
= m_model
->fileItem(index
);
232 if (destItem
.isNull()) {
236 QDropEvent
dropEvent(event
->pos().toPoint(),
237 event
->possibleActions(),
243 DragAndDropHelper::dropUrls(destItem
, destItem
.url(), &dropEvent
, error
);
244 if (!error
.isEmpty()) {
245 emit
errorMessage(error
);
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
, QList
<QUrl
>() << 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 KUrl
& 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
= 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
);