]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/actiontexthelper.h
Improve code quality
[dolphin.git] / src / selectionmode / actiontexthelper.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #ifndef ACTIONTEXTHELPER_H
9 #define ACTIONTEXTHELPER_H
10
11 #include <QAction>
12 #include <QPointer>
13 #include <QString>
14
15 namespace SelectionMode
16 {
17
18 /**
19 * @brief Helps changing the texts of actions depending on the current selection.
20 *
21 * This is useful for actions that directly trigger a change when there is a selection and do something
22 * different when nothing is selected. For example should the copy action read "Copy" when items are
23 * selected but when no items are selected it can read "Copy…" since triggering it will enter selection
24 * mode and ask users to select the files they want to copy first.
25 */
26 class ActionTextHelper : QObject
27 {
28 public:
29 explicit ActionTextHelper(QObject *parent);
30
31 /**
32 * Changes the text of \a action to \a text whenever textsWhenNothingIsSelectedEnabled(true) is called.
33 * The texts can be changed back by calling textsWhenNothingIsSelectedEnabled(false).
34 * @see textsWhenNothingIsSelectedEnabled()
35 */
36 void registerTextWhenNothingIsSelected(QAction *action, QString registeredText);
37
38 /**
39 * Changes all texts that were registered previously using registerTextWhenNothingIsSelected() to those
40 * registered texts if called with \a enabled == true. Otherwise resets the texts to the original one.
41 */
42 void textsWhenNothingIsSelectedEnabled(bool enabled);
43
44 private:
45 enum TextState {
46 TextWhenNothingIsSelected,
47 TextWhenSomethingIsSelected
48 };
49
50 /**
51 * Utility struct to allow switching back and forth between registered actions showing their
52 * distinct texts for when no items are selected or when items are selected.
53 * An example is "Copy" or "Copy…". The latter one is used when nothing is selected and signifies
54 * that it will trigger SelectionMode so items can be selected and then copied.
55 */
56 struct RegisteredActionTextChange {
57 QPointer<QAction> action;
58 QString registeredText;
59 TextState textStateOfRegisteredText;
60
61 RegisteredActionTextChange(QAction *action, QString registeredText, TextState state) :
62 action{action},
63 registeredText{registeredText},
64 textStateOfRegisteredText{state}
65 { };
66 };
67
68 /**
69 * @see RegisteredActionTextChange
70 */
71 std::vector<RegisteredActionTextChange> m_registeredActionTextChanges;
72 };
73
74 }
75
76 #endif // ACTIONTEXTHELPER_H