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>
35 #include <konq_operations.h>
37 #include <QApplication>
40 #include <QGraphicsSceneDragDropEvent>
41 #include <QGraphicsView>
42 #include <QPropertyAnimation>
45 #include <views/draganddrophelper.h>
46 #include <views/renamedialog.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);
73 void FoldersPanel::setHiddenFilesShown(bool show
)
75 FoldersPanelSettings::setHiddenFilesShown(show
);
77 KFileItemModel
* model
= fileItemModel();
78 const QSet
<KUrl
> expandedUrls
= model
->expandedUrls();
79 model
->setShowHiddenFiles(show
);
80 model
->setExpanded(expandedUrls
);
84 bool FoldersPanel::hiddenFilesShown() const
86 return FoldersPanelSettings::hiddenFilesShown();
89 void FoldersPanel::setAutoScrolling(bool enable
)
91 // TODO: Not supported yet in Dolphin 2.0
92 FoldersPanelSettings::setAutoScrolling(enable
);
95 bool FoldersPanel::autoScrolling() const
97 return FoldersPanelSettings::autoScrolling();
100 void FoldersPanel::rename(const KFileItem
& item
)
102 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
103 if (false /* GeneralSettings::renameInline() */) {
104 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
105 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
106 //m_treeView->edit(proxyIndex);
108 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
109 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
112 dialog
->activateWindow();
116 bool FoldersPanel::urlChanged()
118 if (!url().isValid() || url().protocol().contains("search")) {
119 // Skip results shown by a search, as possible identical
120 // directory names are useless without parent-path information.
131 void FoldersPanel::showEvent(QShowEvent
* event
)
133 if (event
->spontaneous()) {
134 Panel::showEvent(event
);
139 // Postpone the creating of the dir lister to the first show event.
140 // This assures that no performance and memory overhead is given when the TreeView is not
141 // used at all (see FoldersPanel::setUrl()).
142 m_dirLister
= new KDirLister();
143 m_dirLister
->setDirOnlyMode(true);
144 m_dirLister
->setAutoUpdate(true);
145 m_dirLister
->setMainWindow(window());
146 m_dirLister
->setDelayedMimeTypes(true);
147 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
149 KFileItemListView
* view
= new KFileItemListView();
150 view
->setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
152 KItemListStyleOption styleOption
= view
->styleOption();
153 styleOption
.margin
= 2;
154 styleOption
.iconSize
= KIconLoader::SizeSmall
;
155 view
->setStyleOption(styleOption
);
157 const qreal itemHeight
= qMax(int(KIconLoader::SizeSmall
), styleOption
.fontMetrics
.height());
158 view
->setItemSize(QSizeF(-1, itemHeight
+ 2 * styleOption
.margin
));
159 view
->setItemLayout(KFileItemListView::DetailsLayout
);
160 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
161 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
162 // opening the folders panel.
165 KFileItemModel
* model
= new KFileItemModel(m_dirLister
, this);
166 model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
167 // Use a QueuedConnection to give the view the possibility to react first on the
169 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
171 KItemListContainer
* container
= new KItemListContainer(this);
172 m_controller
= container
->controller();
173 m_controller
->setView(view
);
174 m_controller
->setModel(model
);
175 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
176 m_controller
->setAutoActivationDelay(750);
178 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
179 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
180 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
181 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
182 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
184 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
185 // to make the background transparent.
186 container
->setFrameShape(QFrame::NoFrame
);
187 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(container
->viewport());
189 // Make the background of the container transparent and apply the window-text color
190 // to the text color, so that enough contrast is given for all color
192 QPalette p
= graphicsView
->palette();
193 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
194 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
195 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
196 graphicsView
->setPalette(p
);
197 graphicsView
->viewport()->setAutoFillBackground(false);
200 QVBoxLayout
* layout
= new QVBoxLayout(this);
201 layout
->setMargin(0);
202 layout
->addWidget(container
);
206 Panel::showEvent(event
);
209 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
211 const int key
= event
->key();
212 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
215 Panel::keyPressEvent(event
);
219 void FoldersPanel::slotItemActivated(int index
)
221 const KFileItem item
= fileItemModel()->fileItem(index
);
222 if (!item
.isNull()) {
223 emit
changeUrl(item
.url(), Qt::LeftButton
);
227 void FoldersPanel::slotItemMiddleClicked(int index
)
229 const KFileItem item
= fileItemModel()->fileItem(index
);
230 if (!item
.isNull()) {
231 emit
changeUrl(item
.url(), Qt::MiddleButton
);
235 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
239 const KFileItem fileItem
= fileItemModel()->fileItem(index
);
241 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
242 contextMenu
.data()->open();
243 if (contextMenu
.data()) {
244 delete contextMenu
.data();
248 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
252 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
253 contextMenu
.data()->open();
254 if (contextMenu
.data()) {
255 delete contextMenu
.data();
259 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
262 KFileItemModel
* model
= fileItemModel();
263 KFileItem destItem
= model
->fileItem(index
);
264 if (destItem
.isNull()) {
265 destItem
= model
->rootItem();
266 if (destItem
.isNull()) {
267 kWarning() << "No destination item available for drop operation.";
272 QDropEvent
dropEvent(event
->pos().toPoint(),
273 event
->possibleActions(),
278 DragAndDropHelper::dropUrls(destItem
, &dropEvent
);
282 void FoldersPanel::slotLoadingCompleted()
284 if (m_controller
->view()->opacity() == 0) {
285 // The loading of the initial tree after opening the Folders panel
286 // has been finished. Trigger the increasing of the opacity after
287 // a short delay to give the view the chance to finish its internal
289 // TODO: Check whether it makes sense to allow accessing the
290 // view-internal delay for usecases like this.
291 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
294 if (!m_updateCurrentItem
) {
298 const int index
= fileItemModel()->index(url());
299 updateCurrentItem(index
);
300 m_updateCurrentItem
= false;
303 void FoldersPanel::startFadeInAnimation()
305 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
306 anim
->setStartValue(0);
307 anim
->setEndValue(1);
308 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
309 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
310 anim
->setDuration(200);
313 void FoldersPanel::loadTree(const KUrl
& url
)
315 Q_ASSERT(m_dirLister
);
317 m_updateCurrentItem
= false;
320 if (url
.isLocalFile()) {
321 // Use the root directory as base for local URLs (#150941)
322 baseUrl
= QDir::rootPath();
324 // Clear the path for non-local URLs and use it as base
326 baseUrl
.setPath(QString('/'));
329 if (m_dirLister
->url() != baseUrl
) {
330 m_updateCurrentItem
= true;
332 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
335 KFileItemModel
* model
= fileItemModel();
336 const int index
= model
->index(url
);
338 updateCurrentItem(index
);
340 m_updateCurrentItem
= true;
341 model
->setExpanded(QSet
<KUrl
>() << url
);
342 // slotLoadingCompleted() will be invoked after the model has
347 void FoldersPanel::updateCurrentItem(int index
)
349 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
350 selectionManager
->setCurrentItem(index
);
351 selectionManager
->clearSelection();
352 selectionManager
->setSelected(index
);
354 m_controller
->view()->scrollToItem(index
);
357 KFileItemModel
* FoldersPanel::fileItemModel() const
359 return static_cast<KFileItemModel
*>(m_controller
->model());
362 #include "folderspanel.moc"