Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
webgui: provide callback argument for Canvas::SaveAs method
Callback function called when file is created (or command fails)
  • Loading branch information
linev committed Jul 27, 2017
commit c820d27a4376eef4e3ae1449290e131ec69c4303
10 changes: 5 additions & 5 deletions graf2d/gpad/v7/inc/ROOT/TCanvas.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
#ifndef ROOT7_TCanvas
#define ROOT7_TCanvas

#include "ROOT/TDrawable.hxx"
#include "ROOT/TypeTraits.hxx"
#include "ROOT/TVirtualCanvasPainter.hxx"

#include <experimental/string_view>
#include <memory>
#include <string>
#include <vector>

#include "ROOT/TDrawable.hxx"
#include "ROOT/TypeTraits.hxx"
#include "ROOT/TVirtualCanvasPainter.hxx"

namespace ROOT {
namespace Experimental {

Expand Down Expand Up @@ -139,7 +139,7 @@ public:
void Update(bool async = false);

/// Save canvas in image file
void SaveAs(const std::string &filename, bool async = false);
void SaveAs(const std::string &filename, bool async = false, CanvasCallback_t callback = nullptr);

/// Get the canvas's title.
const std::string &GetTitle() const { return fTitle; }
Expand Down
9 changes: 6 additions & 3 deletions graf2d/gpad/v7/inc/ROOT/TVirtualCanvasPainter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
#ifndef ROOT7_TVirtualCanvasPainter
#define ROOT7_TVirtualCanvasPainter

#include <memory>

#include "ROOT/TDisplayItem.hxx"

#include <memory>
#include <functional>

namespace ROOT {
namespace Experimental {

using CanvasCallback_t = std::function<void(bool)>;

class TCanvas;

namespace Internal {
Expand Down Expand Up @@ -60,7 +63,7 @@ public:
virtual bool IsCanvasModified(uint64_t) const = 0;

/// perform special action when drawing is ready
virtual void DoWhenReady(const std::string &, const std::string &, bool) = 0;
virtual void DoWhenReady(const std::string &, const std::string &, bool, CanvasCallback_t) = 0;

virtual void NewDisplay(const std::string &where) = 0;

Expand Down
6 changes: 3 additions & 3 deletions graf2d/gpad/v7/src/TCanvas.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ void ROOT::Experimental::TCanvas::Show(const std::string &where)
}
}

void ROOT::Experimental::TCanvas::SaveAs(const std::string &filename, bool async)
void ROOT::Experimental::TCanvas::SaveAs(const std::string &filename, bool async, CanvasCallback_t callback)
{
if (!fPainter)
fPainter = Internal::TVirtualCanvasPainter::Create(*this, true);
if (filename.find(".svg") != std::string::npos)
fPainter->DoWhenReady("SVG", filename, async);
fPainter->DoWhenReady("SVG", filename, async, callback);
else if (filename.find(".png") != std::string::npos)
fPainter->DoWhenReady("PNG", filename, async);
fPainter->DoWhenReady("PNG", filename, async, callback);
}

// TODO: removal from GetHeldCanvases().
12 changes: 8 additions & 4 deletions gui/canvaspainter/v7/src/TCanvasPainter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class TCanvasPainter : public THttpWSHandler,
std::string fName; ///<! command name
std::string fArg; ///<! command arg
bool fRunning; ///<! true when command submitted
WebCommand() : fId(), fName(), fArg(), fRunning(false) {}
ROOT::Experimental::CanvasCallback_t fCallback; ///<! callback function associated with command
WebCommand() : fId(), fName(), fArg(), fRunning(false), fCallback() {}
};

typedef std::list<WebConn> WebConnList;
Expand Down Expand Up @@ -248,7 +249,7 @@ class TCanvasPainter : public THttpWSHandler,
virtual bool IsCanvasModified(uint64_t) const override;

/// perform special action when drawing is ready
virtual void DoWhenReady(const std::string &cmd, const std::string &arg, bool async) final;
virtual void DoWhenReady(const std::string &cmd, const std::string &arg, bool async, ROOT::Experimental::CanvasCallback_t callback) final;

// open new display for the canvas
virtual void NewDisplay(const std::string &where) override;
Expand Down Expand Up @@ -430,7 +431,7 @@ bool TCanvasPainter::WaitWhenCanvasPainted(uint64_t ver)
return false;
}

void TCanvasPainter::DoWhenReady(const std::string &name, const std::string &arg, bool async)
void TCanvasPainter::DoWhenReady(const std::string &name, const std::string &arg, bool async, ROOT::Experimental::CanvasCallback_t callback)
{
if (!async && !fWaitingCmdId.empty()) {
Error("DoWhenReady", "Fail to submit sync command when previous is still awaited - use async");
Expand All @@ -442,6 +443,7 @@ void TCanvasPainter::DoWhenReady(const std::string &name, const std::string &arg
cmd.fName = name;
cmd.fArg = arg;
cmd.fRunning = false;
cmd.fCallback = callback;
fCmds.push_back(cmd);

if (!async) fWaitingCmdId = cmd.fId;
Expand Down Expand Up @@ -472,13 +474,15 @@ void TCanvasPainter::DoWhenReady(const std::string &name, const std::string &arg
/// Remove front command from the command queue
/// If necessary, configured call-back will be invoked

void TCanvasPainter::PopFrontCommand(bool)
void TCanvasPainter::PopFrontCommand(bool result)
{
if (fCmds.size() == 0) return;

// simple condition, which will be checked in waiting loop
if (!fWaitingCmdId.empty() && (fWaitingCmdId == fCmds.front().fId)) fWaitingCmdId.clear();

if (fCmds.front().fCallback) fCmds.front().fCallback(result);

fCmds.pop_front();
}

Expand Down
6 changes: 5 additions & 1 deletion tutorials/v7/draw_v6.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ void draw_v6()

canvas->Update(); // synchronous, wait until painting is finished

canvas->SaveAs("draw.png"); // asynchronous
// request to create PNG file in asynchronous mode and specify lambda function as callback
// when request processed by the client, callback called with result value
canvas->SaveAs("draw.png", true, [](bool res) { std::cout << "Producing PNG done res = " << (res ? "true" : "false") << std::endl; }); // asynchronous

// this function executed in synchronous mode (async = false is default),
// mean previous file saving will be completed as well at this point
canvas->SaveAs("draw.svg"); // asynchronous
}