Logo ROOT   6.12/07
Reference Guide
TDrawingOptsBase.hxx
Go to the documentation of this file.
1 /// \file ROOT/TDrawingOptsBase.hxx
2 /// \ingroup Gpad ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2017-09-26
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_TDrawingOptsBase
17 #define ROOT7_TDrawingOptsBase
18 
19 #include <ROOT/TColor.hxx>
20 #include <ROOT/TDrawingAttrs.hxx>
21 #include <ROOT/TStyle.hxx>
22 
23 #include <RStringView.h>
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 namespace ROOT {
30 namespace Experimental {
31 class TCanvas;
32 class TPadBase;
33 
34 /** \class ROOT::Experimental::TDrawingOptsBase
35  Base class for drawing options. Implements access to the default and the default's initialization
36  from a config file.
37 
38  Drawing options are made up of three kinds of primitives:
39  - TColor
40  - integer (long long)
41  - floating point (double)
42  They register the primitives with their `TCanvas`, and then refer to the registered primitives.
43  Upon destruction, the `TDrawingOptsBase` deregisters the use of its primitives with the `TCanvas`,
44  which empties the respective slots in the tables of the `TCanvas`, unless other options reference
45  the same primitives (through `SameAs()`), and until the last use has deregistered.
46 
47  In derived classes (e.g. drawing options for the class `MyFancyBox`), declare attribute members as
48  TDrawingAttrRef<TColor> fLineColor{*this, "MyFancyBoxLineColor", kRed};
49  The attribute's initial value will be taken from the current style or, if that has no setting for
50  the attribute, from the argument passed to the constructor (`kRed` in the example above).
51  */
52 
54 public:
55  template <class PRIMITIVE>
57  /// Indexes of the `TCanvas`'s attribute table entries used by the options object.
58  std::vector<TDrawingAttrRef<PRIMITIVE>> fRefArray;
59 
60  public:
62  /// Register an attribute.
63  ///\returns the index of the new attribute.
64  TDrawingAttrRef<PRIMITIVE> Register(TCanvas &canv, const PRIMITIVE &val);
65 
66  /// Re-use an existing attribute.
67  ///\returns the index of the attribute (i.e. valRef).
69 
70  /// Re-use an existing attribute.
71  ///\returns the index of the attribute, might be `IsInvalid()` if `val` could not be found.
72  TDrawingAttrRef<PRIMITIVE> SameAs(TCanvas &canv, const PRIMITIVE &val);
73 
74  /// Update the attribute at index `idx` to the value `val`.
75  void Update(TCanvas &canv, TDrawingAttrRef<PRIMITIVE> idx, const PRIMITIVE &val);
76 
77  /// Clear all attribute references, removing their uses in `TCanvas`.
78  void Release(TCanvas &canv);
79 
80  /// Once copied, elements of a OptsAttrRefArr need to increase their use count.
81  void RegisterCopy(TCanvas &canv);
82 
83  /// Access to attribute (const version).
84  const PRIMITIVE &Get(TCanvas &canv, TDrawingAttrRef<PRIMITIVE> idx) const;
85 
86  /// Access to attribute (non-const version).
87  PRIMITIVE &Get(TCanvas &canv, TDrawingAttrRef<PRIMITIVE> idx);
88  };
89 
90 private:
91  /// The `TCanvas` holding the `TDrawable` (or its `TPad`).
92  TCanvas *fCanvas = nullptr;
93 
94  /// Name of these drawing options, e.g. "1D"; will cause a member `TLineAttr{*this, "Line"}` to
95  /// look for a style setting called "1D.Line.Color".
96  std::string fName;
97 
98  /// Indexes of the `TCanvas`'s color table entries used by this options object.
100 
101  /// Indexes of the `TCanvas`'s integer table entries used by this options object.
103 
104  /// Indexes of the `TCanvas`'s floating point table entries used by this options object.
106 
107  /// Access to the attribute tables (non-const version).
111 
112  /// Access to the attribute tables (const version).
114  const OptsAttrRefArr<long long> &GetAttrsRefArr(long long *) const { return fIntIdx; }
115  const OptsAttrRefArr<double> &GetAttrsRefArr(double *) const { return fFPIdx; }
116 
117 protected:
118  /// Construct from the pad that holds our `TDrawable`.
120 
121  /// Default attributes need to register their values in a pad - they will take this pad
122  /// for default attributes of a style, as identified by the style's name.
123  static TPadBase &GetDefaultCanvas(const TStyle &style);
124 
125  /// Whether the canvas is one of the canvases used to store attribute defaults.
126  static bool IsDefaultCanvas(const TPadBase &canv);
127 
128  /// Get the (style config) name of this option set.
129  const std::string &GetName() const { return fName; }
130 
131  /// The `TCanvas` holding the `TDrawable` (or its `TPad`) (non-const version).
132  TCanvas &GetCanvas() { return *fCanvas; }
133 
134  /// The `TCanvas` holding the `TDrawable` (or its `TPad`) (const version).
135  const TCanvas &GetCanvas() const { return *fCanvas; }
136 
137  template <class PRIMITIVE>
138  TDrawingAttrRef<PRIMITIVE> Register(const PRIMITIVE &val)
139  {
140  return GetAttrsRefArr((PRIMITIVE *)nullptr).Register(GetCanvas(), val);
141  }
142 
143  template <class PRIMITIVE>
144  void Update(TDrawingAttrRef<PRIMITIVE> idx, const PRIMITIVE &val)
145  {
146  GetAttrsRefArr((PRIMITIVE *)nullptr).Update(GetCanvas(), idx, val);
147  }
148 
149  template <class PRIMITIVE>
151  {
152  return GetAttrsRefArr((PRIMITIVE *)nullptr).SameAs(GetCanvas(), idx);
153  }
154 
155  template <class PRIMITIVE>
156  TDrawingAttrRef<PRIMITIVE> SameAs(const PRIMITIVE &val)
157  {
158  return GetAttrsRefArr((PRIMITIVE *)nullptr).SameAs(GetCanvas(), val);
159  }
160 
161  template <class PRIMITIVE>
162  friend class TDrawingAttrRef;
163 
164 public:
165  TDrawingOptsBaseNoDefault() = default;
169 
170  /// Access to the attribute (non-const version).
171  template <class PRIMITIVE>
172  PRIMITIVE &Get(TDrawingAttrRef<PRIMITIVE> ref) { return GetAttrsRefArr((PRIMITIVE*)nullptr).Get(GetCanvas(), ref); }
173  /// Access to the attribute (const version).
174  template <class PRIMITIVE>
175  const PRIMITIVE &Get(TDrawingAttrRef<PRIMITIVE> ref) const { return GetAttrsRefArr((PRIMITIVE*)nullptr).Get(GetCanvas(), ref); }
176 };
177 
181 
182 template <class DERIVED>
184 public:
185  TDrawingOptsBase() = default;
186  /// Construct from the pad that holds our `TDrawable`.
188  TDrawingOptsBaseNoDefault(pad, name)
189  {
190  if (!IsDefaultCanvas(pad))
191  Default();
192  }
193 
194  /// Apply the given options to this option set.
195  void Apply(const DERIVED& other) {
196  static_cast<DERIVED&>(*this) = other;
197  }
198 
199  /// Retrieve the default drawing options for the given style.
200  static DERIVED GetDefaultForStyle(const TStyle& style)
201  {
202  return DERIVED(GetDefaultCanvas(style));
203  }
204 
205  /// Retrieve the default drawing options for `DERIVED`. Can be used to query and adjust the
206  /// default options.
207  static DERIVED &Default()
208  {
209  static DERIVED defaultOpts = GetDefaultForStyle(TStyle::GetCurrent());
210  return defaultOpts;
211  }
212 };
213 
214 } // namespace Experimental
215 } // namespace ROOT
216 
217 #endif
OptsAttrRefArr< TColor > & GetAttrsRefArr(TColor *)
Access to the attribute tables (non-const version).
const OptsAttrRefArr< long long > & GetAttrsRefArr(long long *) const
std::vector< TDrawingAttrRef< PRIMITIVE > > fRefArray
Indexes of the TCanvas&#39;s attribute table entries used by the options object.
basic_string_view< char > string_view
Definition: RStringView.h:35
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
const PRIMITIVE & Get(TDrawingAttrRef< PRIMITIVE > ref) const
Access to the attribute (const version).
A window&#39;s topmost TPad.
Definition: TCanvas.hxx:39
TDrawingOptsBase(TPadBase &pad, std::string_view name)
Construct from the pad that holds our TDrawable.
Base class for drawing options.
static DERIVED GetDefaultForStyle(const TStyle &style)
Retrieve the default drawing options for the given style.
TDrawingAttrRef< PRIMITIVE > SameAs(const PRIMITIVE &val)
TCanvas * fCanvas
The TCanvas holding the TDrawable (or its TPad).
A set of defaults for graphics attributes, e.g.
Definition: TStyle.hxx:34
const OptsAttrRefArr< TColor > & GetAttrsRefArr(TColor *) const
Access to the attribute tables (const version).
TDrawingAttrRef< PRIMITIVE > SameAs(TDrawingAttrRef< PRIMITIVE > idx)
OptsAttrRefArr< TColor > fColorIdx
Indexes of the TCanvas&#39;s color table entries used by this options object.
static TStyle & GetCurrent()
Get the current TStyle.
Definition: TStyle.cxx:72
OptsAttrRefArr< double > & GetAttrsRefArr(double *)
void RegisterCopy(TCanvas &canv)
Once copied, elements of a OptsAttrRefArr need to increase their use count.
Base class for graphic containers for TDrawable-s.
Definition: TPad.hxx:41
OptsAttrRefArr< long long > fIntIdx
Indexes of the TCanvas&#39;s integer table entries used by this options object.
OptsAttrRefArr< long long > & GetAttrsRefArr(long long *)
const PRIMITIVE & Get(TCanvas &canv, TDrawingAttrRef< PRIMITIVE > idx) const
Access to attribute (const version).
void Update(TCanvas &canv, TDrawingAttrRef< PRIMITIVE > idx, const PRIMITIVE &val)
Update the attribute at index idx to the value val.
TCanvas & GetCanvas()
The TCanvas holding the TDrawable (or its TPad) (non-const version).
PRIMITIVE & Get(TDrawingAttrRef< PRIMITIVE > ref)
Access to the attribute (non-const version).
static bool IsDefaultCanvas(const TPadBase &canv)
Whether the canvas is one of the canvases used to store attribute defaults.
void Release(TCanvas &canv)
Clear all attribute references, removing their uses in TCanvas.
const TCanvas & GetCanvas() const
The TCanvas holding the TDrawable (or its TPad) (const version).
const std::string & GetName() const
Get the (style config) name of this option set.
The Canvas class.
Definition: TCanvas.h:31
TCanvas * style()
Definition: style.C:1
std::string fName
Name of these drawing options, e.g.
void Update(TDrawingAttrRef< PRIMITIVE > idx, const PRIMITIVE &val)
TDrawingAttrRef< PRIMITIVE > Register(const PRIMITIVE &val)
The TCanvas keep track of TColors, integer and floating point attributes used by the drawing options...
Definition: TCanvas.hxx:32
static TPadBase & GetDefaultCanvas(const TStyle &style)
Default attributes need to register their values in a pad - they will take this pad for default attri...
TDrawingAttrRef< PRIMITIVE > Register(TCanvas &canv, const PRIMITIVE &val)
Register an attribute.
OptsAttrRefArr< double > fFPIdx
Indexes of the TCanvas&#39;s floating point table entries used by this options object.
A color: Red|Green|Blue|Alpha, or a position in a TPalette.
Definition: TColor.hxx:27
static DERIVED & Default()
Retrieve the default drawing options for DERIVED.
const OptsAttrRefArr< double > & GetAttrsRefArr(double *) const
TDrawingAttrRef< PRIMITIVE > SameAs(TCanvas &canv, TDrawingAttrRef< PRIMITIVE > idx)
Re-use an existing attribute.
char name[80]
Definition: TGX11.cxx:109
void Apply(const DERIVED &other)
Apply the given options to this option set.