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"
26 #include <kitemviews/kitemlistselectionmanager.h>
27 #include <kitemviews/kfileitemlistview.h>
28 #include <kitemviews/kfileitemlistwidget.h>
29 #include <kitemviews/kitemlistcontainer.h>
30 #include <kitemviews/kitemlistcontroller.h>
31 #include <kitemviews/kfileitemmodel.h>
34 #include <konq_operations.h>
36 #include <QApplication>
39 #include <QGraphicsSceneDragDropEvent>
40 #include <QGraphicsView>
41 #include <QPropertyAnimation>
44 #include <views/draganddrophelper.h>
48 FoldersPanel::FoldersPanel(QWidget
* parent
) :
50 m_updateCurrentItem(false),
54 setLayoutDirection(Qt::LeftToRight
);
57 FoldersPanel::~FoldersPanel()
59 FoldersPanelSettings::self()->writeConfig();
62 KItemListView
* view
= m_controller
->view();
63 m_controller
->setView(0);
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::setAutoScrolling(bool enable
)
81 // TODO: Not supported yet in Dolphin 2.0
82 FoldersPanelSettings::setAutoScrolling(enable
);
85 bool FoldersPanel::autoScrolling() const
87 return FoldersPanelSettings::autoScrolling();
90 void FoldersPanel::rename(const KFileItem
& item
)
92 const int index
= m_model
->index(item
);
93 m_controller
->view()->editRole(index
, "text");
96 bool FoldersPanel::urlChanged()
98 if (!url().isValid() || url().protocol().contains("search")) {
99 // Skip results shown by a search, as possible identical
100 // directory names are useless without parent-path information.
111 void FoldersPanel::showEvent(QShowEvent
* event
)
113 if (event
->spontaneous()) {
114 Panel::showEvent(event
);
119 // Postpone the creating of the controller to the first show event.
120 // This assures that no performance and memory overhead is given when the folders panel is not
121 // used at all and stays invisible.
122 KFileItemListView
* view
= new KFileItemListView();
123 view
->setSupportsItemExpanding(true);
124 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
125 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
126 // opening the folders panel.
129 connect(view
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
130 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
132 m_model
= new KFileItemModel(this);
133 m_model
->setShowDirectoriesOnly(true);
134 m_model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
135 // Use a QueuedConnection to give the view the possibility to react first on the
137 connect(m_model
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
139 m_controller
= new KItemListController(m_model
, view
, this);
140 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
141 m_controller
->setAutoActivationDelay(750);
142 m_controller
->setSingleClickActivation(true);
144 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
145 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
146 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
147 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
148 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
150 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
151 container
->setEnabledFrame(false);
153 QVBoxLayout
* layout
= new QVBoxLayout(this);
154 layout
->setMargin(0);
155 layout
->addWidget(container
);
159 Panel::showEvent(event
);
162 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
164 const int key
= event
->key();
165 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
168 Panel::keyPressEvent(event
);
172 void FoldersPanel::slotItemActivated(int index
)
174 const KFileItem item
= m_model
->fileItem(index
);
175 if (!item
.isNull()) {
176 emit
folderActivated(item
.url());
180 void FoldersPanel::slotItemMiddleClicked(int index
)
182 const KFileItem item
= m_model
->fileItem(index
);
183 if (!item
.isNull()) {
184 emit
folderMiddleClicked(item
.url());
188 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
192 const KFileItem fileItem
= m_model
->fileItem(index
);
194 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
195 contextMenu
.data()->open();
196 if (contextMenu
.data()) {
197 delete contextMenu
.data();
201 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
205 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
206 contextMenu
.data()->open();
207 if (contextMenu
.data()) {
208 delete contextMenu
.data();
212 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
215 KFileItem destItem
= m_model
->fileItem(index
);
216 if (destItem
.isNull()) {
220 QDropEvent
dropEvent(event
->pos().toPoint(),
221 event
->possibleActions(),
226 const QString error
= DragAndDropHelper::dropUrls(destItem
, destItem
.url(), &dropEvent
);
227 if (!error
.isEmpty()) {
228 emit
errorMessage(error
);
233 void FoldersPanel::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
235 if (role
== "text") {
236 const KFileItem item
= m_model
->fileItem(index
);
237 const QString newName
= value
.toString();
238 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
239 KonqOperations::rename(this, item
.url(), newName
);
244 void FoldersPanel::slotLoadingCompleted()
246 if (m_controller
->view()->opacity() == 0) {
247 // The loading of the initial tree after opening the Folders panel
248 // has been finished. Trigger the increasing of the opacity after
249 // a short delay to give the view the chance to finish its internal
251 // TODO: Check whether it makes sense to allow accessing the
252 // view-internal delay for usecases like this.
253 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
256 if (!m_updateCurrentItem
) {
260 const int index
= m_model
->index(url());
261 updateCurrentItem(index
);
262 m_updateCurrentItem
= false;
265 void FoldersPanel::startFadeInAnimation()
267 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
268 anim
->setStartValue(0);
269 anim
->setEndValue(1);
270 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
271 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
272 anim
->setDuration(200);
275 void FoldersPanel::loadTree(const KUrl
& url
)
277 Q_ASSERT(m_controller
);
279 m_updateCurrentItem
= false;
282 if (url
.isLocalFile()) {
283 // Use the root directory as base for local URLs (#150941)
284 baseUrl
= QDir::rootPath();
286 // Clear the path for non-local URLs and use it as base
288 baseUrl
.setPath(QString('/'));
291 if (m_model
->directory() != baseUrl
) {
292 m_updateCurrentItem
= true;
293 m_model
->refreshDirectory(baseUrl
);
296 const int index
= m_model
->index(url
);
298 updateCurrentItem(index
);
300 m_updateCurrentItem
= true;
301 m_model
->expandParentDirectories(url
);
302 // slotLoadingCompleted() will be invoked after the model has
307 void FoldersPanel::updateCurrentItem(int index
)
309 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
310 selectionManager
->setCurrentItem(index
);
311 selectionManager
->clearSelection();
312 selectionManager
->setSelected(index
);
314 m_controller
->view()->scrollToItem(index
);
317 #include "folderspanel.moc"