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