Logo ROOT   6.18/05
Reference Guide
RDrawingAttr.hxx
Go to the documentation of this file.
1/// \file ROOT/RDrawingAttr.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_RDrawingAttr
17#define ROOT7_RDrawingAttr
18
19#include <memory>
20#include <string>
21#include <type_traits>
22#include <unordered_map>
23#include <vector>
24
25namespace ROOT {
26namespace Experimental {
27
28class RDrawingAttrBase;
29class RDrawingOptsBase;
30
31/// \[ \name Attribute Stringification
32float FromAttributeString(const std::string &strval, const std::string &name, float *);
33double FromAttributeString(const std::string &strval, const std::string &name, double *);
34char FromAttributeString(const std::string &strval, const std::string &name, char *);
35short FromAttributeString(const std::string &strval, const std::string &name, short *);
36int FromAttributeString(const std::string &strval, const std::string &name, int *);
37long FromAttributeString(const std::string &strval, const std::string &name, long *);
38long long FromAttributeString(const std::string &strval, const std::string &name, long long *);
39unsigned char FromAttributeString(const std::string &strval, const std::string &name, unsigned char *);
40unsigned short FromAttributeString(const std::string &strval, const std::string &name, unsigned short *);
41unsigned int FromAttributeString(const std::string &strval, const std::string &name, unsigned int *);
42unsigned long FromAttributeString(const std::string &strval, const std::string &name, unsigned long *);
43unsigned long long FromAttributeString(const std::string &strval, const std::string &name, unsigned long long *);
44
45/// Decode an enum value from its integer representation.
46template <typename ENUM, class = typename std::enable_if<std::is_enum<ENUM>::value>::type>
47ENUM FromAttributeString(const std::string &strval, const std::string &name, ENUM *)
48{
49 return static_cast<ENUM>(FromAttributeString(strval, name, (typename std::underlying_type<ENUM>::type*)nullptr));
50}
51
52
53std::string ToAttributeString(float val);
54std::string ToAttributeString(double val);
55std::string ToAttributeString(char val);
56std::string ToAttributeString(short val);
57std::string ToAttributeString(int val);
58std::string ToAttributeString(long val);
59std::string ToAttributeString(long long val);
60std::string ToAttributeString(unsigned char val);
61std::string ToAttributeString(unsigned short val);
62std::string ToAttributeString(unsigned int val);
63std::string ToAttributeString(unsigned long val);
64std::string ToAttributeString(unsigned long long val);
65
66/// Stringify an enum value through its integer representation.
67template <typename ENUM, class = typename std::enable_if<std::is_enum<ENUM>::value>::type>
68std::string ToAttributeString(ENUM val)
69{
70 return ToAttributeString(static_cast<typename std::underlying_type<ENUM>::type>(val));
71}
72
73/// \]
74
75
76class RDrawingAttrHolder;
77
78/** \class ROOT::Experimental::RDrawingAttrBase
79 A collection of graphics attributes, for instance everything describing a line:
80 color, width, opacity and style.
81 It has a name, so it can be found in the style.
82 */
84public:
85 /// An attribute name part, e.g. "line".
86 struct Name {
87 Name() = default;
88 Name(const std::string &name): fStr(name) {}
89 Name(std::string &&name): fStr(std::move(name)) {}
90 Name(const char *name): fStr(name) {}
91
92 std::string fStr;
93 };
94 /// Combination of names, e.g. "hist2d.box.line.width".
95 struct Path {
96 /// Path in its dotted form.
97 std::string fStr;
98
99 Path() = default;
100
101 explicit Path(const std::string &str): fStr(str) {}
102
103 explicit Path(std::string &&str): fStr(std::move(str)) {}
104
105 void Append(const Name &name) {
106 if (!fStr.empty())
107 fStr += ".";
108 fStr += name.fStr;
109 }
110
112 Append(name);
113 return *this;
114 }
115
116 Path operator+(const Name &name) const {
117 Path ret(*this);
118 ret += name;
119 return ret;
120 }
121
122 bool operator==(const Path &rhs) const { return fStr == rhs.fStr; }
123 bool operator!=(const Path &rhs) const { return !(*this == rhs); }
124
125 const std::string Str() const { return fStr; }
126 };
127
128protected:
129 /// The chain of attribute names, as used in style files.
130 /// E.g. "hist1D.hist.box.line".
132
133 /// The container of the attribute values.
134 std::weak_ptr<RDrawingAttrHolder> fHolder; ///<! I/O not working anyway
135
136protected:
137 /// Get the attribute value as string, for a given attribute name.
138 std::string GetValueString(const Path &path) const;
139
140 /// Insert or update the attribute value identified by the valueIndex (in fValueNames)
141 /// to the value `strVal`.
142 void SetValueString(const Name &name, const std::string &strVal);
143
144 /// Construct a default, unnamed, unconnected attribute.
145 RDrawingAttrBase() = default;
146
147 /// Return `true` if the attribute's value comes from the
148 /// styles, i.e. through `RDrawingAttrHolder::GetAttrFromStyle()`, instead
149 /// if from our `RDrawingAttrHolder` (i.e. explicitly set through `Set()`).
150 bool IsFromStyle(const Path &path) const;
151
152public:
153 /// Construct as a copy.
154 RDrawingAttrBase(const RDrawingAttrBase& other) = default;
155
156 /// Construct as a moved-to.
158
159 /// Construct a named attribute that does not have a parent; e.g.
160 /// because it's the top-most attribute in a drawing option object.
162
163 /// Construct a named attribute that has a parent, e.g.
164 /// because it's some line attribute of the histogram attributes.
165 RDrawingAttrBase(const Name &name, const RDrawingAttrBase &parent);
166
167 /// Tag type to disambiguate construction from options.
168 struct FromOption_t {};
169 static constexpr const FromOption_t FromOption{};
170 /// Construct a top-most attribute from its holder.
171 RDrawingAttrBase(FromOption_t, const Name &name, RDrawingOptsBase &opts);
172
173 /// Construct a top-most attribute from its holder. If this is ambiguous, use the
174 /// tag overload taking an `FromOption_t`.
177
178 /// Copy-assign: this assigns the attribute values to this attribute, *without*
179 /// changing the connected drawing options object / holder or attribute path!
180 ///
181 /// It gives value semantics to attributes:
182 /// ```
183 /// DrawingOpts1 o1;
184 /// DrawingOpts2 o2;
185 /// RAttrLine l1 = o1.DogLine();
186 /// RAttrLine l2 = o2.CatLine();
187 /// l1.SetWidth(42);
188 /// l2 = l1;
189 /// // Now o2.CatLine().GetWidth() is 42!
190 /// ```
192
193 /// Return `true` if the attribute's value comes from the
194 /// styles, i.e. through `RDrawingAttrHolder::GetAttrFromStyle()`, instead
195 /// if from our `RDrawingAttrHolder` (i.e. explicitly set through `Set()`).
196 bool IsFromStyle(const Name &name) const;
197
198 /// Get the attribute value for an attribute value of type `T`.
199 template <class T>
200 T Get(const Name &name) const
201 {
202 Path path = fPath + name;
203 auto strVal = GetValueString(path);
204 return FromAttributeString(strVal, path.Str(), (T*)nullptr);
205 }
206
207 /// Insert or update the attribute value identified by `name` to the given value.
208 template <class T>
209 void Set(const Name &name, const T &val)
210 {
212 }
213
214 /// Return the attribute names that lead to this attribute, starting
215 /// with the topmost attribute, i.e. the parent that does not have a parent
216 /// itself, down to the name of *this (the last entry in the vector).
217 const Path &GetPath() const { return fPath; }
218
219 /// Actual attribute holder.
220 const std::weak_ptr<RDrawingAttrHolder> &GetHolderPtr() const { return fHolder; }
221
222 /// Equality compare to other RDrawingAttrBase.
223 /// They are equal if
224 /// - the same set of attributes are custom set (versus are determined from the style), and
225 /// - the values of all the custom set ones compare equal.
226 /// The set of styles to be taken into account is not compared.
227 bool operator==(const RDrawingAttrBase &other) const;
228
229 /// Compare unequal to other RDrawingAttrBase. Returns the negated `operator==`.
230 bool operator!=(const RDrawingAttrBase &other) const
231 {
232 return !(*this == other);
233 }
234};
235
236
237/** \class ROOT::Experimental::RDrawingAttrHolder
238 A container of (stringified) attributes for which values have been provided.
239 */
241public:
243 using Map_t = std::unordered_map<std::string, std::string>;
244private:
245 /// Map attribute paths to their values.
247
248 /// Attribute style classes of these options that will be "summed" in order,
249 /// e.g. {"trigger", "efficiency"} will look attributes up in the `RDrawingAttrHolderBase` base class,
250 /// if not found using the "trigger" style class, and if not found in the "efficiency" style class.
251 /// Implicitly and as final resort, the attributes from the "default" style class will be used.
252 std::vector<std::string> fStyleClasses;
253
254public:
255 /// RDrawingAttrHolder using only the default style.
257
258 /// RDrawingAttrHolder with an ordered collection of styles taking precedence before the default style.
259 RDrawingAttrHolder(const std::vector<std::string> &styleClasses): fStyleClasses(styleClasses) {}
260
261 /// Get an attribute value as string, given its name path.
262 std::string &At(const Path_t &path) { return fAttrNameVals[path.fStr]; }
263
264 /// Get an attribute value as pointer to string, given its name path, or
265 /// `nullptr` if the attribute does not exist.
266 const std::string *AtIf(const Path_t &path) const;
267
268 /// Get the (stringified) value of the named attribute from the Style.
269 /// Return the empty string if no such value exists - which means that the attribute
270 /// name is unknown even for the (implicit) default style!
271 std::string GetAttrFromStyle(const Path_t &path);
272
273 /// Equality compare the attributes within `path` to those of `other` within `otherpath`.
274 /// Takes all sub-attributes contained in the respective paths (i.e. those starting with that path)
275 /// into account. They compare equal if their set of (sub-)attributes and their respective values are equal.
276 bool Equal(const RDrawingAttrHolder &other, const Path_t &thisPath, const Path_t &otherPath);
277
278 /// Extract contained attributes for a given path (including sub-attributes); returns iterators
279 /// to a subset of fAttrNameVals.
280 std::vector<Map_t::const_iterator> GetAttributesInPath(const Path_t &path) const;
281
282 /// Erase all custom set attributes for a given path (including sub-attributes).
283 void EraseAttributesInPath(const Path_t &path);
284
285 /// Copy attributes within otherPath into
286 void CopyAttributesInPath(const Path_t &targetPath, const RDrawingAttrHolder &source, const Path_t &sourcePath);
287
288 /// Get the attribute style classes of these options.
289 const std::vector<std::string> &GetStyleClasses() const { return fStyleClasses; }
290
291 /// Set the attribute style classes of these options.
292 void SetStyleClasses(const std::vector<std::string> &styles) { fStyleClasses = styles; }
293};
294
295} // namespace Experimental
296} // namespace ROOT
297
298#endif // ROOT7_RDrawingAttr
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
A collection of graphics attributes, for instance everything describing a line: color,...
RDrawingAttrBase()=default
Construct a default, unnamed, unconnected attribute.
T Get(const Name &name) const
Get the attribute value for an attribute value of type T.
void Set(const Name &name, const T &val)
Insert or update the attribute value identified by name to the given value.
bool operator!=(const RDrawingAttrBase &other) const
Compare unequal to other RDrawingAttrBase. Returns the negated operator==.
static constexpr const FromOption_t FromOption
RDrawingAttrBase(const Name &name)
Construct a named attribute that does not have a parent; e.g.
std::string GetValueString(const Path &path) const
Get the attribute value as string, for a given attribute name.
RDrawingAttrBase(const RDrawingAttrBase &other)=default
Construct as a copy.
bool IsFromStyle(const Path &path) const
Return true if the attribute's value comes from the styles, i.e.
const Path & GetPath() const
Return the attribute names that lead to this attribute, starting with the topmost attribute,...
void SetValueString(const Name &name, const std::string &strVal)
Insert or update the attribute value identified by the valueIndex (in fValueNames) to the value strVa...
const std::weak_ptr< RDrawingAttrHolder > & GetHolderPtr() const
Actual attribute holder.
RDrawingAttrBase & operator=(const RDrawingAttrBase &rhs)
Copy-assign: this assigns the attribute values to this attribute, without changing the connected draw...
RDrawingAttrBase(RDrawingAttrBase &&other)=default
Construct as a moved-to.
RDrawingAttrBase(const Name &name, RDrawingOptsBase &opts)
Construct a top-most attribute from its holder.
bool operator==(const RDrawingAttrBase &other) const
Equality compare to other RDrawingAttrBase.
Path fPath
The chain of attribute names, as used in style files.
std::weak_ptr< RDrawingAttrHolder > fHolder
The container of the attribute values.
A container of (stringified) attributes for which values have been provided.
bool Equal(const RDrawingAttrHolder &other, const Path_t &thisPath, const Path_t &otherPath)
Equality compare the attributes within path to those of other within otherpath.
const std::string * AtIf(const Path_t &path) const
Get an attribute value as pointer to string, given its name path, or nullptr if the attribute does no...
std::vector< Map_t::const_iterator > GetAttributesInPath(const Path_t &path) const
Extract contained attributes for a given path (including sub-attributes); returns iterators to a subs...
const std::vector< std::string > & GetStyleClasses() const
Get the attribute style classes of these options.
std::string GetAttrFromStyle(const Path_t &path)
Get the (stringified) value of the named attribute from the Style.
void CopyAttributesInPath(const Path_t &targetPath, const RDrawingAttrHolder &source, const Path_t &sourcePath)
Copy attributes within otherPath into.
RDrawingAttrHolder()=default
RDrawingAttrHolder using only the default style.
Map_t fAttrNameVals
Map attribute paths to their values.
std::string & At(const Path_t &path)
Get an attribute value as string, given its name path.
RDrawingAttrHolder(const std::vector< std::string > &styleClasses)
RDrawingAttrHolder with an ordered collection of styles taking precedence before the default style.
void EraseAttributesInPath(const Path_t &path)
Erase all custom set attributes for a given path (including sub-attributes).
void SetStyleClasses(const std::vector< std::string > &styles)
Set the attribute style classes of these options.
std::unordered_map< std::string, std::string > Map_t
std::vector< std::string > fStyleClasses
Attribute style classes of these options that will be "summed" in order, e.g.
std::string ToAttributeString(const RColor &val)
Return a std::string representation of a RColor, suitable as input to ColorFromString().
Definition: RColor.cxx:144
RColor FromAttributeString(const std::string &str, const std::string &name, RColor *)
Initialize a RColor from a string value.
Definition: RColor.cxx:132
double T(double x)
Definition: ChebyshevPol.h:34
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
const char * Name
Definition: TXMLSetup.cxx:66
Tag type to disambiguate construction from options.
An attribute name part, e.g. "line".
Combination of names, e.g. "hist2d.box.line.width".
Path operator+(const Name &name) const
std::string fStr
Path in its dotted form.
bool operator==(const Path &rhs) const
bool operator!=(const Path &rhs) const