|
| 1 | +#include "JupyterBar.h" |
| 2 | +#include <algorithm> |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +using namespace cnoid; |
| 6 | + |
| 7 | +namespace cnoid { |
| 8 | + |
| 9 | +class ButtonClass |
| 10 | +{ |
| 11 | +public: |
| 12 | + ToolButton *self; |
| 13 | + std::string name; |
| 14 | + bool state; |
| 15 | + long counter; |
| 16 | + |
| 17 | +public: |
| 18 | + ButtonClass (ToolButton *_self) : self(_self), state(false), counter(0) { } |
| 19 | +}; |
| 20 | + |
| 21 | +class JupyterBar::Impl |
| 22 | +{ |
| 23 | +public: |
| 24 | + Impl(JupyterBar* _self); |
| 25 | + |
| 26 | + JupyterBar *self; |
| 27 | + std::vector<ButtonClass *> buttons; |
| 28 | + |
| 29 | + void addButton(const char *icon, const char *tooltip); |
| 30 | +#if 0 |
| 31 | + void addButton(QIcon &icon, const char *tooltip); |
| 32 | +#endif |
| 33 | + void addToggleButton(const char *icon, const char *tooltip); |
| 34 | + |
| 35 | + ButtonClass *findButton(const std::string &name); |
| 36 | +}; |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +JupyterBar* JupyterBar::instance() |
| 41 | +{ |
| 42 | + static JupyterBar* instance = new JupyterBar; |
| 43 | + return instance; |
| 44 | +} |
| 45 | + |
| 46 | +JupyterBar::JupyterBar() |
| 47 | + : ToolBar("JupyterBar") |
| 48 | +{ |
| 49 | + impl = new Impl(this); |
| 50 | +} |
| 51 | + |
| 52 | +JupyterBar::~JupyterBar() |
| 53 | +{ |
| 54 | + delete impl; |
| 55 | +} |
| 56 | + |
| 57 | +JupyterBar::Impl::Impl(JupyterBar* _self) |
| 58 | +{ |
| 59 | + self = _self; |
| 60 | + |
| 61 | + addButton("JBtn", "Sample Jupyter Button"); |
| 62 | + //addToggleButton("J Toggle", "Jupyter Toggle Button"); |
| 63 | +} |
| 64 | + |
| 65 | +void JupyterBar::Impl::addButton(const char *icon, const char *tooltip) |
| 66 | +{ |
| 67 | + ButtonClass *bt = new ButtonClass(self->addButton(icon)); |
| 68 | + bt->name = icon; |
| 69 | + bt->self->setToolTip(tooltip); |
| 70 | + bt->self->sigClicked().connect( [this, bt] () { |
| 71 | + bt->counter++; |
| 72 | + bt->state = !(bt->state); |
| 73 | + } ); |
| 74 | + // |
| 75 | + buttons.push_back(bt); |
| 76 | +} |
| 77 | +void JupyterBar::Impl::addToggleButton(const char *icon, const char *tooltip) |
| 78 | +{ |
| 79 | + |
| 80 | + ButtonClass *bt = new ButtonClass(self->addToggleButton(icon)); |
| 81 | + bt->name = icon; |
| 82 | + bt->self->setToolTip(tooltip); |
| 83 | + bt->self->sigToggled().connect( [this, bt] (bool _in) { |
| 84 | + bt->counter++; |
| 85 | + bt->state = _in; |
| 86 | + } ); |
| 87 | + // |
| 88 | + buttons.push_back(bt); |
| 89 | +} |
| 90 | +#if 0 |
| 91 | +void JupyterBar::Impl::addButton(QIcon &icon, const char *tooltip) |
| 92 | +{ |
| 93 | + ButtonClass *bt = new ButtonClass(self->addButton(icon)); |
| 94 | + bt->self->setToolTip(tooltip); |
| 95 | + bt->self->sigClicked().connect( [this, bt] () { |
| 96 | + bt->counter++; |
| 97 | + bt->state = !(bt->state); |
| 98 | + } ); |
| 99 | + // |
| 100 | + buttons.push_back(bt); |
| 101 | +} |
| 102 | +#endif |
| 103 | + |
| 104 | +ButtonClass *JupyterBar::Impl::findButton(const std::string &name) |
| 105 | +{ |
| 106 | + auto it = std::find_if(buttons.begin(), buttons.end(), |
| 107 | + [&name] (const auto *bt) { |
| 108 | + return bt && bt->name == name; |
| 109 | + } ); |
| 110 | + return (it != buttons.end()) ? *it : nullptr; |
| 111 | +} |
| 112 | + |
| 113 | +bool JupyterBar::addUserButton(const std::string &name) |
| 114 | +{ |
| 115 | + ButtonClass *bt = impl->findButton(name); |
| 116 | + if (!!bt) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + impl->addButton(name.c_str(), name.c_str()); |
| 120 | + return true; |
| 121 | +} |
| 122 | +bool JupyterBar::addUserToggleButton(const std::string &name) |
| 123 | +{ |
| 124 | + ButtonClass *bt = impl->findButton(name); |
| 125 | + if (!!bt) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + impl->addToggleButton(name.c_str(), name.c_str()); |
| 129 | + return true; |
| 130 | +} |
| 131 | +long JupyterBar::getUserButton(const std::string &name) |
| 132 | +{ |
| 133 | + ButtonClass *bt = impl->findButton(name); |
| 134 | + if (!bt) { |
| 135 | + throw std::runtime_error("Button not found: " + name); |
| 136 | + } |
| 137 | + return bt->counter; |
| 138 | +} |
| 139 | +bool JupyterBar::getUserButtonState(const std::string &name) |
| 140 | +{ |
| 141 | + ButtonClass *bt = impl->findButton(name); |
| 142 | + if (!bt) { |
| 143 | + throw std::runtime_error("Button not found: " + name); |
| 144 | + } |
| 145 | + return bt->state; |
| 146 | +} |
0 commit comments