LibrePCB Developers Documentation
editorcommand.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_EDITOR_EDITORCOMMAND_H
21#define LIBREPCB_EDITOR_EDITORCOMMAND_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include <QtCore>
27#include <QtWidgets>
28
29/*******************************************************************************
30 * Namespace / Forward Declarations
31 ******************************************************************************/
32namespace librepcb {
33namespace editor {
34
35/*******************************************************************************
36 * Class EditorCommand
37 ******************************************************************************/
38
42class EditorCommand final : public QObject {
43 Q_OBJECT
44
45public:
46 // Types
47 enum class Flag {
48 OpensPopup = (1 << 0),
49 AboutRole = (1 << 4),
50 AboutQtRole = (1 << 5),
51 PreferencesRole = (1 << 6),
52 QuitRole = (1 << 7),
53 };
54 Q_DECLARE_FLAGS(Flags, Flag)
55
56 enum class ActionFlag {
57 NoShortcuts = (1 << 0),
58 WidgetShortcut = (1 << 1),
59 ApplicationShortcut = (1 << 2),
60 QueuedConnection = (1 << 3),
61 ReactOnToggle = (1 << 4),
62 };
63 Q_DECLARE_FLAGS(ActionFlags, ActionFlag)
64
65 // Constructors / Destructor
66 EditorCommand() = delete;
67 EditorCommand(const EditorCommand& other) = delete;
68 EditorCommand(const QString& identifier, const char* text,
69 const char* description, const QString& iconFp, Flags flags,
70 const QList<QKeySequence>& defaultKeySequences,
71 QObject* parent = nullptr) noexcept;
72 ~EditorCommand() noexcept;
73
74 // Getters
75 const QString& getIdentifier() const noexcept { return mIdentifier; }
76 const QString& getText() const noexcept { return mText; }
77 QString getDisplayText() const noexcept { return unescapeAmpersand(mText); }
78 QString getDisplayTextNoTr() const noexcept {
80 }
81 const QString& getDescription() const noexcept { return mDescription; }
82 const QIcon& getIcon() const noexcept { return mIcon; }
83 const Flags& getFlags() const noexcept { return mFlags; }
84 const QList<QKeySequence> getDefaultKeySequences() const noexcept {
86 }
87 const QList<QKeySequence>& getKeySequences() const noexcept {
88 return mKeySequences;
89 }
90
91 // Setters
92 void setKeySequences(const QList<QKeySequence>& sequences) noexcept;
93
94 // General Methods
95 void updateTranslations() noexcept;
96 QAction* createAction(QObject* parent,
97 ActionFlags flags = ActionFlags()) const noexcept;
98 template <typename TContext, typename TSlot>
99 QAction* createAction(QObject* parent, const TContext* context, TSlot slot,
100 ActionFlags flags = ActionFlags()) const noexcept {
101 QAction* action = createAction(parent, flags);
102 Qt::ConnectionType conType = flags.testFlag(ActionFlag::QueuedConnection)
103 ? Qt::QueuedConnection
104 : Qt::AutoConnection;
105 if (flags.testFlag(ActionFlag::ReactOnToggle)) {
106 QObject::connect(action, &QAction::toggled, context, slot, conType);
107 } else {
108 QObject::connect(action, &QAction::triggered, context, slot, conType);
109 }
110 return action;
111 }
112
113 // Operator Overloadings
114 EditorCommand& operator=(const EditorCommand& rhs) = delete;
115
116signals:
117 void shortcutsChanged(const QList<QKeySequence>& sequences);
118
119private: // Methods
120 QAction* setupAction(QAction* action, ActionFlags flags) const noexcept;
121 bool eventFilter(QObject* obj, QEvent* event) noexcept override;
122 static QString unescapeAmpersand(QString text) noexcept;
123
124private: // Data
125 QString mIdentifier;
126 const char* mTextNoTr;
127 QString mText;
128 const char* mDescriptionNoTr;
130 QIcon mIcon;
131 Flags mFlags;
132 QList<QKeySequence> mDefaultKeySequences;
133 QList<QKeySequence> mKeySequences;
134};
135
136} // namespace editor
137} // namespace librepcb
138
139Q_DECLARE_OPERATORS_FOR_FLAGS(librepcb::editor::EditorCommand::Flags)
140Q_DECLARE_OPERATORS_FOR_FLAGS(librepcb::editor::EditorCommand::ActionFlags)
141
142/*******************************************************************************
143 * End of File
144 ******************************************************************************/
145
146#endif
Command for editors, e.g. to be added to a QMenu.
Definition: editorcommand.h:42
QString mIdentifier
Definition: editorcommand.h:125
QIcon mIcon
Definition: editorcommand.h:130
QString getDisplayTextNoTr() const noexcept
Definition: editorcommand.h:78
Flags mFlags
Definition: editorcommand.h:131
QString mDescription
Definition: editorcommand.h:129
const char * mDescriptionNoTr
Definition: editorcommand.h:128
void setKeySequences(const QList< QKeySequence > &sequences) noexcept
Definition: editorcommand.cpp:90
const QIcon & getIcon() const noexcept
Definition: editorcommand.h:82
static QString unescapeAmpersand(QString text) noexcept
Definition: editorcommand.cpp:205
Flag
Definition: editorcommand.h:47
QList< QKeySequence > mKeySequences
Definition: editorcommand.h:133
EditorCommand & operator=(const EditorCommand &rhs)=delete
void updateTranslations() noexcept
Definition: editorcommand.cpp:102
void shortcutsChanged(const QList< QKeySequence > &sequences)
const QString & getDescription() const noexcept
Definition: editorcommand.h:81
const QList< QKeySequence > getDefaultKeySequences() const noexcept
Definition: editorcommand.h:84
QString getDisplayText() const noexcept
Definition: editorcommand.h:77
QString mText
Definition: editorcommand.h:127
const QList< QKeySequence > & getKeySequences() const noexcept
Definition: editorcommand.h:87
QAction * setupAction(QAction *action, ActionFlags flags) const noexcept
Definition: editorcommand.cpp:118
const QString & getText() const noexcept
Definition: editorcommand.h:76
const char * mTextNoTr
Definition: editorcommand.h:126
const QString & getIdentifier() const noexcept
Definition: editorcommand.h:75
const Flags & getFlags() const noexcept
Definition: editorcommand.h:83
QAction * createAction(QObject *parent, ActionFlags flags=ActionFlags()) const noexcept
Definition: editorcommand.cpp:109
bool eventFilter(QObject *obj, QEvent *event) noexcept override
Definition: editorcommand.cpp:160
QList< QKeySequence > mDefaultKeySequences
Definition: editorcommand.h:132
ActionFlag
Definition: editorcommand.h:56
@ ReactOnToggle
React on toggled() instead of triggered().
@ QueuedConnection
Create a queued signal/slot connection.
Definition: occmodel.cpp:77