Skip to content

Commit e686c17

Browse files
committed
Add initial xwidgets implementation
1 parent dac06e6 commit e686c17

37 files changed

+3025
-181
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ option(
7272
# ============
7373

7474
find_package(xeus-zmq REQUIRED)
75+
find_package(xwidgets REQUIRED)
76+
find_package(xproperty REQUIRED)
7577
find_package(PNG REQUIRED)
7678
find_package(glad REQUIRED)
7779
find_package(glfw3)
@@ -112,6 +114,7 @@ set(
112114
include/xeus-octave/plotstream.hpp
113115
include/xeus-octave/tex2html.hpp
114116
include/xeus-octave/display.hpp
117+
include/xeus-octave/xwidgets.hpp
115118
)
116119

117120
set(
@@ -122,6 +125,7 @@ set(
122125
src/xinterpreter.cpp
123126
src/input.cpp
124127
src/output.cpp
128+
src/xwidgets.cpp
125129
)
126130

127131
set(XEUS_OCTAVE_MAIN_SRC src/main.cpp)
@@ -226,7 +230,7 @@ macro(xeus_octave_create_target target_name linkage output_name)
226230

227231
target_link_libraries(
228232
${target_name}
229-
PUBLIC xtl PkgConfig::octinterp
233+
PUBLIC xtl xwidgets PkgConfig::octinterp
230234
PRIVATE glad::glad glfw PNG::PNG
231235
)
232236
if(XEUS_OCTAVE_USE_SHARED_XEUS)

environment-dev.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ channels:
33
dependencies:
44
# Build dependencies
55
- cxx-compiler
6+
- fortran-compiler
67
- c-compiler
78
- cmake
89
- make
@@ -11,6 +12,7 @@ dependencies:
1112
- libuuid
1213
- xtl
1314
- xeus-zmq =1.*
15+
- xwidgets =0.27.3
1416
- nlohmann_json
1517
- cppzmq
1618
- octave =7.*
@@ -37,4 +39,5 @@ dependencies:
3739
- cmake-format
3840
- plotly
3941
- ipywidgets
42+
- widgetsnbextension =3.6.1
4043
- jupyter-dash

include/xeus-octave/plotstream.hpp

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Giulio Girardi.
2+
* Copyright (C) 2022 Giulio Girardi.
33
*
44
* This file is part of xeus-octave.
55
*
@@ -22,23 +22,81 @@
2222

2323
#include <octave/graphics.h>
2424

25+
#include <xwidgets/ximage.hpp>
26+
27+
namespace oc = octave;
28+
2529
namespace xeus_octave
2630
{
2731

28-
inline std::string getPlotStream(octave::graphics_object const& o)
32+
template <class T> inline T getPlotStream(oc::graphics_object const& o);
33+
34+
/**
35+
* Retrieve from the graphics object the plot_stream property
36+
*/
37+
template <> inline xw::image* getPlotStream<xw::image*>(oc::graphics_object const& o)
38+
{
39+
octave_value ps =
40+
dynamic_cast<oc::figure::properties const&>(o.get_ancestor("figure").get_properties()).get___plot_stream__();
41+
42+
if (ps.isnumeric() && ps.is_scalar_type())
43+
return reinterpret_cast<xw::image*>(ps.long_value());
44+
else
45+
return nullptr;
46+
}
47+
48+
/**
49+
* Retrieve from the graphics object the plot_stream property
50+
*/
51+
template <> inline std::string getPlotStream<std::string>(oc::graphics_object const& o)
52+
{
53+
octave_value ps =
54+
dynamic_cast<oc::figure::properties const&>(o.get_ancestor("figure").get_properties()).get___plot_stream__();
55+
56+
if (ps.is_string())
57+
return ps.string_value();
58+
else
59+
return "";
60+
}
61+
62+
/**
63+
* Set in the graphics object the plot_stream propert
64+
*/
65+
inline void setPlotStream(oc::graphics_object& o, xw::image* p)
2966
{
30-
return dynamic_cast<octave::figure::properties const&>(o.get_ancestor("figure").get_properties())
31-
.get___plot_stream__()
32-
.string_value();
67+
if (o.isa("figure"))
68+
{
69+
auto& fp = dynamic_cast<oc::figure::properties&>(o.get_properties());
70+
fp.set___plot_stream__(reinterpret_cast<intptr_t>(p));
71+
}
3372
}
3473

35-
inline void setPlotStream(octave::graphics_object& o, std::string p)
74+
/**
75+
* Set in the graphics object the plot_stream propert
76+
*/
77+
inline void setPlotStream(oc::graphics_object& o, std::string p)
3678
{
3779
if (o.isa("figure"))
38-
dynamic_cast<octave::figure::properties&>(o.get_properties()).set___plot_stream__(p);
80+
{
81+
auto& fp = dynamic_cast<oc::figure::properties&>(o.get_properties());
82+
fp.set___plot_stream__(p);
83+
}
84+
}
85+
86+
/**
87+
* Set in the graphics object the plot_stream propert (const version)
88+
*/
89+
inline void setPlotStream(oc::graphics_object const& o, xw::image* p)
90+
{
91+
// deCONSTify the graphics_object
92+
auto _go = o;
93+
setPlotStream(_go, p);
3994
}
4095

41-
inline void setPlotStream(octave::graphics_object const& o, std::string p)
96+
/**
97+
* Set in the graphics object the plot_stream propert (const version)
98+
*/
99+
inline void setPlotStream(oc::graphics_object const& o, std::string p)
42100
{
43101
// deCONSTify the graphics_object
44102
auto _go = o;
@@ -47,4 +105,4 @@ inline void setPlotStream(octave::graphics_object const& o, std::string p)
47105

48106
} // namespace xeus_octave
49107

50-
#endif // XEUS_OCTAVE_PLOTSTREAM_H
108+
#endif

include/xeus-octave/tk_notebook.hpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,53 @@
2222

2323
#include <octave/graphics-toolkit.h>
2424
#include <octave/interpreter.h>
25+
#include <vector>
2526

2627
#include "xeus-octave/config.hpp"
2728

2829
namespace xeus_octave::tk::notebook
2930
{
3031

31-
class notebook_graphics_toolkit : public octave::base_graphics_toolkit
32+
class glfw_graphics_toolkit : public octave::base_graphics_toolkit
33+
{
34+
public:
35+
36+
glfw_graphics_toolkit(std::string const& nm);
37+
~glfw_graphics_toolkit();
38+
39+
bool initialize(octave::graphics_object const&) override;
40+
void redraw_figure(octave::graphics_object const&) const override;
41+
virtual void send_figure(
42+
octave::graphics_object const& go, std::vector<char> const& img, int width, int height, double dpr
43+
) const = 0;
44+
};
45+
46+
class notebook_graphics_toolkit : public glfw_graphics_toolkit
3247
{
3348
public:
3449

3550
notebook_graphics_toolkit();
36-
~notebook_graphics_toolkit();
3751

3852
bool is_valid() const override { return true; }
3953

4054
bool initialize(octave::graphics_object const&) override;
41-
void redraw_figure(octave::graphics_object const&) const override;
55+
void send_figure(octave::graphics_object const& go, std::vector<char> const& img, int width, int height, double dpr)
56+
const override;
4257
void show_figure(octave::graphics_object const&) const override;
43-
void update(octave::graphics_object const&, int) override;
58+
};
59+
60+
class widget_graphics_toolkit : public glfw_graphics_toolkit
61+
{
62+
public:
63+
64+
widget_graphics_toolkit();
4465

66+
bool is_valid() const override { return true; }
67+
68+
bool initialize(octave::graphics_object const&) override;
69+
void send_figure(octave::graphics_object const& go, std::vector<char> const& img, int width, int height, double dpr)
70+
const override;
71+
void show_figure(octave::graphics_object const&) const override;
4572
void finalize(octave::graphics_object const&) override;
4673
};
4774

0 commit comments

Comments
 (0)