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 "menu.h"
10 #include "userinterfacemanager.h"
11 
12 namespace fcitx {
13 
14 class ActionPrivate : QPtrHolder<Action> {
15 public:
ActionPrivate(Action * q)16     ActionPrivate(Action *q) : QPtrHolder<Action>(q) {}
17     std::string name_;
18     int id_ = 0;
19     bool checkable_ = false;
20     bool separator_ = false;
21     FCITX_DEFINE_SIGNAL_PRIVATE(Action, Update);
22 };
23 
Action()24 Action::Action() : d_ptr(std::make_unique<ActionPrivate>(this)) {}
25 
~Action()26 Action::~Action() { destroy(); }
27 
isSeparator() const28 bool Action::isSeparator() const {
29     FCITX_D();
30     return d->separator_;
31 }
32 
setSeparator(bool separator)33 Action &Action::setSeparator(bool separator) {
34     FCITX_D();
35     d->separator_ = separator;
36     return *this;
37 }
38 
registerAction(const std::string & name,UserInterfaceManager * manager)39 bool Action::registerAction(const std::string &name,
40                             UserInterfaceManager *manager) {
41     return manager->registerAction(name, this);
42 }
43 
setName(const std::string & name)44 void Action::setName(const std::string &name) {
45     FCITX_D();
46     d->name_ = name;
47 }
48 
id()49 int Action::id() {
50     FCITX_D();
51     return d->id_;
52 }
53 
setId(int id)54 void Action::setId(int id) {
55     FCITX_D();
56     d->id_ = id;
57 }
58 
setCheckable(bool checkable)59 Action &Action::setCheckable(bool checkable) {
60     FCITX_D();
61     d->checkable_ = checkable;
62     return *this;
63 }
64 
isCheckable() const65 bool Action::isCheckable() const {
66     FCITX_D();
67     return d->checkable_;
68 }
69 
setMenu(Menu * menu)70 void Action::setMenu(Menu *menu) {
71     auto *oldMenu = this->menu();
72     if (oldMenu) {
73         oldMenu->removeParent(this);
74     }
75     if (menu) {
76         menu->addParent(this);
77     }
78 }
79 
menu()80 Menu *Action::menu() {
81     auto childList = childs();
82     if (!childList.empty()) {
83         return static_cast<Menu *>(childList.front());
84     }
85     return nullptr;
86 }
87 
name() const88 const std::string &Action::name() const {
89     FCITX_D();
90     return d->name_;
91 }
92 
update(InputContext * ic)93 void Action::update(InputContext *ic) { emit<Update>(ic); }
94 
95 class SimpleActionPrivate : public QPtrHolder<Action> {
96 public:
SimpleActionPrivate(SimpleAction * q)97     SimpleActionPrivate(SimpleAction *q) : QPtrHolder(q) {}
98     FCITX_DEFINE_SIGNAL_PRIVATE(SimpleAction, Activated);
99     std::string longText_;
100     std::string shortText_;
101     std::string icon_;
102     bool checked_ = false;
103 };
104 
SimpleAction()105 SimpleAction::SimpleAction()
106     : Action(), d_ptr(std::make_unique<SimpleActionPrivate>(this)) {}
107 
108 FCITX_DEFINE_DEFAULT_DTOR(SimpleAction);
109 
setIcon(const std::string & icon)110 void SimpleAction::setIcon(const std::string &icon) {
111     FCITX_D();
112     d->icon_ = icon;
113 }
114 
setChecked(bool checked)115 void SimpleAction::setChecked(bool checked) {
116     FCITX_D();
117     d->checked_ = checked;
118 }
119 
setShortText(const std::string & text)120 void SimpleAction::setShortText(const std::string &text) {
121     FCITX_D();
122     d->shortText_ = text;
123 }
124 
setLongText(const std::string & text)125 void SimpleAction::setLongText(const std::string &text) {
126     FCITX_D();
127     d->longText_ = text;
128 }
129 
icon(InputContext *) const130 std::string SimpleAction::icon(InputContext *) const {
131     FCITX_D();
132     return d->icon_;
133 }
134 
isChecked(InputContext *) const135 bool SimpleAction::isChecked(InputContext *) const {
136     FCITX_D();
137     return d->checked_;
138 }
139 
shortText(InputContext *) const140 std::string SimpleAction::shortText(InputContext *) const {
141     FCITX_D();
142     return d->shortText_;
143 }
144 
longText(InputContext *) const145 std::string SimpleAction::longText(InputContext *) const {
146     FCITX_D();
147     return d->longText_;
148 }
149 
activate(InputContext * ic)150 void SimpleAction::activate(InputContext *ic) {
151     emit<SimpleAction::Activated>(ic);
152 }
153 } // namespace fcitx
154