Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 <stdio.h>
13 
14 #include "TLegendEntry.h"
15 #include "TVirtualPad.h"
16 #include "TROOT.h"
17 #include "Riostream.h"
18 
19 ClassImp(TLegendEntry);
20 
21 /** \class TLegendEntry
22 \ingroup BasicGraphics
23 
24 Storage class for one entry of a TLegend.
25 */
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// TLegendEntry do-nothing default constructor
29 
30 TLegendEntry::TLegendEntry(): TAttText(), TAttLine(), TAttFill(), TAttMarker()
31 {
32  fObject = 0;
33 }
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// TLegendEntry normal constructor for one entry in a TLegend.
37 ///
38 /// obj is the object this entry will represent. If obj has
39 /// line/fill/marker attributes, then the TLegendEntry will display
40 /// these attributes.
41 ///
42 /// label is the text that will describe the entry, it is displayed using
43 /// TLatex, so may have a complex format.
44 ///
45 /// option may have values
46 /// - L draw line associated w/ TAttLine if obj inherits from TAttLine
47 /// - P draw polymarker assoc. w/ TAttMarker if obj inherits from TAttMarker
48 /// - F draw a box with fill associated w/ TAttFill if obj inherits TAttFill
49 /// default is object = "LPF"
50 
51 TLegendEntry::TLegendEntry(const TObject* obj, const char* label, Option_t* option )
52  :TAttText(0,0,0,0,0), TAttLine(1,1,1), TAttFill(0,0), TAttMarker(1,21,1)
53 {
54  fObject = 0;
55  if ( !label && obj ) fLabel = obj->GetTitle();
56  else fLabel = label;
57  fOption = option;
58  if (obj) SetObject((TObject*)obj);
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// TLegendEntry copy constructor
63 
64 TLegendEntry::TLegendEntry( const TLegendEntry &entry ) : TObject(entry), TAttText(entry), TAttLine(entry), TAttFill(entry), TAttMarker(entry)
65 {
66  ((TLegendEntry&)entry).Copy(*this);
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// TLegendEntry default destructor
71 
72 TLegendEntry::~TLegendEntry()
73 {
74  fObject = 0;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// copy this TLegendEntry into obj
79 
80 void TLegendEntry::Copy( TObject &obj ) const
81 {
82  TObject::Copy(obj);
83  TAttText::Copy((TLegendEntry&)obj);
84  TAttLine::Copy((TLegendEntry&)obj);
85  TAttFill::Copy((TLegendEntry&)obj);
86  TAttMarker::Copy((TLegendEntry&)obj);
87  ((TLegendEntry&)obj).fObject = fObject;
88  ((TLegendEntry&)obj).fLabel = fLabel;
89  ((TLegendEntry&)obj).fOption = fOption;
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// dump this TLegendEntry to std::cout
94 
95 void TLegendEntry::Print( Option_t *) const
96 {
97  TString output;
98  std::cout << "TLegendEntry: Object ";
99  if ( fObject ) output = fObject->GetName();
100  else output = "NULL";
101  std::cout << output << " Label ";
102  if ( fLabel ) output = fLabel.Data();
103  else output = "NULL";
104  std::cout << output << " Option ";
105  if (fOption ) output = fOption.Data();
106  else output = "NULL";
107  std::cout << output << std::endl;
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Save this TLegendEntry as C++ statements on output stream out
112 /// to be used with the SaveAs .C option
113 
114 void TLegendEntry::SaveEntry(std::ostream &out, const char* name )
115 {
116  char quote = '"';
117  if ( gROOT->ClassSaved( TLegendEntry::Class() ) ) {
118  out << " entry=";
119  } else {
120  out << " TLegendEntry *entry=";
121  }
122  TString objname = "NULL";
123  if ( fObject ) objname = fObject->GetName();
124  out << name << "->AddEntry("<<quote<<objname<<quote<<","<<quote<<
125  fLabel.Data()<<quote<<","<<quote<<fOption.Data()<<quote<<");"<<std::endl;
126  SaveFillAttributes(out,"entry",0,0);
127  SaveLineAttributes(out,"entry",0,0,0);
128  SaveMarkerAttributes(out,"entry",0,0,0);
129  SaveTextAttributes(out,"entry",0,0,0,0,0);
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// (re)set the obj pointed to by this entry
134 
135 void TLegendEntry::SetObject(TObject* obj )
136 {
137  if ( ( fObject && fLabel == fObject->GetTitle() ) || !fLabel ) {
138  if (obj) fLabel = obj->GetTitle();
139  }
140  fObject = obj;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// (re)set the obj pointed to by this entry
145 
146 void TLegendEntry::SetObject( const char* objectName)
147 {
148  TObject* obj = 0;
149  TList* padprimitives = gPad->GetListOfPrimitives();
150  if (padprimitives) obj = padprimitives->FindObject( objectName );
151  if (obj) SetObject( obj );
152 }