Skip to content

Commit

Permalink
Merge pull request #153 from martinRenou/implement_audio_video_widgets
Browse files Browse the repository at this point in the history
Implement audio and video widgets
  • Loading branch information
SylvainCorlay committed Aug 9, 2018
2 parents 322e88b + 5970200 commit cfcfbeb
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ find_package(xproperty 0.7 REQUIRED)

set(XWIDGETS_HEADERS
${XWIDGETS_INCLUDE_DIR}/xwidgets/xaccordion.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xaudio.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xbinary.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xbox.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xboolean.hpp
Expand All @@ -87,6 +88,7 @@ set(XWIDGETS_HEADERS
${XWIDGETS_INCLUDE_DIR}/xwidgets/xlayout.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xlink.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xmaterialize.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xmedia.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xmaker.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xnumber.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xnumeral.hpp
Expand All @@ -109,13 +111,15 @@ set(XWIDGETS_HEADERS
${XWIDGETS_INCLUDE_DIR}/xwidgets/xtogglebuttons.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xtransport.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xvalid.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xvideo.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xwidget.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xwidgets_config.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xwidgets_config_cling.hpp
)

set(XWIDGETS_SOURCES
${XWIDGETS_SOURCE_DIR}/xaccordion.cpp
${XWIDGETS_SOURCE_DIR}/xaudio.cpp
${XWIDGETS_SOURCE_DIR}/xbox.cpp
${XWIDGETS_SOURCE_DIR}/xbutton.cpp
${XWIDGETS_SOURCE_DIR}/xcheckbox.cpp
Expand All @@ -141,6 +145,7 @@ set(XWIDGETS_SOURCES
${XWIDGETS_SOURCE_DIR}/xtogglebuttons.cpp
${XWIDGETS_SOURCE_DIR}/xradiobuttons.cpp
${XWIDGETS_SOURCE_DIR}/xvalid.cpp
${XWIDGETS_SOURCE_DIR}/xvideo.cpp
)

# Output
Expand Down
146 changes: 146 additions & 0 deletions include/xwidgets/xaudio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/***************************************************************************
* Copyright (c) 2017, Sylvain Corlay and Johan Mabille *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XWIDGETS_AUDIO_HPP
#define XWIDGETS_AUDIO_HPP

#include <cstddef>
#include <string>
#include <vector>

#include "xmaterialize.hpp"
#include "xmedia.hpp"

namespace xw
{
/*********************
* audio declaration *
*********************/

template <class D>
class xaudio : public xmedia<D>
{
public:

using base_type = xmedia<D>;
using derived_type = D;

void serialize_state(xeus::xjson& state, xeus::buffer_sequence&) const;
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&);

XPROPERTY(std::string, derived_type, format, "mp4");
XPROPERTY(bool, derived_type, autoplay, true);
XPROPERTY(bool, derived_type, loop, true);
XPROPERTY(bool, derived_type, controls, true);

protected:

xaudio();
using base_type::base_type;

private:

void set_defaults();
};

using audio = xmaterialize<xaudio>;

using audio_generator = xgenerator<xaudio>;

/*************************
* xaudio implementation *
*************************/

template <class D>
inline void xaudio<D>::serialize_state(xeus::xjson& state, xeus::buffer_sequence& buffers) const
{
base_type::serialize_state(state, buffers);

set_patch_from_property(format, state, buffers);
set_patch_from_property(autoplay, state, buffers);
set_patch_from_property(loop, state, buffers);
set_patch_from_property(controls, state, buffers);
}

template <class D>
inline void xaudio<D>::apply_patch(const xeus::xjson& patch, const xeus::buffer_sequence& buffers)
{
base_type::apply_patch(patch, buffers);

set_property_from_patch(format, patch, buffers);
set_property_from_patch(autoplay, patch, buffers);
set_property_from_patch(loop, patch, buffers);
set_property_from_patch(controls, patch, buffers);
}

template <class D>
inline xaudio<D>::xaudio()
: base_type()
{
set_defaults();
}

template <class D>
inline void xaudio<D>::set_defaults()
{
this->_model_name() = "AudioModel";
this->_view_name() = "AudioView";
}

/**********************
* custom serializers *
**********************/

inline void set_patch_from_property(const decltype(audio::value)& property,
xeus::xjson& patch,
xeus::buffer_sequence& buffers)
{
patch[property.name()] = xbuffer_reference_prefix() + std::to_string(buffers.size());
buffers.emplace_back(property().data(), property().size());
}

inline void set_property_from_patch(decltype(audio::value)& property,
const xeus::xjson& patch,
const xeus::buffer_sequence& buffers)
{
auto it = patch.find(property.name());
if (it != patch.end())
{
using value_type = typename decltype(audio::value)::value_type;
std::size_t index = buffer_index(patch[property.name()].template get<std::string>());
const auto& value_buffer = buffers[index];
const char* value_buf = value_buffer.data<const char>();
property = value_type(value_buf, value_buf + value_buffer.size());
}
}

inline audio_generator audio_from_file(const std::string& filename)
{
return audio_generator().value(read_file(filename));
}

inline audio_generator audio_from_url(const std::string& url)
{
std::vector<char> value(url.cbegin(), url.cend());
return audio_generator().value(value).format("url");
}

/*********************
* precompiled types *
*********************/

#ifndef _WIN32
extern template class xmaterialize<xaudio>;
extern template xmaterialize<xaudio>::xmaterialize();
extern template class xtransport<xmaterialize<xaudio>>;
extern template class xgenerator<xaudio>;
extern template xgenerator<xaudio>::xgenerator();
extern template class xtransport<xgenerator<xaudio>>;
#endif
}
#endif
35 changes: 14 additions & 21 deletions include/xwidgets/ximage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <vector>

#include "xmaterialize.hpp"
#include "xwidget.hpp"
#include "xmedia.hpp"

namespace xw
{
Expand All @@ -23,24 +23,19 @@ namespace xw
*********************/

template <class D>
class ximage : public xwidget<D>
class ximage : public xmedia<D>
{
public:

using base_type = xwidget<D>;
using base_type = xmedia<D>;
using derived_type = D;

using value_type = std::vector<char>;

void serialize_state(xeus::xjson& state, xeus::buffer_sequence&) const;
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&);

XPROPERTY(std::string, derived_type, format, "png");
XPROPERTY(std::string, derived_type, width, "");
XPROPERTY(std::string, derived_type, height, "");
XPROPERTY(value_type, derived_type, value);

const std::vector<xjson_path_type>& buffer_paths() const;

protected:

Expand Down Expand Up @@ -68,7 +63,6 @@ namespace xw
set_patch_from_property(format, state, buffers);
set_patch_from_property(width, state, buffers);
set_patch_from_property(height, state, buffers);
set_patch_from_property(value, state, buffers);
}

template <class D>
Expand All @@ -79,14 +73,6 @@ namespace xw
set_property_from_patch(format, patch, buffers);
set_property_from_patch(width, patch, buffers);
set_property_from_patch(height, patch, buffers);
set_property_from_patch(value, patch, buffers);
}

