┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/selectionmode/actiontexthelper.h
blob: 1b88403754e3249ad75ddd857b8db30c3d079ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2022 Felix Ernst <[email protected]>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#ifndef ACTIONTEXTHELPER_H
#define ACTIONTEXTHELPER_H

#include <QAction>
#include <QPointer>
#include <QString>

namespace SelectionMode
{

/**
 * @brief Helps changing the texts of actions depending on the current selection.
 *
 * This is useful for actions that directly trigger a change when there is a selection and do something
 * different when nothing is selected. For example should the copy action read "Copy" when items are
 * selected but when no items are selected it can read "Copy…" since triggering it will enter selection
 * mode and ask users to select the files they want to copy first.
 */
class ActionTextHelper : QObject
{
public:
    explicit ActionTextHelper(QObject *parent);

    /**
     * Changes the text of \a action to \a text whenever textsWhenNothingIsSelectedEnabled(true) is called.
     * The texts can be changed back by calling textsWhenNothingIsSelectedEnabled(false).
     * @see textsWhenNothingIsSelectedEnabled()
     */
    void registerTextWhenNothingIsSelected(QAction *action, QString registeredText);

    /**
     * Changes all texts that were registered previously using registerTextWhenNothingIsSelected() to those
     * registered texts if called with \a enabled == true. Otherwise resets the texts to the original one.
     */
    void textsWhenNothingIsSelectedEnabled(bool enabled);

private:
    enum TextState {
        TextWhenNothingIsSelected,
        TextWhenSomethingIsSelected
    };

    /**
     * Utility struct to allow switching back and forth between registered actions showing their
     * distinct texts for when no items are selected or when items are selected.
     * An example is "Copy" or "Copy…". The latter one is used when nothing is selected and signifies
     * that it will trigger SelectionMode so items can be selected and then copied.
     */
    struct RegisteredActionTextChange {            
        QPointer<QAction> action;
        QString registeredText;
        TextState textStateOfRegisteredText;

        RegisteredActionTextChange(QAction *action, QString registeredText, TextState state) :
            action{action},
            registeredText{registeredText},
            textStateOfRegisteredText{state}
        {   };
    };

    /**
     * @see RegisteredActionTextChange
     */
    std::vector<RegisteredActionTextChange> m_registeredActionTextChanges;
};

}

#endif // ACTIONTEXTHELPER_H