]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Fixed the signal connection that emits completed in Dolphin's KPart
[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 <KFileItem>
34 #include <konq_operations.h>
35
36 #include <QApplication>
37 #include <QBoxLayout>
38 #include <QDropEvent>
39 #include <QGraphicsSceneDragDropEvent>
40 #include <QGraphicsView>
41 #include <QPropertyAnimation>
42 #include <QTimer>
43
44 #include <views/draganddrophelper.h>
45
46 #include <KDebug>
47
48 FoldersPanel::FoldersPanel(QWidget* parent) :
49 Panel(parent),
50 m_updateCurrentItem(false),
51 m_controller(0)
52 {
53 setLayoutDirection(Qt::LeftToRight);
54 }
55
56 FoldersPanel::~FoldersPanel()
57 {
58 FoldersPanelSettings::self()->writeConfig();
59
60 if (m_controller) {
61 KItemListView* view = m_controller->view();
62 m_controller->setView(0);
63 delete view;
64 }
65 }
66
67 void FoldersPanel::setShowHiddenFiles(bool show)
68 {
69 FoldersPanelSettings::setHiddenFilesShown(show);
70 fileItemModel()->setShowHiddenFiles(show);
71 }
72
73 bool FoldersPanel::showHiddenFiles() const
74 {
75 return FoldersPanelSettings::hiddenFilesShown();
76 }
77
78 void FoldersPanel::setAutoScrolling(bool enable)
79 {
80 // TODO: Not supported yet in Dolphin 2.0
81 FoldersPanelSettings::setAutoScrolling(enable);
82 }
83
84 bool FoldersPanel::autoScrolling() const
85 {
86 return FoldersPanelSettings::autoScrolling();
87 }
88
89 void FoldersPanel::rename(const KFileItem& item)
90 {
91 const int index = fileItemModel()->index(item);
92 m_controller->view()->editRole(index, "name");
93 }
94
95 bool FoldersPanel::urlChanged()
96 {
97 if (!url().isValid() || url().protocol().contains("search")) {
98 // Skip results shown by a search, as possible identical
99 // directory names are useless without parent-path information.
100 return false;
101 }
102
103 if (m_controller) {
104 loadTree(url());
105 }
106
107 return true;
108 }
109
110 void FoldersPanel::showEvent(QShowEvent* event)
111 {
112 if (event->spontaneous()) {
113 Panel::showEvent(event);
114 return;
115 }
116
117 if (!m_controller) {
118 // Postpone the creating of the dir lister to the first show event.
119 // This assures that no performance and memory overhead is given when the TreeView is not
120 // used at all (see FoldersPanel::setUrl()).
121 KFileItemListView* view = new KFileItemListView();
122 view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
123
124 KItemListStyleOption styleOption = view->styleOption();
125 styleOption.padding = 2;
126 styleOption.iconSize = KIconLoader::SizeSmall;
127 styleOption.extendedSelectionRegion = true;
128 view->setStyleOption(styleOption);
129
130 const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height());
131 view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.padding));
132 view->setItemLayout(KFileItemListView::DetailsLayout);
133 view->setSupportsItemExpanding(true);
134 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
135 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
136 // opening the folders panel.
137 view->setOpacity(0);
138
139 connect(view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
140 this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
141
142 KFileItemModel* model = new KFileItemModel(this);
143 model->setShowDirectoriesOnly(true);
144 model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
145 // Use a QueuedConnection to give the view the possibility to react first on the
146 // finished loading.
147 connect(model, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
148
149 KItemListContainer* container = new KItemListContainer(this);
150 m_controller = container->controller();
151 m_controller->setView(view);
152 m_controller->setModel(model);
153 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
154 m_controller->setAutoActivationDelay(750);
155 m_controller->setSingleClickActivation(true);
156
157 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
158 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
159 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
160 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
161 connect(m_controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
162
163 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
164 // to make the background transparent.
165 container->setFrameShape(QFrame::NoFrame);
166 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(container->viewport());
167 if (graphicsView) {
168 // Make the background of the container transparent and apply the window-text color
169 // to the text color, so that enough contrast is given for all color
170 // schemes
171 QPalette p = graphicsView->palette();
172 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
173 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
174 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
175 graphicsView->setPalette(p);
176 graphicsView->viewport()->setAutoFillBackground(false);
177 }
178
179 QVBoxLayout* layout = new QVBoxLayout(this);
180 layout->setMargin(0);
181 layout->addWidget(container);
182 }
183
184 loadTree(url());
185 Panel::showEvent(event);
186 }
187
188 void FoldersPanel::keyPressEvent(QKeyEvent* event)
189 {
190 const int key = event->key();
191 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
192 event->accept();
193 } else {
194 Panel::keyPressEvent(event);
195 }
196 }
197
198 void FoldersPanel::slotItemActivated(int index)
199 {
200 const KFileItem item = fileItemModel()->fileItem(index);
201 if (!item.isNull()) {
202 emit changeUrl(item.url(), Qt::LeftButton);
203 }
204 }
205
206 void FoldersPanel::slotItemMiddleClicked(int index)
207 {
208 const KFileItem item = fileItemModel()->fileItem(index);
209 if (!item.isNull()) {
210 emit changeUrl(item.url(), Qt::MiddleButton);
211 }
212 }
213
214 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
215 {
216 Q_UNUSED(pos);
217
218 const KFileItem fileItem = fileItemModel()->fileItem(index);
219
220 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
221 contextMenu.data()->open();
222 if (contextMenu.data()) {
223 delete contextMenu.data();
224 }
225 }
226
227 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
228 {
229 Q_UNUSED(pos);
230
231 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
232 contextMenu.data()->open();
233 if (contextMenu.data()) {
234 delete contextMenu.data();
235 }
236 }
237
238 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
239 {
240 if (index >= 0) {
241 KFileItem destItem = fileItemModel()->fileItem(index);
242 if (destItem.isNull()) {
243 return;
244 }
245
246 QDropEvent dropEvent(event->pos().toPoint(),
247 event->possibleActions(),
248 event->mimeData(),
249 event->buttons(),
250 event->modifiers());
251
252 DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent);
253 }
254 }
255
256 void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
257 {
258 if (role == "name") {
259 const KFileItem item = fileItemModel()->fileItem(index);
260 const QString newName = value.toString();
261 if (!newName.isEmpty() && newName != item.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
262 KonqOperations::rename(this, item.url(), newName);
263 }
264 }
265 }
266
267 void FoldersPanel::slotLoadingCompleted()
268 {
269 if (m_controller->view()->opacity() == 0) {
270 // The loading of the initial tree after opening the Folders panel
271 // has been finished. Trigger the increasing of the opacity after
272 // a short delay to give the view the chance to finish its internal
273 // animations.
274 // TODO: Check whether it makes sense to allow accessing the
275 // view-internal delay for usecases like this.
276 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
277 }
278
279 if (!m_updateCurrentItem) {
280 return;
281 }
282
283 const int index = fileItemModel()->index(url());
284 updateCurrentItem(index);
285 m_updateCurrentItem = false;
286 }
287
288 void FoldersPanel::startFadeInAnimation()
289 {
290 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
291 anim->setStartValue(0);
292 anim->setEndValue(1);
293 anim->setEasingCurve(QEasingCurve::InOutQuad);
294 anim->start(QAbstractAnimation::DeleteWhenStopped);
295 anim->setDuration(200);
296 }
297
298 void FoldersPanel::loadTree(const KUrl& url)
299 {
300 Q_ASSERT(m_controller);
301
302 m_updateCurrentItem = false;
303
304 KUrl baseUrl;
305 if (url.isLocalFile()) {
306 // Use the root directory as base for local URLs (#150941)
307 baseUrl = QDir::rootPath();
308 } else {
309 // Clear the path for non-local URLs and use it as base
310 baseUrl = url;
311 baseUrl.setPath(QString('/'));
312 }
313
314 KFileItemModel* model = fileItemModel();
315 if (model->directory() != baseUrl) {
316 m_updateCurrentItem = true;
317 model->refreshDirectory(baseUrl);
318 }
319
320 const int index = model->index(url);
321 if (index >= 0) {
322 updateCurrentItem(index);
323 } else {
324 m_updateCurrentItem = true;
325 model->expandParentDirectories(url);
326 // slotLoadingCompleted() will be invoked after the model has
327 // expanded the url
328 }
329 }
330
331 void FoldersPanel::updateCurrentItem(int index)
332 {
333 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
334 selectionManager->setCurrentItem(index);
335 selectionManager->clearSelection();
336 selectionManager->setSelected(index);
337
338 m_controller->view()->scrollToItem(index);
339 }
340
341 KFileItemModel* FoldersPanel::fileItemModel() const
342 {
343 return static_cast<KFileItemModel*>(m_controller->model());
344 }
345
346 #include "folderspanel.moc"