dolphinviewactionhandler.cpp 30.3 KB
Newer Older
1
2
3
4
5
6
/*
 * SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
7
8
9

#include "dolphinviewactionhandler.h"

Roman Inflianskas's avatar
Roman Inflianskas committed
10
11
#include "dolphindebug.h"
#include "kitemviews/kfileitemmodel.h"
12
#include "settings/viewpropertiesdialog.h"
13
#include "views/zoomlevelinfo.h"
14
#include "kconfig_version.h"
15

Roman Inflianskas's avatar
Roman Inflianskas committed
16
17
18
#ifdef HAVE_BALOO
#include <Baloo/IndexerConfig>
#endif
Peter Penz's avatar
Peter Penz committed
19
20
#include <KActionCollection>
#include <KActionMenu>
Laurent Montel's avatar
Laurent Montel committed
21
#include <KLocalizedString>
22
#include <KNewFileMenu>
Peter Penz's avatar
Peter Penz committed
23
#include <KPropertiesDialog>
24
#include <KProtocolManager>
Nicolas Fella's avatar
Nicolas Fella committed
25
26

#include <QActionGroup>
Roman Inflianskas's avatar
Roman Inflianskas committed
27
28
#include <QMenu>
#include <QPointer>
Vishesh Handa's avatar
Vishesh Handa committed
29

30
31
32
DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent) :
    QObject(parent),
    m_actionCollection(collection),
Kevin Funk's avatar
Kevin Funk committed
33
    m_currentView(nullptr),
34
35
    m_sortByActions(),
    m_visibleRoles()
36
37
38
39
40
41
42
43
44
{
    Q_ASSERT(m_actionCollection);
    createActions();
}

void DolphinViewActionHandler::setCurrentView(DolphinView* view)
{
    Q_ASSERT(view);

45
    if (m_currentView) {
Kevin Funk's avatar
Kevin Funk committed
46
        disconnect(m_currentView, nullptr, this, nullptr);
47
    }
48
49
50

    m_currentView = view;

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    connect(view, &DolphinView::modeChanged,
            this, &DolphinViewActionHandler::updateViewActions);
    connect(view, &DolphinView::previewsShownChanged,
            this, &DolphinViewActionHandler::slotPreviewsShownChanged);
    connect(view, &DolphinView::sortOrderChanged,
            this, &DolphinViewActionHandler::slotSortOrderChanged);
    connect(view, &DolphinView::sortFoldersFirstChanged,
            this, &DolphinViewActionHandler::slotSortFoldersFirstChanged);
    connect(view, &DolphinView::visibleRolesChanged,
            this, &DolphinViewActionHandler::slotVisibleRolesChanged);
    connect(view, &DolphinView::groupedSortingChanged,
            this, &DolphinViewActionHandler::slotGroupedSortingChanged);
    connect(view, &DolphinView::hiddenFilesShownChanged,
            this, &DolphinViewActionHandler::slotHiddenFilesShownChanged);
    connect(view, &DolphinView::sortRoleChanged,
            this, &DolphinViewActionHandler::slotSortRoleChanged);
    connect(view, &DolphinView::zoomLevelChanged,
            this, &DolphinViewActionHandler::slotZoomLevelChanged);
    connect(view, &DolphinView::writeStateChanged,
            this, &DolphinViewActionHandler::slotWriteStateChanged);
71
72
}

Rahman Duran's avatar
Rahman Duran committed
73
74
75
76
77
DolphinView* DolphinViewActionHandler::currentView()
{
    return m_currentView;
}

78
79
80
void DolphinViewActionHandler::createActions()
{
    // This action doesn't appear in the GUI, it's for the shortcut only.
81
    // KNewFileMenu takes care of the GUI stuff.
82
    QAction* newDirAction = m_actionCollection->addAction(QStringLiteral("create_dir"));
83
    newDirAction->setText(i18nc("@action", "Create Folder..."));
84
85
    m_actionCollection->setDefaultShortcuts(newDirAction, KStandardShortcut::createFolder());
    newDirAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
86
    newDirAction->setEnabled(false);    // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
87
    connect(newDirAction, &QAction::triggered, this, &DolphinViewActionHandler::createDirectoryTriggered);
88

89
    // File menu
90

91
92
93
94
    auto renameAction = KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename, m_actionCollection);
    renameAction->setWhatsThis(xi18nc("@info:whatsthis", "This renames the "
        "items in your current selection.<nl/>Renaming multiple items "
        "at once amounts to their new names differing only in a number."));
95

96
97
98
99
100
101
    auto trashAction = KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated, m_actionCollection);
    auto trashShortcuts = trashAction->shortcuts();
    if (!trashShortcuts.contains(QKeySequence::Delete)) {
        trashShortcuts.append(QKeySequence::Delete);
        m_actionCollection->setDefaultShortcuts(trashAction, trashShortcuts);
    }
102
103
104
105
    trashAction->setWhatsThis(xi18nc("@info:whatsthis", "This moves the "
        "items in your current selection to the <filename>Trash"
        "</filename>.<nl/>The trash is a temporary storage where "
        "items can be deleted from if disk space is needed."));
106

107
108
109
110
111
112
    auto deleteAction = KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems, m_actionCollection);
    auto deleteShortcuts = deleteAction->shortcuts();
    if (!deleteShortcuts.contains(Qt::SHIFT | Qt::Key_Delete)) {
        deleteShortcuts.append(Qt::SHIFT | Qt::Key_Delete);
        m_actionCollection->setDefaultShortcuts(deleteAction, deleteShortcuts);
    }
113
114
115
    deleteAction->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
        "the items in your current selection completely. They can "
        "not be recovered by normal means."));
116

117
    // This action is useful for being enabled when KStandardAction::MoveToTrash should be
118
    // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
119
120
    // can be used for deleting the file (#76016). It needs to be a separate action
    // so that the Edit menu isn't affected.
121
    QAction* deleteWithTrashShortcut = m_actionCollection->addAction(QStringLiteral("delete_shortcut"));
122
    // The descriptive text is just for the shortcuts editor.
123
    deleteWithTrashShortcut->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
124
    m_actionCollection->setDefaultShortcuts(deleteWithTrashShortcut, KStandardShortcut::moveToTrash());
125
    deleteWithTrashShortcut->setEnabled(false);
126
    connect(deleteWithTrashShortcut, &QAction::triggered, this, &DolphinViewActionHandler::slotDeleteItems);
127

Nate Graham's avatar
Nate Graham committed
128
129
130
131
132
133
134
    QAction* duplicateAction = m_actionCollection->addAction(QStringLiteral("duplicate"));
    duplicateAction->setText(i18nc("@action:inmenu File", "Duplicate Here"));
    duplicateAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
    m_actionCollection->setDefaultShortcut(duplicateAction, Qt::CTRL | Qt::Key_D);
    duplicateAction->setEnabled(false);
    connect(duplicateAction, &QAction::triggered, this, &DolphinViewActionHandler::slotDuplicate);

135
    QAction *propertiesAction = m_actionCollection->addAction( QStringLiteral("properties") );
136
    // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
137
    propertiesAction->setText( i18nc("@action:inmenu File", "Properties") );
138
139
140
141
142
143
    propertiesAction->setWhatsThis(xi18nc("@info:whatsthis properties",
        "This shows a complete list of properties of the currently "
        "selected items in a new window.<nl/>If nothing is selected the "
        "window will be about the currently viewed folder instead.<nl/>"
        "You can configure advanced options there like managing "
        "read- and write-permissions."));
144
    propertiesAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
145
    m_actionCollection->setDefaultShortcuts(propertiesAction, {Qt::ALT | Qt::Key_Return, Qt::ALT | Qt::Key_Enter});
146
    connect(propertiesAction, &QAction::triggered, this, &DolphinViewActionHandler::slotProperties);
147

Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
148
    QAction *copyPathAction = m_actionCollection->addAction( QStringLiteral("copy_location") );
Nikita Churaev's avatar
Nikita Churaev committed
149
    copyPathAction->setText(i18nc("@action:incontextmenu", "Copy Location"));
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
150
151
152
153
154
    copyPathAction->setWhatsThis(i18nc("@info:whatsthis copy_location",
                                          "This will copy the path of the first selected item into the clipboard."
                                ));

    copyPathAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
155
    m_actionCollection->setDefaultShortcuts(copyPathAction, {Qt::CTRL | Qt::ALT | Qt::Key_C});
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
156
157
158
    connect(copyPathAction, &QAction::triggered, this, &DolphinViewActionHandler::slotCopyPath);


159
    // View menu
160
    KToggleAction* iconsAction = iconsModeAction();
161
    KToggleAction* compactAction = compactModeAction();
162
163
    KToggleAction* detailsAction = detailsModeAction();

164
165
166
167
168
169
170
171
172
    iconsAction->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
        "<para>This switches to a view mode that focuses on the folder "
        "and file icons. This mode makes it easy to distinguish folders "
        "from files and to detect items with distinctive <emphasis>"
        "file types</emphasis>.</para><para> This mode is handy to "
        "browse through pictures when the <interface>Preview"
        "</interface> option is enabled.</para>"));
    compactAction->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
        "<para>This switches to a compact view mode that lists the folders "
173
        "and files in columns with the names beside the icons.</para><para>"
174
175
176
177
178
179
180
181
182
183
184
        "This helps to keep the overview in folders with many items.</para>"));
    detailsAction->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
        "<para>This switches to a list view mode that focuses on folder "
        "and file details.</para><para>Click on a detail in the column "
        "header to sort the items by it. Click again to sort the other "
        "way around. To select which details should be displayed click "
        "the header with the right mouse button.</para><para>You can "
        "view the contents of a folder without leaving the current "
        "location by clicking to the left of it. This way you can view "
        "the contents of multiple folders in the same list.</para>"));

185
    KSelectAction* viewModeActions = m_actionCollection->add<KSelectAction>(QStringLiteral("view_mode"));
186
187
    viewModeActions->setText(i18nc("@action:intoolbar", "View Mode"));
    viewModeActions->addAction(iconsAction);
188
    viewModeActions->addAction(compactAction);
189
190
    viewModeActions->addAction(detailsAction);
    viewModeActions->setToolBarMode(KSelectAction::MenuMode);
191
    connect(viewModeActions, QOverload<QAction*>::of(&KSelectAction::triggered), this, &DolphinViewActionHandler::slotViewModeActionTriggered);
192

193
    QAction* zoomInAction = KStandardAction::zoomIn(this,
194
                            &DolphinViewActionHandler::zoomIn,
195
                            m_actionCollection);
196
    zoomInAction->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
197

198
199
200
201
202
    QAction* zoomResetAction = m_actionCollection->addAction(QStringLiteral("view_zoom_reset"));
    zoomResetAction->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
    zoomResetAction->setToolTip(i18n("Zoom To Default"));
    zoomResetAction->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
    zoomResetAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
203
    m_actionCollection->setDefaultShortcuts(zoomResetAction, {Qt::CTRL | Qt::Key_0});
204
205
    connect(zoomResetAction, &QAction::triggered, this, &DolphinViewActionHandler::zoomReset);

206
    QAction* zoomOutAction = KStandardAction::zoomOut(this,
207
                             &DolphinViewActionHandler::zoomOut,
208
                             m_actionCollection);
209
    zoomOutAction->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
210

211
    KToggleAction* showPreview = m_actionCollection->add<KToggleAction>(QStringLiteral("show_preview"));
212
    showPreview->setText(i18nc("@action:intoolbar", "Show Previews"));
213
    showPreview->setToolTip(i18nc("@info", "Show preview of files and folders"));
214
215
216
217
    showPreview->setWhatsThis(xi18nc("@info:whatsthis", "When this is "
        "enabled, the icons are based on the actual file or folder "
        "contents.<nl/>For example the icons of images become scaled "
        "down versions of the images."));
218
    showPreview->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
219
    connect(showPreview, &KToggleAction::triggered, this, &DolphinViewActionHandler::togglePreview);
220

221
    KToggleAction* sortFoldersFirst = m_actionCollection->add<KToggleAction>(QStringLiteral("folders_first"));
222
    sortFoldersFirst->setText(i18nc("@action:inmenu Sort", "Folders First"));
223
    connect(sortFoldersFirst, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortFoldersFirst);
224

225
    // View -> Sort By
226
    QActionGroup* sortByActionGroup = createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
227

228
    KActionMenu* sortByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("sort"));
229
    sortByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
230
231
    sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
    sortByActionMenu->setDelayed(false);
232

Alexander Lohnau's avatar
Alexander Lohnau committed
233
234
    const auto sortByActionGroupActions = sortByActionGroup->actions();
    for (QAction* action : sortByActionGroupActions) {
235
236
        sortByActionMenu->addAction(action);
    }
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    sortByActionMenu->addSeparator();

    QActionGroup* group = new QActionGroup(sortByActionMenu);
    group->setExclusive(true);

    KToggleAction* ascendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("ascending"));
    ascendingAction->setActionGroup(group);
    connect(ascendingAction, &QAction::triggered, this, [this] {
        m_currentView->setSortOrder(Qt::AscendingOrder);
    });

    KToggleAction* descendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("descending"));
    descendingAction->setActionGroup(group);
    connect(descendingAction, &QAction::triggered, this, [this] {
        m_currentView->setSortOrder(Qt::DescendingOrder);
    });

    sortByActionMenu->addAction(ascendingAction);
    sortByActionMenu->addAction(descendingAction);
257
258
259
260
    sortByActionMenu->addSeparator();
    sortByActionMenu->addAction(sortFoldersFirst);

    // View -> Additional Information
261
    QActionGroup* visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
262

263
    KActionMenu* visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
264
265
    visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
    visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
266
267
    visibleRolesMenu->setDelayed(false);

Alexander Lohnau's avatar
Alexander Lohnau committed
268
269
    const auto visibleRolesGroupActions = visibleRolesGroup->actions();
    for (QAction* action : visibleRolesGroupActions) {
270
        visibleRolesMenu->addAction(action);
271
    }
272

273
274
    KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
    showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
275
    showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
276
    showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
277
    connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
278

279
    KToggleAction* showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
280
    showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
281
    showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
282
283
284
285
286
287
    showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis", "<para>When "
        "this is enabled <emphasis>hidden</emphasis> files and folders "
        "are visible. They will be displayed semi-transparent.</para>"
        "<para>Hidden items only differ from other ones in that their "
        "name starts with a \".\". In general there is no need for "
        "users to access them which is why they are hidden.</para>"));
288
    m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
289
    connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
290

291
    QAction* adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
292
    adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
293
    adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
294
295
    adjustViewProps->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
        "in which all folder view properties can be adjusted."));
296
    connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
297
298
}

299
QActionGroup* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString& groupPrefix)
300
{
301
    const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
Antonio Prcela's avatar
Antonio Prcela committed
302
    Q_ASSERT(isSortGroup || groupPrefix == QLatin1String("show_"));
303
304
305
306

    QActionGroup* rolesActionGroup = new QActionGroup(m_actionCollection);
    rolesActionGroup->setExclusive(isSortGroup);
    if (isSortGroup) {
307
308
        connect(rolesActionGroup, &QActionGroup::triggered,
                this, &DolphinViewActionHandler::slotSortTriggered);
309
    } else {
310
311
        connect(rolesActionGroup, &QActionGroup::triggered,
                this, &DolphinViewActionHandler::toggleVisibleRole);
312
    }
313

314
    QString groupName;
Kevin Funk's avatar
Kevin Funk committed
315
316
    KActionMenu* groupMenu = nullptr;
    QActionGroup* groupMenuGroup = nullptr;
317

318
    bool indexingEnabled = false;
Vishesh Handa's avatar
Vishesh Handa committed
319
320
321
#ifdef HAVE_BALOO
    Baloo::IndexerConfig config;
    indexingEnabled = config.fileIndexingEnabled();
322
323
#endif

Peter Penz's avatar
Peter Penz committed
324
    const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
Alexander Lohnau's avatar
Alexander Lohnau committed
325
    for (const KFileItemModel::RoleInfo& info : rolesInfo) {
326
327
        if (!isSortGroup && info.role == "text") {
            // It should not be possible to hide the "text" role
328
329
330
            continue;
        }

Kevin Funk's avatar
Kevin Funk committed
331
        KToggleAction* action = nullptr;
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        const QString name = groupPrefix + info.role;
        if (info.group.isEmpty()) {
            action = m_actionCollection->add<KToggleAction>(name);
            action->setActionGroup(rolesActionGroup);
        } else {
            if (!groupMenu || info.group != groupName) {
                groupName = info.group;
                groupMenu = m_actionCollection->add<KActionMenu>(groupName);
                groupMenu->setText(groupName);
                groupMenu->setActionGroup(rolesActionGroup);

                groupMenuGroup = new QActionGroup(groupMenu);
                groupMenuGroup->setExclusive(isSortGroup);
                if (isSortGroup) {
346
347
                    connect(groupMenuGroup, &QActionGroup::triggered,
                            this, &DolphinViewActionHandler::slotSortTriggered);
348
                } else {
349
350
                    connect(groupMenuGroup, &QActionGroup::triggered,
                            this, &DolphinViewActionHandler::toggleVisibleRole);
351
352
353
354
355
356
357
                }
            }

            action = new KToggleAction(groupMenu);
            action->setActionGroup(groupMenuGroup);
            groupMenu->addAction(action);
        }
Peter Penz's avatar
Peter Penz committed
358
359
        action->setText(info.translation);
        action->setData(info.role);
360

Vishesh Handa's avatar
Vishesh Handa committed
361
362
        const bool enable = (!info.requiresBaloo && !info.requiresIndexer) ||
                            (info.requiresBaloo) ||
363
364
365
                            (info.requiresIndexer && indexingEnabled);
        action->setEnabled(enable);

366
367
368
369
370
        if (isSortGroup) {
            m_sortByActions.insert(info.role, action);
        } else {
            m_visibleRoles.insert(info.role, action);
        }
371
    }
372

373
    return rolesActionGroup;
374
375
}

376
377
378
379
void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
{
    const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
    m_currentView->setMode(mode);
380

381
    QAction* viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
382
    viewModeMenu->setIcon(action->icon());
383
384
}

385
386
void DolphinViewActionHandler::slotRename()
{
Alexander Lohnau's avatar
Alexander Lohnau committed
387
    Q_EMIT actionBeingHandled();
388
389
390
    m_currentView->renameSelectedItems();
}

391
void DolphinViewActionHandler::slotTrashActivated()
392
{
Alexander Lohnau's avatar
Alexander Lohnau committed
393
    Q_EMIT actionBeingHandled();
394
    m_currentView->trashSelectedItems();
395
396
397
398
}

void DolphinViewActionHandler::slotDeleteItems()
{
Alexander Lohnau's avatar
Alexander Lohnau committed
399
    Q_EMIT actionBeingHandled();
400
401
402
403
404
    m_currentView->deleteSelectedItems();
}

void DolphinViewActionHandler::togglePreview(bool show)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
405
    Q_EMIT actionBeingHandled();
406
    m_currentView->setPreviewsShown(show);
407
408
}

409
void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
410
{
411
    Q_UNUSED(shown)
412
    // It is not enough to update the 'Show Preview' action, also
413
    // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
414
415
416
    updateViewActions();
}

417
418
419
420
QString DolphinViewActionHandler::currentViewModeActionName() const
{
    switch (m_currentView->mode()) {
    case DolphinView::IconsView:
421
        return QStringLiteral("icons");
422
    case DolphinView::DetailsView:
423
        return QStringLiteral("details");
424
    case DolphinView::CompactView:
425
        return QStringLiteral("compact");
426
427
428
    default:
        Q_ASSERT(false);
        break;
429
430
431
432
    }
    return QString(); // can't happen
}

Rahman Duran's avatar
Rahman Duran committed
433
434
435
436
437
KActionCollection* DolphinViewActionHandler::actionCollection()
{
    return m_actionCollection;
}

438
439
void DolphinViewActionHandler::updateViewActions()
{
440
    QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
441
    if (viewModeAction) {
442
        viewModeAction->setChecked(true);
443

444
        QAction* viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
445
        viewModeMenu->setIcon(viewModeAction->icon());
446
447
    }

448
    QAction* showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
449
    showPreviewAction->setChecked(m_currentView->previewsShown());
450
451

    slotSortOrderChanged(m_currentView->sortOrder());
452
    slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
453
    slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
454
    slotGroupedSortingChanged(m_currentView->groupedSorting());
455
    slotSortRoleChanged(m_currentView->sortRole());
456
    slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
457

Roman Gilg's avatar
Roman Gilg committed
458
459
    // Updates the "show_hidden_files" action state and icon
    slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
460
461
462
463
}

void DolphinViewActionHandler::zoomIn()
{
464
465
    const int level = m_currentView->zoomLevel();
    m_currentView->setZoomLevel(level + 1);
466
467
468
469
470
    updateViewActions();
}

void DolphinViewActionHandler::zoomOut()
{
471
472
    const int level = m_currentView->zoomLevel();
    m_currentView->setZoomLevel(level - 1);
473
474
475
    updateViewActions();
}

476
477
478
479
480
481
void DolphinViewActionHandler::zoomReset()
{
    m_currentView->resetZoomLevel();
    updateViewActions();
}

482
483
void DolphinViewActionHandler::toggleSortFoldersFirst()
{
484
485
    const bool sortFirst = m_currentView->sortFoldersFirst();
    m_currentView->setSortFoldersFirst(!sortFirst);
486
487
}

488
489
void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
{
490
    QAction* descending = m_actionCollection->action(QStringLiteral("descending"));
491
    QAction* ascending = m_actionCollection->action(QStringLiteral("ascending"));
492
493
    const bool sortDescending = (order == Qt::DescendingOrder);
    descending->setChecked(sortDescending);
494
    ascending->setChecked(!sortDescending);
495
496
}

497
498
void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
{
499
    m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
500
501
}

502
void DolphinViewActionHandler::toggleVisibleRole(QAction* action)
503
{
Alexander Lohnau's avatar
Alexander Lohnau committed
504
    Q_EMIT actionBeingHandled();
505

506
    const QByteArray toggledRole = action->data().toByteArray();
507

508
    QList<QByteArray> roles = m_currentView->visibleRoles();
509
510
511

    const bool show = action->isChecked();

512
    const int index = roles.indexOf(toggledRole);
513
514
    const bool containsInfo = (index >= 0);
    if (show && !containsInfo) {
515
516
        roles.append(toggledRole);
        m_currentView->setVisibleRoles(roles);
517
    } else if (!show && containsInfo) {
518
519
520
        roles.removeAt(index);
        m_currentView->setVisibleRoles(roles);
        Q_ASSERT(roles.indexOf(toggledRole) < 0);
521
    }
522
523
}

524
525
void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray>& current,
                                                       const QList<QByteArray>& previous)
526
{
527
    Q_UNUSED(previous)
528

529
    const auto checkedRoles = QSet<QByteArray>(current.constBegin(), current.constEnd());
530
531
532
533
534
535
    QHashIterator<QByteArray, KToggleAction*> it(m_visibleRoles);
    while (it.hasNext()) {
        it.next();
        const QByteArray& role = it.key();
        KToggleAction* action = it.value();
        action->setChecked(checkedRoles.contains(role));
536
    }
537
538
}

539
void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
540
{
541
    m_currentView->setGroupedSorting(grouped);
542
543
}

544
void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
545
{
546
    QAction* showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
547
    showInGroupsAction->setChecked(groupedSorting);
548
549
550
551
}

void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
552
    Q_EMIT actionBeingHandled();
553
    m_currentView->setHiddenFilesShown(show);
554
555
}

556
void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
557
{
558
    QAction* showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
559
    showHiddenFilesAction->setChecked(shown);
560
561
}

562
563
void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
{
564
565
    m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable &&
                                                                         KProtocolManager::supportsMakeDir(currentView()->url()));
566
567
}

568
569
KToggleAction* DolphinViewActionHandler::iconsModeAction()
{
570
    KToggleAction* iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
571
    iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
572
    iconsView->setToolTip(i18nc("@info", "Icons view mode"));
573
    m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_1);
574
    iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
575
576
577
578
    iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
    return iconsView;
}

579
580
KToggleAction* DolphinViewActionHandler::compactModeAction()
{
581
    KToggleAction* iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
582
583
    iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
    iconsView->setToolTip(i18nc("@info", "Compact view mode"));
584
    m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_2);
585
    iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
586
587
588
589
    iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
    return iconsView;
}

590
591
KToggleAction* DolphinViewActionHandler::detailsModeAction()
{
592
    KToggleAction* detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
593
    detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
594
    detailsView->setToolTip(i18nc("@info", "Details view mode"));
595
    m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL | Qt::Key_3);
596
    detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
597
598
599
600
    detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
    return detailsView;
}

601
void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
602
{
603
    KToggleAction* action = m_sortByActions.value(role);
604
    if (action) {
605
        action->setChecked(true);
606

607
        if (!action->icon().isNull()) {
608
            QAction* sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
609
            sortByMenu->setIcon(action->icon());
610
        }
611
    }
612
613
614
615
616
617
618
619

    QAction* descending = m_actionCollection->action(QStringLiteral("descending"));
    QAction* ascending = m_actionCollection->action(QStringLiteral("ascending"));

    if (role == "text" || role == "type" || role == "tags" || role == "comment") {
        descending->setText(i18nc("Sort descending", "Z-A"));
        ascending->setText(i18nc("Sort ascending", "A-Z"));
    } else if (role == "size") {
Nikita Churaev's avatar
Nikita Churaev committed
620
621
        descending->setText(i18nc("Sort descending", "Largest First"));
        ascending->setText(i18nc("Sort ascending", "Smallest First"));
622
    } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
Nikita Churaev's avatar
Nikita Churaev committed
623
624
        descending->setText(i18nc("Sort descending", "Newest First"));
        ascending->setText(i18nc("Sort ascending", "Oldest First"));
625
    } else if (role == "rating") {
Nikita Churaev's avatar
Nikita Churaev committed
626
627
        descending->setText(i18nc("Sort descending", "Highest First"));
        ascending->setText(i18nc("Sort ascending", "Lowest First"));
628
629
630
631
632
633
    } else {
        descending->setText(i18nc("Sort descending", "Descending"));
        ascending->setText(i18nc("Sort ascending", "Ascending"));
    }

    slotSortOrderChanged(m_currentView->sortOrder());
634
635
}

636
void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
637
{
638
    Q_UNUSED(previous)
639

640
    QAction* zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
641
    if (zoomInAction) {
642
        zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
643
644
645
    }

    QAction* zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
646
    if (zoomOutAction) {
647
        zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
648
649
650
    }
}

651
652
void DolphinViewActionHandler::slotSortTriggered(QAction* action)
{
653
654
655
656
    // The radiobuttons of the "Sort By"-menu are split between the main-menu
    // and several sub-menus. Because of this they don't have a common
    // action-group that assures an exclusive toggle-state between the main-menu
    // actions and the sub-menu-actions. If an action gets checked, it must
657
658
659
    // be assured that all other actions get unchecked, except the ascending/
    // descending actions
    for (QAction *groupAction : qAsConst(m_sortByActions)) {
660
661
        KActionMenu* actionMenu = qobject_cast<KActionMenu*>(groupAction);
        if (actionMenu) {
Alexander Lohnau's avatar
Alexander Lohnau committed
662
663
            const auto actions = actionMenu->menu()->actions();
            for (QAction* subAction : actions) {
664
665
666
667
668
669
670
671
672
                subAction->setChecked(false);
            }
        } else if (groupAction->actionGroup()) {
            groupAction->setChecked(false);
        }
    }
    action->setChecked(true);

    // Apply the activated sort-role to the view
673
674
    const QByteArray role = action->data().toByteArray();
    m_currentView->setSortRole(role);
675
}
676
677
678

void DolphinViewActionHandler::slotAdjustViewProperties()
{
Alexander Lohnau's avatar
Alexander Lohnau committed
679
    Q_EMIT actionBeingHandled();
680
681
682
    QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
    dialog->exec();
    delete dialog;
683
}
684

Nate Graham's avatar
Nate Graham committed
685
686
void DolphinViewActionHandler::slotDuplicate()
{
Alexander Lohnau's avatar
Alexander Lohnau committed
687
    Q_EMIT actionBeingHandled();
Nate Graham's avatar
Nate Graham committed
688
689
690
    m_currentView->duplicateSelectedItems();
}

691
692
void DolphinViewActionHandler::slotProperties()
{
Kevin Funk's avatar
Kevin Funk committed
693
    KPropertiesDialog* dialog = nullptr;
694
695
    const KFileItemList list = m_currentView->selectedItems();
    if (list.isEmpty()) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
696
        const QUrl url = m_currentView->url();
697
698
699
700
701
702
703
704
705
706
        dialog = new KPropertiesDialog(url, m_currentView);
    } else {
        dialog = new KPropertiesDialog(list, m_currentView);
    }

    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();
    dialog->raise();
    dialog->activateWindow();
}
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
707
708
709
710
711

void DolphinViewActionHandler::slotCopyPath()
{
    m_currentView->copyPathToClipboard();
}