File indexing completed on 2025-01-05 05:14:47

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAction>
0010 #include <QMap>
0011 #include <QObject>
0012 
0013 #include "libkommitwidgets_export.h"
0014 
0015 #define ADD_ACTION_IMPL(name, text, slot, enabled)                                                                                                             \
0016     _##name = new QAction(this);                                                                                                                               \
0017     _##name->setText(i18n(text));                                                                                                                              \
0018     _actions.append(_##name);                                                                                                                                  \
0019     setActionEnabled(_##name, enabled);                                                                                                                        \
0020     connect(_##name, &QAction::triggered, this, slot);
0021 
0022 #define ADD_HIDDEN_ACTION(name, text, slot) ADD_ACTION_IMPL(name, text, slot, true)
0023 
0024 #define ADD_ACTION_ENABLED(name, text, slot)                                                                                                                   \
0025     ADD_ACTION_IMPL(name, text, slot, true);                                                                                                                   \
0026     _menu->addAction(_##name);
0027 
0028 #define ADD_ACTION_DISABLED(name, text, slot)                                                                                                                  \
0029     ADD_ACTION_IMPL(name, text, slot, false);                                                                                                                  \
0030     _menu->addAction(_##name);
0031 
0032 #define ADD_ACTION(name, text, slot) ADD_ACTION_ENABLED(name, text, slot);
0033 
0034 #define DEFINE_ACTION(name)                                                                                                                                    \
0035 private:                                                                                                                                                       \
0036     QAction *_##name;                                                                                                                                          \
0037                                                                                                                                                                \
0038 public:                                                                                                                                                        \
0039     QAction *name() const                                                                                                                                      \
0040     {                                                                                                                                                          \
0041         return _##name;                                                                                                                                        \
0042     }
0043 
0044 namespace Git
0045 {
0046 class Manager;
0047 }
0048 
0049 class QWidget;
0050 class QMenu;
0051 class LIBKOMMITWIDGETS_EXPORT AbstractActions : public QObject
0052 {
0053     Q_OBJECT
0054 
0055 protected:
0056     Git::Manager *mGit = nullptr;
0057     QList<QAction *> mActions;
0058     QMenu *mMenu = nullptr;
0059     QWidget *mParent = nullptr;
0060     void setActionEnabled(QAction *action, bool enabled = true);
0061 
0062     template<class Receiver, class Func>
0063     inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type *
0064     addAction(const QString &text, const Receiver *receiver, Func slot, bool enabled = true, bool addToMenu = true)
0065     {
0066         auto a = createAction(text, enabled, addToMenu);
0067         connect(a, &QAction::triggered, receiver, slot);
0068         return a;
0069     }
0070 
0071     template<class Receiver, class Func>
0072     inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type *
0073     addActionHidden(const QString &text, const Receiver *receiver, Func slot)
0074     {
0075         return addAction(text, receiver, slot, true, false);
0076     }
0077 
0078     template<class Receiver, class Func>
0079     inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type *
0080     addActionDisabled(const QString &text, const Receiver *receiver, Func slot)
0081     {
0082         return addAction(text, receiver, slot, false, true);
0083     }
0084 
0085 public:
0086     explicit AbstractActions(Git::Manager *git, QWidget *parent);
0087     void popup();
0088     void popup(const QPoint &pos);
0089 
0090 private Q_SLOTS:
0091     void git_reloaded();
0092 
0093 private:
0094     QAction *createAction(const QString &text, bool enabled, bool addToMenu);
0095     QMap<QAction *, bool> mActionStatuses;
0096 };