Skip to content

Commit 6945642

Browse files
Merge pull request #26 from YoheiKakiuchi/add_py_bindings
[pybind11] add user button for jupyter
2 parents 2ca0961 + 5032dc8 commit 6945642

6 files changed

Lines changed: 209 additions & 0 deletions

File tree

src/plugin/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ set(sources
44
JupyterPlugin.cpp
55
PythonProcess.cpp
66
cnoid_interpreter.cpp
7+
#
8+
JupyterBar.cpp
79
)
810
set(headers
911
JupyterPlugin.h
1012
PythonProcess.h
1113
cnoid_interpreter.hpp
14+
#
15+
JupyterBar.h
1216
)
1317
if (NOT WIN32)
1418
add_definitions(-Wno-deprecated-declarations)
@@ -58,6 +62,8 @@ else()
5862
add_definitions(-DEXT_BUNDLE)
5963
choreonoid_add_plugin(${target} ${sources} HEADERS ${headers})
6064
target_link_libraries(${target} PUBLIC CnoidUtil ${xeuslib} Threads::Threads)
65+
## pybind11
66+
add_subdirectory(pybind11)
6167
## install
6268
install(FILES ../../share/jupyter/kernels/choreonoid/kernel.json DESTINATION ${CHOREONOID_SHARE_SUBDIR}/jupyter/kernels/choreonoid)
6369
endif()

src/plugin/JupyterBar.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}

src/plugin/JupyterBar.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef CNOID_JUPYTER_PLUGIN_BAR_H
2+
#define CNOID_JUPYTER_PLUGIN_BAR_H
3+
4+
#include <cnoid/ToolBar>
5+
6+
#include "exportdecl.h"
7+
8+
namespace cnoid {
9+
10+
class CNOID_EXPORT JupyterBar : public ToolBar
11+
{
12+
public:
13+
static JupyterBar* instance();
14+
virtual ~JupyterBar();
15+
16+
bool addUserButton(const std::string &name);
17+
bool addUserToggleButton(const std::string &name);
18+
long getUserButton(const std::string &name);
19+
bool getUserButtonState(const std::string &name);
20+
private:
21+
JupyterBar();
22+
23+
class Impl;
24+
Impl* impl;
25+
};
26+
27+
}
28+
#endif

src/plugin/JupyterPlugin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "JupyterPlugin.h"
22
#include "PythonProcess.h"
3+
#include "JupyterBar.h"
34
#include <fmt/format.h>
45
//#define IRSL_DEBUG
56
#include "irsl_debug.h"
@@ -39,6 +40,7 @@ bool JupyterPlugin::customizeApplication(AppCustomizationUtil& app)
3940
bool JupyterPlugin::initialize()
4041
{
4142
DEBUG_PRINT();
43+
addToolBar(JupyterBar::instance());
4244
if(!!impl) {
4345
return impl->initialize();
4446
} else {

src/plugin/pybind11/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set(target PyJupyterPlugin)
2+
3+
choreonoid_add_python_module(${target}
4+
PyJupyterPluginModule.cpp
5+
)
6+
7+
target_link_libraries(${target} CnoidJupyterPlugin)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <cnoid/PyUtil>
2+
3+
#include "../JupyterBar.h"
4+
5+
using namespace cnoid;
6+
namespace py = pybind11;
7+
8+
PYBIND11_MODULE(JupyterPlugin, m)
9+
{
10+
m.doc() = "Choreonoid(IRSL) JupyterPlugin module";
11+
12+
auto base = py::module::import("cnoid.Base");
13+
14+
py::class_<JupyterBar> (m, "JupyterBar")
15+
.def_static("instance", &JupyterBar::instance, py::return_value_policy::reference)
16+
.def("addUserButton", &JupyterBar::addUserButton)
17+
.def("addUserToggleButton", &JupyterBar::addUserToggleButton)
18+
.def("getUserButton", &JupyterBar::getUserButton)
19+
.def("getUserButtonState", &JupyterBar::getUserButtonState);
20+
}

0 commit comments

Comments
 (0)