dolphinviewactionhandler.cpp 30.2 KB
Newer Older
1
2
/***************************************************************************
 *   Copyright (C) 2008 by David Faure <faure@kde.org>                     *
3
 *   Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com>             *
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "dolphinviewactionhandler.h"

Roman Inflianskas's avatar
Roman Inflianskas committed
23
24
#include "dolphindebug.h"
#include "kitemviews/kfileitemmodel.h"
25
#include "settings/viewpropertiesdialog.h"
26
#include "views/zoomlevelinfo.h"
27

Roman Inflianskas's avatar
Roman Inflianskas committed
28
29
30
#ifdef HAVE_BALOO
#include <Baloo/IndexerConfig>
#endif
Peter Penz's avatar
Peter Penz committed
31
32
#include <KActionCollection>
#include <KActionMenu>
Laurent Montel's avatar
Laurent Montel committed
33
#include <KLocalizedString>
34
#include <KNewFileMenu>
Peter Penz's avatar
Peter Penz committed
35
#include <KPropertiesDialog>
36
#include <KProtocolManager>
Rahman Duran's avatar
Rahman Duran committed
37

Roman Inflianskas's avatar
Roman Inflianskas committed
38
39
#include <QMenu>
#include <QPointer>
Vishesh Handa's avatar
Vishesh Handa committed
40

41
42
43
DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent) :
    QObject(parent),
    m_actionCollection(collection),
Kevin Funk's avatar
Kevin Funk committed
44
    m_currentView(nullptr),
45
46
    m_sortByActions(),
    m_visibleRoles()
47
48
49
50
51
52
53
54
55
{
    Q_ASSERT(m_actionCollection);
    createActions();
}

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

56
    if (m_currentView) {
Kevin Funk's avatar
Kevin Funk committed
57
        disconnect(m_currentView, nullptr, this, nullptr);
58
    }
59
60
61

    m_currentView = view;

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    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);
82
83
}

Rahman Duran's avatar
Rahman Duran committed
84
85
86
87
88
DolphinView* DolphinViewActionHandler::currentView()
{
    return m_currentView;
}

89
90
91
void DolphinViewActionHandler::createActions()
{
    // This action doesn't appear in the GUI, it's for the shortcut only.
92
    // KNewFileMenu takes care of the GUI stuff.
93
    QAction* newDirAction = m_actionCollection->addAction(QStringLiteral("create_dir"));
94
    newDirAction->setText(i18nc("@action", "Create Folder..."));
95
    m_actionCollection->setDefaultShortcut(newDirAction, Qt::Key_F10);
96
    newDirAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
97
    newDirAction->setEnabled(false);    // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
98
    connect(newDirAction, &QAction::triggered, this, &DolphinViewActionHandler::createDirectoryTriggered);
99

100
    // File menu
101

102
103
104
105
    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."));
106

107
108
109
110
111
112
    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);
    }
113
114
115
116
    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."));
117

118
119
120
121
122
123
    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);
    }
124
125
126
    deleteAction->setWhatsThis(xi18nc("@info:whatsthis", "This deletes "
        "the items in your current selection completely. They can "
        "not be recovered by normal means."));
127

128
    // This action is useful for being enabled when KStandardAction::MoveToTrash should be
129
    // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
130
131
    // can be used for deleting the file (#76016). It needs to be a separate action
    // so that the Edit menu isn't affected.
132
    QAction* deleteWithTrashShortcut = m_actionCollection->addAction(QStringLiteral("delete_shortcut"));
133
    // The descriptive text is just for the shortcuts editor.
134
    deleteWithTrashShortcut->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
135
    m_actionCollection->setDefaultShortcuts(deleteWithTrashShortcut, KStandardShortcut::moveToTrash());
136
    deleteWithTrashShortcut->setEnabled(false);
137
    connect(deleteWithTrashShortcut, &QAction::triggered, this, &DolphinViewActionHandler::slotDeleteItems);
138

139
    QAction *propertiesAction = m_actionCollection->addAction( QStringLiteral("properties") );
140
    // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
141
    propertiesAction->setText( i18nc("@action:inmenu File", "Properties") );
142
143
144
145
146
147
    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."));
148
    propertiesAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
149
    m_actionCollection->setDefaultShortcuts(propertiesAction, {Qt::ALT + Qt::Key_Return, Qt::ALT + Qt::Key_Enter});
150
    connect(propertiesAction, &QAction::triggered, this, &DolphinViewActionHandler::slotProperties);
151

152
    // View menu
153
    KToggleAction* iconsAction = iconsModeAction();
154
    KToggleAction* compactAction = compactModeAction();
155
156
    KToggleAction* detailsAction = detailsModeAction();

157
158
159
160
161
162
163
164
165
    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 "
166
        "and files in columns with the names beside the icons.</para><para>"
167
168
169
170
171
172
173
174
175
176
177
        "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>"));

178
    KSelectAction* viewModeActions = m_actionCollection->add<KSelectAction>(QStringLiteral("view_mode"));
179
180
    viewModeActions->setText(i18nc("@action:intoolbar", "View Mode"));
    viewModeActions->addAction(iconsAction);
181
    viewModeActions->addAction(compactAction);
182
183
    viewModeActions->addAction(detailsAction);
    viewModeActions->setToolBarMode(KSelectAction::MenuMode);
184
    connect(viewModeActions, QOverload<QAction*>::of(&KSelectAction::triggered), this, &DolphinViewActionHandler::slotViewModeActionTriggered);
185

186
    QAction* zoomInAction = KStandardAction::zoomIn(this,
187
                            &DolphinViewActionHandler::zoomIn,
188
                            m_actionCollection);
189
    zoomInAction->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
190

191
192
193
194
195
196
197
198
    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")));
    m_actionCollection->setDefaultShortcuts(zoomResetAction, {Qt::CTRL + Qt::Key_0});
    connect(zoomResetAction, &QAction::triggered, this, &DolphinViewActionHandler::zoomReset);

199
    QAction* zoomOutAction = KStandardAction::zoomOut(this,
200
                             &DolphinViewActionHandler::zoomOut,
201
                             m_actionCollection);
202
    zoomOutAction->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
203

204
    KToggleAction* showPreview = m_actionCollection->add<KToggleAction>(QStringLiteral("show_preview"));
205
    showPreview->setText(i18nc("@action:intoolbar", "Preview"));
206
    showPreview->setToolTip(i18nc("@info", "Show preview of files and folders"));
207
208
209
210
    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."));
211
    showPreview->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
212
    connect(showPreview, &KToggleAction::triggered, this, &DolphinViewActionHandler::togglePreview);
213

214
    KToggleAction* sortFoldersFirst = m_actionCollection->add<KToggleAction>(QStringLiteral("folders_first"));
215
    sortFoldersFirst->setText(i18nc("@action:inmenu Sort", "Folders First"));
216
    connect(sortFoldersFirst, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortFoldersFirst);
217

218
    // View -> Sort By
219
    QActionGroup* sortByActionGroup = createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
220

221
    KActionMenu* sortByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("sort"));
222
    sortByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
223
224
    sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
    sortByActionMenu->setDelayed(false);
225

226
227
228
    foreach (QAction* action, sortByActionGroup->actions()) {
        sortByActionMenu->addAction(action);
    }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

    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);
249
250
251
252
    sortByActionMenu->addSeparator();
    sortByActionMenu->addAction(sortFoldersFirst);

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

255
    KActionMenu* visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
256
257
258
259
260
    visibleRolesMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
    visibleRolesMenu->setDelayed(false);

    foreach (QAction* action, visibleRolesGroup->actions()) {
        visibleRolesMenu->addAction(action);
261
    }
262

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

269
    KToggleAction* showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
Roman Gilg's avatar
Roman Gilg committed
270
271
    showHiddenFiles->setText(i18nc("@action:inmenu View", "Hidden Files"));
    showHiddenFiles->setToolTip(i18nc("@info", "Visibility of hidden files and folders"));
272
273
274
275
276
277
    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>"));
278
    m_actionCollection->setDefaultShortcuts(showHiddenFiles, {Qt::ALT + Qt::Key_Period, Qt::CTRL + Qt::Key_H, Qt::Key_F8});
279
    connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
280

281
    QAction* adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
282
    adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
283
284
    adjustViewProps->setWhatsThis(i18nc("@info:whatsthis", "This opens a window "
        "in which all folder view properties can be adjusted."));
285
    connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
286
287
}

288
QActionGroup* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString& groupPrefix)
289
{
290
    const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
Antonio Prcela's avatar
Antonio Prcela committed
291
    Q_ASSERT(isSortGroup || groupPrefix == QLatin1String("show_"));
292
293
294
295

    QActionGroup* rolesActionGroup = new QActionGroup(m_actionCollection);
    rolesActionGroup->setExclusive(isSortGroup);
    if (isSortGroup) {
296
297
        connect(rolesActionGroup, &QActionGroup::triggered,
                this, &DolphinViewActionHandler::slotSortTriggered);
298
    } else {
299
300
        connect(rolesActionGroup, &QActionGroup::triggered,
                this, &DolphinViewActionHandler::toggleVisibleRole);
301
    }
302

303
    QString groupName;
Kevin Funk's avatar
Kevin Funk committed
304
305
    KActionMenu* groupMenu = nullptr;
    QActionGroup* groupMenuGroup = nullptr;
306

307
    bool indexingEnabled = false;
Vishesh Handa's avatar
Vishesh Handa committed
308
309
310
#ifdef HAVE_BALOO
    Baloo::IndexerConfig config;
    indexingEnabled = config.fileIndexingEnabled();
311
312
#endif

Peter Penz's avatar
Peter Penz committed
313
314
    const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
    foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
315
316
        if (!isSortGroup && info.role == "text") {
            // It should not be possible to hide the "text" role
317
318
319
            continue;
        }

Kevin Funk's avatar
Kevin Funk committed
320
        KToggleAction* action = nullptr;
321
322
323
324
325
326
327
328
329
330
331
332
333
334
        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) {
335
336
                    connect(groupMenuGroup, &QActionGroup::triggered,
                            this, &DolphinViewActionHandler::slotSortTriggered);
337
                } else {
338
339
                    connect(groupMenuGroup, &QActionGroup::triggered,
                            this, &DolphinViewActionHandler::toggleVisibleRole);
340
341
342
343
344
345
346
                }
            }

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

Vishesh Handa's avatar
Vishesh Handa committed
350
351
        const bool enable = (!info.requiresBaloo && !info.requiresIndexer) ||
                            (info.requiresBaloo) ||
352
353
354
                            (info.requiresIndexer && indexingEnabled);
        action->setEnabled(enable);

355
356
357
358
359
        if (isSortGroup) {
            m_sortByActions.insert(info.role, action);
        } else {
            m_visibleRoles.insert(info.role, action);
        }
360
    }
361

362
    return rolesActionGroup;
363
364
}

365
366
367
368
void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
{
    const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
    m_currentView->setMode(mode);
369

370
    QAction* viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
371
    viewModeMenu->setIcon(action->icon());
372
373
}

374
375
376
377
378
379
void DolphinViewActionHandler::slotRename()
{
    emit actionBeingHandled();
    m_currentView->renameSelectedItems();
}

380
void DolphinViewActionHandler::slotTrashActivated()
381
382
{
    emit actionBeingHandled();
383
    m_currentView->trashSelectedItems();
384
385
386
387
388
389
390
391
392
393
394
}

void DolphinViewActionHandler::slotDeleteItems()
{
    emit actionBeingHandled();
    m_currentView->deleteSelectedItems();
}

void DolphinViewActionHandler::togglePreview(bool show)
{
    emit actionBeingHandled();
395
    m_currentView->setPreviewsShown(show);
396
397
}

398
void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
399
{
400
    Q_UNUSED(shown);
401
    // It is not enough to update the 'Show Preview' action, also
402
    // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
403
404
405
    updateViewActions();
}

406
407
408
409
QString DolphinViewActionHandler::currentViewModeActionName() const
{
    switch (m_currentView->mode()) {
    case DolphinView::IconsView:
410
        return QStringLiteral("icons");
411
    case DolphinView::DetailsView:
412
        return QStringLiteral("details");
413
    case DolphinView::CompactView:
414
        return QStringLiteral("compact");
415
416
417
    default:
        Q_ASSERT(false);
        break;
418
419
420
421
    }
    return QString(); // can't happen
}

Rahman Duran's avatar
Rahman Duran committed
422
423
424
425
426
KActionCollection* DolphinViewActionHandler::actionCollection()
{
    return m_actionCollection;
}

427
428
void DolphinViewActionHandler::updateViewActions()
{
429
    QAction* viewModeAction = m_actionCollection->action(currentViewModeActionName());
430
    if (viewModeAction) {
431
        viewModeAction->setChecked(true);
432

433
        QAction* viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
434
        viewModeMenu->setIcon(viewModeAction->icon());
435
436
    }

437
    QAction* showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
438
    showPreviewAction->setChecked(m_currentView->previewsShown());
439
440

    slotSortOrderChanged(m_currentView->sortOrder());
441
    slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
442
    slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
443
    slotGroupedSortingChanged(m_currentView->groupedSorting());
444
    slotSortRoleChanged(m_currentView->sortRole());
445
    slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
446

Roman Gilg's avatar
Roman Gilg committed
447
448
    // Updates the "show_hidden_files" action state and icon
    slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
449
450
451
452
}

void DolphinViewActionHandler::zoomIn()
{
453
454
    const int level = m_currentView->zoomLevel();
    m_currentView->setZoomLevel(level + 1);
455
456
457
458
459
    updateViewActions();
}

void DolphinViewActionHandler::zoomOut()
{
460
461
    const int level = m_currentView->zoomLevel();
    m_currentView->setZoomLevel(level - 1);
462
463
464
    updateViewActions();
}

465
466
467
468
469
470
void DolphinViewActionHandler::zoomReset()
{
    m_currentView->resetZoomLevel();
    updateViewActions();
}

471
472
void DolphinViewActionHandler::toggleSortFoldersFirst()
{
473
474
    const bool sortFirst = m_currentView->sortFoldersFirst();
    m_currentView->setSortFoldersFirst(!sortFirst);
475
476
}

477
478
void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
{
479
    QAction* descending = m_actionCollection->action(QStringLiteral("descending"));
480
    QAction* ascending = m_actionCollection->action(QStringLiteral("ascending"));
481
482
    const bool sortDescending = (order == Qt::DescendingOrder);
    descending->setChecked(sortDescending);
483
    ascending->setChecked(!sortDescending);
484
485
}

486
487
void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
{
488
    m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
489
490
}

491
void DolphinViewActionHandler::toggleVisibleRole(QAction* action)
492
493
{
    emit actionBeingHandled();
494

495
    const QByteArray toggledRole = action->data().toByteArray();
496

497
    QList<QByteArray> roles = m_currentView->visibleRoles();
498
499
500

    const bool show = action->isChecked();

501
    const int index = roles.indexOf(toggledRole);
502
503
    const bool containsInfo = (index >= 0);
    if (show && !containsInfo) {
504
505
        roles.append(toggledRole);
        m_currentView->setVisibleRoles(roles);
506
    } else if (!show && containsInfo) {
507
508
509
        roles.removeAt(index);
        m_currentView->setVisibleRoles(roles);
        Q_ASSERT(roles.indexOf(toggledRole) < 0);
510
    }
511
512
}

513
514
void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray>& current,
                                                       const QList<QByteArray>& previous)
515
{
516
517
    Q_UNUSED(previous);

518
    const QSet<QByteArray> checkedRoles = current.toSet();
519
520
521
522
523
524
    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));
525
    }
526
527
}

528
void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
529
{
530
    m_currentView->setGroupedSorting(grouped);
531
532
}

533
void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
534
{
535
    QAction* showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
536
    showInGroupsAction->setChecked(groupedSorting);
537
538
539
540
541
}

void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
{
    emit actionBeingHandled();
542
    m_currentView->setHiddenFilesShown(show);
543
544
}

545
void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
546
{
547
    QAction* showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
548
    showHiddenFilesAction->setChecked(shown);
Roman Gilg's avatar
Roman Gilg committed
549

550
551
    // #374508: don't overwrite custom icons.
    const QString iconName = showHiddenFilesAction->icon().name();
552
    if (!iconName.isEmpty() && iconName != QLatin1String("view-visible") && iconName != QLatin1String("view-hidden")) {
553
554
555
        return;
    }

556
    showHiddenFilesAction->setIcon(QIcon::fromTheme(shown ? QStringLiteral("view-visible") : QStringLiteral("view-hidden")));
557
558
}

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

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

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

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

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

604
        if (!action->icon().isNull()) {
605
            QAction* sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
606
            sortByMenu->setIcon(action->icon());
607
        }
608
    }
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630

    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") {
        descending->setText(i18nc("Sort descending", "Largest first"));
        ascending->setText(i18nc("Sort ascending", "Smallest first"));
    } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
        descending->setText(i18nc("Sort descending", "Newest first"));
        ascending->setText(i18nc("Sort ascending", "Oldest first"));
    } else if (role == "rating") {
        descending->setText(i18nc("Sort descending", "Highest first"));
        ascending->setText(i18nc("Sort ascending", "Lowest first"));
    } else {
        descending->setText(i18nc("Sort descending", "Descending"));
        ascending->setText(i18nc("Sort ascending", "Ascending"));
    }

    slotSortOrderChanged(m_currentView->sortOrder());
631
632
}

633
void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
634
{
635
636
    Q_UNUSED(previous);

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

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

648
649
void DolphinViewActionHandler::slotSortTriggered(QAction* action)
{
650
651
652
653
    // 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
654
655
    // be assured that all other actions get unchecked, except the ascending/
    // descending actions
656
    QAction* sortByMenu =  m_actionCollection->action(QStringLiteral("sort"));
657
    for (QAction *groupAction : qAsConst(m_sortByActions)) {
658
659
660
661
662
663
664
665
666
667
668
669
        KActionMenu* actionMenu = qobject_cast<KActionMenu*>(groupAction);
        if (actionMenu) {
            foreach (QAction* subAction, actionMenu->menu()->actions()) {
                subAction->setChecked(false);
            }
        } else if (groupAction->actionGroup()) {
            groupAction->setChecked(false);
        }
    }
    action->setChecked(true);

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

void DolphinViewActionHandler::slotAdjustViewProperties()
{
    emit actionBeingHandled();
677
678
679
    QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
    dialog->exec();
    delete dialog;
680
}
681

682
683
void DolphinViewActionHandler::slotProperties()
{
Kevin Funk's avatar
Kevin Funk committed
684
    KPropertiesDialog* dialog = nullptr;
685
686
    const KFileItemList list = m_currentView->selectedItems();
    if (list.isEmpty()) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
687
        const QUrl url = m_currentView->url();
688
689
690
691
692
693
694
695
696
697
        dialog = new KPropertiesDialog(url, m_currentView);
    } else {
        dialog = new KPropertiesDialog(list, m_currentView);
    }

    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();
    dialog->raise();
    dialog->activateWindow();
}