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