Logo ROOT  
Reference Guide
RWebDisplayArgs.cxx
Go to the documentation of this file.
1// Author: Sergey Linev <s.linev@gsi.de>
2// Date: 2018-10-24
3// Warning: This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
14
15#include <ROOT/RConfig.hxx>
16#include <ROOT/RLogger.hxx>
17#include <ROOT/RWebWindow.hxx>
18
19#include "TROOT.h"
20#include <string>
21
22using namespace ROOT::Experimental;
23
25{
26 static RLogChannel sLog("ROOT.WebGUI");
27 return sLog;
28}
29
30
31/** \class ROOT::Experimental::RWebDisplayArgs
32\ingroup webdisplay
33
34Holds different arguments for starting browser with RWebDisplayHandle::Display() method
35
36 */
37
38///////////////////////////////////////////////////////////////////////////////////////////
39/// Default constructor - browser kind configured from gROOT->GetWebDisplay()
40
41RWebDisplayArgs::RWebDisplayArgs()
42{
44}
45
46///////////////////////////////////////////////////////////////////////////////////////////
47/// Constructor - browser kind specified as std::string
48/// See SetBrowserKind() method for description of allowed parameters
49
50RWebDisplayArgs::RWebDisplayArgs(const std::string &browser)
51{
52 SetBrowserKind(browser);
53}
54
55///////////////////////////////////////////////////////////////////////////////////////////
56/// Constructor - browser kind specified as const char *
57/// See SetBrowserKind() method for description of allowed parameters
58
60{
61 SetBrowserKind(browser);
62}
63
64///////////////////////////////////////////////////////////////////////////////////////////
65/// Constructor - specify window width and height
66
67RWebDisplayArgs::RWebDisplayArgs(int width, int height, int x, int y, const std::string &browser)
68{
69 SetSize(width, height);
70 SetPos(x, y);
71 SetBrowserKind(browser);
72}
73
74///////////////////////////////////////////////////////////////////////////////////////////
75/// Constructor - specify master window and channel (if reserved already)
76
77RWebDisplayArgs::RWebDisplayArgs(std::shared_ptr<RWebWindow> master, int channel)
78{
79 SetMasterWindow(master, channel);
80}
81
82
83///////////////////////////////////////////////////////////////////////////////////////////
84/// Destructor
85
87{
88 // must be defined here to correctly call RWebWindow destructor
89}
90
91///////////////////////////////////////////////////////////////////////////////////////////
92/// Set size of web browser window as string like "800x600"
93
94bool RWebDisplayArgs::SetSizeAsStr(const std::string &str)
95{
96 auto separ = str.find("x");
97 if ((separ == std::string::npos) || (separ == 0) || (separ == str.length()-1)) return false;
98
99 int width = 0, height = 0;
100
101 try {
102 width = std::stoi(str.substr(0,separ));
103 height = std::stoi(str.substr(separ+1));
104 } catch(...) {
105 return false;
106 }
107
108 if ((width<=0) || (height<=0))
109 return false;
110
111 SetSize(width, height);
112 return true;
113}
114
115///////////////////////////////////////////////////////////////////////////////////////////
116/// Set position of web browser window as string like "100,100"
117
118bool RWebDisplayArgs::SetPosAsStr(const std::string &str)
119{
120 auto separ = str.find(",");
121 if ((separ == std::string::npos) || (separ == 0) || (separ == str.length()-1)) return false;
122
123 int x = 0, y = 0;
124
125 try {
126 x = std::stoi(str.substr(0,separ));
127 y = std::stoi(str.substr(separ+1));
128 } catch(...) {
129 return false;
130 }
131
132 if ((x<0) || (y<0))
133 return false;
134
135 SetPos(x, y);
136 return true;
137}
138
139///////////////////////////////////////////////////////////////////////////////////////////
140/// Set browser kind as string argument
141/// Recognized values:
142/// chrome - use Google Chrome web browser, supports headless mode from v60, default
143/// firefox - use Mozilla Firefox browser, supports headless mode from v57
144/// native - (or empty string) either chrome or firefox, only these browsers support batch (headless) mode
145/// browser - default system web-browser, no batch mode
146/// safari - Safari browser on Mac
147/// cef - Chromium Embeded Framework, local display, local communication
148/// qt5 - Qt5 WebEngine, local display, local communication
149/// local - either cef or qt5
150/// <prog> - any program name which will be started instead of default browser, like /usr/bin/opera
151
153{
154 std::string kind = _kind;
155
156 auto pos = kind.find("?");
157 if (pos == 0) {
158 SetUrlOpt(kind.substr(1));
159 kind.clear();
160 } else if (pos != std::string::npos) {
161 SetUrlOpt(kind.substr(pos+1));
162 kind.resize(pos);
163 }
164
165 pos = kind.find("size:");
166 if (pos != std::string::npos) {
167 auto epos = kind.find_first_of(" ;", pos+5);
168 if (epos == std::string::npos) epos = kind.length();
169 SetSizeAsStr(kind.substr(pos+5, epos-pos-5));
170 kind.erase(pos, epos-pos);
171 }
172
173 pos = kind.find("pos:");
174 if (pos != std::string::npos) {
175 auto epos = kind.find_first_of(" ;", pos+4);
176 if (epos == std::string::npos) epos = kind.length();
177 SetPosAsStr(kind.substr(pos+4, epos-pos-4));
178 kind.erase(pos, epos-pos);
179 }
180
181 pos = kind.rfind("headless");
182 if ((pos != std::string::npos) && (pos == kind.length() - 8)) {
183 SetHeadless(true);
184 kind.resize(pos);
185 if ((pos > 0) && (kind[pos-1] == ';')) kind.resize(pos-1);
186 }
187
188 // very special handling of qt5 which can specify pointer as a string
189 if (kind.find("qt5:") == 0) {
190 SetDriverData((void *) std::stoul(kind.substr(4)));
191 kind.resize(3);
192 }
193
194 // remove all trailing spaces
195 while ((kind.length() > 0) && (kind[kind.length()-1] == ' '))
196 kind.resize(kind.length()-1);
197
198 // remove any remaining spaces?
199 // kind.erase(remove_if(kind.begin(), kind.end(), std::isspace), kind.end());
200
201 if (kind.empty())
202 kind = gROOT->GetWebDisplay().Data();
203
204 if (kind == "local")
206 else if (kind.empty() || (kind == "native"))
208 else if (kind == "firefox")
210 else if ((kind == "chrome") || (kind == "chromium"))
212 else if ((kind == "cef") || (kind == "cef3"))
214 else if ((kind == "qt") || (kind == "qt5"))
216 else if ((kind == "embed") || (kind == "embedded"))
218 else if (!SetSizeAsStr(kind))
219 SetCustomExec(kind);
220
221 return *this;
222}
223
224/////////////////////////////////////////////////////////////////////
225/// Returns configured browser name
226
228{
229 switch (GetBrowserKind()) {
230 case kChrome: return "chrome";
231 case kFirefox: return "firefox";
232 case kNative: return "native";
233 case kCEF: return "cef";
234 case kQt5: return "qt5";
235 case kLocal: return "local";
236 case kStandard: return "default";
237 case kEmbedded: return "embed";
238 case kCustom:
239 auto pos = fExec.find(" ");
240 return (pos == std::string::npos) ? fExec : fExec.substr(0,pos);
241 }
242
243 return "";
244}
245
246///////////////////////////////////////////////////////////////////////////////////////////
247/// Assign window and channel id where other window will be embed
248
249void RWebDisplayArgs::SetMasterWindow(std::shared_ptr<RWebWindow> master, int channel)
250{
252 fMaster = master;
253 fMasterChannel = channel;
254}
255
256///////////////////////////////////////////////////////////////////////////////////////////
257/// Append string to url options
258/// Add "&" as separator if any options already exists
259
260void RWebDisplayArgs::AppendUrlOpt(const std::string &opt)
261{
262 if (opt.empty()) return;
263
264 if (!fUrlOpt.empty())
265 fUrlOpt.append("&");
266
267 fUrlOpt.append(opt);
268}
269
270///////////////////////////////////////////////////////////////////////////////////////////
271/// Returns full url, which is combined from URL and extra URL options
272/// Takes into account "#" symbol in url - options are inserted before that symbol
273
275{
276 std::string url = GetUrl(), urlopt = GetUrlOpt();
277 if (url.empty() || urlopt.empty()) return url;
278
279 auto rpos = url.find("#");
280 if (rpos == std::string::npos) rpos = url.length();
281
282 if (url.find("?") != std::string::npos)
283 url.insert(rpos, "&");
284 else
285 url.insert(rpos, "?");
286 url.insert(rpos+1, urlopt);
287
288 return url;
289}
290
291///////////////////////////////////////////////////////////////////////////////////////////
292/// Configure custom web browser
293/// Either just name of browser which can be used like "opera"
294/// or full execution string which must includes $url like "/usr/bin/opera $url"
295
296void RWebDisplayArgs::SetCustomExec(const std::string &exec)
297{
299 fExec = exec;
300}
301
302///////////////////////////////////////////////////////////////////////////////////////////
303/// returns custom executable to start web browser
304
306{
307 if (GetBrowserKind() != kCustom)
308 return "";
309
310#ifdef R__MACOSX
311 if ((fExec == "safari") || (fExec == "Safari"))
312 return "open -a Safari";
313#endif
314
315 return fExec;
316}
317
318///////////////////////////////////////////////////////////////////////////////////////////
319/// returns string which can be used as argument in RWebWindow::Show() method
320/// to display web window in provided QWidget
321/// After RWebWindow is displayed created QWebEngineView can be found with the command:
322/// auto view = qparent->findChild<QWebEngineView*>("RootWebView");
323
324std::string RWebDisplayArgs::GetQt5EmbedQualifier(const void *qparent, const std::string &urlopt)
325{
326 std::string where = "qt5";
327 if (qparent) {
328 where.append(":");
329 where.append(std::to_string((unsigned long) qparent));
330 }
331 if (!urlopt.empty()) {
332 where.append("?");
333 where.append(urlopt);
334 }
335 return where;
336}
337
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
#define gROOT
Definition: TROOT.h:404
A log configuration for a channel, e.g.
Definition: RLogger.hxx:101
Holds different arguments for starting browser with RWebDisplayHandle::Display() method.
std::string GetBrowserName() const
Returns configured browser name.
const std::string & GetUrlOpt() const
returns window url options
bool SetPosAsStr(const std::string &str)
Set position of web browser window as string like "100,100".
RWebDisplayArgs & SetPos(int x=-1, int y=-1)
static std::string GetQt5EmbedQualifier(const void *qparent, const std::string &urlopt="")
returns string which can be used as argument in RWebWindow::Show() method to display web window in pr...
void SetHeadless(bool on=true)
set headless mode
int fMasterChannel
! used master channel
void SetMasterWindow(std::shared_ptr< RWebWindow > master, int channel=-1)
Assign window and channel id where other window will be embed.
EBrowserKind GetBrowserKind() const
returns configured browser kind, see EBrowserKind for supported values
void SetCustomExec(const std::string &exec)
set custom executable to start web browser
std::shared_ptr< RWebWindow > fMaster
! master window
void AppendUrlOpt(const std::string &opt)
append extra url options, add "&" as separator if required
std::string GetFullUrl() const
returns window url with append options
RWebDisplayArgs & SetBrowserKind(const std::string &kind)
Set browser kind as string argument Recognized values: chrome - use Google Chrome web browser,...
std::string fExec
! string to run browser, used with kCustom type
std::string GetCustomExec() const
returns custom executable to start web browser
std::string fUrlOpt
! extra URL options, which are append to window URL
@ kFirefox
Mozilla Firefox browser.
@ kCEF
Chromium Embedded Framework - local display with CEF libs.
@ kCustom
custom web browser, execution string should be provided
@ kNative
either Chrome or Firefox - both support major functionality
@ kEmbedded
window will be embedded into other, no extra browser need to be started
@ kLocal
either CEF or Qt5 - both runs on local display without real http server
@ kStandard
standard system web browser, not recognized by ROOT, without batch mode
@ kQt5
QWebEngine libraries - Chrome code packed in qt5.
RWebDisplayArgs & SetUrlOpt(const std::string &opt)
set window url options
void SetDriverData(void *data)
[internal] set web-driver data, used to start window
RWebDisplayArgs & SetSize(int w, int h)
const std::string & GetUrl() const
returns window url
bool SetSizeAsStr(const std::string &str)
Set size of web browser window as string like "800x600".
RWebDisplayArgs()
Default constructor - browser kind configured from gROOT->GetWebDisplay()
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
RLogChannel & WebGUILog()
Log channel for WebGUI diagnostics.