GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4MarkerPainter.cxx
Go to the documentation of this file.
1 // $Id: TGo4MarkerPainter.cxx 999 2013-07-25 11:58:59Z 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 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 "TGo4MarkerPainter.h"
15 
16 #include "TROOT.h"
17 #include "TList.h"
18 #include "TVirtualPad.h"
19 #include "snprintf.h"
20 
21 #include "TGo4Marker.h"
22 #include "TH1.h"
23 
26  fxMarker(0),
27  fxConnector(0),
28  fbIsConStreamed(kTRUE)
29 {
30 }
31 
32 TGo4MarkerPainter::TGo4MarkerPainter(const char* name, const char* title) :
33  TGo4LabelPainter(name, title),
34  fxMarker(0),
35  fxConnector(0),
36  fbIsConStreamed(kFALSE)
37 {
38 }
39 
41 {
42  if (fxConnector!=0) {
43  delete fxConnector;
44  fxConnector = 0;
45  }
46 }
47 
48 
49 
50 void TGo4MarkerPainter::PaintLabel(Option_t* opt)
51 {
52 if(gPad==0) return;
53 if(fxMarker && fxMarker->HasLabel())
54  {
55  //------ find out initial coordinates for labels near marker boundaries:
56  Double_t xoff=0.015*(gPad->GetUxmax()-gPad->GetUxmin());
57  Double_t yoff=0.015*(gPad->GetUymax()-gPad->GetUymin()); // offset is in pad coordinates
58  Double_t xpmin=gPad->PadtoX(gPad->XtoPad(fxMarker->GetLabelX())+xoff);
59  Double_t ypmin=gPad->PadtoY(gPad->YtoPad(fxMarker->GetLabelY())+yoff);
60  Bool_t drx=fxMarker->IsXDraw();
61  Bool_t dry=fxMarker->IsYDraw();
62  Bool_t drxbin=fxMarker->IsXbinDraw();
63  Bool_t drybin=fxMarker->IsYbinDraw();
64  Bool_t drcont=fxMarker->IsContDraw();
65  TString fmt=fxMarker->GetNumFormat();
66  SetX0(xpmin);
67  SetY0(ypmin); // initial coordinates are real axis coordinates
68  SetLineColor(fxMarker->GetMarkerColor());
69  //SetTextColor(fxMarker->GetMarkerColor());
70  TString cap=fxMarker->GetName();
71  TH1* his=fxMarker->GetHistogram();
72  if(his)
73  {
74  cap+=":";
75  cap+=his->GetName();
76  }
77  SetCaption(cap.Data());
78  TGo4LabelPainter::PaintLabel();// this creates new label
79  if(drx)
80  AddToLabel(Form(Form("X = %s",fmt.Data()),fxMarker->GetX()));
81  if(dry)
82  AddToLabel(Form(Form("Y = %s",fmt.Data()),fxMarker->GetY()));
83  if(drxbin)
84  AddToLabel(Form("Xbin = %d",fxMarker->GetXbin()));
85  if(drybin)
86  AddToLabel(Form("Ybin = %d",fxMarker->GetYbin()));
87  if(drcont)
88  AddToLabel(Form("C = %d",fxMarker->GetCont()));
89  RePaintLabel();
91  }//if(fxMarker && fxMarker->HasLabel())
92 else
93  {
94  UnPaintLabel();
95  }
96 }
97 
99 {
100 if(gPad==0) return;
102 UnPaintConnector(opt);
103 }
104 
105 
107 {
108  if(fbIsConStreamed && fxConnector!=0) {
109  // case of Connector was streamed from file: not yet in cleanup list!
110  fxConnector->SetOwner(this);
111  fbIsConStreamed=kFALSE;
112  return kTRUE;
113  }
115  {
116  // our label was deleted by user mouse menu just before
118  fxConnector=0; // reset reference, will re-create label on next paint
119  return kFALSE;
120  }
121 
122 
123  return fxConnector!=0;
124 }
125 
127 {
128  if(gPad==0) return;
129  if(fxMarker && fxMarker->HasConnector()) {
130  Double_t xmark=0;
131  Double_t ymark=0;
132  Double_t xmark0=fxMarker->GetX();
133  Double_t ymark0=fxMarker->GetY();
134  Double_t xlab,ylab;
135  Double_t xlablo=GetLabelXlo();
136  Double_t ylablo=GetLabelYlo();
137  Double_t xlabup=GetLabelXup();
138  Double_t ylabup=GetLabelYup();
139  Double_t xlabmid=0.5*(xlabup+xlablo);
140  Double_t ylabmid=0.5*(ylabup+ylablo);
141  xlablo=gPad->PadtoX(xlablo);
142  ylablo=gPad->PadtoY(ylablo);
143  xlabup=gPad->PadtoX(xlabup);
144  ylabup=gPad->PadtoY(ylabup);
145  xlabmid=gPad->PadtoX(xlabmid);
146  ylabmid=gPad->PadtoY(ylabmid);
147  // evaluate connection to nearest label edge or side centre:
148  if(TMath::Abs(xlablo-xmark0) < TMath::Abs(xlabup-xmark0))
149  {
150  if(TMath::Abs(xlablo-xmark0) < TMath::Abs(xlabmid-xmark0))
151  xlab=xlablo;
152  else
153  xlab=xlabmid;
154  }
155  else
156  {
157  if(TMath::Abs(xlabup-xmark0) < TMath::Abs(xlabmid-xmark0))
158  xlab=xlabup;
159  else
160  xlab=xlabmid;
161  }
162 
163  if(TMath::Abs(ylablo-ymark0) < TMath::Abs(ylabup-ymark0))
164  {
165  if(TMath::Abs(ylablo-ymark0) < TMath::Abs(ylabmid-ymark0))
166  ylab=ylablo;
167  else
168  ylab=ylabmid;
169  }
170  else
171  {
172  if(TMath::Abs(ylabup-ymark0) < TMath::Abs(ylabmid-ymark0))
173  ylab=ylabup;
174  else
175  ylab=ylabmid;
176  }
177  // offset from marker center:
178  Double_t f=0.92; // fraction of direct distance between marker and label
179  // we calculate connector geometry in pad coordinates for log scale!
180  Double_t xmark_pad=0;
181  Double_t ymark_pad=0;
182  Double_t xlab_pad=gPad->XtoPad(xlab);
183  Double_t ylab_pad=gPad->YtoPad(ylab);
184  Double_t xmark0_pad=gPad->XtoPad(xmark0);
185  Double_t ymark0_pad=gPad->YtoPad(ymark0);
186 // if(gPad->GetLogx() || gPad->GetLogy())
187 // {
188 // //std::cout <<"using percentage for f" << std::endl;
189 // f=0.95;
190 // }
191 // else
192 // {
193 // // note: this method only works if we have equal axis scaling in x and y!
194 // //std::cout <<"using roff for f" << std::endl;
195 // Double_t dr=TMath::Sqrt(TMath::Power(xlab-xmark0,2) + TMath::Power(ylab-ymark0,2));
196 // Size_t msize=fxMarker->GetMarkerSize();
197 // Double_t xoff=0.008*msize*(gPad->GetUxmax()-gPad->GetUxmin());
198 // Double_t yoff=0.008*msize*(gPad->GetUymax()-gPad->GetUymin());
199 // Double_t roff= TMath::Sqrt(TMath::Power(xoff,2) + TMath::Power(yoff,2));
200 // if(dr) f=1- roff/dr;// we do it relative to marker size
201 // }
202 // std::cout <<"f="<<f << std::endl;
203  if(xlab_pad>xmark0_pad)
204  xmark_pad=xlab_pad-f*(xlab_pad-xmark0_pad);
205  else
206  xmark_pad=xlab_pad+f*(xmark0_pad-xlab_pad);
207  if(ylab_pad>ymark0_pad)
208  ymark_pad=ylab_pad-f*(ylab_pad-ymark0_pad);
209  else
210  ymark_pad=ylab_pad+f*(ymark0_pad-ylab_pad);
211 
212  xmark=gPad->PadtoX(xmark_pad); // back to real axis coordinates
213  ymark=gPad->PadtoY(ymark_pad);
214  if(!CheckConnector()) {
215  // connection line not existing or deleted by root:
216  fxConnector=new TGo4LabelConnector(xmark,ymark,xlab,ylab);
217  fxConnector->SetOwner(this);
218  fxConnector->SetLineStyle(4);
219  fxConnector->AppendPad();
220  } else {
221  // connection line is still exisiting:
222  if (gPad->GetListOfPrimitives()->FindObject(fxConnector)==0)
223  fxConnector->AppendPad();// connection line was removed from pad, we redraw it:
224  fxConnector->SetX1(xmark);
225  fxConnector->SetY1(ymark);
226  fxConnector->SetX2(xlab);
227  fxConnector->SetY2(ylab);
228  }
229  fxConnector->SetLineColor(fxMarker->GetMarkerColor());
230  //fxConnector->Paint(); // for direct update of view
231  }
232 else
233  {
234  UnPaintConnector(opt);
235  }
236 }
237 
239 {
240  TString option=opt;
241  if(option.Contains("reset")) {
242  // case of reset option: discard old connector line style
243  if(CheckConnector()) {
244  delete fxConnector;
245  fxConnector=0;
246  }
247  }
248 }
249 
251 {
253 PaintConnector(); // pop line to front if enabled
254 if(fxMarker) fxMarker->TObject::Pop(); // then pop marker
255 TGo4LabelPainter::DisplayToFront(opt); // label itself will be frontmost
256 }
257 
258 
virtual const char * GetName() const
Definition: TGo4Marker.h:116
Int_t GetCont()
Definition: TGo4Marker.cxx:187
void SetX0(Double_t x=0)
void UnPaintConnector(Option_t *opt="")
TGo4LabelConnector * fxConnector
Int_t GetXbin()
Definition: TGo4Marker.cxx:172
virtual void UnPaintLabel(Option_t *opt="")
virtual void PaintLabel(Option_t *opt="")
Bool_t HasConnector()
Definition: TGo4Marker.h:86
TH1 * GetHistogram() const
Definition: TGo4Marker.h:113
virtual void DisplayToFront(Option_t *opt="")
virtual void PaintLabel(Option_t *opt="")
void SetCaption(const char *txt)
Int_t GetYbin()
Definition: TGo4Marker.cxx:179
void SetY0(Double_t y=0)
Bool_t HasLabel()
Definition: TGo4Marker.h:84
Double_t GetLabelY()
Definition: TGo4Marker.cxx:203
const char * GetNumFormat()
Definition: TGo4Marker.h:98
TGo4Marker * fxMarker
static char * fxLastDeleted
Bool_t IsXDraw()
Definition: TGo4Marker.h:88
Bool_t IsYbinDraw()
Definition: TGo4Marker.h:94
Double_t GetLabelX()
Definition: TGo4Marker.cxx:195
Bool_t IsXbinDraw()
Definition: TGo4Marker.h:92
void SetOwner(TObject *ob)
Bool_t IsContDraw()
Definition: TGo4Marker.h:96
virtual void UnPaintLabel(Option_t *opt="")
void AddToLabel(const char *txt)
virtual void DisplayToFront(Option_t *opt="")
Bool_t IsYDraw()
Definition: TGo4Marker.h:90
void PaintConnector(Option_t *opt="")
virtual void RePaintLabel(Option_t *opt="")