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 1772ce0
Show file tree
Hide file tree
Showing 37 changed files with 2,748 additions and 212 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
99 changes: 73 additions & 26 deletions include/xeus-octave/plotstream.hpp
@@ -1,44 +1,91 @@
/*
* Copyright (C) 2020 Giulio Girardi.
/***************************************************************************
* Copyright (c) 2022, Giulio Girardi
*
* This file is part of xeus-octave.
* Distributed under the terms of the GNU General Public License v3.
*
* xeus-octave is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* xeus-octave is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xeus-octave. If not, see <http://www.gnu.org/licenses/>.
*/
* The full license is in the file LICENSE, distributed with this software.
****************************************************************************/

#ifndef XEUS_OCTAVE_PLOTSTREAM_H
#define XEUS_OCTAVE_PLOTSTREAM_H
#ifndef XEUS_OCTAVE_PLOTSTREAM_HPP
#define XEUS_OCTAVE_PLOTSTREAM_HPP

#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)
{
return dynamic_cast<octave::figure::properties const&>(o.get_ancestor("figure").get_properties())
.get___plot_stream__()
.string_value();
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 "";
}

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, xw::image* 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__(reinterpret_cast<intptr_t>(p));
}
}

inline void setPlotStream(octave::graphics_object const& 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"))
{
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);
}

/**
* 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 +94,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 1772ce0

Please sign in to comment.