Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TInspectCanvas.cxx
Go to the documentation of this file.
1// @(#)root/gpad:$Id$
2// Author: Rene Brun 08/01/2000
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TROOT.h"
13#include "TDatime.h"
14#include "TGuiFactory.h"
15#include "TInspectCanvas.h"
16#include "TButton.h"
17#include "TClass.h"
18#include "TLine.h"
19#include "TLink.h"
20#include "TDataMember.h"
21#include "TDataType.h"
22#include "TRealData.h"
23#include "strlcpy.h"
24#include "snprintf.h"
25
27
28
29/** \class TInspectorObject
30\ingroup gpad
31
32This class is designed to wrap a Foreign object in order to
33inject it into the Browse sub-system.
34*/
35
36class TInspectorObject : public TObject
37{
38public:
39
40 TInspectorObject(void *obj, TClass *cl) : fObj(obj),fClass(cl) {};
41 ~TInspectorObject(){;}
42
43 void *GetObject() const { return fObj; };
44 void Inspect() const {
45 gGuiFactory->CreateInspectorImp(this, 400, 200);
46 };
47 TClass *IsA() const { return fClass; }
48
49private:
50 void *fObj; //! pointer to the foreign object
51 TClass *fClass; //! pointer to class of the foreign object
52
53};
54
55
56/** \class TInspectCanvas
57\ingroup gpad
58
59A TInspectCanvas is a canvas specialized to inspect Root objects.
60*/
61
62////////////////////////////////////////////////////////////////////////////////
63/// InspectCanvas default constructor.
64
66{
67 fBackward = 0;
68 fForward = 0;
69 fCurObject = 0;
70 fObjects = 0;
71 fLogx = kFALSE;
72 fLogy = kFALSE;
73 SetFillColor(0);
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// InspectCanvas constructor.
78
80 : TCanvas("inspect","ROOT Object Inspector",ww,wh)
81{
82 fBackward = 0;
83 fForward = 0;
84 fCurObject = 0;
85 fObjects = new TList;
86 fLogx = kFALSE;
87 fLogy = kFALSE;
88 SetFillColor(0);
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// InspectCanvas default destructor.
93
95{
96 if (fObjects) {
97 fObjects->Clear("nodelete");
98 delete fObjects;
99 }
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Dump contents of obj in a graphics canvas.
104/// Same action as TObject::Dump but in a graphical form.
105/// In addition pointers to other objects can be followed.
106///
107/// The following picture is the Inspect of a histogram object:
108/// \image html gpad_inspect.png
109
111{
112 Int_t cdate = 0;
113 Int_t ctime = 0;
114 UInt_t *cdatime = 0;
115 Bool_t isdate = kFALSE;
116 Bool_t isbits = kFALSE;
117 const Int_t kname = 1;
118 const Int_t kvalue = 25;
119 const Int_t ktitle = 37;
120 const Int_t kline = 1024;
121 char line[kline];
122 char *pname;
123
124 TClass *cl = obj->IsA();
125 if (cl == 0) return;
126 TInspectorObject *proxy=0;
127 if (!cl->IsTObject()) {
128 // This is possible only if obj is actually a TInspectorObject
129 // wrapping a non-TObject.
130 proxy = (TInspectorObject*)obj;
131 obj = (TObject*)proxy->GetObject();
132 }
133
134 if (!cl->GetListOfRealData()) cl->BuildRealData(obj);
135
136 // Count number of data members in order to resize the canvas
137 TRealData *rd;
138 TIter next(cl->GetListOfRealData());
139 Int_t nreal = cl->GetListOfRealData()->GetSize();
140 if (nreal == 0) return;
141
142 Int_t nrows = 33;
143 if (nreal+7 > nrows) nrows = nreal+7;
144 Int_t nh = nrows*15;
145 Int_t nw = 700;
146 TVirtualPad *canvas = GetVirtCanvas();
147 if (canvas) {
148 canvas->Clear(); // remove primitives from canvas
149 canvas->SetCanvasSize(nw, nh); // set new size of drawing area
150 canvas->Range(0,-3,20,nreal+4);
151 }
152
153 Float_t xvalue = 5;
154 Float_t xtitle = 8;
155 Float_t dy = 1;
156 Float_t ytext = Float_t(nreal) - 1.5;
157 Float_t tsize = 0.99/ytext;
158 if (tsize < 0.02) tsize = 0.02;
159 if (tsize > 0.03) tsize = 0.03;
160
161 // Create text objects
162 TText tname, tvalue, ttitle;
163 TText *tval;
164 tname.SetTextFont(61);
165 tname.SetTextAngle(0);
166 tname.SetTextAlign(12);
167 tname.SetTextColor(1);
168 tname.SetTextSize(tsize);
169 tvalue.SetTextFont(61);
170 tvalue.SetTextAngle(0);
171 tvalue.SetTextAlign(12);
172 tvalue.SetTextColor(1);
173 tvalue.SetTextSize(tsize);
174 ttitle.SetTextFont(62);
175 ttitle.SetTextAngle(0);
176 ttitle.SetTextAlign(12);
177 ttitle.SetTextColor(1);
178 ttitle.SetTextSize(tsize);
179
180 Float_t x1 = 0.2;
181 Float_t x2 = 19.8;
182 Float_t y1 = -0.5;
183 Float_t y2 = Float_t(nreal) - 0.5;
184 Float_t y3 = y2 + 1;
185 Float_t y4 = y3 + 1.5;
186 Float_t db = 25./GetWh();
187 Float_t btop = 0.999;
188
189 // Draw buttons
190 fBackward = new TButton("backward","TInspectCanvas::GoBackward();",.01,btop-db,.15,btop);
191 fBackward->Draw();
192 fBackward->SetToolTipText("Inspect previous object");
193 fForward = new TButton("forward", "TInspectCanvas::GoForward();", .21,btop-db,.35,btop);
194 fForward->Draw();
195 fForward->SetToolTipText("Inspect next object");
196
197 // Draw surrounding box and title areas
198 TLine frame;
199 frame.SetLineColor(1);
200 frame.SetLineStyle(1);
201 frame.SetLineWidth(1);
202 frame.DrawLine(x1, y1, x2, y1);
203 frame.DrawLine(x2, y1, x2, y4);
204 frame.DrawLine(x2, y4, x1, y4);
205 frame.DrawLine(x1, y4, x1, y1);
206 frame.DrawLine(x1, y2, x2, y2);
207 frame.DrawLine(x1, y3, x2, y3);
208 frame.DrawLine(xvalue, y1, xvalue, y3);
209 frame.DrawLine(xtitle, y1, xtitle, y3);
210 ttitle.SetTextSize(0.8*tsize);
211 ttitle.SetTextAlign(21);
212 ttitle.DrawText(0.5*(x1+xvalue), y2+0.1, "Member Name");
213 ttitle.DrawText(0.5*(xvalue+xtitle), y2+0.1, "Value");
214 ttitle.DrawText(0.5*(xtitle+x2), y2+0.1, "Title");
215 ttitle.SetTextSize(1.2*tsize);
216 ttitle.SetTextColor(2);
217 ttitle.SetTextAlign(11);
218 ttitle.DrawText(x1+0.2, y3+0.1, cl->GetName());
219 if (proxy==0) {
220 ttitle.SetTextColor(4);
221 strlcpy(line,obj->GetName(),kline);
222 ttitle.DrawText(xvalue+0.2, y3+0.1, line);
223 ttitle.SetTextColor(6);
224 ttitle.DrawText(xtitle+2, y3+0.1, obj->GetTitle());
225 } else {
226 ttitle.SetTextColor(4);
227 snprintf(line,1023,"%s:%d","Foreign object",0);
228 ttitle.DrawText(xvalue+0.2, y3+0.1, line);
229 ttitle.SetTextColor(6);
230 ttitle.DrawText(xtitle+2, y3+0.1, "no title given");
231 }
232 ttitle.SetTextSize(tsize);
233 ttitle.SetTextColor(1);
234 ttitle.SetTextFont(11);
235 ttitle.SetTextAlign(12);
236
237 //---Now loop on data members-----------------------
238 // We make 3 passes. Faster than one single pass because changing
239 // font parameters is time consuming
240 for (Int_t pass = 0; pass < 3; pass++) {
241 ytext = y2 - 0.5;
242 next.Reset();
243 while ((rd = (TRealData*) next())) {
244 TDataMember *member = rd->GetDataMember();
245 if (!member) continue;
246 TDataType *membertype = member->GetDataType();
247 isdate = kFALSE;
248 if (strcmp(member->GetName(),"fDatime") == 0 && membertype && membertype->GetType() == kUInt_t) {
249 isdate = kTRUE;
250 }
251 isbits = kFALSE;
252 if (strcmp(member->GetName(),"fBits") == 0 && membertype && membertype->GetType() == kUInt_t) {
253 isbits = kTRUE;
254 }
255
256 // Encode data member name
257 pname = &line[kname];
258 for (Int_t i=0;i<kline;i++) line[i] = ' ';
259 line[kline-1] = 0;
260 strlcpy(pname,rd->GetName(),kline-kname);
261 if (strstr(member->GetFullTypeName(),"**")) strlcat(pname,"**",kline-kname);
262
263 // Encode data value or pointer value
264 tval = &tvalue;
265 Int_t offset = rd->GetThisOffset();
266 char *pointer = (char*)obj + offset;
267 char **ppointer = (char**)(pointer);
268 TLink *tlink = 0;
269
270 TClass *clm=0;
271 if (!membertype) {
272 clm = member->GetClass();
273 }
274
275 if (member->IsaPointer()) {
276 char **p3pointer = (char**)(*ppointer);
277 if (clm && !clm->IsStartingWithTObject() ) {
278 //NOTE: memory leak!
279 p3pointer = (char**)new TInspectorObject(p3pointer,clm);
280 }
281
282 if (!p3pointer) {
283 snprintf(&line[kvalue],kline-kvalue,"->0");
284 } else if (!member->IsBasic()) {
285 if (pass == 1) {
286 tlink = new TLink(xvalue+0.1, ytext, p3pointer);
287 }
288 } else if (membertype) {
289 if (!strcmp(membertype->GetTypeName(), "char"))
290 strlcpy(&line[kvalue], *ppointer,kline-kvalue);
291 else
292 strlcpy(&line[kvalue], membertype->AsString(p3pointer),kline-kvalue);
293 } else if (!strcmp(member->GetFullTypeName(), "char*") ||
294 !strcmp(member->GetFullTypeName(), "const char*")) {
295 strlcpy(&line[kvalue], *ppointer,kline-kvalue);
296 } else {
297 if (pass == 1) tlink = new TLink(xvalue+0.1, ytext, p3pointer);
298 }
299 } else if (membertype)
300 if (isdate) {
301 cdatime = (UInt_t*)pointer;
302 TDatime::GetDateTime(cdatime[0],cdate,ctime);
303 snprintf(&line[kvalue],kline-kvalue,"%d/%d",cdate,ctime);
304 } else if (isbits) {
305 snprintf(&line[kvalue],kline-kvalue,"0x%08x", *(UInt_t*)pointer);
306 } else {
307 strlcpy(&line[kvalue], membertype->AsString(pointer),kline-kvalue);
308 }
309 else
310 snprintf(&line[kvalue],kline-kvalue,"->%lx ", (Long_t)pointer);
311
312 // Encode data member title
313 Int_t ltit = 0;
314 if (isdate == kFALSE && strcmp(member->GetFullTypeName(), "char*") &&
315 strcmp(member->GetFullTypeName(), "const char*")) {
316 Int_t lentit = strlen(member->GetTitle());
317 if (lentit >= kline-ktitle) lentit = kline-ktitle-1;
318 strlcpy(&line[ktitle],member->GetTitle(),kline-ktitle);
319 line[ktitle+lentit] = 0;
320 ltit = ktitle;
321 }
322
323 // Ready to draw the name, value and title columns
324 if (pass == 0)tname.DrawText( x1+0.1, ytext, &line[kname]);
325 if (pass == 1) {
326 if (tlink) {
327 tlink->SetTextFont(61);
328 tlink->SetTextAngle(0);
329 tlink->SetTextAlign(12);
330 tlink->SetTextColor(2);
331 tlink->SetTextSize(tsize);
332 tlink->SetBit(kCanDelete);
333 tlink->Draw();
334 if (strstr(member->GetFullTypeName(),"**")) tlink->SetBit(TLink::kIsStarStar);
335 tlink->SetName(member->GetTypeName());
336 } else {
337 tval->DrawText(xvalue+0.1, ytext, &line[kvalue]);
338 }
339 }
340 if (pass == 2 && ltit) ttitle.DrawText(xtitle+0.3, ytext, &line[ltit]);
341 ytext -= dy;
342 }
343 }
344 Update();
345 fCurObject = obj;
346}
347
348////////////////////////////////////////////////////////////////////////////////
349/// static function , inspect previous object
350
352{
353 TInspectCanvas *inspect = (TInspectCanvas*)(gROOT->GetListOfCanvases())->FindObject("inspect");
354 if (!inspect) return;
355 TObject *cur = inspect->GetCurObject();
356 TObject *obj = inspect->GetObjects()->Before(cur);
357 if (obj) inspect->InspectObject(obj);
358}
359
360////////////////////////////////////////////////////////////////////////////////
361/// static function , inspect next object
362
364{
365 TInspectCanvas *inspect = (TInspectCanvas*)(gROOT->GetListOfCanvases())->FindObject("inspect");
366 if (!inspect) return;
367 TObject *cur = inspect->GetCurObject();
368 TObject *obj = inspect->GetObjects()->After(cur);
369 if (obj) inspect->InspectObject(obj);
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// static function , interface to InspectObject.
374/// Create the InspectCanvas if it does not exist yet.
375
377{
378 TVirtualPad *padsav = gPad;
379 TInspectCanvas *inspect = (TInspectCanvas*)(gROOT->GetListOfCanvases())->FindObject("inspect");
380 if (!inspect) inspect = new TInspectCanvas(700,600);
381 else inspect->cd();
382
383 inspect->InspectObject(obj);
384 inspect->GetObjects()->Add(obj);
385 //obj->SetBit(kMustCleanup);
386
387 if (padsav) padsav->cd();
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Recursively remove object from the list of objects.
392
394{
395 fObjects->Remove(obj);
397}
Cppyy::TCppType_t fClass
static const double x2[5]
static const double x1[5]
const Bool_t kFALSE
Definition RtypesCore.h:92
long Long_t
Definition RtypesCore.h:54
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
@ kUInt_t
Definition TDataType.h:30
R__EXTERN TGuiFactory * gGuiFactory
Definition TGuiFactory.h:66
#define gROOT
Definition TROOT.h:406
#define gPad
#define snprintf
Definition civetweb.c:1540
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:42
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:41
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:42
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:43
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:45
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:46
A TButton object is a user interface object.
Definition TButton.h:18
virtual void Draw(Option_t *option="")
Draw this button with its current attributes.
Definition TButton.cxx:139
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:708
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2504
UInt_t GetWh() const override
Definition TCanvas.h:162
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
void BuildRealData(void *pointer=0, Bool_t isTransient=kFALSE)
Build a full list of persistent data members.
Definition TClass.cxx:2020
Bool_t IsStartingWithTObject() const
Returns true if this class inherits from TObject and if the start of the TObject parts is at the very...
Definition TClass.cxx:5906
TList * GetListOfRealData() const
Definition TClass.h:450
Bool_t IsTObject() const
Return kTRUE is the class inherits from TObject.
Definition TClass.cxx:5915
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
Bool_t IsBasic() const
Return true if data member is a basic type, e.g. char, int, long...
Bool_t IsaPointer() const
Return true if data member is a pointer.
TDataType * GetDataType() const
Definition TDataMember.h:76
const char * GetTypeName() const
Get type of data member, e,g.: "class TDirectory*" -> "TDirectory".
const char * GetFullTypeName() const
Get full type description of data member, e,g.: "class TDirectory*".
TClass * GetClass() const
Definition TDataMember.h:75
Basic data type descriptor (datatype information is obtained from CINT).
Definition TDataType.h:44
Int_t GetType() const
Definition TDataType.h:68
const char * AsString(void *buf) const
Return string containing value in buffer formatted according to the basic data type.
TString GetTypeName()
Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
static void GetDateTime(UInt_t datetime, Int_t &date, Int_t &time)
Static function that returns the date and time.
Definition TDatime.cxx:435
virtual TInspectorImp * CreateInspectorImp(const TObject *obj, UInt_t width, UInt_t height)
Create a batch version of TInspectorImp.
A TInspectCanvas is a canvas specialized to inspect Root objects.
TButton * fBackward
Pointer to the Backward button.
TInspectCanvas()
InspectCanvas default constructor.
static void Inspector(TObject *obj)
static function , interface to InspectObject.
TList * fObjects
List of objects inspected.
TObject * fCurObject
Pointer to object being inspected.
TList * GetObjects() const
TObject * GetCurObject() const
static void GoBackward()
static function , inspect previous object
TButton * fForward
Pointer to the Forward button.
static void GoForward()
static function , inspect next object
virtual void InspectObject(TObject *obj)
Dump contents of obj in a graphics canvas.
virtual void RecursiveRemove(TObject *obj)
Recursively remove object from the list of objects.
virtual ~TInspectCanvas()
InspectCanvas default destructor.
void Reset()
A simple line.
Definition TLine.h:22
virtual TLine * DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition TLine.cxx:89
A doubly linked list.
Definition TList.h:44
virtual void Add(TObject *obj)
Definition TList.h:87
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition TList.cxx:330
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition TList.cxx:822
virtual TObject * Before(const TObject *obj) const
Returns the object before object obj.
Definition TList.cxx:371
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition TList.cxx:402
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:37
virtual void Inspect() const
Dump contents of this object in a graphics canvas.
Definition TObject.cxx:466
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:359
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:696
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:403
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:197
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:58
void RecursiveRemove(TObject *obj) override
Recursively remove object from a pad and its sub-pads.
Definition TPad.cxx:5258
void SetToolTipText(const char *text, Long_t delayms=1000) override
Set tool tip text associated with this pad.
Definition TPad.cxx:6539
TVirtualPad * GetVirtCanvas() const override
Get virtual canvas.
Definition TPad.cxx:2705
Int_t fLogx
(=0 if X linear scale, =1 if log scale)
Definition TPad.h:87
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:2625
Int_t fLogy
(=0 if Y linear scale, =1 if log scale)
Definition TPad.h:88
The TRealData class manages the effective list of all data members for a given class.
Definition TRealData.h:30
TDataMember * GetDataMember() const
Definition TRealData.h:53
virtual const char * GetName() const
Returns name of object.
Definition TRealData.h:52
Long_t GetThisOffset() const
Definition TRealData.h:55
Base class for several text objects.
Definition TText.h:22
virtual TText * DrawText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:175
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void SetCanvasSize(UInt_t ww, UInt_t wh)=0
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
virtual void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)=0
void Clear(Option_t *option="") override=0
TLine * line