Logo ROOT   6.14/05
Reference Guide
TCanvas.cxx
Go to the documentation of this file.
1 /// \file TCanvas.cxx
2 /// \ingroup Gpad ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-07-10
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-2015, 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/TCanvas.hxx"
17 
18 #include "ROOT/TLogger.hxx"
19 
20 #include <algorithm>
21 #include <memory>
22 #include <stdio.h>
23 #include <string.h>
24 
25 #include "TROOT.h"
26 
27 namespace {
28 static std::vector<std::shared_ptr<ROOT::Experimental::TCanvas>> &GetHeldCanvases()
29 {
30  static std::vector<std::shared_ptr<ROOT::Experimental::TCanvas>> sCanvases;
31  return sCanvases;
32 }
33 } // namespace
34 
35 const std::vector<std::shared_ptr<ROOT::Experimental::TCanvas>> &ROOT::Experimental::TCanvas::GetCanvases()
36 {
37  return GetHeldCanvases();
38 }
39 
40 // void ROOT::Experimental::TCanvas::Paint() {
41 // for (auto&& drw: fPrimitives) {
42 // drw->Paint(*this);
43 // }
44 // }
45 
46 ///////////////////////////////////////////////////////////////////////////////////////
47 /// Generates unique ID inside the canvas
48 
50 {
51  return std::to_string(fIdCounter++);
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////////////
55 /// Returns true is canvas was modified since last painting
56 
58 {
59  return fPainter ? fPainter->IsCanvasModified(fModified) : fModified;
60 }
61 
63 {
64  if (fPainter)
65  fPainter->CanvasUpdated(fModified, async, callback);
66 
67  // SnapshotList_t lst;
68  // for (auto&& drw: fPrimitives) {
69  // TSnapshot *snap = drw->CreateSnapshot(*this);
70  // lst.push_back(std::unique_ptr<TSnapshot>(snap));
71  // }
72 }
73 
74 std::shared_ptr<ROOT::Experimental::TCanvas> ROOT::Experimental::TCanvas::Create(const std::string &title)
75 {
76  auto pCanvas = std::make_shared<TCanvas>();
77  pCanvas->SetTitle(title);
78  GetHeldCanvases().emplace_back(pCanvas);
79  return pCanvas;
80 }
81 
82 //////////////////////////////////////////////////////////////////////////
83 /// Create new display for the canvas
84 /// Parameter \par where specifies which program could be used for display creation
85 /// Possible values:
86 ///
87 /// cef - Chromium Embeded Framework, local display, local communication
88 /// qt5 - Qt5 WebEngine (when running via rootqt5), local display, local communication
89 /// browser - default system web-browser, communication via random http port from range 8800 - 9800
90 /// <prog> - any program name which will be started instead of default browser, like firefox or /usr/bin/opera
91 /// one could also specify $url in program name, which will be replaced with canvas URL
92 /// native - either any available local display or default browser
93 ///
94 /// Canvas can be displayed in several different places
95 
96 void ROOT::Experimental::TCanvas::Show(const std::string &where)
97 {
98  if (fPainter) {
99  bool isany = (fPainter->NumDisplays() > 0);
100 
101  if (!where.empty())
102  fPainter->NewDisplay(where);
103 
104  if (isany) return;
105  }
106 
107  if (!fModified)
108  fModified = 1; // 0 is special value, means no changes and no drawings
109 
110  if (!fPainter)
112 
113  if (fPainter) {
114  fPainter->NewDisplay(where);
115  fPainter->CanvasUpdated(fModified, true, nullptr); // trigger async display
116  }
117 }
118 
119 //////////////////////////////////////////////////////////////////////////
120 /// Close all canvas displays
121 
123 {
124  if (fPainter)
125  delete fPainter.release();
126 }
127 
128 //////////////////////////////////////////////////////////////////////////
129 /// Create image file for the canvas
130 /// Supported SVG (extension .svg), JPEG (extension .jpg or .jpeg) and PNG (extension .png)
131 /// \param async specifies if file can be created asynchronous to the caller thread
132 /// When operation completed, callback function is called
133 
134 void ROOT::Experimental::TCanvas::SaveAs(const std::string &filename, bool async, CanvasCallback_t callback)
135 {
136 
137  if (filename.find(".json") != std::string::npos) {
139  fPainter->DoWhenReady("JSON", filename, async, callback);
140  return;
141  }
142 
143  if (!fPainter || (fPainter->NumDisplays()==0))
144  Show("batch_canvas");
145 
146  if (filename.find(".svg") != std::string::npos)
147  fPainter->DoWhenReady("SVG", filename, async, callback);
148  else if (filename.find(".png") != std::string::npos)
149  fPainter->DoWhenReady("PNG", filename, async, callback);
150  else if ((filename.find(".jpg") != std::string::npos) || (filename.find(".jpeg") != std::string::npos))
151  fPainter->DoWhenReady("JPEG", filename, async, callback);
152 }
153 
154 // TODO: removal from GetHeldCanvases().
std::string GenerateUniqueId()
Generates unique ID inside the canvas.
Definition: TCanvas.cxx:49
uint64_t fModified
Modify counter, incremented every time canvas is changed.
Definition: TCanvas.hxx:43
uint64_t fIdCounter
counter for objects, id==1 is canvas itself
Definition: TCanvas.hxx:45
std::unique_ptr< Internal::TVirtualCanvasPainter > fPainter
The painter of this canvas, bootstrapping the graphics connection.
Definition: TCanvas.hxx:50
static std::unique_ptr< TVirtualCanvasPainter > Create(const TCanvas &canv)
Loads the plugin that implements this class.
void Update(bool async=false, CanvasCallback_t callback=nullptr)
update drawing
Definition: TCanvas.cxx:62
void Hide()
Close all canvas displays.
Definition: TCanvas.cxx:122
bool IsModified() const
Returns true is canvas was modified since last painting.
Definition: TCanvas.cxx:57
void SaveAs(const std::string &filename, bool async=false, CanvasCallback_t callback=nullptr)
Save canvas in image file.
Definition: TCanvas.cxx:134
std::function< void(bool)> CanvasCallback_t
void Show(const std::string &where="")
Display the canvas.
Definition: TCanvas.cxx:96
static std::shared_ptr< TCanvas > Create(const std::string &title)
Definition: TCanvas.cxx:74
static const std::vector< std::shared_ptr< TCanvas > > & GetCanvases()
Definition: TCanvas.cxx:35