Belle II Software  release-08-01-10
Styling.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingCDC/display/ColorMapping.h>
11 #include <tracking/trackFindingCDC/display/Mapping.h>
12 #include <tracking/trackFindingCDC/display/AttributeMap.h>
13 
14 
15 #include <map>
16 #include <sstream>
17 #include <memory>
18 
19 namespace Belle2 {
24  class RecoTrack;
25  class CDCHit;
26 
27  namespace TrackFindingCDC {
28  class CDCSegment2D;
29 
31  template <class AObject>
32  class Styling {
33 
34  public:
36  virtual ~Styling() = default;
37 
39  virtual AttributeMap map(int index __attribute__((unused)),
40  AObject& object __attribute__((unused)))
41  {
42  return {};
43  }
44 
46  virtual std::string info()
47  {
48  return "(no info available)\n";
49  }
50  };
51 
53  template <class AObject>
54  class FixedStyling : public Styling<AObject> {
55 
56  public:
58  AttributeMap map(int index __attribute__((unused)),
59  AObject& object __attribute__((unused))) override
60  {
61  return m_attributeMap;
62  }
63 
65  std::string info() override
66  {
67  std::ostringstream oss;
68  for (const auto& keyValue : m_attributeMap) {
69  oss << keyValue.first << ": " << keyValue.second << '\n';
70  }
71  return oss.str();
72  }
73 
75  void set(const std::string& key, const std::string& value)
76  {
77  m_attributeMap.emplace(key, value);
78  }
79 
81  void setStroke(const std::string& value)
82  {
83  set("stroke", value);
84  }
85 
87  void setStrokeWidth(const std::string& value)
88  {
89  set("stroke-width", value);
90  }
91 
92  private:
94  AttributeMap m_attributeMap;
95  };
96 
98  template <class AObject>
99  class ChooseableStyling : public Styling<AObject> {
100 
101  public:
104 
105  public:
107  AttributeMap map(int index, AObject& object) override
108  {
109  AttributeMap attributeMap;
110  for (const auto& keyMapping : m_mappingsByKey) {
111  const std::string& key = keyMapping.first;
112  const std::unique_ptr<ObjectMapping>& mapping = keyMapping.second;
113  attributeMap[key] = mapping->map(index, object);
114  }
115  return attributeMap;
116  }
117 
119  std::string info() override
120  {
121  std::ostringstream oss;
122  for (const auto& keyMapping : m_mappingsByKey) {
123  const std::string& key = keyMapping.first;
124  const std::unique_ptr<ObjectMapping>& mapping = keyMapping.second;
125  oss << key << ": " << mapping->info();
126  }
127  return oss.str();
128  }
129 
136  virtual std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName)
137  {
138  return std::make_unique<ConstantMapping<AObject>>(mappingName);
139  }
140 
142  void set(const std::string& key, std::unique_ptr<ObjectMapping> mapping)
143  {
144  if (mapping) {
145  m_mappingsByKey[key] = std::move(mapping);
146  }
147  }
148 
150  void set(const std::string& key, const std::string& mappingName)
151  {
152  std::unique_ptr<ObjectMapping> objectMapping = this->createMapping(mappingName);
153  if (objectMapping) {
154  set(key, std::move(objectMapping));
155  }
156  }
157 
163  void setStroke(const std::string& mappingName)
164  {
165  set("stroke", mappingName);
166  }
167 
173  void setStrokeWidth(const std::string& mappingName)
174  {
175  set("stroke-width", mappingName);
176  }
177 
178  protected:
180  std::map<std::string, std::unique_ptr<ObjectMapping> > m_mappingsByKey;
181  };
182 
184  template <class AObject>
185  class DefaultColorCycleStyling : public ChooseableStyling<AObject> {
186  public:
189  {
190  this->set("stroke", std::make_unique<DefaultColorCycleMapping<AObject>>());
191  }
192  };
193 
198  class ChooseableRecoTrackStyling : public ChooseableStyling<const RecoTrack> {
199 
200  private:
203 
205  using Super::ObjectMapping;
206 
207  public:
209  std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName) override;
210  };
211 
216  class ChooseableSegmentStyling : public ChooseableStyling<const CDCSegment2D> {
217 
218  private:
221 
223  using Super::ObjectMapping;
224 
225  public:
227  std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName) override;
228  };
229 
234  class ChooseableHitStyling : public ChooseableStyling<const CDCHit> {
235 
236  private:
239 
241  using Super::ObjectMapping;
242 
243  public:
245  std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName) override;
246  };
247  }
249 }
This Class handles the mapping from the colormapping-method name given as a string to the actual colo...
Definition: Styling.h:234
std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName) override
Method defining the available mapping names.
Definition: Styling.cc:53
This Class handles the mapping from the colormapping-method name given as a string to the actual colo...
Definition: Styling.h:198
std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName) override
Method defining the available mapping names.
Definition: Styling.cc:17
This Class handles the mapping from the colormapping-method name given as a string to the actual ACol...
Definition: Styling.h:216
std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName) override
Method defining the available mapping names.
Definition: Styling.cc:31
Implementation of a styling composed from several predefined mappings chooseable by their name.
Definition: Styling.h:99
void set(const std::string &key, const std::string &mappingName)
Sets the given attribute to the fixed value.
Definition: Styling.h:150
AttributeMap map(int index, AObject &object) override
Create a map of attributes from the stored attribute maps.
Definition: Styling.h:107
Mapping< const RecoTrack > ObjectMapping
Mapping for the object type.
Definition: Styling.h:103
std::map< std::string, std::unique_ptr< ObjectMapping > > m_mappingsByKey
Map of attribute keys to mappings to be used.
Definition: Styling.h:180
void set(const std::string &key, std::unique_ptr< ObjectMapping > mapping)
Sets the given attribute to the attribute mapping.
Definition: Styling.h:142
virtual std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName)
Create a mapping for the object from a name.
Definition: Styling.h:136
void setStrokeWidth(const std::string &mappingName)
Legacy method to set the mapping on how to match a object to the stroke width.
Definition: Styling.h:173
void setStroke(const std::string &mappingName)
Legacy method to set the mapping on how to match a object to the stroke color.
Definition: Styling.h:163
std::string info() override
Returns informal string about the currently set mappings.
Definition: Styling.h:119
Class template for coloring objects in different Colors.
Definition: ColorMapping.h:26
Class template for coloring objects with stroke colors prepared to be the default color cycle.
Definition: Styling.h:185
DefaultColorCycleStyling()
Constructor. Sets the stroke mapping the the default color cycle.
Definition: Styling.h:188
Implementation of a styling from fixed attribute map.
Definition: Styling.h:54
void setStrokeWidth(const std::string &value)
Legacy - Sets the stroke width to the fixed value.
Definition: Styling.h:87
AttributeMap map(int index, AObject &object) override
Return the fixed attributes on each invocation.
Definition: Styling.h:58
AttributeMap m_attributeMap
Memory for the fixed attribute values.
Definition: Styling.h:94
void set(const std::string &key, const std::string &value)
Sets the given attribute to the fixed value.
Definition: Styling.h:75
void setStroke(const std::string &value)
Legacy - Sets the stroke color to the fixed value.
Definition: Styling.h:81
std::string info() override
Informal string summarizing the translation from the object to the styling attributes.
Definition: Styling.h:65
Interface defining a mapping of objects to attribute values e.g. a color.
Definition: Mapping.h:23
Interface for a mapping of object and an index to styling attributes.
Definition: Styling.h:32
virtual AttributeMap map(int index, AObject &object)
Maps the object at the given index to attribute values.
Definition: Styling.h:39
virtual ~Styling()=default
Make destructor of interface virtual.
virtual std::string info()
Informal string summarizing the translation from the object to the styling attributes.
Definition: Styling.h:46
Abstract base class for different kinds of events.