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>
39 #include <QGraphicsView>
40 #include <QPropertyAnimation>
43 #include <views/renamedialog.h>
47 FoldersPanel::FoldersPanel(QWidget
* parent
) :
49 m_updateCurrentItem(false),
53 setLayoutDirection(Qt::LeftToRight
);
56 FoldersPanel::~FoldersPanel()
58 FoldersPanelSettings::self()->writeConfig();
61 KItemListView
* view
= m_controller
->view();
62 m_controller
->setView(0);
70 void FoldersPanel::setHiddenFilesShown(bool show
)
72 FoldersPanelSettings::setHiddenFilesShown(show
);
74 KFileItemModel
* model
= fileItemModel();
75 const QSet
<KUrl
> expandedUrls
= model
->expandedUrls();
76 m_dirLister
->setShowingDotFiles(show
);
77 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
78 model
->setExpanded(expandedUrls
);
82 bool FoldersPanel::hiddenFilesShown() const
84 return FoldersPanelSettings::hiddenFilesShown();
87 void FoldersPanel::setAutoScrolling(bool enable
)
89 //m_treeView->setAutoHorizontalScroll(enable);
90 FoldersPanelSettings::setAutoScrolling(enable
);
93 bool FoldersPanel::autoScrolling() const
95 return FoldersPanelSettings::autoScrolling();
98 void FoldersPanel::rename(const KFileItem
& item
)
100 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
101 if (false /* GeneralSettings::renameInline() */) {
102 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
103 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
104 //m_treeView->edit(proxyIndex);
106 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
107 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
110 dialog
->activateWindow();
114 bool FoldersPanel::urlChanged()
116 if (!url().isValid() || url().protocol().contains("search")) {
117 // Skip results shown by a search, as possible identical
118 // directory names are useless without parent-path information.
129 void FoldersPanel::showEvent(QShowEvent
* event
)
131 if (event
->spontaneous()) {
132 Panel::showEvent(event
);
137 // Postpone the creating of the dir lister to the first show event.
138 // This assures that no performance and memory overhead is given when the TreeView is not
139 // used at all (see FoldersPanel::setUrl()).
140 m_dirLister
= new KDirLister();
141 m_dirLister
->setDirOnlyMode(true);
142 m_dirLister
->setAutoUpdate(true);
143 m_dirLister
->setMainWindow(window());
144 m_dirLister
->setDelayedMimeTypes(true);
145 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
146 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::hiddenFilesShown());
148 KFileItemListView
* view
= new KFileItemListView();
149 view
->setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
151 KItemListStyleOption styleOption
= view
->styleOption();
152 styleOption
.margin
= 2;
153 styleOption
.iconSize
= KIconLoader::SizeSmall
;
154 view
->setStyleOption(styleOption
);
156 const qreal itemHeight
= qMax(int(KIconLoader::SizeSmall
), styleOption
.fontMetrics
.height());
157 view
->setItemSize(QSizeF(-1, itemHeight
+ 2 * styleOption
.margin
));
158 view
->setItemLayout(KFileItemListView::DetailsLayout
);
159 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
160 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
161 // opening the folders panel.
164 KFileItemModel
* model
= new KFileItemModel(m_dirLister
, this);
165 // Use a QueuedConnection to give the view the possibility to react first on the
167 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
169 KItemListContainer
* container
= new KItemListContainer(this);
170 m_controller
= container
->controller();
171 m_controller
->setView(view
);
172 m_controller
->setModel(model
);
173 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
174 m_controller
->setAutoActivationDelay(750);
176 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
177 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
178 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
179 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
181 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
182 // to make the background transparent.
183 container
->setFrameShape(QFrame::NoFrame
);
184 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(container
->viewport());
186 // Make the background of the container transparent and apply the window-text color
187 // to the text color, so that enough contrast is given for all color
189 QPalette p
= graphicsView
->palette();
190 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
191 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
192 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
193 graphicsView
->setPalette(p
);
194 graphicsView
->viewport()->setAutoFillBackground(false);
197 QVBoxLayout
* layout
= new QVBoxLayout(this);
198 layout
->setMargin(0);
199 layout
->addWidget(container
);
203 Panel::showEvent(event
);
206 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
208 const int key
= event
->key();
209 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
211 //updateActiveView(m_treeView->currentIndex());
213 Panel::keyPressEvent(event
);
217 void FoldersPanel::slotItemActivated(int index
)
219 const KFileItem item
= fileItemModel()->fileItem(index
);
220 if (!item
.isNull()) {
221 emit
changeUrl(item
.url(), Qt::LeftButton
);
225 void FoldersPanel::slotItemMiddleClicked(int index
)
227 const KFileItem item
= fileItemModel()->fileItem(index
);
228 if (!item
.isNull()) {
229 emit
changeUrl(item
.url(), Qt::MiddleButton
);
233 void FoldersPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
237 const KFileItem fileItem
= fileItemModel()->fileItem(index
);
239 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, fileItem
);
240 contextMenu
.data()->open();
241 if (contextMenu
.data()) {
242 delete contextMenu
.data();
246 void FoldersPanel::slotViewContextMenuRequested(const QPointF
& pos
)
250 QWeakPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, KFileItem());
251 contextMenu
.data()->open();
252 if (contextMenu
.data()) {
253 delete contextMenu
.data();
257 void FoldersPanel::slotLoadingCompleted()
259 if (m_controller
->view()->opacity() == 0) {
260 // The loading of the initial tree after opening the Folders panel
261 // has been finished. Trigger the increasing of the opacity after
262 // a short delay to give the view the chance to finish its internal
264 // TODO: Check whether it makes sense to allow accessing the
265 // view-internal delay for usecases like this.
266 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
269 if (!m_updateCurrentItem
) {
273 const int index
= fileItemModel()->index(url());
274 updateCurrentItem(index
);
275 m_updateCurrentItem
= false;
278 void FoldersPanel::slotHorizontalScrollBarMoved(int value
)
281 // Disable the auto-scrolling until the vertical scrollbar has
282 // been moved by the user.
283 //m_treeView->setAutoHorizontalScroll(false);
286 void FoldersPanel::slotVerticalScrollBarMoved(int value
)
289 // Enable the auto-scrolling again (it might have been disabled by
290 // moving the horizontal scrollbar).
291 //m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
294 void FoldersPanel::startFadeInAnimation()
296 QPropertyAnimation
* anim
= new QPropertyAnimation(m_controller
->view(), "opacity", this);
297 anim
->setStartValue(0);
298 anim
->setEndValue(1);
299 anim
->setEasingCurve(QEasingCurve::InOutQuad
);
300 anim
->start(QAbstractAnimation::DeleteWhenStopped
);
301 anim
->setDuration(200);
304 void FoldersPanel::loadTree(const KUrl
& url
)
306 Q_ASSERT(m_dirLister
);
308 m_updateCurrentItem
= false;
311 if (url
.isLocalFile()) {
312 // Use the root directory as base for local URLs (#150941)
313 baseUrl
= QDir::rootPath();
315 // Clear the path for non-local URLs and use it as base
317 baseUrl
.setPath(QString('/'));
320 if (m_dirLister
->url() != baseUrl
) {
321 m_updateCurrentItem
= true;
323 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
326 KFileItemModel
* model
= fileItemModel();
327 const int index
= model
->index(url
);
329 updateCurrentItem(index
);
331 m_updateCurrentItem
= true;
332 model
->setExpanded(QSet
<KUrl
>() << url
);
333 // slotLoadingCompleted() will be invoked after the model has
338 void FoldersPanel::updateCurrentItem(int index
)
340 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
341 selectionManager
->setCurrentItem(index
);
342 selectionManager
->clearSelection();
343 selectionManager
->setSelected(index
);
345 m_controller
->view()->scrollToItem(index
);
348 KFileItemModel
* FoldersPanel::fileItemModel() const
350 return static_cast<KFileItemModel
*>(m_controller
->model());
353 #include "folderspanel.moc"