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
);
76 fileItemModel()->setShowHiddenFiles(show
);
79 bool FoldersPanel::hiddenFilesShown() const
81 return FoldersPanelSettings::hiddenFilesShown();
84 void FoldersPanel::setAutoScrolling(bool enable
)
86 // TODO: Not supported yet in Dolphin 2.0
87 FoldersPanelSettings::setAutoScrolling(enable
);
90 bool FoldersPanel::autoScrolling() const
92 return FoldersPanelSettings::autoScrolling();
95 void FoldersPanel::rename(const KFileItem
& item
)
97 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
98 if (false /* GeneralSettings::renameInline() */) {
99 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
100 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
101 //m_treeView->edit(proxyIndex);
103 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
104 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
107 dialog
->activateWindow();
111 bool FoldersPanel::urlChanged()
113 if (!url().isValid() || url().protocol().contains("search")) {
114 // Skip results shown by a search, as possible identical
115 // directory names are useless without parent-path information.
126 void FoldersPanel::showEvent(QShowEvent
* event
)
128 if (event
->spontaneous()) {
129 Panel::showEvent(event
);
134 // Postpone the creating of the dir lister to the first show event.
135 // This assures that no performance and memory overhead is given when the TreeView is not
136 // used at all (see FoldersPanel::setUrl()).
137 m_dirLister
= new KDirLister();
138 m_dirLister
->setAutoUpdate(true);
139 m_dirLister
->setMainWindow(window());
140 m_dirLister
->setDelayedMimeTypes(true);
141 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
143 KFileItemListView
* view
= new KFileItemListView();
144 view
->setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
146 KItemListStyleOption styleOption
= view
->styleOption();
147 styleOption
.margin
= 2;
148 styleOption
.iconSize
= KIconLoader::SizeSmall
;
149 view
->setStyleOption(styleOption
);
151 const qreal itemHeight
= qMax(int(KIconLoader::SizeSmall
), styleOption
.fontMetrics
.height());
152 view
->setItemSize(QSizeF(-1, itemHeight
+ 2 * styleOption
.margin
));
153 view
->setItemLayout(KFileItemListView::DetailsLayout
);
154 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
155 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
156 // opening the folders panel.
159 KFileItemModel
* model
= new KFileItemModel(m_dirLister
, this);
160 model
->setShowFoldersOnly(true);
161 model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
162 // Use a QueuedConnection to give the view the possibility to react first on the
164 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
166 KItemListContainer
* container
= new KItemListContainer(this);
167 m_controller
= container
->controller();
168 m_controller
->setView(view
);
169 m_controller
->setModel(model
);
170 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
171 m_controller
->setAutoActivationDelay(750);
172 m_controller
->setSingleClickActivation(true);
174 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
175 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
176 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
177 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
178 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
180 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
181 // to make the background transparent.
182 container
->setFrameShape(QFrame::NoFrame
);
183 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(container
->viewport());
185 // Make the background of the container transparent and apply the window-text color
186 // to the text color, so that enough contrast is given for all color
188 QPalette p
= graphicsView
->palette();
189 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
190 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
191 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
192 graphicsView
->setPalette(p
);
193 graphicsView
->viewport()->setAutoFillBackground(false);
196 QVBoxLayout
* layout
= new QVBoxLayout(this);
197 layout
->setMargin(0);
198 layout
->addWidget(container
);
202 Panel::showEvent(event
);
205 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
207 const int key
= event
->key();
208 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
211 Panel::keyPressEvent(event
);
215 void FoldersPanel::slotItemActivated(int index
)
217 const KFileItem item
= fileItemModel()->fileItem(index
);
218 if (!item
.isNull()) {
219 emit
changeUrl(item
.url(), Qt::LeftButton
);
223 void FoldersPanel::slotItemMiddleClicked(int index
)
225 const KFileItem item
= fileItemModel()->fileItem(index
);
226 if (!item
.isNull()) {
227 emit
changeUrl(item
.url(), Qt::MiddleButton
);
231 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
235 const KFileItem fileItem
= fileItemModel()->fileItem(index
);
237 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
238 contextMenu
.data()->open();
239 if (contextMenu
.data()) {
240 delete contextMenu
.data();
244 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
248 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
249 contextMenu
.data()->open();
250 if (contextMenu
.data()) {
251 delete contextMenu
.data();
255 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
258 KFileItemModel
* model
= fileItemModel();
259 KFileItem destItem
= model
->fileItem(index
);
260 if (destItem
.isNull()) {
264 QDropEvent
dropEvent(event
->pos().toPoint(),
265 event
->possibleActions(),
270 DragAndDropHelper::dropUrls(destItem
, destItem
.url(), &dropEvent
);
274 void FoldersPanel::slotLoadingCompleted()
276 if (m_controller
->view()->opacity() == 0) {
277 // The loading of the initial tree after opening the Folders panel
278 // has been finished. Trigger the increasing of the opacity after
279 // a short delay to give the view the chance to finish its internal
281 // TODO: Check whether it makes sense to allow accessing the
282 // view-internal delay for usecases like this.
283 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
286 if (!m_updateCurrentItem
) {
290 const int index
= fileItemModel()->index(url());
291 updateCurrentItem(index
);
292 m_updateCurrentItem
= false;
295 void FoldersPanel::startFadeInAnimation()
297 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
298 anim
->setStartValue(0);
299 anim
->setEndValue(1);
300 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
301 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
302 anim
->setDuration(200);
305 void FoldersPanel::loadTree(const KUrl
& url
)
307 Q_ASSERT(m_dirLister
);
309 m_updateCurrentItem
= false;
312 if (url
.isLocalFile()) {
313 // Use the root directory as base for local URLs (#150941)
314 baseUrl
= QDir::rootPath();
316 // Clear the path for non-local URLs and use it as base
318 baseUrl
.setPath(QString('/'));
321 if (m_dirLister
->url() != baseUrl
) {
322 m_updateCurrentItem
= true;
324 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
327 KFileItemModel
* model
= fileItemModel();
328 const int index
= model
->index(url
);
330 updateCurrentItem(index
);
332 m_updateCurrentItem
= true;
333 model
->setExpanded(QSet
<KUrl
>() << url
);
334 // slotLoadingCompleted() will be invoked after the model has
339 void FoldersPanel::updateCurrentItem(int index
)
341 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
342 selectionManager
->setCurrentItem(index
);
343 selectionManager
->clearSelection();
344 selectionManager
->setSelected(index
);
346 m_controller
->view()->scrollToItem(index
);
349 KFileItemModel
* FoldersPanel::fileItemModel() const
351 return static_cast<KFileItemModel
*>(m_controller
->model());
354 #include "folderspanel.moc"