Fcitx
action.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "action.h"
9 #include <memory>
10 #include <string>
12 #include "fcitx-utils/key.h"
13 #include "fcitx-utils/macros.h"
14 #include "menu.h"
15 #include "userinterfacemanager.h"
16 
17 namespace fcitx {
18 
19 class ActionPrivate : QPtrHolder<Action> {
20 public:
22  std::string name_;
23  int id_ = 0;
24  bool checkable_ = false;
25  bool separator_ = false;
26  KeyList hotkey_;
27  FCITX_DEFINE_SIGNAL_PRIVATE(Action, Update);
28 };
29 
30 Action::Action() : d_ptr(std::make_unique<ActionPrivate>(this)) {}
31 
32 Action::~Action() { destroy(); }
33 
34 bool Action::isSeparator() const {
35  FCITX_D();
36  return d->separator_;
37 }
38 
39 Action &Action::setSeparator(bool separator) {
40  FCITX_D();
41  d->separator_ = separator;
42  return *this;
43 }
44 
45 bool Action::registerAction(const std::string &name,
46  UserInterfaceManager *manager) {
47  return manager->registerAction(name, this);
48 }
49 
50 void Action::setName(const std::string &name) {
51  FCITX_D();
52  d->name_ = name;
53 }
54 
55 int Action::id() {
56  FCITX_D();
57  return d->id_;
58 }
59 
60 void Action::setId(int id) {
61  FCITX_D();
62  d->id_ = id;
63 }
64 
65 Action &Action::setCheckable(bool checkable) {
66  FCITX_D();
67  d->checkable_ = checkable;
68  return *this;
69 }
70 
71 bool Action::isCheckable() const {
72  FCITX_D();
73  return d->checkable_;
74 }
75 
76 void Action::setMenu(Menu *menu) {
77  auto *oldMenu = this->menu();
78  if (oldMenu) {
79  oldMenu->removeParent(this);
80  }
81  if (menu) {
82  menu->addParent(this);
83  }
84 }
85 
87  auto childList = childs();
88  if (!childList.empty()) {
89  return static_cast<Menu *>(childList.front());
90  }
91  return nullptr;
92 }
93 
94 const std::string &Action::name() const {
95  FCITX_D();
96  return d->name_;
97 }
98 
99 void Action::update(InputContext *ic) { emit<Update>(ic); }
100 
101 const KeyList &Action::hotkey() const {
102  FCITX_D();
103  return d->hotkey_;
104 }
105 
106 void Action::setHotkey(const KeyList &hotkey) {
107  FCITX_D();
108  d->hotkey_ = hotkey;
109 }
110 
111 class SimpleActionPrivate : public QPtrHolder<Action> {
112 public:
115  std::string longText_;
116  std::string shortText_;
117  std::string icon_;
118  bool checked_ = false;
119 };
120 
121 SimpleAction::SimpleAction()
122  : d_ptr(std::make_unique<SimpleActionPrivate>(this)) {}
123 
124 FCITX_DEFINE_DEFAULT_DTOR(SimpleAction);
125 
126 void SimpleAction::setIcon(const std::string &icon) {
127  FCITX_D();
128  d->icon_ = icon;
129 }
130 
131 void SimpleAction::setChecked(bool checked) {
132  FCITX_D();
133  d->checked_ = checked;
134 }
135 
136 void SimpleAction::setShortText(const std::string &text) {
137  FCITX_D();
138  d->shortText_ = text;
139 }
140 
141 void SimpleAction::setLongText(const std::string &text) {
142  FCITX_D();
143  d->longText_ = text;
144 }
145 
146 std::string SimpleAction::icon(InputContext * /*unused*/) const {
147  FCITX_D();
148  return d->icon_;
149 }
150 
151 bool SimpleAction::isChecked(InputContext * /*unused*/) const {
152  FCITX_D();
153  return d->checked_;
154 }
155 
156 std::string SimpleAction::shortText(InputContext * /*unused*/) const {
157  FCITX_D();
158  return d->shortText_;
159 }
160 
161 std::string SimpleAction::longText(InputContext * /*unused*/) const {
162  FCITX_D();
163  return d->longText_;
164 }
165 
167  emit<SimpleAction::Activated>(ic);
168 }
169 } // namespace fcitx
std::string icon(InputContext *) const override
Icon name of this action of given input context.
Definition: action.cpp:146
void activate(fcitx::InputContext *) override
Activate this action.
Definition: action.cpp:166
bool isChecked(InputContext *) const override
Return if this action is checked.
Definition: action.cpp:151
std::string shortText(InputContext *) const override
Short description for this action of given input context.
Definition: action.cpp:156
bool registerAction(const std::string &name, Action *action)
Register an named action.
Action & setSeparator(bool separator)
Set whether this action is a separator.
Definition: action.cpp:39
Definition: action.cpp:17
void setMenu(Menu *menu)
Set the sub menu of this action.
Definition: action.cpp:76
The Action class provides an abstraction for user commands that can be added to user interfaces...
Definition: action.h:34
bool registerAction(const std::string &name, UserInterfaceManager *uiManager)
Register an action to UserInterfaceManager.
Definition: action.cpp:45
void setHotkey(const KeyList &hotkey)
Set associated hotkey for display.
Definition: action.cpp:106
Utilities to enable use object with signal.
int id()
Return the unique integer id of action.
Definition: action.cpp:55
Manager class for user interface.
Action class.
Action & setCheckable(bool checkable)
Set whether this action is a checkable action.
Definition: action.cpp:65
const KeyList & hotkey() const
Hotkey bound to the action.
Definition: action.cpp:101
bool isSeparator() const
Whether the action is a separator action.
Definition: action.cpp:34
#define FCITX_DEFINE_SIGNAL_PRIVATE(CLASS_NAME, NAME)
Declare a signal in pimpl class.
void update(InputContext *ic)
Notify that this action is required to be updated of given input context.
Definition: action.cpp:99
bool isCheckable() const
Whether the action is a checkable action.
Definition: action.cpp:71
Menu that contains a list of actions.
Definition: menu.h:31
const std::string & name() const
The action name when this action is registered.
Definition: action.cpp:94
std::string longText(InputContext *) const override
Return a long description for this action.
Definition: action.cpp:161
An input context represents a client of Fcitx.
Definition: inputcontext.h:47
Class to represent a key.
Menu * menu()
Return the sub menu of this action.
Definition: action.cpp:86