Fcitx
menu.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 "menu.h"
9 
10 namespace fcitx {
11 
12 class MenuPrivate : public QPtrHolder<Menu> {
13 public:
15  std::unordered_map<Action *, ScopedConnection> actions_;
16  FCITX_DEFINE_SIGNAL_PRIVATE(Menu, Update);
17 };
18 
19 Menu::Menu() : d_ptr(std::make_unique<MenuPrivate>(this)) {}
20 
21 Menu::~Menu() { destroy(); }
22 
23 void Menu::addAction(Action *action) { return insertAction(nullptr, action); }
24 
25 void Menu::insertAction(Action *before, Action *action) {
26  FCITX_D();
27  insertChild(before, action);
28  ScopedConnection conn = action->connect<ObjectDestroyed>([this](void *p) {
29  auto *action = static_cast<Action *>(p);
30  removeAction(action);
31  });
32  d->actions_.emplace(std::make_pair(action, std::move(conn)));
33  emit<Update>();
34 }
35 
36 void Menu::removeAction(Action *action) {
37  FCITX_D();
38  removeChild(action);
39  d->actions_.erase(action);
40  emit<Update>();
41 }
42 
43 std::vector<Action *> Menu::actions() {
44  std::vector<Action *> result;
45  for (const auto &p : childs()) {
46  result.push_back(static_cast<Action *>(p));
47  }
48  return result;
49 }
50 } // namespace fcitx
Definition: action.cpp:17
The Action class provides an abstraction for user commands that can be added to user interfaces...
Definition: action.h:34
Connection that will disconnection when it goes out of scope.
Definition: signals.h:88
Menu that contains a list of actions.
Definition: menu.h:31