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>
45 #include <views/renamedialog.h>
49 FoldersPanel::FoldersPanel(QWidget
* parent
) :
51 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 fileItemModel()->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 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
93 if (false /* GeneralSettings::renameInline() */) {
94 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
95 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
96 //m_treeView->edit(proxyIndex);
98 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
99 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
102 dialog
->activateWindow();
106 bool FoldersPanel::urlChanged()
108 if (!url().isValid() || url().protocol().contains("search")) {
109 // Skip results shown by a search, as possible identical
110 // directory names are useless without parent-path information.
121 void FoldersPanel::showEvent(QShowEvent
* event
)
123 if (event
->spontaneous()) {
124 Panel::showEvent(event
);
129 // Postpone the creating of the dir lister to the first show event.
130 // This assures that no performance and memory overhead is given when the TreeView is not
131 // used at all (see FoldersPanel::setUrl()).
132 KFileItemListView
* view
= new KFileItemListView();
133 view
->setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
135 KItemListStyleOption styleOption
= view
->styleOption();
136 styleOption
.padding
= 2;
137 styleOption
.iconSize
= KIconLoader::SizeSmall
;
138 styleOption
.extendedSelectionRegion
= true;
139 view
->setStyleOption(styleOption
);
141 const qreal itemHeight
= qMax(int(KIconLoader::SizeSmall
), styleOption
.fontMetrics
.height());
142 view
->setItemSize(QSizeF(-1, itemHeight
+ 2 * styleOption
.padding
));
143 view
->setItemLayout(KFileItemListView::DetailsLayout
);
144 view
->setSupportsItemExpanding(true);
145 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
146 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
147 // opening the folders panel.
150 KFileItemModel
* model
= new KFileItemModel(this);
151 model
->setShowDirectoriesOnly(true);
152 model
->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
153 // Use a QueuedConnection to give the view the possibility to react first on the
155 connect(model
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
157 KItemListContainer
* container
= new KItemListContainer(this);
158 m_controller
= container
->controller();
159 m_controller
->setView(view
);
160 m_controller
->setModel(model
);
161 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
162 m_controller
->setAutoActivationDelay(750);
163 m_controller
->setSingleClickActivation(true);
165 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
166 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
167 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
168 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
169 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
171 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
172 // to make the background transparent.
173 container
->setFrameShape(QFrame::NoFrame
);
174 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(container
->viewport());
176 // Make the background of the container transparent and apply the window-text color
177 // to the text color, so that enough contrast is given for all color
179 QPalette p
= graphicsView
->palette();
180 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
181 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
182 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
183 graphicsView
->setPalette(p
);
184 graphicsView
->viewport()->setAutoFillBackground(false);
187 QVBoxLayout
* layout
= new QVBoxLayout(this);
188 layout
->setMargin(0);
189 layout
->addWidget(container
);
193 Panel::showEvent(event
);
196 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
198 const int key
= event
->key();
199 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
202 Panel::keyPressEvent(event
);
206 void FoldersPanel::slotItemActivated(int index
)
208 const KFileItem item
= fileItemModel()->fileItem(index
);
209 if (!item
.isNull()) {
210 emit
changeUrl(item
.url(), Qt::LeftButton
);
214 void FoldersPanel::slotItemMiddleClicked(int index
)
216 const KFileItem item
= fileItemModel()->fileItem(index
);
217 if (!item
.isNull()) {
218 emit
changeUrl(item
.url(), Qt::MiddleButton
);
222 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
226 const KFileItem fileItem
= fileItemModel()->fileItem(index
);
228 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
229 contextMenu
.data()->open();
230 if (contextMenu
.data()) {
231 delete contextMenu
.data();
235 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
239 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
240 contextMenu
.data()->open();
241 if (contextMenu
.data()) {
242 delete contextMenu
.data();
246 void FoldersPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
249 KFileItem destItem
= fileItemModel()->fileItem(index
);
250 if (destItem
.isNull()) {
254 QDropEvent
dropEvent(event
->pos().toPoint(),
255 event
->possibleActions(),
260 DragAndDropHelper::dropUrls(destItem
, destItem
.url(), &dropEvent
);
264 void FoldersPanel::slotLoadingCompleted()
266 if (m_controller
->view()->opacity() == 0) {
267 // The loading of the initial tree after opening the Folders panel
268 // has been finished. Trigger the increasing of the opacity after
269 // a short delay to give the view the chance to finish its internal
271 // TODO: Check whether it makes sense to allow accessing the
272 // view-internal delay for usecases like this.
273 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
276 if (!m_updateCurrentItem
) {
280 const int index
= fileItemModel()->index(url());
281 updateCurrentItem(index
);
282 m_updateCurrentItem
= false;
285 void FoldersPanel::startFadeInAnimation()
287 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
288 anim
->setStartValue(0);
289 anim
->setEndValue(1);
290 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
291 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
292 anim
->setDuration(200);
295 void FoldersPanel::loadTree(const KUrl
& url
)
297 Q_ASSERT(m_controller
);
299 m_updateCurrentItem
= false;
302 if (url
.isLocalFile()) {
303 // Use the root directory as base for local URLs (#150941)
304 baseUrl
= QDir::rootPath();
306 // Clear the path for non-local URLs and use it as base
308 baseUrl
.setPath(QString('/'));
311 KFileItemModel
* model
= fileItemModel();
312 if (model
->directory() != baseUrl
) {
313 m_updateCurrentItem
= true;
314 model
->refreshDirectory(baseUrl
);
317 const int index
= model
->index(url
);
319 updateCurrentItem(index
);
321 m_updateCurrentItem
= true;
322 model
->expandParentDirectories(url
);
323 // slotLoadingCompleted() will be invoked after the model has
328 void FoldersPanel::updateCurrentItem(int index
)
330 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
331 selectionManager
->setCurrentItem(index
);
332 selectionManager
->clearSelection();
333 selectionManager
->setSelected(index
);
335 m_controller
->view()->scrollToItem(index
);
338 KFileItemModel
* FoldersPanel::fileItemModel() const
340 return static_cast<KFileItemModel
*>(m_controller
->model());
343 #include "folderspanel.moc"