]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Don't show a expanding-toggle in the Folders Panel if there are no subdirectories
[dolphin.git] / src / panels / folders / folderspanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "folderspanel.h"
21
22 #include "dolphin_folderspanelsettings.h"
23 #include "dolphin_generalsettings.h"
24 #include "treeviewcontextmenu.h"
25
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>
32
33 #include <KDirLister>
34 #include <KFileItem>
35 #include <konq_operations.h>
36
37 #include <QApplication>
38 #include <QBoxLayout>
39 #include <QDropEvent>
40 #include <QGraphicsSceneDragDropEvent>
41 #include <QGraphicsView>
42 #include <QPropertyAnimation>
43 #include <QTimer>
44
45 #include <views/draganddrophelper.h>
46 #include <views/renamedialog.h>
47
48 #include <KDebug>
49
50 FoldersPanel::FoldersPanel(QWidget* parent) :
51 Panel(parent),
52 m_updateCurrentItem(false),
53 m_dirLister(0),
54 m_controller(0)
55 {
56 setLayoutDirection(Qt::LeftToRight);
57 }
58
59 FoldersPanel::~FoldersPanel()
60 {
61 FoldersPanelSettings::self()->writeConfig();
62
63 if (m_controller) {
64 KItemListView* view = m_controller->view();
65 m_controller->setView(0);
66 delete view;
67 }
68
69 delete m_dirLister;
70 m_dirLister = 0;
71 }
72
73 void FoldersPanel::setHiddenFilesShown(bool show)
74 {
75 FoldersPanelSettings::setHiddenFilesShown(show);
76 if (m_dirLister) {
77 KFileItemModel* model = fileItemModel();
78 const QSet<KUrl> expandedUrls = model->expandedUrls();
79 model->setShowHiddenFiles(show);
80 model->setExpanded(expandedUrls);
81 }
82 }
83
84 bool FoldersPanel::hiddenFilesShown() const
85 {
86 return FoldersPanelSettings::hiddenFilesShown();
87 }
88
89 void FoldersPanel::setAutoScrolling(bool enable)
90 {
91 // TODO: Not supported yet in Dolphin 2.0
92 FoldersPanelSettings::setAutoScrolling(enable);
93 }
94
95 bool FoldersPanel::autoScrolling() const
96 {
97 return FoldersPanelSettings::autoScrolling();
98 }
99
100 void FoldersPanel::rename(const KFileItem& item)
101 {
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);
107 } else {
108 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
109 dialog->setAttribute(Qt::WA_DeleteOnClose);
110 dialog->show();
111 dialog->raise();
112 dialog->activateWindow();
113 }
114 }
115
116 bool FoldersPanel::urlChanged()
117 {
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.
121 return false;
122 }
123
124 if (m_dirLister) {
125 loadTree(url());
126 }
127
128 return true;
129 }
130
131 void FoldersPanel::showEvent(QShowEvent* event)
132 {
133 if (event->spontaneous()) {
134 Panel::showEvent(event);
135 return;
136 }
137
138 if (!m_dirLister) {
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->setAutoUpdate(true);
144 m_dirLister->setMainWindow(window());
145 m_dirLister->setDelayedMimeTypes(true);
146 m_dirLister->setAutoErrorHandlingEnabled(false, this);
147
148 KFileItemListView* view = new KFileItemListView();
149 view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
150
151 KItemListStyleOption styleOption = view->styleOption();
152 styleOption.margin = 2;
153 styleOption.iconSize = KIconLoader::SizeSmall;
154 view->setStyleOption(styleOption);
155
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.
162 view->setOpacity(0);
163
164 KFileItemModel* model = new KFileItemModel(m_dirLister, this);
165 model->setShowFoldersOnly(true);
166 model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
167 // Use a QueuedConnection to give the view the possibility to react first on the
168 // finished loading.
169 connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
170
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);
177 m_controller->setSingleClickActivation(true);
178
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*)));
184
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());
189 if (graphicsView) {
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
192 // schemes
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);
199 }
200
201 QVBoxLayout* layout = new QVBoxLayout(this);
202 layout->setMargin(0);
203 layout->addWidget(container);
204 }
205
206 loadTree(url());
207 Panel::showEvent(event);
208 }
209
210 void FoldersPanel::keyPressEvent(QKeyEvent* event)
211 {
212 const int key = event->key();
213 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
214 event->accept();
215 } else {
216 Panel::keyPressEvent(event);
217 }
218 }
219
220 void FoldersPanel::slotItemActivated(int index)
221 {
222 const KFileItem item = fileItemModel()->fileItem(index);
223 if (!item.isNull()) {
224 emit changeUrl(item.url(), Qt::LeftButton);
225 }
226 }
227
228 void FoldersPanel::slotItemMiddleClicked(int index)
229 {
230 const KFileItem item = fileItemModel()->fileItem(index);
231 if (!item.isNull()) {
232 emit changeUrl(item.url(), Qt::MiddleButton);
233 }
234 }
235
236 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
237 {
238 Q_UNUSED(pos);
239
240 const KFileItem fileItem = fileItemModel()->fileItem(index);
241
242 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
243 contextMenu.data()->open();
244 if (contextMenu.data()) {
245 delete contextMenu.data();
246 }
247 }
248
249 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
250 {
251 Q_UNUSED(pos);
252
253 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
254 contextMenu.data()->open();
255 if (contextMenu.data()) {
256 delete contextMenu.data();
257 }
258 }
259
260 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
261 {
262 if (index >= 0) {
263 KFileItemModel* model = fileItemModel();
264 KFileItem destItem = model->fileItem(index);
265 if (destItem.isNull()) {
266 destItem = model->rootItem();
267 if (destItem.isNull()) {
268 kWarning() << "No destination item available for drop operation.";
269 return;
270 }
271 }
272
273 QDropEvent dropEvent(event->pos().toPoint(),
274 event->possibleActions(),
275 event->mimeData(),
276 event->buttons(),
277 event->modifiers());
278
279 DragAndDropHelper::dropUrls(destItem, &dropEvent);
280 }
281 }
282
283 void FoldersPanel::slotLoadingCompleted()
284 {
285 if (m_controller->view()->opacity() == 0) {
286 // The loading of the initial tree after opening the Folders panel
287 // has been finished. Trigger the increasing of the opacity after
288 // a short delay to give the view the chance to finish its internal
289 // animations.
290 // TODO: Check whether it makes sense to allow accessing the
291 // view-internal delay for usecases like this.
292 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
293 }
294
295 if (!m_updateCurrentItem) {
296 return;
297 }
298
299 const int index = fileItemModel()->index(url());
300 updateCurrentItem(index);
301 m_updateCurrentItem = false;
302 }
303
304 void FoldersPanel::startFadeInAnimation()
305 {
306 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
307 anim->setStartValue(0);
308 anim->setEndValue(1);
309 anim->setEasingCurve(QEasingCurve::InOutQuad);
310 anim->start(QAbstractAnimation::DeleteWhenStopped);
311 anim->setDuration(200);
312 }
313
314 void FoldersPanel::loadTree(const KUrl& url)
315 {
316 Q_ASSERT(m_dirLister);
317
318 m_updateCurrentItem = false;
319
320 KUrl baseUrl;
321 if (url.isLocalFile()) {
322 // Use the root directory as base for local URLs (#150941)
323 baseUrl = QDir::rootPath();
324 } else {
325 // Clear the path for non-local URLs and use it as base
326 baseUrl = url;
327 baseUrl.setPath(QString('/'));
328 }
329
330 if (m_dirLister->url() != baseUrl) {
331 m_updateCurrentItem = true;
332 m_dirLister->stop();
333 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
334 }
335
336 KFileItemModel* model = fileItemModel();
337 const int index = model->index(url);
338 if (index >= 0) {
339 updateCurrentItem(index);
340 } else {
341 m_updateCurrentItem = true;
342 model->setExpanded(QSet<KUrl>() << url);
343 // slotLoadingCompleted() will be invoked after the model has
344 // expanded the url
345 }
346 }
347
348 void FoldersPanel::updateCurrentItem(int index)
349 {
350 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
351 selectionManager->setCurrentItem(index);
352 selectionManager->clearSelection();
353 selectionManager->setSelected(index);
354
355 m_controller->view()->scrollToItem(index);
356 }
357
358 KFileItemModel* FoldersPanel::fileItemModel() const
359 {
360 return static_cast<KFileItemModel*>(m_controller->model());
361 }
362
363 #include "folderspanel.moc"