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#include <ROOT/RWebWindow.hxx>
11#include "TBufferJSON.h"
12#include <vector>
13#include <string>
14
15/** Simple structure for ComboBox item */
16struct ComboBoxItem {
17 std::string fId;
18 std::string fName;
19 ComboBoxItem() = default;
20 ComboBoxItem(const std::string &id, const std::string &name) : fId(id), fName(name) {}
21};
22
23/** Full model used to configure openui5 widget */
24struct TestPanelModel {
25 std::string fSampleText;
26 std::vector<ComboBoxItem> fComboItems;
27 std::string fSelectId;
28 std::string fButtonText;
29};
30
31std::shared_ptr<ROOT::RWebWindow> window;
32std::unique_ptr<TestPanelModel> model;
33int sendcnt = 0;
34
35void ProcessConnection(unsigned connid)
36{
37 printf("connection established %u\n", connid);
38 TString json = TBufferJSON::ToJSON(model.get());
39 window->Send(connid, std::string("MODEL:") + json.Data());
40}
41
42void ProcessCloseConnection(unsigned connid)
43{
44 printf("connection closed %u\n", connid);
45}
46
47void ProcessData(unsigned connid, const std::string &arg)
48{
49 if (arg == "REFRESH") {
50 // send model to client again
51 printf("Send model again\n");
52 model->fButtonText = Form("Custom button %d", ++sendcnt);
53 TString json = TBufferJSON::ToJSON(model.get());
54 window->Send(connid, std::string("MODEL:") + json.Data());
55 } else if (arg.find("MODEL:") == 0) {
56 // analyze new model send from client
57 auto m = TBufferJSON::FromJSON<TestPanelModel>(arg.substr(6));
58 if (m) {
59 printf("New model, selected: %s\n", m->fSelectId.c_str());
60 std::swap(model, m);
61 } else {
62 printf("Fail to decode model: %s\n", arg.c_str());
63 }
64 }
65}
66
67void webpanel()
68{
69 // prepare model
70 model = std::make_unique<TestPanelModel>();
71 model->fSampleText = "This is openui5 widget";
72 model->fComboItems = {{"item1", "Text 1"}, {"item2", "Text 2"}, {"item3", "Text 3"}, {"item4", "Text 4"}};
73 model->fSelectId = "item2";
74 model->fButtonText = "Custom button";
75
76 // create window
77 window = ROOT::RWebWindow::Create();
78
79 // Important - defines name of openui5 widget
80 // "localapp" prefix will be point on current directory, where script executed
81 // "localapp.view.TestPanel" means file ./view/TestPanel.view.xml will be loaded
82 window->SetPanelName("localapp.view.TestPanel");
83
84 // Provide window client version to control browser cache
85 // When value changed, URL for JSROOT, UI5 and local files will differ
86 // Therefore web browser automatically reload all these files
87 // window->SetClientVersion("1.2");
88
89 // these are different callbacks
90 window->SetCallBacks(ProcessConnection, ProcessData, ProcessCloseConnection);
91
92 window->SetGeometry(400, 500); // configure window geometry
93
94 window->Show();
95}
nlohmann::json json
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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