Skip to content

Commit

Permalink
Add support for SVG display
Browse files Browse the repository at this point in the history
Issue #53
  • Loading branch information
barche committed Feb 18, 2018
1 parent b26ae10 commit aaf11a0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion deps/src/qmlwrap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_definitions(-DJULIA_ENABLE_THREADING)

find_package(Qt5Quick)
find_package(Qt5Core)
find_package(Qt5Svg)
find_package(Qt5Widgets)
find_package(JlCxx)

Expand Down Expand Up @@ -77,7 +78,7 @@ add_library(qmlwrap SHARED
wrap_qml.cpp
${MOC_BUILT_SOURCES} ${UI_BUILT_SOURCES} ${RESOURCES})

target_link_libraries(qmlwrap Qt5::Core Qt5::Quick Qt5::Widgets JlCxx::jlcxx)
target_link_libraries(qmlwrap Qt5::Core Qt5::Quick Qt5::Svg Qt5::Widgets JlCxx::jlcxx)

install(TARGETS
qmlwrap
Expand Down
28 changes: 27 additions & 1 deletion deps/src/qmlwrap/julia_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ JuliaDisplay::JuliaDisplay(QQuickItem *parent) : QQuickPaintedItem(parent)

void JuliaDisplay::paint(QPainter *painter)
{
painter->drawPixmap(0,0,m_pixmap);
if(!m_pixmap.isNull())
{
painter->drawPixmap(0,0,m_pixmap);
}
else if(m_svg_renderer != nullptr)
{
m_svg_renderer->render(painter);
}
}

void JuliaDisplay::load_png(jlcxx::ArrayRef<unsigned char> data)
{
if(m_svg_renderer != nullptr)
{
delete m_svg_renderer;
m_svg_renderer = nullptr;
}
if(m_pixmap.isNull())
{
clear();
Expand All @@ -28,6 +40,20 @@ void JuliaDisplay::load_png(jlcxx::ArrayRef<unsigned char> data)
update();
}

void JuliaDisplay::load_svg(jlcxx::ArrayRef<unsigned char> data)
{
if(m_svg_renderer == nullptr)
{
m_svg_renderer = new QSvgRenderer(this);
}

if(!m_svg_renderer->load(QByteArray(reinterpret_cast<char*>(data.data()), data.size())))
{
qWarning() << "Failed to load SVG data";
}
update();
}

void JuliaDisplay::clear()
{
m_pixmap = QPixmap(width(), height());
Expand Down
4 changes: 4 additions & 0 deletions deps/src/qmlwrap/julia_display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QObject>
#include <QPixmap>
#include <QQuickPaintedItem>
#include <QSvgRenderer>

namespace qmlwrap
{
Expand All @@ -21,11 +22,14 @@ class JuliaDisplay : public QQuickPaintedItem
void paint(QPainter *painter);

void load_png(jlcxx::ArrayRef<unsigned char> data);
void load_svg(jlcxx::ArrayRef<unsigned char> data);

void clear();

private:
QPixmap m_pixmap;
QSvgRenderer* m_svg_renderer = nullptr;

};

} // namespace qmlwrap
Expand Down
3 changes: 2 additions & 1 deletion deps/src/qmlwrap/wrap_qml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ JULIA_CPP_MODULE_BEGIN(registry)
});

qml_module.add_type<qmlwrap::JuliaDisplay>("JuliaDisplay", julia_type("CppDisplay"))
.method("load_png", &qmlwrap::JuliaDisplay::load_png);
.method("load_png", &qmlwrap::JuliaDisplay::load_png)
.method("load_svg", &qmlwrap::JuliaDisplay::load_svg);

qml_module.add_type<QPaintDevice>("QPaintDevice")
.method("width", &QPaintDevice::width)
Expand Down
1 change: 1 addition & 0 deletions example/image.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Base.Test
using QML
using Images # for show of png
using TestImages

function test_display(d::JuliaDisplay)
Expand Down
17 changes: 15 additions & 2 deletions src/QML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,21 @@ end

function Base.display(d::JuliaDisplay, x)
buf = IOBuffer()
Base.show(buf, MIME"image/png"(), x)
load_png(d, take!(buf))
supported_types = (MIME"image/svg+xml"(), MIME"image/png"())
write_methods = (load_svg, load_png)
written = false
for (t,write_method) in zip(supported_types, write_methods)
if mimewritable(t,x)
Base.show(buf, t, x)
write_method(d, take!(buf))
written = true
break
end
end
if !written
throw(ErrorException("Can't display using any of the types $supported_types"))
end

end

function Base.displayable(d::JuliaDisplay, mime::AbstractString)
Expand Down

0 comments on commit aaf11a0

Please sign in to comment.