Logo ROOT   6.12/07
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 
47 {
48  return fPainter ? fPainter->IsCanvasModified(fModified) : fModified;
49 }
50 
52 {
53  if (fPainter)
54  fPainter->CanvasUpdated(fModified, async, callback);
55 
56  // SnapshotList_t lst;
57  // for (auto&& drw: fPrimitives) {
58  // TSnapshot *snap = drw->CreateSnapshot(*this);
59  // lst.push_back(std::unique_ptr<TSnapshot>(snap));
60  // }
61 }
62 
63 std::shared_ptr<ROOT::Experimental::TCanvas> ROOT::Experimental::TCanvas::Create(const std::string &title)
64 {
65  auto pCanvas = std::make_shared<TCanvas>();
66  pCanvas->SetTitle(title);
67  GetHeldCanvases().emplace_back(pCanvas);
68  return pCanvas;
69 }
70 
71 //////////////////////////////////////////////////////////////////////////
72 /// Create new display for the canvas
73 /// Parameter \par where specifies which program could be used for display creation
74 /// Possible values:
75 ///
76 /// cef - Chromium Embeded Framework, local display, local communication
77 /// qt5 - Qt5 WebEngine (when running via rootqt5), local display, local communication
78 /// browser - default system web-browser, communication via random http port from range 8800 - 9800
79 /// <prog> - any program name which will be started instead of default browser, like firefox or /usr/bin/opera
80 /// one could also specify $url in program name, which will be replaced with canvas URL
81 /// native - either any available local display or default browser
82 ///
83 /// Canvas can be displayed in several different places
84 
85 void ROOT::Experimental::TCanvas::Show(const std::string &where)
86 {
87  if (fPainter) {
88  if (!where.empty())
89  fPainter->NewDisplay(where);
90  return;
91  }
92 
93  bool batch_mode = gROOT->IsBatch();
94  if (!fModified)
95  fModified = 1; // 0 is special value, means no changes and no drawings
96 
98  if (fPainter) {
99  fPainter->NewDisplay(where);
100  fPainter->CanvasUpdated(fModified, true, nullptr); // trigger async display
101  }
102 }
103 
104 //////////////////////////////////////////////////////////////////////////
105 /// Close all canvas displays
106 
108 {
109  if (fPainter)
110  delete fPainter.release();
111 }
112 
113 void ROOT::Experimental::TCanvas::SaveAs(const std::string &filename, bool async, CanvasCallback_t callback)
114 {
115  if (!fPainter)
117  if (filename.find(".svg") != std::string::npos)
118  fPainter->DoWhenReady("SVG", filename, async, callback);
119  else if (filename.find(".png") != std::string::npos)
120  fPainter->DoWhenReady("PNG", filename, async, callback);
121  else if ((filename.find(".jpg") != std::string::npos) || (filename.find(".jpeg") != std::string::npos))
122  fPainter->DoWhenReady("JPEG", filename, async, callback);
123 }
124 
125 // TODO: removal from GetHeldCanvases().
#define gROOT
Definition: TROOT.h:402
uint64_t fModified
Modify counter, incremented every time canvas is changed.
Definition: TCanvas.hxx:57
std::unique_ptr< Internal::TVirtualCanvasPainter > fPainter
The painter of this canvas, bootstrapping the graphics connection.
Definition: TCanvas.hxx:62
void Update(bool async=false, CanvasCallback_t callback=nullptr)
update drawing
Definition: TCanvas.cxx:51
static std::unique_ptr< TVirtualCanvasPainter > Create(const TCanvas &canv, bool batch_mode=false)
Loads the plugin that implements this class.
void Hide()
Close all canvas displays.
Definition: TCanvas.cxx:107
void SaveAs(const std::string &filename, bool async=false, CanvasCallback_t callback=nullptr)
Save canvas in image file.
Definition: TCanvas.cxx:113
std::function< void(bool)> CanvasCallback_t
void Show(const std::string &where="")
Display the canvas.
Definition: TCanvas.cxx:85
static std::shared_ptr< TCanvas > Create(const std::string &title)
Definition: TCanvas.cxx:63
static const std::vector< std::shared_ptr< TCanvas > > & GetCanvases()
Definition: TCanvas.cxx:35