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 <konq_operations.h>
38 #include <QApplication>
41 #include <QGraphicsSceneDragDropEvent>
42 #include <QGraphicsView>
43 #include <QPropertyAnimation>
46 #include <views/draganddrophelper.h>
50 FoldersPanel::FoldersPanel(QWidget
* parent
) :
52 m_updateCurrentItem(false),
56 setLayoutDirection(Qt::LeftToRight
);
59 FoldersPanel::~FoldersPanel()
61 FoldersPanelSettings::self()->writeConfig();
64 KItemListView
* view
= m_controller
->view();
65 m_controller
->setView(0);
70 void FoldersPanel::setShowHiddenFiles(bool show
)
72 FoldersPanelSettings::setHiddenFilesShown(show
);
73 m_model
->setShowHiddenFiles(show
);
76 bool FoldersPanel::showHiddenFiles() const
78 return FoldersPanelSettings::hiddenFilesShown();
81 void FoldersPanel::setAutoScrolling(bool enable
)
83 // TODO: Not supported yet in Dolphin 2.0
84 FoldersPanelSettings::setAutoScrolling(enable
);
87 bool FoldersPanel::autoScrolling() const
89 return FoldersPanelSettings::autoScrolling();
92 void FoldersPanel::rename(const KFileItem
& item
)
94 if (GeneralSettings::renameInline()) {
95 const int index
= m_model
->index(item
);
96 m_controller
->view()->editRole(index
, "text");
98 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
99 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
102 dialog
->activateWindow();
106 bool FoldersPanel::urlChanged()
108 if (!url().isValid() || url().protocol().contains("search")) {
109 // Skip results shown by a search, as possible identical
110 // directory names are useless without parent-path information.
121 void FoldersPanel::showEvent(QShowEvent
* event
)
123 if (event
->spontaneous()) {
124 Panel::showEvent(event
);
129 // Postpone the creating of the controller to the first show event.
130 // This assures that no performance and memory overhead is given when the folders panel is not
131 // used at all and stays invisible.
132 KFileItemListView
* view
= new KFileItemListView();
133 view
->setWidgetCreator(new KItemListWidgetCreator
<FoldersItemListWidget
>());
134 view
->setSupportsItemExpanding(true);
135 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
136 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
137 // opening the folders panel.
140 connect(view
, &KFileItemListView::roleEditingFinished
,
141 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
->setAutoActivationDelay(750);
155 m_controller
->setSingleClickActivationEnforced(true);
157 connect(m_controller
, &KItemListController::itemActivated
, this, &FoldersPanel::slotItemActivated
);
158 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &FoldersPanel::slotItemMiddleClicked
);
159 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &FoldersPanel::slotItemContextMenuRequested
);
160 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &FoldersPanel::slotViewContextMenuRequested
);
161 connect(m_controller
, &KItemListController::itemDropEvent
, this, &FoldersPanel::slotItemDropEvent
);
163 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
164 container
->setEnabledFrame(false);
166 QVBoxLayout
* layout
= new QVBoxLayout(this);
167 layout
->setMargin(0);
168 layout
->addWidget(container
);
172 Panel::showEvent(event
);
175 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
177 const int key
= event
->key();
178 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
181 Panel::keyPressEvent(event
);
185 void FoldersPanel::slotItemActivated(int index
)
187 const KFileItem item
= m_model
->fileItem(index
);
188 if (!item
.isNull()) {
189 emit
folderActivated(item
.url());
193 void FoldersPanel::slotItemMiddleClicked(int index
)
195 const KFileItem item
= m_model
->fileItem(index
);
196 if (!item
.isNull()) {
197 emit
folderMiddleClicked(item
.url());
201 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
205 const KFileItem fileItem
= m_model
->fileItem(index
);
207 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
208 contextMenu
.data()->open();
209 if (contextMenu
.data()) {
210 delete contextMenu
.data();
214 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
218 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
219 contextMenu
.data()->open();
220 if (contextMenu
.data()) {
221 delete contextMenu
.data();
225 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
228 KFileItem destItem
= m_model
->fileItem(index
);
229 if (destItem
.isNull()) {
233 QDropEvent
dropEvent(event
->pos().toPoint(),
234 event
->possibleActions(),
240 DragAndDropHelper::dropUrls(destItem
, destItem
.url(), &dropEvent
, error
);
241 if (!error
.isEmpty()) {
242 emit
errorMessage(error
);
247 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
249 if (role
== "text") {
250 const KFileItem item
= m_model
->fileItem(index
);
251 const QString newName
= value
.toString();
252 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
253 KonqOperations::rename(this, item
.url(), newName
);
258 void FoldersPanel::slotLoadingCompleted()
260 if (m_controller
->view()->opacity() == 0) {
261 // The loading of the initial tree after opening the Folders panel
262 // has been finished. Trigger the increasing of the opacity after
263 // a short delay to give the view the chance to finish its internal
265 // TODO: Check whether it makes sense to allow accessing the
266 // view-internal delay for usecases like this.
267 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
270 if (!m_updateCurrentItem
) {
274 const int index
= m_model
->index(url());
275 updateCurrentItem(index
);
276 m_updateCurrentItem
= false;
279 void FoldersPanel::startFadeInAnimation()
281 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
282 anim
->setStartValue(0);
283 anim
->setEndValue(1);
284 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
285 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
286 anim
->setDuration(200);
289 void FoldersPanel::loadTree(const KUrl
& url
)
291 Q_ASSERT(m_controller
);
293 m_updateCurrentItem
= false;
296 if (url
.isLocalFile()) {
297 // Use the root directory as base for local URLs (#150941)
298 baseUrl
= QDir::rootPath();
300 // Clear the path for non-local URLs and use it as base
302 baseUrl
.setPath(QString('/'));
305 if (m_model
->directory() != baseUrl
) {
306 m_updateCurrentItem
= true;
307 m_model
->refreshDirectory(baseUrl
);
310 const int index
= m_model
->index(url
);
312 updateCurrentItem(index
);
314 m_updateCurrentItem
= true;
315 m_model
->expandParentDirectories(url
);
316 // slotLoadingCompleted() will be invoked after the model has
321 void FoldersPanel::updateCurrentItem(int index
)
323 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
324 selectionManager
->setCurrentItem(index
);
325 selectionManager
->clearSelection();
326 selectionManager
->setSelected(index
);
328 m_controller
->view()->scrollToItem(index
);
331 #include "folderspanel.moc"