Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 */
15 struct 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 */
23 struct TestPanelModel {
24  std::string fSampleText;
25  std::vector<ComboBoxItem> fComboItems;
26  std::string fSelectId;
27  std::string fButtonText;
28 };
29 
30 std::shared_ptr<ROOT::Experimental::RWebWindow> window;
31 std::unique_ptr<TestPanelModel> model;
32 
33 
34 void 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 
41 void ProcessCloseConnection(unsigned connid)
42 {
43  printf("connection closed %u\n", connid);
44 }
45 
46 void ProcessData(unsigned connid, const std::string &arg)
47 {
48  if (arg == "REFRESH") {
49  // send model to client again
50  printf("Resend model\n");
51  TString json = TBufferJSON::ToJSON(model.get());
52  window->Send(connid, std::string("MODEL:") + json.Data());
53  } else if (arg.find("MODEL:") == 0) {
54  printf("Decode model %s\n", arg.c_str());
55 
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  }
61  }
62 }
63 
64 void server()
65 {
66  // prepare model
67  model = std::make_unique<TestPanelModel>();
68  model->fSampleText = "This is openui5 widget";
69  model->fComboItems = {{"item1", "Text 1"}, {"item2", "Text 2"}, {"item3", "Text 3"}, {"item4", "Text 4"}};
70  model->fSelectId = "item2";
71  model->fButtonText = "Custom button";
72 
73  // create window
74  window = ROOT::Experimental::RWebWindow::Create();
75 
76  // Important - defines name of openui5 widget
77  // "localapp" prefix will be point on current directory, where script executed
78  // "localapp.view.TestPanel" means file ./view/TestPanel.view.xml will be loaded
79  window->SetPanelName("localapp.view.TestPanel");
80 
81  // Provide window client version to control browser cache
82  // When value changed, URL for JSROOT, UI5 and local files will differ
83  // Therefore web browser automatically reload all these files
84  // window->SetClientVersion("1.2");
85 
86  // these are different callbacks
87  window->SetCallBacks(ProcessConnection, ProcessData, ProcessCloseConnection);
88 
89  window->SetGeometry(400, 500); // configure window geometry
90 
91  window->Show();
92 }