Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
webpanel.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webgui
3/// \ingroup webwidgets
4/// This macro demonstrates simple openui5 panel, shown with RWebWindow
5///
6/// \macro_code
7///
8/// \author Sergey Linev
9
10
11#include <ROOT/RWebWindow.hxx>
12#include "TBufferJSON.h"
13#include <vector>
14#include <string>
15
16/** Simple structure for ComboBox item */
17struct ComboBoxItem {
18 std::string fId;
19 std::string fName;
20 ComboBoxItem() = default;
21 ComboBoxItem(const std::string &id, const std::string &name) : fId(id), fName(name) {}
22};
23
24/** Full model used to configure openui5 widget */
25struct TestPanelModel {
26 std::string fSampleText;
27 std::vector<ComboBoxItem> fComboItems;
28 std::string fSelectId;
29 std::string fButtonText;
30};
31
32std::shared_ptr<ROOT::RWebWindow> window;
33std::unique_ptr<TestPanelModel> model;
34int sendcnt = 0;
35
36void ProcessConnection(unsigned connid)
37{
38 printf("connection established %u\n", connid);
39 TString json = TBufferJSON::ToJSON(model.get());
40 window->Send(connid, std::string("MODEL:") + json.Data());
41}
42
43void ProcessCloseConnection(unsigned connid)
44{
45 printf("connection closed %u\n", connid);
46}
47
48void ProcessData(unsigned connid, const std::string &arg)
49{
50 if (arg == "REFRESH") {
51 // send model to client again
52 printf("Send model again\n");
53 model->fButtonText = Form("Custom button %d", ++sendcnt);
54 TString json = TBufferJSON::ToJSON(model.get());
55 window->Send(connid, std::string("MODEL:") + json.Data());
56 } else if (arg.find("MODEL:") == 0) {
57 // analyze new model send from client
58 auto m = TBufferJSON::FromJSON<TestPanelModel>(arg.substr(6));
59 if (m) {
60 printf("New model, selected: %s\n", m->fSelectId.c_str());
61 std::swap(model, m);
62 } else {
63 printf("Fail to decode model: %s\n", arg.c_str());
64 }
65 }
66}
67
68void webpanel()
69{
70 // prepare model
71 model = std::make_unique<TestPanelModel>();
72 model->fSampleText = "This is openui5 widget";
73 model->fComboItems = {{"item1", "Text 1"}, {"item2", "Text 2"}, {"item3", "Text 3"}, {"item4", "Text 4"}};
74 model->fSelectId = "item2";
75 model->fButtonText = "Custom button";
76
77 // create window
78 window = ROOT::RWebWindow::Create();
79
80 // Important - defines name of openui5 widget
81 // "localapp" prefix will be point on current directory, where script executed
82 // "localapp.view.TestPanel" means file ./view/TestPanel.view.xml will be loaded
83 window->SetPanelName("localapp.view.TestPanel");
84
85 // Provide window client version to control browser cache
86 // When value changed, URL for JSROOT, UI5 and local files will differ
87 // Therefore web browser automatically reload all these files
88 // window->SetClientVersion("1.2");
89
90 // these are different callbacks
91 window->SetCallBacks(ProcessConnection, ProcessData, ProcessCloseConnection);
92
93 window->SetGeometry(400, 500); // configure window geometry
94
95 window->Show();
96}
nlohmann::json json
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
static std::shared_ptr< RWebWindow > Create()
Create new RWebWindow Using default RWebWindowsManager.
static TString ToJSON(const T *obj, Int_t compact=0, const char *member_name=nullptr)
Definition TBufferJSON.h:75
Basic string class.
Definition TString.h:139
TMarker m
Definition textangle.C:8