GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4LabelPainter.cxx
Go to the documentation of this file.
1 // $Id: TGo4LabelPainter.cxx 1855 2016-02-29 12:56:48Z adamczew $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4LabelPainter.h"
15 
16 #include "TROOT.h"
17 #include "TVirtualPad.h"
18 #include "Riostream.h"
19 
21  TNamed(),
22  fxLabel(0),
23  fdX0(0),
24  fdY0(0),
25  fbIsLabStreamed(kTRUE)
26 {
27  //std::cout <<"TGo4LabelPainter "<< (long)this<<" default ctor "<< std::endl;
29 }
30 
31 TGo4LabelPainter::TGo4LabelPainter(const char* name, const char* title) :
32  TNamed(name, title ? title : "Go4 LabelPainter"),
33  fxLabel(0),
34  fdX0(0),
35  fdY0(0),
36  fbIsLabStreamed(kFALSE)
37 {
38  //std::cout <<"TGo4LabelPainter "<< (long)this<<" ctor from name "<<name << std::endl;
40 }
41 
42 
44 {
45  SetBit(kMustCleanup);
46  fdWidth=0.25;
47  fdHeight=0.22;
48  SetCaption("Empty label");
49  SetTextAlign(12); // left and central aligned
50  //SetTextSize(0.03); // % of pad height
51  SetTextSize(0); // this will let root scale fontsize depending on label frame
52  SetTextFont(102); // proportional font for alignment of equations
53  SetTextColor(GetLineColor());
54 }
55 
57 {
58  //std::cout <<"TGo4LabelPainter dtor of "<< (long)this <<" will delete label"<< fxLabel << std::endl;
59  if (fxLabel!=0) {
60  delete fxLabel;
61  fxLabel = 0;
62  }
63 }
64 
65 void TGo4LabelPainter::PaintLabel(Option_t* opt)
66 {
67  if(gPad==0) return;
68  Double_t xrange=(gPad->GetUxmax()-gPad->GetUxmin());
69  Double_t yrange=(gPad->GetUymax()-gPad->GetUymin());
70  if(!CheckLabel()) {
71  // label was deleted by us or by root:
72  if(fdX0==0)
73  fdX0=xrange/2; // default: place at x center
74  if(fdY0==0)
75  fdY0=yrange/2; // default: place at y center
76  // JAM: these initial coordinates can be problematic if assigned histogram is not yet drawn completely...
78  fxLabel->AppendPad(opt); // only append to pad if not already there
79  } else {
80  // label is exisiting:
81  if (gPad->GetListOfPrimitives()->FindObject(fxLabel)==0)
82  fxLabel->AppendPad(opt); // label was cleared from pad, we redraw it
83 
84  // JAM: catch here the case that label box was drawn with unsuited coordinates:
85  if(((fxLabel->GetX2()- fxLabel->GetX1())<0.05*xrange) || (fxLabel->GetY2()- fxLabel->GetY1())<0.05*yrange)
86  {
87  Double_t x0=xrange/2;
88  Double_t y0=yrange/2;
89  Double_t xmax=0;
90  Double_t ymax=0;
91  LabelCoords(x0,y0,xmax,ymax);
92  fxLabel->SetX1(x0);
93  fxLabel->SetX2(xmax);
94  fxLabel->SetY1(y0);
95  fxLabel->SetY2(ymax);
96  //printf("TGo4LabelPainter::PaintLabel corrected label coords to: (%f %f) (%f %f)",x0,y0,xmax,ymax);
97  }
98 
99  fxLabel->SetLineColor(GetLineColor());
100  fxLabel->Clear(); // remove old text lines
101  }
102  fxLabel->AddText(fxCaption.Data());
103  fxLabel->AddText(" "); // dummy for automatic position of next line
104  fxLabel->AddLine(0,0,0,0);
105 
106  //std::cout <<"TGo4LabelPainter::PaintLabel of this="<< (long)this <<" with label "<<(long) fxLabel << std::endl;
107 
108 }
109 
111 {
112  gROOT->GetListOfCanvases()->RecursiveRemove(fxLabel);
113 
114  // we do not delete label, but restore graphics properties though invisible
115  TString option=opt;
116  if(option.Contains("reset")) {
117  // case of reset option: discard old label geometry if
118  if(CheckLabel()) {
119  delete fxLabel;
120  fxLabel=0;
121  }
122  }
123 }
124 
126 {
127 if(fxLabel)
128  fxLabel->Paint(opt);
129 
130 }
131 
133 {
134 if(fxLabel)
135  fxLabel->Pop();
136 
137 }
138 
139 
140 
142 {
143  if(gPad==0) return 0;
144  Double_t x0=x;
145  Double_t y0=y; // buffer external variables, since LabelCoords will change them
146  Double_t xmax=0;
147  Double_t ymax=0;
148  LabelCoords(x0,y0,xmax,ymax);
149  TGo4Label* label=new TGo4Label(x0,y0,xmax,ymax);
150  //printf("TGo4LabelPainter::CreateCurrentLabel has new label=%d \n",(long) label); std::cout << std::endl;
151  label->SetOwner(this);
152  TAttText::Copy(*label);
153  TAttLine::Copy(*label);
154  TAttFill::Copy(*label);
155  return label;
156 }
157 
159 {
160  if(fbIsLabStreamed && fxLabel!=0) {
161  // case of label was streamed from file: not yet in cleanup list!
162  fxLabel->SetOwner(this);
163  fbIsLabStreamed=kFALSE;
164  //std::cout <<"CheckLabel with fbIsLabStreamed" << std::endl;
165  return kTRUE;
166  }
167 
168  if(TGo4Label::fxLastDeleted==(char*) fxLabel)
169  {
170  // our label was deleted by user mouse menu (or pad clear!)just before
172  fxLabel=0; // reset reference, will re-create label on next paint
173  //std::cout <<"CheckLabel with lastdeleted case" << std::endl;
174  return kFALSE;
175  }
176  //std::cout <<"CheckLabel sees label "<<(long) fxLabel <<" and returns "<< (bool) (fxLabel!=0) << std::endl;
177  return fxLabel!=0;
178 }
179 
180 
181 void TGo4LabelPainter::LabelCoords(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax)
182 {
183  Double_t xpmin =gPad->GetUxmin();
184  Double_t xpmax =gPad->GetUxmax();
185  Double_t ypmin =gPad->GetUymin();
186  Double_t ypmax =gPad->GetUymax(); // this delivers pad coordinates
187  Double_t dx=(xpmax-xpmin);
188  Double_t dy=(ypmax-ypmin);
189  Double_t width=fdWidth*dx;
190  Double_t height=fdHeight*dy; // width is in pad coordinates
191  xmax=gPad->XtoPad(xmin)+width;
192  ymax=gPad->YtoPad(ymin)+height; // calculate label geometry in pad coordinates
193  xmax=gPad->PadtoX(xmax);
194  ymax=gPad->PadtoY(ymax); // back to axis coordinates for initial pavetext
195 }
196 
198 {
199 if(fxLabel)
200  return fxLabel->GetX1();
201 else
202  return 0;
203 }
204 
206 {
207 if(fxLabel)
208  return fxLabel->GetY1();
209 else
210  return 0;
211 }
212 
214 {
215 if(fxLabel)
216  return fxLabel->GetX2();
217 else
218  return 0;
219 }
220 
222 {
223 if(fxLabel)
224  return fxLabel->GetY2();
225 else
226  return 0;
227 }
228 
230 
231 char* TGo4Label::fxLastDeleted = 0;
232 
233 void TGo4Label::Paint(Option_t* opt)
234 {
235 if(gPad==0) return;
236 if(fxOwner)
237 {
238  TPaveText::Paint(opt);
239 }
240 
241 // suppress painting of labels without owner. This case
242 // happens after insert canvas in go4 (separate cloning
243 // of lists of primitive members), thus leading to a
244 // second "ghost label" for markers and conditions
245 }
246 
248 void TGo4Label::ExecuteEvent(Int_t event, Int_t px, Int_t py)
249 {
250 if(gPad==0) return;
251 TPaveText::ExecuteEvent(event,px,py);
252 if(event==kButton1Up)
253  {
254  TGo4LabelPainter* painter=dynamic_cast<TGo4LabelPainter*>(fxOwner);
255  if(painter)
256  {
257  painter->DisplayToFront();
258  //std::cout <<"TGo4Label::ExecuteEvent with Display to front for "<<painter->GetName() << std::endl;
259  }
260  }
261 }
262 
264 
266 
268 void TGo4LabelConnector::Paint(Option_t* opt)
269 {
270 if(gPad==0) return;
271 if(fxOwner) TLine::Paint(opt);
272 // suppress painting of label connectors without owner. This case
273 // happens after insert canvas in go4 (separate cloning
274 // of lists of primitive members), thus leading to a
275 // second "ghost label connector" for markers
276 }
virtual void Paint(Option_t *opt="")
TObject * fxOwner
virtual void UnPaintLabel(Option_t *opt="")
virtual void Paint(Option_t *opt="")
TGo4Label * CreateCurrentLabel(Double_t x, Double_t y)
virtual ~TGo4LabelPainter()
virtual void PaintLabel(Option_t *opt="")
void SetCaption(const char *txt)
void LabelCoords(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax)
static char * fxLastDeleted
void SetOwner(TObject *ob)
static char * fxLastDeleted
virtual void DisplayToFront(Option_t *opt="")
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual void RePaintLabel(Option_t *opt="")