Skip to content

Commit 4e177b9

Browse files
committed
Provide TWebCanvas::CreateWebCanvas() static method
It creates TCanvas with pre-configured web-based implementation. Such canvas can be directly displayed or embed into other widgets. Simplify use of web-based canvas in RBrowser, qtweb demo or any other tutorials where embed web canvas should be used.
1 parent 197658c commit 4e177b9

File tree

2 files changed

+88
-38
lines changed

2 files changed

+88
-38
lines changed

gui/webgui6/inc/TWebCanvas.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ friend class TWebCanvasTimer;
188188
TWebCanvas(TCanvas *c, const char *name, Int_t x, Int_t y, UInt_t width, UInt_t height, Bool_t readonly = kTRUE);
189189
~TWebCanvas() override;
190190

191+
void CreateWebWindow();
192+
191193
void ShowWebWindow(const ROOT::RWebDisplayArgs &user_args = "");
192194

193195
const std::shared_ptr<ROOT::RWebWindow> &GetWebWindow() const { return fWindow; }
@@ -276,6 +278,8 @@ friend class TWebCanvasTimer;
276278

277279
static TCanvasImp *NewCanvas(TCanvas *c, const char *name, Int_t x, Int_t y, UInt_t width, UInt_t height);
278280

281+
static TCanvas *CreateWebCanvas(const char *name, const char *title, UInt_t width = 1200, UInt_t height = 800);
282+
279283
ClassDefOverride(TWebCanvas, 0) // Web-based implementation for TCanvasImp
280284
};
281285

gui/webgui6/src/TWebCanvas.cxx

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,55 +1344,63 @@ void TWebCanvas::Close()
13441344
}
13451345

13461346
//////////////////////////////////////////////////////////////////////////////////////////
1347-
/// Show canvas in specified place.
1348-
/// If parameter args not specified, default ROOT web display will be used
1347+
/// Create web window for the canvas
13491348

1350-
void TWebCanvas::ShowWebWindow(const ROOT::RWebDisplayArgs &args)
1349+
void TWebCanvas::CreateWebWindow()
13511350
{
1352-
if (!fWindow) {
1353-
fWindow = ROOT::RWebWindow::Create();
1351+
if (fWindow)
1352+
return;
13541353

1355-
fWindow->SetConnLimit(0); // configure connections limit
1354+
fWindow = ROOT::RWebWindow::Create();
13561355

1357-
fWindow->SetDefaultPage("file:rootui5sys/canv/canvas6.html");
1356+
fWindow->SetConnLimit(0); // configure connections limit
13581357

1359-
fWindow->SetCallBacks(
1360-
// connection
1361-
[this](unsigned connid) {
1362-
if (fWindow->GetConnectionId(0) == connid)
1363-
fWebConn.emplace(fWebConn.begin() + 1, connid);
1364-
else
1365-
fWebConn.emplace_back(connid);
1366-
CheckDataToSend(connid);
1367-
},
1368-
// data
1369-
[this](unsigned connid, const std::string &arg) {
1370-
ProcessData(connid, arg);
1371-
CheckDataToSend();
1372-
},
1373-
// disconnect
1374-
[this](unsigned connid) {
1375-
unsigned indx = 0;
1376-
for (auto &c : fWebConn) {
1377-
if (c.fConnId == connid) {
1378-
fWebConn.erase(fWebConn.begin() + indx);
1379-
break;
1380-
}
1381-
indx++;
1358+
fWindow->SetDefaultPage("file:rootui5sys/canv/canvas6.html");
1359+
1360+
fWindow->SetCallBacks(
1361+
// connection
1362+
[this](unsigned connid) {
1363+
if (fWindow->GetConnectionId(0) == connid)
1364+
fWebConn.emplace(fWebConn.begin() + 1, connid);
1365+
else
1366+
fWebConn.emplace_back(connid);
1367+
CheckDataToSend(connid);
1368+
},
1369+
// data
1370+
[this](unsigned connid, const std::string &arg) {
1371+
ProcessData(connid, arg);
1372+
CheckDataToSend();
1373+
},
1374+
// disconnect
1375+
[this](unsigned connid) {
1376+
unsigned indx = 0;
1377+
for (auto &c : fWebConn) {
1378+
if (c.fConnId == connid) {
1379+
fWebConn.erase(fWebConn.begin() + indx);
1380+
break;
13821381
}
1383-
});
1384-
}
1382+
indx++;
1383+
}
1384+
});
1385+
}
13851386

1386-
auto w = Canvas()->GetWindowWidth(), h = Canvas()->GetWindowHeight();
1387-
if ((w > 0) && (w < 50000) && (h > 0) && (h < 30000))
1388-
fWindow->SetGeometry(w, h);
1387+
//////////////////////////////////////////////////////////////////////////////////////////
1388+
/// Show canvas in specified place.
1389+
/// If parameter args not specified, default ROOT web display will be used
1390+
1391+
void TWebCanvas::ShowWebWindow(const ROOT::RWebDisplayArgs &args)
1392+
{
1393+
CreateWebWindow();
13891394

13901395
if ((args.GetBrowserKind() == ROOT::RWebDisplayArgs::kQt5) ||
1391-
(args.GetBrowserKind() == ROOT::RWebDisplayArgs::kQt6) ||
1392-
(args.GetBrowserKind() == ROOT::RWebDisplayArgs::kCEF))
1396+
(args.GetBrowserKind() == ROOT::RWebDisplayArgs::kQt6) || (args.GetBrowserKind() == ROOT::RWebDisplayArgs::kCEF))
13931397
SetLongerPolling(kTRUE);
13941398

1395-
fWindow->Show(args);
1399+
auto w = Canvas()->GetWindowWidth(), h = Canvas()->GetWindowHeight();
1400+
if ((w > 0) && (w < 50000) && (h > 0) && (h < 30000))
1401+
fWindow->SetGeometry(w, h);
1402+
1403+
ROOT::RWebWindow::ShowWindow(fWindow, args);
13961404
}
13971405

