Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TLegendEntry.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Matthew.Adam.Dobbs 06/09/99
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 <cstdio>
13
14#include "TLegendEntry.h"
15#include "TVirtualPad.h"
16#include "TROOT.h"
17#include <iostream>
18
19
20/** \class TLegendEntry
21\ingroup BasicGraphics
22
23Storage class for one entry of a TLegend.
24*/
25
26////////////////////////////////////////////////////////////////////////////////
27/// TLegendEntry do-nothing default constructor
28
33
34////////////////////////////////////////////////////////////////////////////////
35/// TLegendEntry normal constructor for one entry in a TLegend.
36///
37/// obj is the object this entry will represent. If obj has
38/// line/fill/marker attributes, then the TLegendEntry will display
39/// these attributes.
40///
41/// label is the text that will describe the entry, it is displayed using
42/// TLatex, so may have a complex format.
43///
44/// option may have values
45/// - L draw line associated w/ TAttLine if obj inherits from TAttLine
46/// - P draw polymarker assoc. w/ TAttMarker if obj inherits from TAttMarker
47/// - F draw a box with fill associated w/ TAttFill if obj inherits TAttFill
48/// default is object = "LPF"
49
50TLegendEntry::TLegendEntry(const TObject* obj, const char* label, Option_t* option )
51 :TAttText(0,0,0,0,0), TAttLine(1,1,1), TAttFill(0,0), TAttMarker(1,21,1)
52{
53 fObject = nullptr;
54 if ( !label && obj ) fLabel = obj->GetTitle();
55 else fLabel = label;
57 if (obj) SetObject((TObject*)obj);
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// TLegendEntry copy constructor
62
67
68////////////////////////////////////////////////////////////////////////////////
69/// TLegendEntry default destructor
70
72{
73 fObject = nullptr;
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// copy this TLegendEntry into obj
78
79void TLegendEntry::Copy( TObject &obj ) const
80{
81 TObject::Copy(obj);
86 ((TLegendEntry&)obj).fObject = fObject;
87 ((TLegendEntry&)obj).fLabel = fLabel;
88 ((TLegendEntry&)obj).fOption = fOption;
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// dump this TLegendEntry to std::cout
93
95{
97 std::cout << "TLegendEntry: Object ";
98 if ( fObject ) output = fObject->GetName();
99 else output = "NULL";
100 std::cout << output << " Label ";
101 if ( fLabel ) output = fLabel.Data();
102 else output = "NULL";
103 std::cout << output << " Option ";
104 if (fOption ) output = fOption.Data();
105 else output = "NULL";
106 std::cout << output << std::endl;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Save this TLegendEntry as C++ statements on output stream out
111/// to be used with the SaveAs .C option
112
113void TLegendEntry::SaveEntry(std::ostream &out, const char* name)
114{
115 if (gROOT->ClassSaved(TLegendEntry::Class()))
116 out << " legentry = ";
117 else
118 out << " TLegendEntry *legentry = ";
119
120 TString objname = fObject ? fObject->GetName() : "NULL";
121 out << name << "->AddEntry(\"" << objname << "\",\"" << TString(fLabel).ReplaceSpecialCppChars() << "\",\""
122 << TString(fOption).ReplaceSpecialCppChars() << "\");\n";
123 // if default style is detected - copy attributes from object
124 // it can happen when legend was not paint before writing into the file
125 if (fObject && GetFillStyle() == 0 && GetFillColor() == 0 && GetLineStyle() == 1 && GetLineColor() == 1 && GetLineWidth() == 1) {
126 TString opt = fOption;
127 opt.ToLower();
128 if (opt.Contains("f") && fObject->InheritsFrom(TAttFill::Class()))
129 dynamic_cast<TAttFill*>(fObject)->Copy(*this);
131 dynamic_cast<TAttMarker*>(fObject)->Copy(*this);
132 if ((opt.Contains("l") || opt.Contains("f")) && fObject->InheritsFrom(TAttLine::Class()))
133 dynamic_cast<TAttLine*>(fObject)->Copy(*this);
134 }
135
136 SaveFillAttributes(out, "legentry", 0, 0);
137 SaveLineAttributes(out, "legentry", 1, 1, 1);
138 SaveMarkerAttributes(out, "legentry", 1, 21, 1);
139 SaveTextAttributes(out, "legentry", 0, 0, 0, 0, 0);
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// (re)set the obj pointed to by this entry
144
146{
147 if ( ( fObject && fLabel == fObject->GetTitle() ) || !fLabel ) {
148 if (obj) fLabel = obj->GetTitle();
149 }
150 fObject = obj;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// (re)set the obj pointed to by this entry
155
156void TLegendEntry::SetObject(const char* objectName)
157{
158 TList *padprimitives = gPad ? gPad->GetListOfPrimitives() : nullptr;
159 TObject *obj = padprimitives ? padprimitives->FindObject(objectName) : nullptr;
160 if (obj) SetObject(obj);
161}
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:411
#define gPad
Fill Area Attributes class.
Definition TAttFill.h:20
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:31
void Copy(TAttFill &attfill) const
Copy this fill attributes to a new TAttFill.
Definition TAttFill.cxx:206
virtual Style_t GetFillStyle() const
Return the fill area style.
Definition TAttFill.h:32
static TClass * Class()
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:238
Line Attributes class.
Definition TAttLine.h:20
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:35
virtual Width_t GetLineWidth() const
Return the line width.
Definition TAttLine.h:37
static TClass * Class()
virtual Style_t GetLineStyle() const
Return the line style.
Definition TAttLine.h:36
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition TAttLine.cxx:176
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:274
Marker Attributes class.
Definition TAttMarker.h:20
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
static TClass * Class()
Text Attributes class.
Definition TAttText.h:20
virtual void SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef=12, Float_t angdef=0, Int_t coldef=1, Int_t fondef=61, Float_t sizdef=1)
Save text attributes as C++ statement(s) on output stream out.
Definition TAttText.cxx:372
void Copy(TAttText &atttext) const
Copy this text attributes to a new TAttText.
Definition TAttText.cxx:293
Storage class for one entry of a TLegend.
TObject * fObject
pointer to object being represented by this entry
TLegendEntry()
TLegendEntry do-nothing default constructor.
virtual void SaveEntry(std::ostream &out, const char *name)
Save this TLegendEntry as C++ statements on output stream out to be used with the SaveAs ....
static TClass * Class()
~TLegendEntry() override
TLegendEntry default destructor.
TString fLabel
Text associated with the entry, will become latex.
virtual void SetObject(TObject *obj)
(re)set the obj pointed to by this entry
void Copy(TObject &obj) const override
copy this TLegendEntry into obj
void Print(Option_t *option="") const override
dump this TLegendEntry to std::cout
TString fOption
Options associated with this entry.
A doubly linked list.
Definition TList.h:38
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:543
virtual void Copy(TObject &object) const
Copy this to obj.
Definition TObject.cxx:159
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:501
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
const char * Data() const
Definition TString.h:384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
static void output()