File indexing completed on 2024-04-28 05:45:14

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef ACTIONTEXTHELPER_H
0009 #define ACTIONTEXTHELPER_H
0010 
0011 #include <QAction>
0012 #include <QPointer>
0013 #include <QString>
0014 
0015 namespace SelectionMode
0016 {
0017 
0018 /**
0019  * @brief Helps changing the texts of actions depending on the current selection.
0020  *
0021  * This is useful for actions that directly trigger a change when there is a selection and do something
0022  * different when nothing is selected. For example should the copy action read "Copy" when items are
0023  * selected but when no items are selected it can read "Copy…" since triggering it will enter selection
0024  * mode and ask users to select the files they want to copy first.
0025  */
0026 class ActionTextHelper : QObject
0027 {
0028 public:
0029     explicit ActionTextHelper(QObject *parent);
0030 
0031     /**
0032      * Changes the text of \a action to \a text whenever textsWhenNothingIsSelectedEnabled(true) is called.
0033      * The texts can be changed back by calling textsWhenNothingIsSelectedEnabled(false).
0034      * @see textsWhenNothingIsSelectedEnabled()
0035      */
0036     void registerTextWhenNothingIsSelected(QAction *action, QString registeredText);
0037 
0038     /**
0039      * Changes all texts that were registered previously using registerTextWhenNothingIsSelected() to those
0040      * registered texts if called with \a enabled == true. Otherwise resets the texts to the original one.
0041      */
0042     void textsWhenNothingIsSelectedEnabled(bool enabled);
0043 
0044 private:
0045     enum TextState { TextWhenNothingIsSelected, TextWhenSomethingIsSelected };
0046 
0047     /**
0048      * Utility struct to allow switching back and forth between registered actions showing their
0049      * distinct texts for when no items are selected or when items are selected.
0050      * An example is "Copy" or "Copy…". The latter one is used when nothing is selected and signifies
0051      * that it will trigger SelectionMode so items can be selected and then copied.
0052      */
0053     struct RegisteredActionTextChange {
0054         QPointer<QAction> action;
0055         QString registeredText;
0056         TextState textStateOfRegisteredText;
0057 
0058         RegisteredActionTextChange(QAction *action, QString registeredText, TextState state)
0059             : action{action}
0060             , registeredText{registeredText}
0061             , textStateOfRegisteredText{state} {};
0062     };
0063 
0064     /**
0065      * @see RegisteredActionTextChange
0066      */
0067     std::vector<RegisteredActionTextChange> m_registeredActionTextChanges;
0068 };
0069 
0070 }
0071 
0072 #endif // ACTIONTEXTHELPER_H