Skip to content

Commit

Permalink
Add initial xwidgets implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rapgenic committed Dec 27, 2022
1 parent dac06e6 commit e686c17
Show file tree
Hide file tree
Showing 37 changed files with 3,025 additions and 181 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Expand Up @@ -72,6 +72,8 @@ option(
# ============

find_package(xeus-zmq REQUIRED)
find_package(xwidgets REQUIRED)
find_package(xproperty REQUIRED)
find_package(PNG REQUIRED)
find_package(glad REQUIRED)
find_package(glfw3)
Expand Down Expand Up @@ -112,6 +114,7 @@ set(
include/xeus-octave/plotstream.hpp
include/xeus-octave/tex2html.hpp
include/xeus-octave/display.hpp
include/xeus-octave/xwidgets.hpp
)

set(
Expand All @@ -122,6 +125,7 @@ set(
src/xinterpreter.cpp
src/input.cpp
src/output.cpp
src/xwidgets.cpp
)

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

target_link_libraries(
${target_name}
PUBLIC xtl PkgConfig::octinterp
PUBLIC xtl xwidgets PkgConfig::octinterp
PRIVATE glad::glad glfw PNG::PNG
)
if(XEUS_OCTAVE_USE_SHARED_XEUS)
Expand Down
3 changes: 3 additions & 0 deletions environment-dev.yml
Expand Up @@ -3,6 +3,7 @@ channels:
dependencies:
# Build dependencies
- cxx-compiler
- fortran-compiler
- c-compiler
- cmake
- make
Expand All @@ -11,6 +12,7 @@ dependencies:
- libuuid
- xtl
- xeus-zmq =1.*
- xwidgets =0.27.3
- nlohmann_json
- cppzmq
- octave =7.*
Expand All @@ -37,4 +39,5 @@ dependencies:
- cmake-format
- plotly
- ipywidgets
- widgetsnbextension =3.6.1
- jupyter-dash
76 changes: 67 additions & 9 deletions include/xeus-octave/plotstream.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Giulio Girardi.
* Copyright (C) 2022 Giulio Girardi.
*
* This file is part of xeus-octave.
*
Expand All @@ -22,23 +22,81 @@

#include <octave/graphics.h>

#include <xwidgets/ximage.hpp>

namespace oc = octave;

namespace xeus_octave
{

inline std::string getPlotStream(octave::graphics_object const& o)
template <class T> inline T getPlotStream(oc::graphics_object const& o);

/**
* Retrieve from the graphics object the plot_stream property
*/
template <> inline xw::image* getPlotStream<xw::image*>(oc::graphics_object const& o)
{
octave_value ps =
dynamic_cast<oc::figure::properties const&>(o.get_ancestor("figure").get_properties()).get___plot_stream__();

if (ps.isnumeric() && ps.is_scalar_type())
return reinterpret_cast<xw::image*>(ps.long_value());
else
return nullptr;
}

/**
* Retrieve from the graphics object the plot_stream property
*/
template <> inline std::string getPlotStream<std::string>(oc::graphics_object const& o)
{
octave_value ps =
dynamic_cast<oc::figure::properties const&>(o.get_ancestor("figure").get_properties()).get___plot_stream__();

if (ps.is_string())
return ps.string_value();
else
return "";
}

/**
* Set in the graphics object the plot_stream propert
*/
inline void setPlotStream(oc::graphics_object& o, xw::image* p)
{
return dynamic_cast<octave::figure::properties const&>(o.get_ancestor("figure").get_properties())
.get___plot_stream__()
.string_value();
if (o.isa("figure"))
{
auto& fp = dynamic_cast<oc::figure::properties&>(o.get_properties());
fp.set___plot_stream__(reinterpret_cast<intptr_t>(p));
}
}

inline void setPlotStream(octave::graphics_object& o, std::string p)
/**
* Set in the graphics object the plot_stream propert
*/
inline void setPlotStream(oc::graphics_object& o, std::string p)
{
if (o.isa("figure"))
dynamic_cast<octave::figure::properties&>(o.get_properties()).set___plot_stream__(p);
{
auto& fp = dynamic_cast<oc::figure::properties&>(o.get_properties());
fp.set___plot_stream__(p);
}
}

/**
* Set in the graphics object the plot_stream propert (const version)
*/
inline void setPlotStream(oc::graphics_object const& o, xw::image* p)
{
// deCONSTify the graphics_object
auto _go = o;
setPlotStream(_go, p);
}

inline void setPlotStream(octave::graphics_object const& o, std::string p)
/**
* Set in the graphics object the plot_stream propert (const version)
*/
inline void setPlotStream(oc::graphics_object const& o, std::string p)
{
// deCONSTify the graphics_object
auto _go = o;
Expand All @@ -47,4 +105,4 @@ inline void setPlotStream(octave::graphics_object const& o, std::string p)

} // namespace xeus_octave

#endif // XEUS_OCTAVE_PLOTSTREAM_H
#endif
35 changes: 31 additions & 4 deletions include/xeus-octave/tk_notebook.hpp
Expand Up @@ -22,26 +22,53 @@

#include <octave/graphics-toolkit.h>
#include <octave/interpreter.h>
#include <vector>

#include "xeus-octave/config.hpp"

namespace xeus_octave::tk::notebook
{

class notebook_graphics_toolkit : public octave::base_graphics_toolkit
class glfw_graphics_toolkit : public octave::base_graphics_toolkit
{
public:

glfw_graphics_toolkit(std::string const& nm);
~glfw_graphics_toolkit();

bool initialize(octave::graphics_object const&) override;
void redraw_figure(octave::graphics_object const&) const override;
virtual void send_figure(
octave::graphics_object const& go, std::vector<char> const& img, int width, int height, double dpr
) const = 0;
};

class notebook_graphics_toolkit : public glfw_graphics_toolkit
{
public:

notebook_graphics_toolkit();
~notebook_graphics_toolkit();

bool is_valid() const override { return true; }

bool initialize(octave::graphics_object const&) override;
void redraw_figure(octave::graphics_object const&) const override;
void send_figure(octave::graphics_object const& go, std::vector<char> const& img, int width, int height, double dpr)
const override;
void show_figure(octave::graphics_object const&) const override;
void update(octave::graphics_object const&, int) override;
};

class widget_graphics_toolkit : public glfw_graphics_toolkit
{
public:

widget_graphics_toolkit();

bool is_valid() const override { return true; }

bool initialize(octave::graphics_object const&) override;
void send_figure(octave::graphics_object const& go, std::vector<char> const& img, int width, int height, double dpr)
const override;
void show_figure(octave::graphics_object const&) const override;
void finalize(octave::graphics_object const&) override;
};

Expand Down

0 comments on commit e686c17

Please sign in to comment.