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