Logo ROOT   6.18/05
Reference Guide
RDrawingAttr.cxx
Go to the documentation of this file.
1/// \file RDrawingAttrBase.cxx
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-2018, 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#include "ROOT/RDrawingAttr.hxx"
17
19#include "ROOT/RLogger.hxx"
20
21#include <algorithm>
22#include <iterator>
23
25 fPath(parent.GetPath() + name), fHolder(parent.GetHolderPtr())
26{
27}
28
30 fPath{name.fStr}, fHolder(opts.GetHolder())
31{
32}
33
35{
36 auto otherHolder = rhs.fHolder.lock();
37 if (!otherHolder)
38 return *this;
39
40 auto thisHolder = fHolder.lock();
41 if (!thisHolder)
42 return *this;
43
44 // First, remove all attributes in fPath; we will replace them with what's in rhs (if any).
45 thisHolder->EraseAttributesInPath(fPath);
46 thisHolder->CopyAttributesInPath(fPath, *otherHolder, rhs.fPath);
47 return *this;
48}
49
50void ROOT::Experimental::RDrawingAttrBase::SetValueString(const Name &name, const std::string &strVal)
51{
52 if (auto holder = GetHolderPtr().lock())
53 holder->At(GetPath() + name) = strVal;
54}
55
57{
58 auto holder = GetHolderPtr().lock();
59 if (!holder)
60 return "";
61
62 if (const std::string *pStr = holder->AtIf(path))
63 return *pStr;
64 return holder->GetAttrFromStyle(path);
65}
66
68{
69 auto holder = GetHolderPtr().lock();
70 if (!holder)
71 return "";
72
73 return !holder->AtIf(path);
74}
75
77{
78 return IsFromStyle(GetPath() + name);
79}
80
82{
83 auto thisHolder = GetHolderPtr().lock();
84 auto otherHolder = other.GetHolderPtr().lock();
85 if (!thisHolder && !otherHolder)
86 return true;
87 if (!thisHolder != !otherHolder)
88 return false;
89
90 // We have valid holders for both.
91 return thisHolder->Equal(*otherHolder.get(), GetPath(), other.GetPath());
92}
93
94float ROOT::Experimental::FromAttributeString(const std::string &val, const std::string &/*name*/, float *)
95{
96 return std::stof(val);
97}
98
99double ROOT::Experimental::FromAttributeString(const std::string &val, const std::string &/*name*/, double *)
100{
101 return std::stod(val);
102}
103
104int ROOT::Experimental::FromAttributeString(const std::string &val, const std::string &/*name*/, int*)
105{
106 return std::stoi(val);
107}
108
110{
111 return std::to_string(val);
112}
113
115{
116 return std::to_string(val);
117}
118
120{
121 return std::to_string(val);
122}
123
124const std::string *ROOT::Experimental::RDrawingAttrHolder::AtIf(const Path_t &path) const
125{
126 auto it = fAttrNameVals.find(path.fStr);
127 if (it != fAttrNameVals.end())
128 return &it->second;
129 return nullptr;
130}
131
133{
134 R__WARNING_HERE("Graf2d") << "Failed to get attribute for "
135 << path.fStr << ": not yet implemented!";
136 return "";
137}
138
139bool ROOT::Experimental::RDrawingAttrHolder::Equal(const RDrawingAttrHolder &other, const Path_t &thisPath, const Path_t &otherPath)
140{
141 std::vector<Map_t::const_iterator> thisIters = GetAttributesInPath(thisPath);
142 std::vector<Map_t::const_iterator> otherIters = other.GetAttributesInPath(otherPath);
143
144 if (thisIters.size() != otherIters.size())
145 return false;
146
147 for (auto thisIter: thisIters) {
148 // thisIters and otherIters have equal size. If any element in thisIters does not exist
149 // in other.fAttrNameVals then they are not equal (if other.fAttrNameVals has an entry that
150 // does not exist in this->fAttrNameVals, there must also be a key in this->fAttrNameVals
151 // that does not exist in other.fAttrNameVals or the counts of thisIters and otherIters
152 // would differ).
153 // If all keys' values are equal, thisIters and otherIters are equal.
154 auto otherIter = other.fAttrNameVals.find(thisIter->first);
155 if (otherIter == other.fAttrNameVals.end())
156 return false;
157 if (thisIter->second != otherIter->second)
158 return false;
159 }
160 return true;
161}
162
163std::vector<ROOT::Experimental::RDrawingAttrHolder::Map_t::const_iterator>
166 std::vector<Map_t::const_iterator> ret;
167 const std::string &stem = path.fStr;
168 for (auto i = fAttrNameVals.begin(), e = fAttrNameVals.end(); i !=e; ++i)
169 if (i->first.compare(0, stem.length(), stem) == 0) {
170 // Require i->first to be complete stem, or more but then stem followed by ".":
171 // stem "a.b", i->first can be "a.b" or "a.b.c.d"
172 if (stem.length() == i->first.length()
173 || i->first[stem.length()] == '.')
174 ret.emplace_back(i);
175 }
176 return ret;
177}
178
180{
181 // Iterators are stable under erase()ing!
182 auto iters = GetAttributesInPath(path);
183 for (auto iter: iters)
184 fAttrNameVals.erase(iter);
185}
186
187
189{
190 auto sourceIters = source.GetAttributesInPath(sourcePath);
191 if (targetPath != sourcePath) {
192 for (auto sourceIter: sourceIters)
193 fAttrNameVals.emplace(sourceIter->first, sourceIter->second);
194 } else {
195 for (auto sourceIter: sourceIters) {
196 std::string newPath = targetPath.fStr + sourceIter->first.substr(sourcePath.fStr.length());
197 fAttrNameVals.emplace(newPath, sourceIter->second);
198 }
199 }
200}
#define R__WARNING_HERE(GROUP)
Definition: RLogger.hxx:184
#define e(i)
Definition: RSha256.hxx:103
char name[80]
Definition: TGX11.cxx:109
A collection of graphics attributes, for instance everything describing a line: color,...
RDrawingAttrBase()=default
Construct a default, unnamed, unconnected attribute.
std::string GetValueString(const Path &path) const
Get the attribute value as string, for a given attribute name.
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...
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...
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.
Map_t fAttrNameVals
Map attribute paths to their values.
void EraseAttributesInPath(const Path_t &path)
Erase all custom set attributes for a given path (including sub-attributes).
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
Tag type to disambiguate construction from options.
An attribute name part, e.g. "line".
Combination of names, e.g. "hist2d.box.line.width".
std::string fStr
Path in its dotted form.