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