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