]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Added missing icons to panel places items
[dolphin.git] / src / panels / places / placespanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesView from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
23
24 #include "placespanel.h"
25
26 #include "dolphin_generalsettings.h"
27 #include "global.h"
28 #include "kitemviews/kitemlistcontainer.h"
29 #include "kitemviews/kitemlistcontroller.h"
30 #include "kitemviews/kitemlistselectionmanager.h"
31 #include "kitemviews/kstandarditem.h"
32 #include "placesitem.h"
33 #include "placesitemeditdialog.h"
34 #include "placesitemlistgroupheader.h"
35 #include "placesitemlistwidget.h"
36 #include "placesitemmodel.h"
37 #include "placesview.h"
38 #include "trash/dolphintrash.h"
39 #include "views/draganddrophelper.h"
40
41 #include <KFilePlacesModel>
42 #include <KIO/DropJob>
43 #include <KIO/EmptyTrashJob>
44 #include <KIO/Job>
45 #include <KIconLoader>
46 #include <KLocalizedString>
47
48 #include <QGraphicsSceneDragDropEvent>
49 #include <QIcon>
50 #include <QMenu>
51 #include <QMimeData>
52 #include <QVBoxLayout>
53
54 PlacesPanel::PlacesPanel(QWidget* parent) :
55 Panel(parent),
56 m_controller(nullptr),
57 m_model(nullptr),
58 m_view(nullptr),
59 m_storageSetupFailedUrl(),
60 m_triggerStorageSetupButton(),
61 m_itemDropEventIndex(-1),
62 m_itemDropEventMimeData(nullptr),
63 m_itemDropEvent(nullptr)
64 {
65 }
66
67 PlacesPanel::~PlacesPanel()
68 {
69 }
70
71 void PlacesPanel::proceedWithTearDown()
72 {
73 m_model->proceedWithTearDown();
74 }
75
76 bool PlacesPanel::urlChanged()
77 {
78 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
79 // Skip results shown by a search, as possible identical
80 // directory names are useless without parent-path information.
81 return false;
82 }
83
84 if (m_controller) {
85 selectClosestItem();
86 }
87
88 return true;
89 }
90
91 void PlacesPanel::readSettings()
92 {
93 if (m_controller) {
94 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
95 m_controller->setAutoActivationDelay(delay);
96 }
97 }
98
99 void PlacesPanel::showEvent(QShowEvent* event)
100 {
101 if (event->spontaneous()) {
102 Panel::showEvent(event);
103 return;
104 }
105
106 if (!m_controller) {
107 // Postpone the creating of the controller to the first show event.
108 // This assures that no performance and memory overhead is given when the folders panel is not
109 // used at all and stays invisible.
110 m_model = new PlacesItemModel(this);
111 m_model->setGroupedSorting(true);
112 connect(m_model, &PlacesItemModel::errorMessage,
113 this, &PlacesPanel::errorMessage);
114 connect(m_model, &PlacesItemModel::storageTearDownRequested,
115 this, &PlacesPanel::storageTearDownRequested);
116 connect(m_model, &PlacesItemModel::storageTearDownExternallyRequested,
117 this, &PlacesPanel::storageTearDownExternallyRequested);
118
119 m_view = new PlacesView();
120 m_view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
121 m_view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
122
123 m_controller = new KItemListController(m_model, m_view, this);
124 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
125 m_controller->setSingleClickActivationEnforced(true);
126
127 readSettings();
128
129 connect(m_controller, &KItemListController::itemActivated, this, &PlacesPanel::slotItemActivated);
130 connect(m_controller, &KItemListController::itemMiddleClicked, this, &PlacesPanel::slotItemMiddleClicked);
131 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &PlacesPanel::slotItemContextMenuRequested);
132 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &PlacesPanel::slotViewContextMenuRequested);
133 connect(m_controller, &KItemListController::itemDropEvent, this, &PlacesPanel::slotItemDropEvent);
134 connect(m_controller, &KItemListController::aboveItemDropEvent, this, &PlacesPanel::slotAboveItemDropEvent);
135
136 KItemListContainer* container = new KItemListContainer(m_controller, this);
137 container->setEnabledFrame(false);
138
139 QVBoxLayout* layout = new QVBoxLayout(this);
140 layout->setMargin(0);
141 layout->addWidget(container);
142
143 selectClosestItem();
144 }
145
146 Panel::showEvent(event);
147 }
148
149 void PlacesPanel::slotItemActivated(int index)
150 {
151 triggerItem(index, Qt::LeftButton);
152 }
153
154 void PlacesPanel::slotItemMiddleClicked(int index)
155 {
156 triggerItem(index, Qt::MiddleButton);
157 }
158
159 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
160 {
161 PlacesItem* item = m_model->placesItem(index);
162 if (!item) {
163 return;
164 }
165
166 QMenu menu(this);
167
168 QAction* emptyTrashAction = nullptr;
169 QAction* editAction = nullptr;
170 QAction* teardownAction = nullptr;
171 QAction* ejectAction = nullptr;
172 QAction* mountAction = nullptr;
173
174 const bool isDevice = !item->udi().isEmpty();
175 const bool isTrash = (item->url().scheme() == QLatin1String("trash"));
176 if (isDevice) {
177 ejectAction = m_model->ejectAction(index);
178 if (ejectAction) {
179 ejectAction->setParent(&menu);
180 menu.addAction(ejectAction);
181 }
182
183 teardownAction = m_model->teardownAction(index);
184 if (teardownAction) {
185 teardownAction->setParent(&menu);
186 menu.addAction(teardownAction);
187 }
188
189 if (item->storageSetupNeeded()) {
190 mountAction = menu.addAction(QIcon::fromTheme(QStringLiteral("media-mount")), i18nc("@action:inmenu", "Mount"));
191 }
192
193 if (teardownAction || ejectAction || mountAction) {
194 menu.addSeparator();
195 }
196 } else {
197 if (isTrash) {
198 emptyTrashAction = menu.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
199 emptyTrashAction->setEnabled(item->icon() == QLatin1String("user-trash-full"));
200 menu.addSeparator();
201 }
202 }
203
204 QAction* openInNewWindowAction = menu.addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@item:inmenu", "Open in New Window"));
205 QAction* openInNewTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@item:inmenu", "Open in New Tab"));
206 if (!isDevice && !isTrash) {
207 menu.addSeparator();
208 }
209
210 if (!isDevice) {
211 editAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-entry")), i18nc("@item:inmenu", "Edit..."));
212 }
213
214 QAction* removeAction = nullptr;
215 if (!isDevice && !item->isSystemItem()) {
216 removeAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
217 }
218
219 QAction* hideAction = menu.addAction(QIcon::fromTheme(QStringLiteral("hint")), i18nc("@item:inmenu", "Hide"));
220 hideAction->setCheckable(true);
221 hideAction->setChecked(item->isHidden());
222
223 buildGroupContextMenu(&menu, index);
224
225 QAction* action = menu.exec(pos.toPoint());
226 if (action) {
227 if (action == emptyTrashAction) {
228 Trash::empty(this);
229 } else {
230 // The index might have changed if devices were added/removed while
231 // the context menu was open.
232 index = m_model->index(item);
233 if (index < 0) {
234 // The item is not in the model any more, probably because it was an
235 // external device that has been removed while the context menu was open.
236 return;
237 }
238
239 if (action == editAction) {
240 editEntry(index);
241 } else if (action == removeAction) {
242 m_model->deleteItem(index);
243 } else if (action == hideAction) {
244 item->setHidden(hideAction->isChecked());
245 } else if (action == openInNewWindowAction) {
246 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model->data(index).value("url").toUrl())}, this);
247 } else if (action == openInNewTabAction) {
248 // TriggerItem does set up the storage first and then it will
249 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
250 triggerItem(index, Qt::MiddleButton);
251 } else if (action == mountAction) {
252 m_model->requestStorageSetup(index);
253 } else if (action == teardownAction) {
254 m_model->requestTearDown(index);
255 } else if (action == ejectAction) {
256 m_model->requestEject(index);
257 }
258 }
259 }
260
261 selectClosestItem();
262 }
263
264 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
265 {
266 QMenu menu(this);
267
268 QAction* addAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
269
270 QAction* showAllAction = nullptr;
271 if (m_model->hiddenCount() > 0) {
272 showAllAction = menu.addAction(QIcon::fromTheme(QStringLiteral("visibility")), i18nc("@item:inmenu", "Show All Entries"));
273 showAllAction->setCheckable(true);
274 showAllAction->setChecked(m_model->hiddenItemsShown());
275 }
276
277 buildGroupContextMenu(&menu, m_controller->indexCloseToMousePressedPosition());
278
279 QMenu* iconSizeSubMenu = new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu);
280
281 struct IconSizeInfo
282 {
283 int size;
284 const char* context;
285 const char* text;
286 };
287
288 const int iconSizeCount = 4;
289 static const IconSizeInfo iconSizes[iconSizeCount] = {
290 {KIconLoader::SizeSmall, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
291 {KIconLoader::SizeSmallMedium, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
292 {KIconLoader::SizeMedium, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
293 {KIconLoader::SizeLarge, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
294 };
295
296 QHash<QAction*, int> iconSizeActionMap;
297 QActionGroup* iconSizeGroup = new QActionGroup(iconSizeSubMenu);
298
299 for (int i = 0; i < iconSizeCount; ++i) {
300 const int size = iconSizes[i].size;
301 const QString text = i18nc(iconSizes[i].context, iconSizes[i].text,
302 size, size);
303
304 QAction* action = iconSizeSubMenu->addAction(text);
305 iconSizeActionMap.insert(action, size);
306 action->setActionGroup(iconSizeGroup);
307 action->setCheckable(true);
308 action->setChecked(m_view->iconSize() == size);
309 }
310
311 menu.addMenu(iconSizeSubMenu);
312
313 menu.addSeparator();
314 foreach (QAction* action, customContextMenuActions()) {
315 menu.addAction(action);
316 }
317
318 QAction* action = menu.exec(pos.toPoint());
319 if (action) {
320 if (action == addAction) {
321 addEntry();
322 } else if (action == showAllAction) {
323 m_model->setHiddenItemsShown(showAllAction->isChecked());
324 } else if (iconSizeActionMap.contains(action)) {
325 m_view->setIconSize(iconSizeActionMap.value(action));
326 }
327 }
328
329 selectClosestItem();
330 }
331
332 QAction *PlacesPanel::buildGroupContextMenu(QMenu *menu, int index)
333 {
334 if (index == -1) {
335 return nullptr;
336 }
337
338 KFilePlacesModel::GroupType groupType = m_model->groupType(index);
339 QAction *hideGroupAction = menu->addAction(QIcon::fromTheme(QStringLiteral("hint")), i18nc("@item:inmenu", "Hide Section '%1'", m_model->item(index)->group()));
340 hideGroupAction->setCheckable(true);
341 hideGroupAction->setChecked(m_model->isGroupHidden(groupType));
342
343 connect(hideGroupAction, &QAction::triggered, this, [this, groupType, hideGroupAction]{
344 m_model->setGroupHidden(groupType, hideGroupAction->isChecked());
345 });
346
347 return hideGroupAction;
348 }
349
350 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
351 {
352 if (index < 0) {
353 return;
354 }
355
356 const PlacesItem* destItem = m_model->placesItem(index);
357
358 if (destItem->isSearchOrTimelineUrl()) {
359 return;
360 }
361
362 if (m_model->storageSetupNeeded(index)) {
363 connect(m_model, &PlacesItemModel::storageSetupDone,
364 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
365
366 m_itemDropEventIndex = index;
367
368 // Make a full copy of the Mime-Data
369 m_itemDropEventMimeData = new QMimeData;
370 m_itemDropEventMimeData->setText(event->mimeData()->text());
371 m_itemDropEventMimeData->setHtml(event->mimeData()->html());
372 m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
373 m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
374 m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
375
376 m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
377 event->possibleActions(),
378 m_itemDropEventMimeData,
379 event->buttons(),
380 event->modifiers());
381
382 m_model->requestStorageSetup(index);
383 return;
384 }
385
386 QUrl destUrl = destItem->url();
387 QDropEvent dropEvent(event->pos().toPoint(),
388 event->possibleActions(),
389 event->mimeData(),
390 event->buttons(),
391 event->modifiers());
392
393 slotUrlsDropped(destUrl, &dropEvent, this);
394 }
395
396 void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
397 {
398 disconnect(m_model, &PlacesItemModel::storageSetupDone,
399 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
400
401 if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
402 if (success) {
403 QUrl destUrl = m_model->placesItem(index)->url();
404 slotUrlsDropped(destUrl, m_itemDropEvent, this);
405 }
406
407 delete m_itemDropEventMimeData;
408 delete m_itemDropEvent;
409
410 m_itemDropEventIndex = -1;
411 m_itemDropEventMimeData = nullptr;
412 m_itemDropEvent = nullptr;
413 }
414 }
415
416 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
417 {
418 m_model->dropMimeDataBefore(index, event->mimeData());
419 }
420
421 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
422 {
423 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
424 if (job) {
425 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
426 }
427 }
428
429 void PlacesPanel::slotStorageSetupDone(int index, bool success)
430 {
431 disconnect(m_model, &PlacesItemModel::storageSetupDone,
432 this, &PlacesPanel::slotStorageSetupDone);
433
434 if (m_triggerStorageSetupButton == Qt::NoButton) {
435 return;
436 }
437
438 if (success) {
439 Q_ASSERT(!m_model->storageSetupNeeded(index));
440 triggerItem(index, m_triggerStorageSetupButton);
441 m_triggerStorageSetupButton = Qt::NoButton;
442 } else {
443 setUrl(m_storageSetupFailedUrl);
444 m_storageSetupFailedUrl = QUrl();
445 }
446 }
447
448 void PlacesPanel::addEntry()
449 {
450 const int index = m_controller->selectionManager()->currentItem();
451 const QUrl url = m_model->data(index).value("url").toUrl();
452
453 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
454 dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
455 dialog->setAllowGlobal(true);
456 dialog->setUrl(url);
457 if (dialog->exec() == QDialog::Accepted) {
458 m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
459 }
460
461 delete dialog;
462 }
463
464 void PlacesPanel::editEntry(int index)
465 {
466 QHash<QByteArray, QVariant> data = m_model->data(index);
467
468 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
469 dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
470 dialog->setIcon(data.value("iconName").toString());
471 dialog->setText(data.value("text").toString());
472 dialog->setUrl(data.value("url").toUrl());
473 dialog->setAllowGlobal(true);
474 if (dialog->exec() == QDialog::Accepted) {
475 PlacesItem* oldItem = m_model->placesItem(index);
476 if (oldItem) {
477 oldItem->setText(dialog->text());
478 oldItem->setUrl(dialog->url());
479 oldItem->setIcon(dialog->icon());
480 m_model->refresh();
481 }
482 }
483
484 delete dialog;
485 }
486
487 void PlacesPanel::selectClosestItem()
488 {
489 const int index = m_model->closestItem(url());
490 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
491 selectionManager->setCurrentItem(index);
492 selectionManager->clearSelection();
493 selectionManager->setSelected(index);
494 }
495
496 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
497 {
498 const PlacesItem* item = m_model->placesItem(index);
499 if (!item) {
500 return;
501 }
502
503 if (m_model->storageSetupNeeded(index)) {
504 m_triggerStorageSetupButton = button;
505 m_storageSetupFailedUrl = url();
506
507 connect(m_model, &PlacesItemModel::storageSetupDone,
508 this, &PlacesPanel::slotStorageSetupDone);
509
510 m_model->requestStorageSetup(index);
511 } else {
512 m_triggerStorageSetupButton = Qt::NoButton;
513
514 const QUrl url = m_model->data(index).value("url").toUrl();
515 if (!url.isEmpty()) {
516 if (button == Qt::MiddleButton) {
517 emit placeMiddleClicked(KFilePlacesModel::convertedUrl(url));
518 } else {
519 emit placeActivated(KFilePlacesModel::convertedUrl(url));
520 }
521 }
522 }
523 }