Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RLegend.hxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
9#ifndef ROOT7_RLegend
10#define ROOT7_RLegend
11
12#include <ROOT/RPave.hxx>
13#include <ROOT/RAttrLine.hxx>
14#include <ROOT/RAttrFill.hxx>
15#include <ROOT/RAttrMarker.hxx>
16#include <ROOT/RDisplayItem.hxx>
17
18#include <initializer_list>
19#include <memory>
20#include <vector>
21
22namespace ROOT {
23namespace Experimental {
24
25/** \class RLegend
26\ingroup GrafROOT7
27\brief A legend for several drawables
28\author Sergey Linev <S.Linev@gsi.de>
29\date 2019-10-09
30\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
31*/
32
33class RLegend : public RPave {
34
35public:
36
37 /** \class RCustomDrawable
38 \ingroup GrafROOT7
39 \brief Special drawable to let provide line, fill or marker attributes for legend
40 \author Sergey Linev <S.Linev@gsi.de>
41 \date 2021-07-06
42 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
43 is welcome!
44 */
45
46 class RCustomDrawable final : public RDrawable {
47 public:
48 RCustomDrawable() : RDrawable("lentry") {}
49
50 RAttrLine line{this, "line"}; ///<! line attributes
51 RAttrFill fill{this, "fill"}; ///<! fill attributes
52 RAttrMarker marker{this, "marker"}; ///<! marker attributes
53 };
54
55 /** \class REntry
56 \ingroup GrafROOT7
57 \brief An entry in RLegend, references RDrawable and its attributes
58 \author Sergey Linev <S.Linev@gsi.de>
59 \date 2019-10-09
60 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
61 is welcome!
62 */
63
64 class REntry {
65
66 friend class RLegend;
67
68 std::string fLabel; ///< label shown for the entry
69
70 bool fLine{true}, fFill{false}, fMarker{false}, fError{false}; ///< enable line, fill, marker, error showing
71
72 Internal::RIOShared<RDrawable> fDrawable; ///< reference to RDrawable
73
74 std::string fDrawableId; ///< drawable id, used only when display item
75
76 bool IsCustomDrawable() const { return dynamic_cast<const RCustomDrawable *>(fDrawable.get()) != nullptr; }
77
78 void DecodeOptions(const std::string &opt)
79 {
80 if (opt.find('l') != std::string::npos) SetLine(true);
81 if (opt.find('f') != std::string::npos) SetFill(true);
82 if (opt.find('m') != std::string::npos) SetMarker(true);
83 if (opt.find('e') != std::string::npos) SetError(true);
84 }
85
86 public:
87 REntry() = default;
88
89 /** Create entry without reference to existing drawable object, can assign attributes */
90 REntry(const std::string &lbl, const std::string &opt)
91 {
92 fDrawableId = "custom";
93 fLabel = lbl;
94 DecodeOptions(opt);
95 }
96
97 /** Create entry with reference to existing drawable object */
98 REntry(std::shared_ptr<RDrawable> drawable, const std::string &lbl, const std::string &opt)
99 {
100 fDrawable = drawable;
101 fLabel = lbl;
102 DecodeOptions(opt);
103 }
104
105 REntry &SetLabel(const std::string &lbl) { fLabel = lbl; return *this; }
106 const std::string &GetLabel() const { return fLabel; }
107
108 REntry &SetLine(bool on = true) { fLine = on; return *this; }
109 bool GetLine() const { return fLine; }
110
111 REntry &SetFill(bool on = true) { fFill = on; return *this; }
112 bool GetFill() const { return fFill; }
113
114 REntry &SetMarker(bool on = true) { fMarker = on; return *this; }
115 bool GetMarker() const { return fMarker; }
116
117 REntry &SetError(bool on = true) { fError = on; return *this; }
118 bool GetError() const { return fError; }
119
120 std::shared_ptr<RDrawable> GetDrawable() const { return fDrawable.get_shared(); }
121 };
122
123private:
124 std::string fTitle; ///< legend title
125
126 std::vector<REntry> fEntries; ///< list of entries which should be displayed
127
128protected:
130 {
131 for (auto &entry : fEntries) {
132 vect.emplace_back(&entry.fDrawable);
133 if (entry.fDrawable)
134 entry.fDrawable->CollectShared(vect);
135 }
136 }
137
138 /** hide I/O pointers when creating display item */
139 std::unique_ptr<RDisplayItem> Display(const RDisplayContext &) override
140 {
141 for (auto &entry : fEntries) {
142 if (!entry.IsCustomDrawable()) {
143 entry.fDrawableId = RDisplayItem::ObjectIDFromPtr(entry.fDrawable.get());
144 entry.fDrawable.reset_io();
145 }
146 }
147
148 return std::make_unique<RDrawableDisplayItem>(*this);
149 }
150
151 /** when display item destroyed - restore I/O pointers */
153 {
154 for (auto &centry : fEntries) {
155 if (!centry.IsCustomDrawable()) {
156 auto entry = const_cast<REntry *>(&centry);
157 entry->fDrawable.restore_io();
158 entry->fDrawableId.clear();
159 }
160 }
161 }
162
163public:
164 RLegend() : RPave("legend") {}
165
166 RLegend(const std::string &title) : RLegend() { SetTitle(title); }
167
168 RLegend(const RPadPos &offset, const RPadExtent &size) : RLegend()
169 {
170 offsetX = offset.Horiz();
171 offsetY = offset.Vert();
172 width = size.Horiz();
173 height = size.Vert();
174 }
175
176 RLegend &SetTitle(const std::string &title) { fTitle = title; return *this; }
177 const std::string &GetTitle() const { return fTitle; }
178
179 std::shared_ptr<RCustomDrawable> AddEntry(const std::string &lbl, const std::string &opt = "")
180 {
181 fEntries.emplace_back(lbl, opt);
182 auto drawable = std::make_shared<RCustomDrawable>();
183 fEntries.back().fDrawable = drawable;
184 return drawable;
185 }
186
187 void AddEntry(const std::shared_ptr<RDrawable> &drawable, const std::string &lbl, const std::string &opt = "")
188 {
189 fEntries.emplace_back(drawable, lbl, opt);
190 }
191
192 auto NumEntries() const { return fEntries.size(); }
193
194 auto &GetEntry(int n) { return fEntries[n]; }
195};
196
197} // namespace Experimental
198} // namespace ROOT
199
200#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Drawing fill attributes for different objects.
Definition RAttrFill.hxx:26
Drawing line attributes for different objects.
Definition RAttrLine.hxx:26
Base class for painting data for JS.
static std::string ObjectIDFromPtr(const void *ptr)
Construct fillid using pointer value.
RDrawable(const RDrawable &)=delete
Special drawable to let provide line, fill or marker attributes for legend.
Definition RLegend.hxx:46
RAttrMarker marker
! marker attributes
Definition RLegend.hxx:52
An entry in RLegend, references RDrawable and its attributes.
Definition RLegend.hxx:64
REntry & SetFill(bool on=true)
Definition RLegend.hxx:111
std::shared_ptr< RDrawable > GetDrawable() const
Definition RLegend.hxx:120
REntry & SetLine(bool on=true)
Definition RLegend.hxx:108
REntry(std::shared_ptr< RDrawable > drawable, const std::string &lbl, const std::string &opt)
Create entry with reference to existing drawable object.
Definition RLegend.hxx:98
REntry & SetMarker(bool on=true)
Definition RLegend.hxx:114
Internal::RIOShared< RDrawable > fDrawable
reference to RDrawable
Definition RLegend.hxx:72
void DecodeOptions(const std::string &opt)
Definition RLegend.hxx:78
const std::string & GetLabel() const
Definition RLegend.hxx:106
REntry(const std::string &lbl, const std::string &opt)
Create entry without reference to existing drawable object, can assign attributes.
Definition RLegend.hxx:90
bool fError
enable line, fill, marker, error showing
Definition RLegend.hxx:70
REntry & SetError(bool on=true)
Definition RLegend.hxx:117
std::string fLabel
label shown for the entry
Definition RLegend.hxx:68
std::string fDrawableId
drawable id, used only when display item
Definition RLegend.hxx:74
REntry & SetLabel(const std::string &lbl)
Definition RLegend.hxx:105
std::string fTitle
legend title
Definition RLegend.hxx:124
RLegend(const RPadPos &offset, const RPadExtent &size)
Definition RLegend.hxx:168
void CollectShared(Internal::RIOSharedVector_t &vect) override
Definition RLegend.hxx:129
std::unique_ptr< RDisplayItem > Display(const RDisplayContext &) override
hide I/O pointers when creating display item
Definition RLegend.hxx:139
std::vector< REntry > fEntries
list of entries which should be displayed
Definition RLegend.hxx:126
const std::string & GetTitle() const
Definition RLegend.hxx:177
RLegend & SetTitle(const std::string &title)
Definition RLegend.hxx:176
void AddEntry(const std::shared_ptr< RDrawable > &drawable, const std::string &lbl, const std::string &opt="")
Definition RLegend.hxx:187
RLegend(const std::string &title)
Definition RLegend.hxx:166
std::shared_ptr< RCustomDrawable > AddEntry(const std::string &lbl, const std::string &opt="")
Definition RLegend.hxx:179
void OnDisplayItemDestroyed(RDisplayItem *) const override
when display item destroyed - restore I/O pointers
Definition RLegend.hxx:152
An extent / size (horizontal and vertical) in a RPad.
A position (horizontal and vertical) in a RPad.
Definition RPadPos.hxx:28
RAttrValue< RPadLength > offsetX
! offset X relative to selected frame or pad corner
Definition RPave.hxx:54
RPave(const char *csstype)
Definition RPave.hxx:36
RAttrValue< RPadLength > offsetY
! offset Y relative to selected frame or pad corner
Definition RPave.hxx:55
RAttrValue< RPadLength > height
! pave height
Definition RPave.hxx:51
RAttrValue< RPadLength > width
! pave width
Definition RPave.hxx:50
const Int_t n
Definition legend1.C:16
std::vector< RIOSharedBase * > RIOSharedVector_t
Definition RDrawable.hxx:52
Namespace for ROOT features in testing.
Definition TROOT.h:100
Small utility to parse cmdline options.
Definition RExports.h:71
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")