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