]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Remove toolbar-spacer if the menu has been disabled
[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::setShowHiddenFiles(bool show)
74 {
75 FoldersPanelSettings::setHiddenFilesShown(show);
76 fileItemModel()->setShowHiddenFiles(show);
77 }
78
79 bool FoldersPanel::showHiddenFiles() const
80 {
81 return FoldersPanelSettings::hiddenFilesShown();
82 }
83
84 void FoldersPanel::setAutoScrolling(bool enable)
85 {
86 // TODO: Not supported yet in Dolphin 2.0
87 FoldersPanelSettings::setAutoScrolling(enable);
88 }
89
90 bool FoldersPanel::autoScrolling() const
91 {
92 return FoldersPanelSettings::autoScrolling();
93 }
94
95 void FoldersPanel::rename(const KFileItem& item)
96 {
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);
102 } else {
103 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
104 dialog->setAttribute(Qt::WA_DeleteOnClose);
105 dialog->show();
106 dialog->raise();
107 dialog->activateWindow();
108 }
109 }
110
111 bool FoldersPanel::urlChanged()
112 {
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.
116 return false;
117 }
118
119 if (m_dirLister) {
120 loadTree(url());
121 }
122
123 return true;
124 }
125
126 void FoldersPanel::showEvent(QShowEvent* event)
127 {
128 if (event->spontaneous()) {
129 Panel::showEvent(event);
130 return;
131 }
132
133 if (!m_dirLister) {
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);
142
143 KFileItemListView* view = new KFileItemListView();
144 view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
145
146 KItemListStyleOption styleOption = view->styleOption();
147 styleOption.padding = 2;
148 styleOption.iconSize = KIconLoader::SizeSmall;
149 styleOption.extendedSelectionRegion = true;
150 view->setStyleOption(styleOption);
151
152 const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height());
153 view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.padding));
154 view->setItemLayout(KFileItemListView::DetailsLayout);
155 view->setSupportsItemExpanding(true);
156 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
157 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
158 // opening the folders panel.
159 view->setOpacity(0);
160
161 KFileItemModel* model = new KFileItemModel(m_dirLister, this);
162 model->setShowFoldersOnly(true);
163 model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
164 // Use a QueuedConnection to give the view the possibility to react first on the
165 // finished loading.
166 connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
167
168 KItemListContainer* container = new KItemListContainer(this);
169 m_controller = container->controller();
170 m_controller->setView(view);
171 m_controller->setModel(model);
172 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
173 m_controller->setAutoActivationDelay(750);
174 m_controller->setSingleClickActivation(true);
175
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)));
180 connect(m_controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
181
182 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
183 // to make the background transparent.
184 container->setFrameShape(QFrame::NoFrame);
185 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(container->viewport());
186 if (graphicsView) {
187 // Make the background of the container transparent and apply the window-text color
188 // to the text color, so that enough contrast is given for all color
189 // schemes
190 QPalette p = graphicsView->palette();
191 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
192 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
193 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
194 graphicsView->setPalette(p);
195 graphicsView->viewport()->setAutoFillBackground(false);
196 }
197
198 QVBoxLayout* layout = new QVBoxLayout(this);
199 layout->setMargin(0);
200 layout->addWidget(container);
201 }
202
203 loadTree(url());
204 Panel::showEvent(event);
205 }
206
207 void FoldersPanel::keyPressEvent(QKeyEvent* event)
208 {
209 const int key = event->key();
210 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
211 event->accept();
212 } else {
213 Panel::keyPressEvent(event);
214 }
215 }
216
217 void FoldersPanel::slotItemActivated(int index)
218 {
219 const KFileItem item = fileItemModel()->fileItem(index);
220 if (!item.isNull()) {
221 emit changeUrl(item.url(), Qt::LeftButton);
222 }
223 }
224
225 void FoldersPanel::slotItemMiddleClicked(int index)
226 {
227 const KFileItem item = fileItemModel()->fileItem(index);
228 if (!item.isNull()) {
229 emit changeUrl(item.url(), Qt::MiddleButton);
230 }
231 }
232
233 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
234 {
235 Q_UNUSED(pos);
236
237 const KFileItem fileItem = fileItemModel()->fileItem(index);
238
239 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
240 contextMenu.data()->open();
241 if (contextMenu.data()) {
242 delete contextMenu.data();
243 }
244 }
245
246 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
247 {
248 Q_UNUSED(pos);
249
250 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
251 contextMenu.data()->open();
252 if (contextMenu.data()) {
253 delete contextMenu.data();
254 }
255 }
256
257 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
258 {
259 if (index >= 0) {
260 KFileItem destItem = fileItemModel()->fileItem(index);
261 if (destItem.isNull()) {
262 return;
263 }
264
265 QDropEvent dropEvent(event->pos().toPoint(),
266 event->possibleActions(),
267 event->mimeData(),
268 event->buttons(),
269 event->modifiers());
270
271 DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent);
272 }
273 }
274
275 void FoldersPanel::slotLoadingCompleted()
276 {
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
281 // animations.
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()));
285 }
286
287 if (!m_updateCurrentItem) {
288 return;
289 }
290
291 const int index = fileItemModel()->index(url());
292 updateCurrentItem(index);
293 m_updateCurrentItem = false;
294 }
295
296 void FoldersPanel::startFadeInAnimation()
297 {
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);
304 }
305
306 void FoldersPanel::loadTree(const KUrl& url)
307 {
308 Q_ASSERT(m_dirLister);
309
310 m_updateCurrentItem = false;
311
312 KUrl baseUrl;
313 if (url.isLocalFile()) {
314 // Use the root directory as base for local URLs (#150941)
315 baseUrl = QDir::rootPath();
316 } else {
317 // Clear the path for non-local URLs and use it as base
318 baseUrl = url;
319 baseUrl.setPath(QString('/'));
320 }
321
322 if (m_dirLister->url() != baseUrl) {
323 m_updateCurrentItem = true;
324 m_dirLister->stop();
325 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
326 }
327
328 KFileItemModel* model = fileItemModel();
329 const int index = model->index(url);
330 if (index >= 0) {
331 updateCurrentItem(index);
332 } else {
333 m_updateCurrentItem = true;
334 model->expandParentItems(url);
335 // slotLoadingCompleted() will be invoked after the model has
336 // expanded the url
337 }
338 }
339
340 void FoldersPanel::updateCurrentItem(int index)
341 {
342 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
343 selectionManager->setCurrentItem(index);
344 selectionManager->clearSelection();
345 selectionManager->setSelected(index);
346
347 m_controller->view()->scrollToItem(index);
348 }
349
350 KFileItemModel* FoldersPanel::fileItemModel() const
351 {
352 return static_cast<KFileItemModel*>(m_controller->model());
353 }
354
355 #include "folderspanel.moc"