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 m_dirLister
->setShowingDotFiles(show
);
80 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
81 model
->setExpanded(expandedUrls
);
85 bool FoldersPanel::hiddenFilesShown() const
87 return FoldersPanelSettings::hiddenFilesShown();
90 void FoldersPanel::setAutoScrolling(bool enable
)
92 // TODO: Not supported yet in Dolphin 2.0
93 FoldersPanelSettings::setAutoScrolling(enable
);
96 bool FoldersPanel::autoScrolling() const
98 return FoldersPanelSettings::autoScrolling();
101 void FoldersPanel::rename(const KFileItem
& item
)
103 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
104 if (false /* GeneralSettings::renameInline() */) {
105 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
106 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
107 //m_treeView->edit(proxyIndex);
109 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
110 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
113 dialog
->activateWindow();
117 bool FoldersPanel::urlChanged()
119 if (!url().isValid() || url().protocol().contains("search")) {
120 // Skip results shown by a search, as possible identical
121 // directory names are useless without parent-path information.
132 void FoldersPanel::showEvent(QShowEvent
* event
)
134 if (event
->spontaneous()) {
135 Panel::showEvent(event
);
140 // Postpone the creating of the dir lister to the first show event.
141 // This assures that no performance and memory overhead is given when the TreeView is not
142 // used at all (see FoldersPanel::setUrl()).
143 m_dirLister
= new KDirLister();
144 m_dirLister
->setDirOnlyMode(true);
145 m_dirLister
->setAutoUpdate(true);
146 m_dirLister
->setMainWindow(window());
147 m_dirLister
->setDelayedMimeTypes(true);
148 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
149 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::hiddenFilesShown());
151 KFileItemListView
* view
= new KFileItemListView();
152 view
->setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
154 KItemListStyleOption styleOption
= view
->styleOption();
155 styleOption
.margin
= 2;
156 styleOption
.iconSize
= KIconLoader::SizeSmall
;
157 view
->setStyleOption(styleOption
);
159 const qreal itemHeight
= qMax(int(KIconLoader::SizeSmall
), styleOption
.fontMetrics
.height());
160 view
->setItemSize(QSizeF(-1, itemHeight
+ 2 * styleOption
.margin
));
161 view
->setItemLayout(KFileItemListView::DetailsLayout
);
162 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
163 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
164 // opening the folders panel.
167 KFileItemModel
* model
= new KFileItemModel(m_dirLister
, this);
168 // Use a QueuedConnection to give the view the possibility to react first on the
170 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
172 KItemListContainer
* container
= new KItemListContainer(this);
173 m_controller
= container
->controller();
174 m_controller
->setView(view
);
175 m_controller
->setModel(model
);
176 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
177 m_controller
->setAutoActivationDelay(750);
179 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
180 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
181 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
182 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
183 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
185 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
186 // to make the background transparent.
187 container
->setFrameShape(QFrame::NoFrame
);
188 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(container
->viewport());
190 // Make the background of the container transparent and apply the window-text color
191 // to the text color, so that enough contrast is given for all color
193 QPalette p
= graphicsView
->palette();
194 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
195 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
196 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
197 graphicsView
->setPalette(p
);
198 graphicsView
->viewport()->setAutoFillBackground(false);
201 QVBoxLayout
* layout
= new QVBoxLayout(this);
202 layout
->setMargin(0);
203 layout
->addWidget(container
);
207 Panel::showEvent(event
);
210 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
212 const int key
= event
->key();
213 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
216 Panel::keyPressEvent(event
);
220 void FoldersPanel::slotItemActivated(int index
)
222 const KFileItem item
= fileItemModel()->fileItem(index
);
223 if (!item
.isNull()) {
224 emit
changeUrl(item
.url(), Qt::LeftButton
);
228 void FoldersPanel::slotItemMiddleClicked(int index
)
230 const KFileItem item
= fileItemModel()->fileItem(index
);
231 if (!item
.isNull()) {
232 emit
changeUrl(item
.url(), Qt::MiddleButton
);
236 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
240 const KFileItem fileItem
= fileItemModel()->fileItem(index
);
242 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
243 contextMenu
.data()->open();
244 if (contextMenu
.data()) {
245 delete contextMenu
.data();
249 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
253 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
254 contextMenu
.data()->open();
255 if (contextMenu
.data()) {
256 delete contextMenu
.data();
260 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
263 const KFileItem destItem
= fileItemModel()->fileItem(index
);
265 QDropEvent
dropEvent(event
->pos().toPoint(),
266 event
->possibleActions(),
271 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
);
275 void FoldersPanel::slotLoadingCompleted()
277 if (m_controller
->view()->opacity() == 0) {
278 // The loading of the initial tree after opening the Folders panel
279 // has been finished. Trigger the increasing of the opacity after
280 // a short delay to give the view the chance to finish its internal
282 // TODO: Check whether it makes sense to allow accessing the
283 // view-internal delay for usecases like this.
284 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
287 if (!m_updateCurrentItem
) {
291 const int index
= fileItemModel()->index(url());
292 updateCurrentItem(index
);
293 m_updateCurrentItem
= false;
296 void FoldersPanel::startFadeInAnimation()
298 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
299 anim
->setStartValue(0);
300 anim
->setEndValue(1);
301 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
302 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
303 anim
->setDuration(200);
306 void FoldersPanel::loadTree(const KUrl
& url
)
308 Q_ASSERT(m_dirLister
);
310 m_updateCurrentItem
= false;
313 if (url
.isLocalFile()) {
314 // Use the root directory as base for local URLs (#150941)
315 baseUrl
= QDir::rootPath();
317 // Clear the path for non-local URLs and use it as base
319 baseUrl
.setPath(QString('/'));
322 if (m_dirLister
->url() != baseUrl
) {
323 m_updateCurrentItem
= true;
325 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
328 KFileItemModel
* model
= fileItemModel();
329 const int index
= model
->index(url
);
331 updateCurrentItem(index
);
333 m_updateCurrentItem
= true;
334 model
->setExpanded(QSet
<KUrl
>() << url
);
335 // slotLoadingCompleted() will be invoked after the model has
340 void FoldersPanel::updateCurrentItem(int index
)
342 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
343 selectionManager
->setCurrentItem(index
);
344 selectionManager
->clearSelection();
345 selectionManager
->setSelected(index
);
347 m_controller
->view()->scrollToItem(index
);
350 KFileItemModel
* FoldersPanel::fileItemModel() const
352 return static_cast<KFileItemModel
*>(m_controller
->model());
355 #include "folderspanel.moc"