Logo ROOT  
Reference Guide
RDrawable.hxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2020, 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_RDrawable
10 #define ROOT7_RDrawable
11 
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include <ROOT/RAttrMap.hxx>
17 #include <ROOT/RStyle.hxx>
18 
19 namespace ROOT {
20 namespace Experimental {
21 
22 class RMenuItems;
23 class RPadBase;
24 class RAttrBase;
25 class RDisplayItem;
26 class RDrawableDisplayItem;
27 class RIndirectDisplayItem;
28 class RLegend;
29 class RCanvas;
30 class RChangeAttrRequest;
31 class RDrawableMenuRequest;
32 class RDrawableExecRequest;
33 
34 namespace Internal {
35 
36 /** \class RIOSharedBase
37 \ingroup GpadROOT7
38 \author Sergey Linev <s.linev@gsi.de>
39 \date 2019-09-24
40 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
41 */
42 
43 class RIOSharedBase {
44 public:
45  virtual const void *GetIOPtr() const = 0;
46  virtual bool HasShared() const = 0;
47  virtual void *MakeShared() = 0;
48  virtual void SetShared(void *shared) = 0;
49  virtual ~RIOSharedBase() = default;
50 };
51 
52 using RIOSharedVector_t = std::vector<RIOSharedBase *>;
53 
54 template<class T>
55 class RIOShared final : public RIOSharedBase {
56  std::shared_ptr<T> fShared; ///<! holder of object
57  T* fIO{nullptr}; ///< plain pointer for IO
58 public:
59  const void *GetIOPtr() const final { return fIO; }
60  bool HasShared() const final { return fShared.get() != nullptr; }
61  void *MakeShared() final { fShared.reset(fIO); return &fShared; }
62  void SetShared(void *shared) final { fShared = *((std::shared_ptr<T> *) shared); }
63 
64  RIOShared() = default;
65 
66  RIOShared(const std::shared_ptr<T> &ptr) : RIOSharedBase()
67  {
68  fShared = ptr;
69  fIO = ptr.get();
70  }
71 
72  RIOShared &operator=(const std::shared_ptr<T> &ptr)
73  {
74  fShared = ptr;
75  fIO = ptr.get();
76  return *this;
77  }
78 
79  operator bool() const { return !!fShared || !!fIO; }
80 
81  const T *get() const { return fShared ? fShared.get() : fIO; }
82  T *get() { return fShared ? fShared.get() : fIO; }
83 
84  const T *operator->() const { return get(); }
85  T *operator->() { return get(); }
86 
87  std::shared_ptr<T> get_shared() const { return fShared; }
88 
89  void reset() { fShared.reset(); fIO = nullptr; }
90 
91  // reset IO pointer, object will not be stored in normal output operation
92  void reset_io() { fIO = nullptr; }
93 
94  // restore IO pointer, object will be stored in normal output operation
95  void restore_io() { fIO = fShared.get(); }
96 };
97 
98 } // namespace Internal
99 
100 /** \class RDrawable
101 \ingroup GpadROOT7
102 \brief Base class for drawable entities: objects that can be painted on a `RPad`.
103 \author Axel Naumann <axel@cern.ch>
104 \author Sergey Linev <s.linev@gsi.de>
105 \date 2015-08-07
106 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
107 */
108 
109 class RDrawable {
110 
111 friend class RPadBase; // to access Display method and IsFrameRequired
112 friend class RAttrBase;
113 friend class RStyle;
114 friend class RLegend; // to access CollectShared method
115 friend class RDrawableDisplayItem; // to call OnDisplayItemDestroyed
116 friend class RIndirectDisplayItem; // to access attributes and other members
117 friend class RChangeAttrRequest; // access SetDrawableVersion and AttrMap
118 friend class RDrawableMenuRequest; // access PopulateMenu method
119 friend class RDrawableExecRequest; // access Execute() method
120 
121 public:
122 
123  using Version_t = uint64_t;
124 
126  RCanvas *fCanvas{nullptr}; ///<! canvas where drawable is displayed
127  RPadBase *fPad{nullptr}; ///<! subpad where drawable is displayed
128  RDrawable *fDrawable{nullptr}; ///<! reference on the drawable
129  Version_t fLastVersion{0}; ///<! last displayed version
130  unsigned fIndex{0}; ///<! index in list of primitives
131  unsigned fConnId{0}; ///<! connection id
132  bool fMainConn{false}; ///<! is main connection
133 
134  public:
135 
136  RDisplayContext() = default;
137 
138  RDisplayContext(RCanvas *canv, RPadBase *pad, Version_t vers = 0) :
139  fCanvas(canv), fPad(pad), fLastVersion(vers)
140  {
141  }
142 
143  /** Set canvas */
144  void SetCanvas(RCanvas *canv) { fCanvas = canv; }
145  /** Set pad */
146  void SetPad(RPadBase *pad) { fPad = pad; }
147  /** Set drawable and its index in list of primitives */
148  void SetDrawable(RDrawable *dr, unsigned indx)
149  {
150  fDrawable = dr;
151  fIndex = indx;
152  }
153  /** Set connection id and ismain flag for connection */
154  void SetConnection(unsigned connid, bool ismain)
155  {
156  fConnId = connid;
157  fMainConn = ismain;
158  }
159 
160  RCanvas *GetCanvas() const { return fCanvas; }
161  RPadBase *GetPad() const { return fPad; }
162  RDrawable *GetDrawable() const { return fDrawable; }
163  unsigned GetIndex() const { return fIndex; }
164 
165  Version_t GetLastVersion() const { return fLastVersion; }
166 
167  unsigned GetConnId() const { return fConnId; }
168  bool IsMainConn() const { return fMainConn; }
169  };
170 
171 private:
172  RAttrMap fAttr; ///< attributes values
173  std::weak_ptr<RStyle> fStyle; ///<! style applied for RDrawable, not stored when canvas is saved
174  std::string fCssType; ///<! drawable type, not stored in the root file, must be initialized in constructor
175  std::string fCssClass; ///< user defined drawable class, can later go inside map
176  std::string fId; ///< optional object identifier, may be used in CSS as well
177  Version_t fVersion{1}; ///<! drawable version, changed from the canvas
178 
179 protected:
180 
181  virtual void CollectShared(Internal::RIOSharedVector_t &) {}
182 
183  virtual bool IsFrameRequired() const { return false; }
184 
185  RAttrMap &GetAttrMap() { return fAttr; }
186  const RAttrMap &GetAttrMap() const { return fAttr; }
187 
188  bool MatchSelector(const std::string &selector) const;
189 
190  virtual std::unique_ptr<RDisplayItem> Display(const RDisplayContext &);
191 
192  virtual void OnDisplayItemDestroyed(RDisplayItem *) const {}
193 
194  virtual void SetDrawableVersion(Version_t vers) { fVersion = vers; }
195  Version_t GetVersion() const { return fVersion; }
196 
197  virtual void PopulateMenu(RMenuItems &);
198 
199  virtual void Execute(const std::string &);
200 
201  RDrawable(const RDrawable &) = delete;
202  RDrawable &operator=(const RDrawable &) = delete;
203 
204 public:
205 
206  explicit RDrawable(const std::string &type) : fCssType(type) {}
207 
208  virtual ~RDrawable();
209 
210  virtual void UseStyle(const std::shared_ptr<RStyle> &style) { fStyle = style; }
211  void ClearStyle() { UseStyle(nullptr); }
212 
213  void SetCssClass(const std::string &cl) { fCssClass = cl; }
214  const std::string &GetCssClass() const { return fCssClass; }
215 
216  const std::string &GetCssType() const { return fCssType; }
217 
218  const std::string &GetId() const { return fId; }
219  void SetId(const std::string &id) { fId = id; }
220 };
221 
222 /// Central method to insert drawable in list of pad primitives
223 /// By default drawable placed as is.
224 
225 template <class DRAWABLE, std::enable_if_t<std::is_base_of<RDrawable, DRAWABLE>{}>* = nullptr>
226 inline auto GetDrawable(const std::shared_ptr<DRAWABLE> &drawable)
227 {
228  return drawable;
229 }
230 
231 } // namespace Experimental
232 } // namespace ROOT
233 
234 #endif
ROOT::Experimental::RDrawable::RDisplayContext::fPad
RPadBase * fPad
! subpad where drawable is displayed
Definition: RDrawable.hxx:139
ROOT::Experimental::RDrawable::GetAttrMap
RAttrMap & GetAttrMap()
Definition: RDrawable.hxx:197
ROOT::Experimental::RDrawable::SetId
void SetId(const std::string &id)
Definition: RDrawable.hxx:231
ROOT::Experimental::Internal::RIOShared::get
const T * get() const
Definition: RDrawable.hxx:93
ROOT::Experimental::RChangeAttrRequest
Definition: RCanvas.hxx:36
Version_t
short Version_t
Definition: RtypesCore.h:65
ROOT::Experimental::RDrawableDisplayItem
Generic display item for RDrawable, just reference drawable itself.
Definition: RDisplayItem.hxx:67
ROOT::Experimental::RDrawable::RDisplayContext::GetPad
RPadBase * GetPad() const
Definition: RDrawable.hxx:173
ROOT::Experimental::RDrawable::RDrawable
RDrawable(const RDrawable &)=delete
ROOT::Experimental::Internal::RIOSharedBase
Definition: RDrawable.hxx:55
ROOT::Experimental::Internal::RIOSharedBase::SetShared
virtual void SetShared(void *shared)=0
ROOT::Experimental::Internal::RIOShared::operator=
RIOShared & operator=(const std::shared_ptr< T > &ptr)
Definition: RDrawable.hxx:84
ROOT::Experimental::RDrawable::RLegend
friend class RLegend
Definition: RDrawable.hxx:126
ROOT::Experimental::RDrawableMenuRequest
Request menu items for the drawable object.
Definition: RMenuItems.hxx:202
ROOT::Experimental::Internal::RIOShared
Definition: RDrawable.hxx:67
ROOT::Experimental::RDrawable::OnDisplayItemDestroyed
virtual void OnDisplayItemDestroyed(RDisplayItem *) const
Definition: RDrawable.hxx:204
ROOT::Experimental::RAttrMap
Definition: RAttrMap.hxx:45
ROOT::Experimental::RDrawable::RDisplayContext::GetLastVersion
Version_t GetLastVersion() const
Definition: RDrawable.hxx:177
ROOT::Experimental::RDrawable::SetDrawableVersion
virtual void SetDrawableVersion(Version_t vers)
Definition: RDrawable.hxx:206
ROOT::Experimental::Internal::RIOShared::MakeShared
void * MakeShared() final
Definition: RDrawable.hxx:73
ROOT::Experimental::GetDrawable
auto GetDrawable(const std::shared_ptr< DRAWABLE > &drawable)
Central method to insert drawable in list of pad primitives By default drawable placed as is.
Definition: RDrawable.hxx:238
ROOT::Experimental::RAttrBase
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:39
ROOT::Experimental::RDrawable::MatchSelector
bool MatchSelector(const std::string &selector) const
Preliminary method which checks if drawable matches with given selector Following selector are allowe...
Definition: RDrawable.cxx:59
ROOT::Experimental::RDrawable::fStyle
std::weak_ptr< RStyle > fStyle
! style applied for RDrawable, not stored when canvas is saved
Definition: RDrawable.hxx:185
ROOT::Experimental::Internal::RIOSharedBase::GetIOPtr
virtual const void * GetIOPtr() const =0
ROOT::Experimental::RDrawable::fAttr
RAttrMap fAttr
attributes values
Definition: RDrawable.hxx:184
ROOT::Experimental::Internal::RIOShared::reset_io
void reset_io()
Definition: RDrawable.hxx:104
ROOT::Experimental::Internal::RIOShared::SetShared
void SetShared(void *shared) final
Definition: RDrawable.hxx:74
ROOT::Experimental::RDrawable::~RDrawable
virtual ~RDrawable()
ROOT::Experimental::RDrawable::Execute
virtual void Execute(const std::string &)
Definition: RDrawable.cxx:40
ROOT::Experimental::Internal::RIOSharedBase::HasShared
virtual bool HasShared() const =0
ROOT::Experimental::RMenuItems
List of items for object context menu.
Definition: RMenuItems.hxx:153
ROOT::Experimental::Internal::RIOShared::restore_io
void restore_io()
Definition: RDrawable.hxx:107
ROOT::Experimental::RDrawable::RDisplayContext::fDrawable
RDrawable * fDrawable
! reference on the drawable
Definition: RDrawable.hxx:140
ROOT::Experimental::RDrawable::RDisplayContext::fLastVersion
Version_t fLastVersion
! last displayed version
Definition: RDrawable.hxx:141
ROOT::Experimental::RDrawableExecRequest
Request execution of method of referenced drawable, no reply.
Definition: RDrawableRequest.hxx:101
bool
ROOT::Experimental::RDrawable::RDisplayContext::IsMainConn
bool IsMainConn() const
Definition: RDrawable.hxx:180
id
XFontStruct * id
Definition: TGX11.cxx:109
style
TCanvas * style()
Definition: style.C:1
ROOT::Experimental::RDrawable::RDisplayContext::SetConnection
void SetConnection(unsigned connid, bool ismain)
Set connection id and ismain flag for connection.
Definition: RDrawable.hxx:166
ROOT::Experimental::RDrawable::RDisplayContext::fIndex
unsigned fIndex
! index in list of primitives
Definition: RDrawable.hxx:142
ROOT::Experimental::RDrawable::Version_t
uint64_t Version_t
Definition: RDrawable.hxx:135
ROOT::Experimental::RDrawable::fCssType
std::string fCssType
! drawable type, not stored in the root file, must be initialized in constructor
Definition: RDrawable.hxx:186
ROOT::Experimental::RDrawable::RDisplayContext::fCanvas
RCanvas * fCanvas
! canvas where drawable is displayed
Definition: RDrawable.hxx:138
ROOT::Experimental::RDrawable::GetCssClass
const std::string & GetCssClass() const
Definition: RDrawable.hxx:226
ROOT::Experimental::Internal::RIOShared::HasShared
bool HasShared() const final
Definition: RDrawable.hxx:72
ROOT::Experimental::RDrawable::RDisplayContext
Definition: RDrawable.hxx:137
ROOT::Experimental::RDrawable::ClearStyle
void ClearStyle()
Definition: RDrawable.hxx:223
RAttrMap.hxx
ROOT::Experimental::Internal::RIOShared::get_shared
std::shared_ptr< T > get_shared() const
Definition: RDrawable.hxx:99
ROOT::Experimental::RDisplayItem
Base class for painting data for JS.
Definition: RDisplayItem.hxx:31
ROOT::Experimental::Internal::RIOShared::fIO
T * fIO
plain pointer for IO
Definition: RDrawable.hxx:69
ROOT::Experimental::Internal::RIOShared::RIOShared
RIOShared()=default
ROOT::Experimental::RDrawable::RDisplayContext::SetDrawable
void SetDrawable(RDrawable *dr, unsigned indx)
Set drawable and its index in list of primitives.
Definition: RDrawable.hxx:160
ROOT::Experimental::Internal::RIOShared::operator->
const T * operator->() const
Definition: RDrawable.hxx:96
ROOT::Experimental::RDrawable::RDisplayContext::SetPad
void SetPad(RPadBase *pad)
Set pad.
Definition: RDrawable.hxx:158
ROOT::Experimental::RDrawable::operator=
RDrawable & operator=(const RDrawable &)=delete
ROOT::Experimental::RDrawable::RDisplayContext::GetConnId
unsigned GetConnId() const
Definition: RDrawable.hxx:179
ROOT::Experimental::RDrawable::IsFrameRequired
virtual bool IsFrameRequired() const
Definition: RDrawable.hxx:195
ROOT::Experimental::RDrawable::RDisplayContext::fConnId
unsigned fConnId
! connection id
Definition: RDrawable.hxx:143
ROOT::Experimental::Internal::RIOShared::fShared
std::shared_ptr< T > fShared
! holder of object
Definition: RDrawable.hxx:68
ROOT::Experimental::RDrawable::RDisplayContext::GetIndex
unsigned GetIndex() const
Definition: RDrawable.hxx:175
ROOT::Experimental::RDrawable::UseStyle
virtual void UseStyle(const std::shared_ptr< RStyle > &style)
Definition: RDrawable.hxx:222
ROOT::Experimental::RDrawable::fCssClass
std::string fCssClass
user defined drawable class, can later go inside map
Definition: RDrawable.hxx:187
ROOT::Experimental::RDrawable::RDisplayContext::RDisplayContext
RDisplayContext()=default
ROOT::Experimental::RDrawable::RDisplayContext::GetDrawable
RDrawable * GetDrawable() const
Definition: RDrawable.hxx:174
ROOT::Experimental::RDrawable
Base class for drawable entities: objects that can be painted on a RPad.
Definition: RDrawable.hxx:121
ROOT::Experimental::RDrawable::RDisplayContext::GetCanvas
RCanvas * GetCanvas() const
Definition: RDrawable.hxx:172
ROOT::Experimental::RDrawable::PopulateMenu
virtual void PopulateMenu(RMenuItems &)
Definition: RDrawable.cxx:30
ROOT::Experimental::Internal::RIOSharedVector_t
std::vector< RIOSharedBase * > RIOSharedVector_t
Definition: RDrawable.hxx:64
ROOT::Experimental::Internal::RIOSharedBase::MakeShared
virtual void * MakeShared()=0
ROOT::Experimental::Internal::RIOShared::GetIOPtr
const void * GetIOPtr() const final
Definition: RDrawable.hxx:71
ROOT::Experimental::RCanvas
A window's topmost RPad.
Definition: RCanvas.hxx:59
ROOT::Experimental::RDrawable::RDisplayContext::fMainConn
bool fMainConn
! is main connection
Definition: RDrawable.hxx:144
ROOT::Experimental::RIndirectDisplayItem
Extract (reference) only basic attributes from drawable, but not drawable itself.
Definition: RDisplayItem.hxx:95
ROOT::Experimental::RDrawable::SetCssClass
void SetCssClass(const std::string &cl)
Definition: RDrawable.hxx:225
ROOT::Experimental::RDrawable::GetCssType
const std::string & GetCssType() const
Definition: RDrawable.hxx:228
ROOT::Experimental::RDrawable::GetVersion
Version_t GetVersion() const
Definition: RDrawable.hxx:207
ROOT::Experimental::RDrawable::fId
std::string fId
optional object identifier, may be used in CSS as well
Definition: RDrawable.hxx:188
ROOT::Experimental::RStyle
A set of defaults for graphics attributes, e.g. for histogram fill color, line width,...
Definition: RStyle.hxx:43
ROOT::Experimental::RDrawable::Display
virtual std::unique_ptr< RDisplayItem > Display(const RDisplayContext &)
Creates display item for drawable By default item contains drawable data itself.
Definition: RDrawable.cxx:68
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:52
ROOT::Experimental::RDrawable::fVersion
Version_t fVersion
! drawable version, changed from the canvas
Definition: RDrawable.hxx:189
type
int type
Definition: TGX11.cxx:121
ROOT::Experimental::RDrawable::CollectShared
virtual void CollectShared(Internal::RIOSharedVector_t &)
Definition: RDrawable.hxx:193
ROOT::Experimental::Internal::RIOShared::reset
void reset()
Definition: RDrawable.hxx:101
ROOT::Experimental::Internal::RIOSharedBase::~RIOSharedBase
virtual ~RIOSharedBase()=default
RStyle.hxx
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Experimental::RDrawable::GetId
const std::string & GetId() const
Definition: RDrawable.hxx:230
ROOT::Experimental::RPadBase
Base class for graphic containers for RDrawable-s.
Definition: RPadBase.hxx:49
ROOT::Experimental::RDrawable::RDisplayContext::SetCanvas
void SetCanvas(RCanvas *canv)
Set canvas.
Definition: RDrawable.hxx:156