Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RCanvas.hxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2015, 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_RCanvas
10 #define ROOT7_RCanvas
11 
12 #include "ROOT/RPadBase.hxx"
14 
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 namespace ROOT {
20 namespace Experimental {
21 
22 /** \class RCanvas
23 \ingroup GpadROOT7
24 \brief A window's topmost `RPad`.
25 \author Axel Naumann <axel@cern.ch>
26 \date 2015-07-08
27 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
28 */
29 
30 class RCanvas: public RPadBase {
31 friend class RPadBase; /// use for ID generation
32 friend class RCanvasPainter; /// used for primitives drawing
33 private:
34  /// Title of the canvas.
35  std::string fTitle;
36 
37  /// Size of the canvas in pixels,
38  std::array<RPadLength::Pixel, 2> fSize;
39 
40  /// Modify counter, incremented every time canvas is changed
41  uint64_t fModified{0}; ///<!
42 
43  /// The painter of this canvas, bootstrapping the graphics connection.
44  /// Unmapped canvases (those that never had `Draw()` invoked) might not have
45  /// a painter.
46  std::unique_ptr<Internal::RVirtualCanvasPainter> fPainter; ///<!
47 
48  /// Disable copy construction for now.
49  RCanvas(const RCanvas &) = delete;
50 
51  /// Disable assignment for now.
52  RCanvas &operator=(const RCanvas &) = delete;
53 
54 public:
55  static std::shared_ptr<RCanvas> Create(const std::string &title);
56 
57  /// Create a temporary RCanvas; for long-lived ones please use Create().
58  RCanvas() = default;
59 
60  ~RCanvas() = default;
61 
62  const RCanvas *GetCanvas() const override { return this; }
63 
64  /// Access to the top-most canvas, if any (non-const version).
65  RCanvas *GetCanvas() override { return this; }
66 
67  /// Return canvas pixel size as array with two elements - width and height
68  const std::array<RPadLength::Pixel, 2> &GetSize() const { return fSize; }
69 
70  /// Set canvas pixel size as array with two elements - width and height
71  RCanvas &SetSize(const std::array<RPadLength::Pixel, 2> &sz)
72  {
73  fSize = sz;
74  return *this;
75  }
76 
77  /// Set canvas pixel size - width and height
78  RCanvas &SetSize(const RPadLength::Pixel &width, const RPadLength::Pixel &height)
79  {
80  fSize[0] = width;
81  fSize[1] = height;
82  return *this;
83  }
84 
85  /// Display the canvas.
86  void Show(const std::string &where = "");
87 
88  /// Returns window name used to display canvas
89  std::string GetWindowAddr() const;
90 
91  /// Hide all canvas displays
92  void Hide();
93 
94  /// Remove canvas from global canvas lists, will be destroyed when shared_ptr will be removed
95  void Remove();
96 
97  /// Insert panel into the canvas, canvas should be shown at this moment
98  template <class PANEL>
99  bool AddPanel(std::shared_ptr<PANEL> &panel)
100  {
101  if (!fPainter) return false;
102  return fPainter->AddPanel(panel->GetWindow());
103  }
104 
105  // Indicates that primitives list was changed or any primitive was modified
106  void Modified() { fModified++; }
107 
108  // Return if canvas was modified and not yet updated
109  bool IsModified() const;
110 
111  /// update drawing
112  void Update(bool async = false, CanvasCallback_t callback = nullptr);
113 
114  /// Run canvas functionality for given time (in seconds)
115  void Run(double tm = 0.);
116 
117  /// Save canvas in image file
118  void SaveAs(const std::string &filename, bool async = false, CanvasCallback_t callback = nullptr);
119 
120  /// Get the canvas's title.
121  const std::string &GetTitle() const { return fTitle; }
122 
123  /// Set the canvas's title.
124  RCanvas &SetTitle(const std::string &title)
125  {
126  fTitle = title;
127  return *this;
128  }
129 
130  void ResolveSharedPtrs();
131 
132  /// Convert a `Pixel` position to Canvas-normalized positions.
133  std::array<RPadLength::Normal, 2> PixelsToNormal(const std::array<RPadLength::Pixel, 2> &pos) const final
134  {
135  return {{pos[0] / fSize[0], pos[1] / fSize[1]}};
136  }
137 
138  static const std::vector<std::shared_ptr<RCanvas>> GetCanvases();
139 };
140 
141 } // namespace Experimental
142 } // namespace ROOT
143 
144 #endif