13981406
//////////////////////////////////////////////////////////////////////////////////////////
@@ -2962,3 +2970,41 @@ TCanvasImp *TWebCanvas::NewCanvas(TCanvas *c, const char *name, Int_t x, Int_t y
29622970
return imp;
29632971
}
29642972

2973+
//////////////////////////////////////////////////////////////////////////////////////////////////
2974+
/// Create TCanvas and assign TWebCanvas implementation to it
2975+
/// Canvas is not displayed automatically, therefore canv->Show() method must be called
2976+
/// Or canvas can be embed in other widgets.
2977+
2978+
TCanvas *TWebCanvas::CreateWebCanvas(const char *name, const char *title, UInt_t width, UInt_t height)
2979+
{
2980+
auto canvas = new TCanvas(kFALSE);
2981+
canvas->SetName(name);
2982+
canvas->SetTitle(title);
2983+
canvas->ResetBit(TCanvas::kShowEditor);
2984+
canvas->ResetBit(TCanvas::kShowToolBar);
2985+
canvas->SetBit(TCanvas::kMenuBar, kTRUE);
2986+
canvas->SetCanvas(canvas);
2987+
canvas->SetBatch(kTRUE); // mark canvas as batch
2988+
canvas->SetEditable(kTRUE); // ensure fPrimitives are created
2989+
2990+
auto imp = static_cast<TWebCanvas *> (NewCanvas(canvas, name, 0, 0, width, height));
2991+
2992+
canvas->SetCanvasImp(imp);
2993+
2994+
canvas->cd();
2995+
2996+
{
2997+
R__LOCKGUARD(gROOTMutex);
2998+
auto l1 = gROOT->GetListOfCleanups();
2999+
if (!l1->FindObject(canvas))
3000+
l1->Add(canvas);
3001+
auto l2 = gROOT->GetListOfCanvases();
3002+
if (!l2->FindObject(canvas))
3003+
l2->Add(canvas);
3004+
}
3005+
3006+
// ensure creation of web window
3007+
imp->CreateWebWindow();
3008+
3009+
return canvas;
3010+
}

0 commit comments

Comments
 (0)