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 <kitemviews/kitemlistselectionmanager.h>
28 #include <kitemviews/kfileitemlistview.h>
29 #include <kitemviews/kfileitemlistwidget.h>
30 #include <kitemviews/kitemlistcontainer.h>
31 #include <kitemviews/kitemlistcontroller.h>
32 #include <kitemviews/kfileitemmodel.h>
35 #include <konq_operations.h>
37 #include <QApplication>
40 #include <QGraphicsSceneDragDropEvent>
41 #include <QGraphicsView>
42 #include <QPropertyAnimation>
45 #include <views/draganddrophelper.h>
49 FoldersPanel::FoldersPanel(QWidget
* parent
) :
51 m_updateCurrentItem(false),
55 setLayoutDirection(Qt::LeftToRight
);
58 FoldersPanel::~FoldersPanel()
60 FoldersPanelSettings::self()->writeConfig();
63 KItemListView
* view
= m_controller
->view();
64 m_controller
->setView(0);
69 void FoldersPanel::setShowHiddenFiles(bool show
)
71 FoldersPanelSettings::setHiddenFilesShown(show
);
72 m_model
->setShowHiddenFiles(show
);
75 bool FoldersPanel::showHiddenFiles() const
77 return FoldersPanelSettings::hiddenFilesShown();
80 void FoldersPanel::setAutoScrolling(bool enable
)
82 // TODO: Not supported yet in Dolphin 2.0
83 FoldersPanelSettings::setAutoScrolling(enable
);
86 bool FoldersPanel::autoScrolling() const
88 return FoldersPanelSettings::autoScrolling();
91 void FoldersPanel::rename(const KFileItem
& item
)
93 const int index
= m_model
->index(item
);
94 m_controller
->view()->editRole(index
, "text");
97 bool FoldersPanel::urlChanged()
99 if (!url().isValid() || url().protocol().contains("search")) {
100 // Skip results shown by a search, as possible identical
101 // directory names are useless without parent-path information.
112 void FoldersPanel::showEvent(QShowEvent
* event
)
114 if (event
->spontaneous()) {
115 Panel::showEvent(event
);
120 // Postpone the creating of the controller to the first show event.
121 // This assures that no performance and memory overhead is given when the folders panel is not
122 // used at all and stays invisible.
123 KFileItemListView
* view
= new KFileItemListView();
124 view
->setWidgetCreator(new KItemListWidgetCreator
<FoldersItemListWidget
>());
125 view
->setSupportsItemExpanding(true);
126 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
127 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
128 // opening the folders panel.
131 connect(view
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
132 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
134 m_model
= new KFileItemModel(this);
135 m_model
->setShowDirectoriesOnly(true);
136 m_model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
137 // Use a QueuedConnection to give the view the possibility to react first on the
139 connect(m_model
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
141 m_controller
= new KItemListController(m_model
, view
, this);
142 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
143 m_controller
->setAutoActivationBehavior(KItemListController::ExpansionOnly
);
144 m_controller
->setAutoActivationDelay(750);
145 m_controller
->setSingleClickActivation(true);
147 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
148 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
149 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
150 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
151 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
153 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
154 container
->setEnabledFrame(false);
156 QVBoxLayout
* layout
= new QVBoxLayout(this);
157 layout
->setMargin(0);
158 layout
->addWidget(container
);
162 Panel::showEvent(event
);
165 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
167 const int key
= event
->key();
168 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
171 Panel::keyPressEvent(event
);
175 void FoldersPanel::slotItemActivated(int index
)
177 const KFileItem item
= m_model
->fileItem(index
);
178 if (!item
.isNull()) {
179 emit
folderActivated(item
.url());
183 void FoldersPanel::slotItemMiddleClicked(int index
)
185 const KFileItem item
= m_model
->fileItem(index
);
186 if (!item
.isNull()) {
187 emit
folderMiddleClicked(item
.url());
191 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
195 const KFileItem fileItem
= m_model
->fileItem(index
);
197 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
198 contextMenu
.data()->open();
199 if (contextMenu
.data()) {
200 delete contextMenu
.data();
204 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
208 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
209 contextMenu
.data()->open();
210 if (contextMenu
.data()) {
211 delete contextMenu
.data();
215 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
218 KFileItem destItem
= m_model
->fileItem(index
);
219 if (destItem
.isNull()) {
223 QDropEvent
dropEvent(event
->pos().toPoint(),
224 event
->possibleActions(),
229 const QString error
= DragAndDropHelper::dropUrls(destItem
, destItem
.url(), &dropEvent
);
230 if (!error
.isEmpty()) {
231 emit
errorMessage(error
);
236 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
238 if (role
== "text") {
239 const KFileItem item
= m_model
->fileItem(index
);
240 const QString newName
= value
.toString();
241 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
242 KonqOperations::rename(this, item
.url(), newName
);
247 void FoldersPanel::slotLoadingCompleted()
249 if (m_controller
->view()->opacity() == 0) {
250 // The loading of the initial tree after opening the Folders panel
251 // has been finished. Trigger the increasing of the opacity after
252 // a short delay to give the view the chance to finish its internal
254 // TODO: Check whether it makes sense to allow accessing the
255 // view-internal delay for usecases like this.
256 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
259 if (!m_updateCurrentItem
) {
263 const int index
= m_model
->index(url());
264 updateCurrentItem(index
);
265 m_updateCurrentItem
= false;
268 void FoldersPanel::startFadeInAnimation()
270 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
271 anim
->setStartValue(0);
272 anim
->setEndValue(1);
273 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
274 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
275 anim
->setDuration(200);
278 void FoldersPanel::loadTree(const KUrl
& url
)
280 Q_ASSERT(m_controller
);
282 m_updateCurrentItem
= false;
285 if (url
.isLocalFile()) {
286 // Use the root directory as base for local URLs (#150941)
287 baseUrl
= QDir::rootPath();
289 // Clear the path for non-local URLs and use it as base
291 baseUrl
.setPath(QString('/'));
294 if (m_model
->directory() != baseUrl
) {
295 m_updateCurrentItem
= true;
296 m_model
->refreshDirectory(baseUrl
);
299 const int index
= m_model
->index(url
);
301 updateCurrentItem(index
);
303 m_updateCurrentItem
= true;
304 m_model
->expandParentDirectories(url
);
305 // slotLoadingCompleted() will be invoked after the model has
310 void FoldersPanel::updateCurrentItem(int index
)
312 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
313 selectionManager
->setCurrentItem(index
);
314 selectionManager
->clearSelection();
315 selectionManager
->setSelected(index
);
317 m_controller
->view()->scrollToItem(index
);
320 #include "folderspanel.moc"