Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBrowserTCanvasWidget.cxx
Go to the documentation of this file.
1// Author: Sergey Linev <S.Linev@gsi.de>
2// Date: 2021-01-25
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-2021, 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
13#include "RBrowserWidget.hxx"
14
16
17#include "TCanvas.h"
18#include "TROOT.h"
19#include "TClass.h"
20#include "TEnv.h"
21#include "TWebCanvas.h"
22
23#include <map>
24
25using namespace ROOT;
26
27using namespace std::string_literals;
28
29
31
32 TString fCanvasName; ///<! canvas name
33 TCanvas *fCanvas{nullptr}; ///<! last canvas pointer
34 TWebCanvas *fWebCanvas{nullptr}; ///<! web implementation, owned by TCanvas
35
36 std::multimap<TVirtualPad *, std::unique_ptr<Browsable::RHolder>> fObjects; ///<! objects holder, associated with pads
37
39 {
40 if (!fCanvas)
41 return false;
42
43 auto c = gROOT->GetListOfCanvases()->FindObject(fCanvasName.Data());
44 if (c && (fCanvas == c))
45 return true;
46
47 fCanvas = nullptr;
48 return false;
49 }
50
51 void SetPrivateCanvasFields(bool on_init)
52 {
54 if (offset > 0) {
55 Int_t *id = (Int_t *)((char *) fCanvas + offset);
56 if (*id == fCanvas->GetCanvasID()) *id = on_init ? 111222333 : -1;
57 } else {
58 printf("ERROR: Cannot modify TCanvas::fCanvasID data member\n");
59 }
60
62 if (offset > 0) {
63 Int_t *id = (Int_t *)((char *) fCanvas + offset);
64 if (*id == fCanvas->GetPixmapID()) *id = on_init ? 332211 : -1;
65 } else {
66 printf("ERROR: Cannot modify TCanvas::fPixmapID data member\n");
67 }
68
70 if (offset > 0) {
71 TPad **moth = (TPad **)((char *) fCanvas + offset);
72 if (*moth == fCanvas->GetMother()) *moth = on_init ? fCanvas : nullptr;
73 } else {
74 printf("ERROR: Cannot set TCanvas::fMother data member\n");
75 }
76
78 if (offset > 0) {
79 UInt_t *cw = (UInt_t *)((char *) fCanvas + offset);
80 if (*cw == fCanvas->GetWw()) *cw = on_init ? 800 : 0;
81 } else {
82 printf("ERROR: Cannot set TCanvas::fCw data member\n");
83 }
84
86 if (offset > 0) {
87 UInt_t *ch = (UInt_t *)((char *) fCanvas + offset);
88 if (*ch == fCanvas->GetWh()) *ch = on_init ? 600 : 0;
89 } else {
90 printf("ERROR: Cannot set TCanvas::fCw data member\n");
91 }
92 }
93
95 {
97 auto l1 = gROOT->GetListOfCleanups();
98 if (!l1->FindObject(fCanvas))
99 l1->Add(fCanvas);
100 auto l2 = gROOT->GetListOfCanvases();
101 if (!l2->FindObject(fCanvas))
102 l2->Add(fCanvas);
103 }
104
105public:
106
108 {
109 fCanvasName = name.c_str();
110
111 fCanvas = new TCanvas(kFALSE);
118 fCanvas->SetBatch(kTRUE); // mark canvas as batch
119 fCanvas->SetEditable(kTRUE); // ensure fPrimitives are created
120
121 Bool_t readonly = gEnv->GetValue("WebGui.FullCanvas", (Int_t) 1) == 0;
122
123 // create implementation
124 fWebCanvas = new TWebCanvas(fCanvas, "title", 0, 0, 800, 600, readonly);
125
126 // use async mode to prevent blocking inside qt5/qt6/cef
128
129 // assign implementation
132 fCanvas->cd();
133
135 }
136
137 RBrowserTCanvasWidget(const std::string &name, std::unique_ptr<TCanvas> &canv) : RBrowserWidget(name)
138 {
139 fCanvas = canv.release();
141 fCanvas->SetBatch(kTRUE); // mark canvas as batch
142
143 Bool_t readonly = gEnv->GetValue("WebGui.FullCanvas", (Int_t) 1) == 0;
144
145 // create implementation
146 fWebCanvas = new TWebCanvas(fCanvas, "title", 0, 0, 800, 600, readonly);
147
148 // use async mode to prevent blocking inside qt5/qt6/cef
150
151 // assign implementation
154 fCanvas->cd();
155
157 }
158
160 {
161 if (!fCanvas || !gROOT->GetListOfCanvases()->FindObject(fCanvas))
162 return;
163
164 {
166 gROOT->GetListOfCleanups()->Remove(fCanvas);
167 }
168
170
171 gROOT->GetListOfCanvases()->Remove(fCanvas);
172
173 if ((fCanvas->GetCanvasID() == -1) && (fCanvas->GetCanvasImp() == fWebCanvas)) {
174 fCanvas->SetCanvasImp(nullptr);
175 delete fWebCanvas;
176 }
177
178 fCanvas->Close();
179 delete fCanvas;
180 }
181
182 std::string GetKind() const override { return "tcanvas"s; }
183
184 void SetActive() override
185 {
186 if (CheckCanvasPointer())
187 fCanvas->cd();
188 }
189
190 void Show(const std::string &arg) override
191 {
192 if (CheckCanvasPointer())
194 }
195
196 std::string GetUrl() override
197 {
198 if (CheckCanvasPointer())
199 return fWebCanvas->GetWebWindow()->GetUrl(false);
200 return ""s;
201 }
202
203 std::string GetTitle() override
204 {
205 return fCanvasName.Data();
206 }
207
208 bool DrawElement(std::shared_ptr<Browsable::RElement> &elem, const std::string &opt = "") override
209 {
210 if (!elem->IsCapable(Browsable::RElement::kActDraw6))
211 return false;
212
213 std::unique_ptr<Browsable::RHolder> obj = elem->GetObject();
214 if (!obj)
215 return false;
216
217 if (!CheckCanvasPointer())
218 return false;
219
220 Browsable::RProvider::ExtendProgressHandle(elem.get(), obj.get());
221
222 std::string drawopt = opt;
223
224 // first remove all objects which may belong to removed pads
225 bool find_removed_pad;
226 do {
227 find_removed_pad = false;
228 for (auto &entry : fObjects)
229 if ((entry.first != fCanvas) && !fCanvas->FindObject(entry.first)) {
230 fObjects.erase(entry.first);
231 find_removed_pad = true;
232 break;
233 }
234 } while (find_removed_pad);
235
236 TVirtualPad *pad = fCanvas;
237 if (gPad && fCanvas->FindObject(gPad))
238 pad = gPad;
239
240 if (drawopt.compare(0,8,"<append>") == 0) {
241 drawopt.erase(0,8);
242 } else {
243 pad->GetListOfPrimitives()->Clear();
244 if (pad == fCanvas)
245 fObjects.clear();
246 else
247 fObjects.erase(pad);
248 pad->Range(0,0,1,1); // set default range
249 }
250
251 if (drawopt == "<dflt>")
252 drawopt = Browsable::RProvider::GetClassDrawOption(obj->GetClass());
253
254 if (Browsable::RProvider::Draw6(pad, obj, drawopt)) {
255 fObjects.emplace(pad, std::move(obj));
256 pad->Modified();
258 return true;
259 }
260
261 return false;
262 }
263
264 void CheckModified() override
265 {
268 }
269
270 bool IsValid() override
271 {
272 return CheckCanvasPointer();
273 }
274
275};
276
277// ======================================================================
278
280protected:
281 std::shared_ptr<RBrowserWidget> Create(const std::string &name) final
282 {
283 return std::make_shared<RBrowserTCanvasWidget>(name);
284 }
285
286 std::shared_ptr<RBrowserWidget> CreateFor(const std::string &name, std::shared_ptr<Browsable::RElement> &elem) final
287 {
288 auto holder = elem->GetObject();
289 if (!holder) return nullptr;
290
291 auto canv = holder->get_unique<TCanvas>();
292 if (!canv) return nullptr;
293
294 return std::make_shared<RBrowserTCanvasWidget>(name, canv);
295 }
296
297public:
301
RBrowserTCanvasProvider sRBrowserTCanvasProvider
#define c(i)
Definition RSha256.hxx:101
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
char name[80]
Definition TGX11.cxx:110
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:406
#define R__LOCKGUARD(mutex)
#define gPad
~RBrowserTCanvasProvider()=default
std::shared_ptr< RBrowserWidget > Create(const std::string &name) final
std::shared_ptr< RBrowserWidget > CreateFor(const std::string &name, std::shared_ptr< Browsable::RElement > &elem) final
TCanvas * fCanvas
! last canvas pointer
TWebCanvas * fWebCanvas
! web implementation, owned by TCanvas
TString fCanvasName
! canvas name
RBrowserTCanvasWidget(const std::string &name, std::unique_ptr< TCanvas > &canv)
void Show(const std::string &arg) override
std::string GetTitle() override
std::string GetKind() const override
std::multimap< TVirtualPad *, std::unique_ptr< Browsable::RHolder > > fObjects
! objects holder, associated with pads
RBrowserTCanvasWidget(const std::string &name)
bool DrawElement(std::shared_ptr< Browsable::RElement > &elem, const std::string &opt="") override
std::string GetUrl() override
void SetPrivateCanvasFields(bool on_init)
@ kActDraw6
can be drawn inside ROOT6 canvas
Definition RElement.hxx:55
static bool Draw6(TVirtualPad *subpad, std::unique_ptr< RHolder > &obj, const std::string &opt="")
Invoke drawing of object on TCanvas sub-pad All existing providers are checked, first checked are cla...
static std::string GetClassDrawOption(const ClassArg &)
Return configured draw option for the class.
static void ExtendProgressHandle(void *handle, void *handle2)
Extend progress handle - to be able react on sub item.
Abstract Web-based widget, which can be used in the RBrowser Used to embed canvas,...
The Canvas class.
Definition TCanvas.h:23
TCanvasImp * GetCanvasImp() const override
Definition TCanvas.h:158
void Close(Option_t *option="") override
Close canvas.
Definition TCanvas.cxx:788
void SetCanvasImp(TCanvasImp *i)
Definition TCanvas.h:205
void UpdateAsync() override
Asynchronous pad update.
Definition TCanvas.cxx:2545
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:719
void SetTitle(const char *title="") override
Set canvas title.
Definition TCanvas.cxx:2155
Int_t GetCanvasID() const override
Definition TCanvas.h:157
void SetName(const char *name="") override
Set canvas name.
Definition TCanvas.cxx:2047
@ kShowToolBar
Definition TCanvas.h:92
@ kMenuBar
Definition TCanvas.h:91
@ kShowEditor
Definition TCanvas.h:93
UInt_t GetWw() const override
Definition TCanvas.h:163
UInt_t GetWh() const override
Definition TCanvas.h:164
static TClass * Class()
void SetBatch(Bool_t batch=kTRUE) override
Toggle batch mode.
Definition TCanvas.cxx:1957
Longptr_t GetDataMemberOffset(const char *membername) const
return offset for member name.
Definition TClass.cxx:3544
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:491
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition TList.cxx:400
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:786
void ResetBit(UInt_t f)
Definition TObject.h:198
The most important graphics class in the ROOT system.
Definition TPad.h:28
Int_t GetPixmapID() const override
Definition TPad.h:266
void SetEditable(Bool_t mode=kTRUE) override
Set pad editable yes/no If a pad is not editable:
Definition TPad.cxx:6055
Bool_t IsModified() const override
Definition TPad.h:274
TVirtualPad * GetMother() const override
Definition TPad.h:259
void SetCanvas(TCanvas *c) override
Definition TPad.h:327
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2700
const char * GetName() const override
Returns name of object.
Definition TPad.h:260
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void Modified(Bool_t flag=1)=0
virtual TList * GetListOfPrimitives() const =0
virtual void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)=0
Basic TCanvasImp ABI implementation for Web-based Graphics Provides painting of main ROOT classes in ...
Definition TWebCanvas.h:35
const std::shared_ptr< ROOT::RWebWindow > & GetWebWindow() const
Definition TWebCanvas.h:192
void ShowWebWindow(const ROOT::RWebDisplayArgs &user_args="")
Show canvas in specified place.
void SetAsyncMode(Bool_t on=kTRUE)
Definition TWebCanvas.h:253
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...