template <class D>
inline const std::vector<xjson_path_type>& ximage<D>::buffer_paths() const
{
static const std::vector<xjson_path_type> default_buffer_paths = {{"value"}};
return default_buffer_paths;
}

template <class D>
Expand All @@ -101,10 +87,17 @@ namespace xw
{
this->_model_name() = "ImageModel";
this->_view_name() = "ImageView";
this->_model_module() = "@jupyter-widgets/controls";
this->_view_module() = "@jupyter-widgets/controls";
this->_model_module_version() = XWIDGETS_CONTROLS_VERSION;
this->_view_module_version() = XWIDGETS_CONTROLS_VERSION;
}

inline image_generator image_from_file(const std::string& filename)
{
return image_generator().value(read_file(filename));
}

inline image_generator image_from_url(const std::string& url)
{
std::vector<char> value(url.cbegin(), url.cend());
return image_generator().value(value).format("url");
}

/**********************
Expand Down
120 changes: 120 additions & 0 deletions include/xwidgets/xmedia.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/***************************************************************************
* Copyright (c) 2018, Sylvain Corlay, Johan Mabille an *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XWIDGETS_MEDIA_HPP
#define XWIDGETS_MEDIA_HPP

#include <fstream>
#include <cstddef>
#include <string>
#include <vector>

#include "xmaterialize.hpp"
#include "xwidget.hpp"

namespace xw
{
/*********************
* media declaration *
*********************/

template <class D>
class xmedia : public xwidget<D>
{
public:

using base_type = xwidget<D>;
using derived_type = D;

using value_type = std::vector<char>;

void serialize_state(xeus::xjson& state, xeus::buffer_sequence&) const;
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&);

XPROPERTY(value_type, derived_type, value);

const std::vector<xjson_path_type>& buffer_paths() const;

protected:

xmedia();
using base_type::base_type;

private:

void set_defaults();
};

using media = xmaterialize<xmedia>;

using media_generator = xgenerator<xmedia>;

/*************************
* xmedia implementation *
*************************/

template <class D>
inline void xmedia<D>::serialize_state(xeus::xjson& state, xeus::buffer_sequence& buffers) const
{
base_type::serialize_state(state, buffers);

set_patch_from_property(value, state, buffers);
}

template <class D>
inline void xmedia<D>::apply_patch(const xeus::xjson& patch, const xeus::buffer_sequence& buffers)
{
base_type::apply_patch(patch, buffers);

set_property_from_patch(value, patch, buffers);
}

template <class D>
inline const std::vector<xjson_path_type>& xmedia<D>::buffer_paths() const
{
static const std::vector<xjson_path_type> default_buffer_paths = {{"value"}};
return default_buffer_paths;
}

template <class D>
inline xmedia<D>::xmedia()
: base_type()
{
set_defaults();
}

template <class D>
inline void xmedia<D>::set_defaults()
{
this->_model_module() = "@jupyter-widgets/controls";
this->_view_module() = "@jupyter-widgets/controls";
this->_model_module_version() = XWIDGETS_CONTROLS_VERSION;
this->_view_module_version() = XWIDGETS_CONTROLS_VERSION;
}

inline std::vector<char> read_file(const std::string& filename)
{
const char* cstr = filename.c_str();
std::basic_ifstream<char> file(cstr, std::ios::binary);
return std::vector<char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}

/*********************
* precompiled types *
*********************/

#ifndef _WIN32
extern template class xmaterialize<xmedia>;
extern template xmaterialize<xmedia>::xmaterialize();
extern template class xtransport<xmaterialize<xmedia>>;
extern template class xgenerator<xmedia>;
extern template xgenerator<xmedia>::xgenerator();
extern template class xtransport<xgenerator<xmedia>>;
#endif
}
#endif

0 comments on commit cfcfbeb

Please sign in to comment.