Skip to content

Commit 1772ce0

Browse files
committed
Add initial xwidgets implementation
1 parent dac06e6 commit 1772ce0

37 files changed

+2748
-212
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: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,91 @@
1-
/*
2-
* Copyright (C) 2020 Giulio Girardi.
1+
/***************************************************************************
2+
* Copyright (c) 2022, Giulio Girardi
33
*
4-
* This file is part of xeus-octave.
4+
* Distributed under the terms of the GNU General Public License v3.
55
*
6-
* xeus-octave is free software: you can redistribute it and/or modify
7-
* it under the terms of the GNU General Public License as published by
8-
* the Free Software Foundation, either version 3 of the License, or
9-
* (at your option) any later version.
10-
*
11-
* xeus-octave is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU General Public License
17-
* along with xeus-octave. If not, see <http://www.gnu.org/licenses/>.
18-
*/
6+
* The full license is in the file LICENSE, distributed with this software.
7+
****************************************************************************/
198

20-
#ifndef XEUS_OCTAVE_PLOTSTREAM_H
21-
#define XEUS_OCTAVE_PLOTSTREAM_H
9+
#ifndef XEUS_OCTAVE_PLOTSTREAM_HPP
10+
#define XEUS_OCTAVE_PLOTSTREAM_HPP
2211

2312
#include <octave/graphics.h>
2413

14+
#include <xwidgets/ximage.hpp>
15+
16+
namespace oc = octave;
17+
2518
namespace xeus_octave
2619
{
2720

28-
inline std::string getPlotStream(octave::graphics_object const& o)
21+
template <class T> inline T getPlotStream(oc::graphics_object const& o);
22+
23+
/**
24+
* Retrieve from the graphics object the plot_stream property
25+
*/
26+
template <> inline xw::image* getPlotStream<xw::image*>(oc::graphics_object const& o)
27+
{
28+
octave_value ps =
29+
dynamic_cast<oc::figure::properties const&>(o.get_ancestor("figure").get_properties()).get___plot_stream__();
30+
31+
if (ps.isnumeric() && ps.is_scalar_type())
32+
return reinterpret_cast<xw::image*>(ps.long_value());
33+
else
34+
return nullptr;
35+
}
36+
37+
/**
38+
* Retrieve from the graphics object the plot_stream property
39+
*/
40+
template <> inline std::string getPlotStream<std::string>(oc::graphics_object const& o)
2941
{
30-
return dynamic_cast<octave::figure::properties const&>(o.get_ancestor("figure").get_properties())
31-
.get___plot_stream__()
32-
.string_value();
42+
octave_value ps =
43+
dynamic_cast<oc::figure::properties const&>(o.get_ancestor("figure").get_properties()).get___plot_stream__();
44+
45+
if (ps.is_string())
46+
return ps.string_value();
47+
else
48+
return "";
3349
}
3450

35-
inline void setPlotStream(octave::graphics_object& o, std::string p)
51+
/**
52+
* Set in the graphics object the plot_stream propert
53+
*/
54+
inline void setPlotStream(oc::graphics_object& o, xw::image* p)
3655
{
3756
if (o.isa("figure"))
38-
dynamic_cast<octave::figure::properties&>(o.get_properties()).set___plot_stream__(p);
57+
{
58+
auto& fp = dynamic_cast<oc::figure::properties&>(o.get_properties());
59+
fp.set___plot_stream__(reinterpret_cast<intptr_t>(p));
60+
}
3961
}
4062

41-
inline void setPlotStream(octave::graphics_object const& o, std::string p)
63+
/**
64+
* Set in the graphics object the plot_stream propert
65+
*/
66+
inline void setPlotStream(oc::graphics_object& o, std::string p)
67+
{
68+
if (o.isa("figure"))
69+
{
70+
auto& fp = dynamic_cast<oc::figure::properties&>(o.get_properties());
71+
fp.set___plot_stream__(p);
72+
}
73+
}
74+
75+
/**
76+
* Set in the graphics object the plot_stream propert (const version)
77+
*/
78+
inline void setPlotStream(oc::graphics_object const& o, xw::image* p)
79+
{
80+
// deCONSTify the graphics_object
81+
auto _go = o;
82+
setPlotStream(_go, p);
83+
}
84+
85+
/**
86+
* Set in the graphics object the plot_stream propert (const version)
87+
*/
88+
inline void setPlotStream(oc::graphics_object const& o, std::string p)
4289
{
4390
// deCONSTify the graphics_object
4491
auto _go = o;
@@ -47,4 +94,4 @@ inline void setPlotStream(octave::graphics_object const& o, std::string p)
4794

4895
} // namespace xeus_octave
4996

50-
#endif // XEUS_OCTAVE_PLOTSTREAM_H
97+
#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)