Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Painter.cxx
1/*
2 * Painter.cxx
3 *
4 * Created on: 17 lut 2024
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "Painter.h"
10
11#include <iostream>
12
13#include "Cout.h" //KURWA
14#include "PadStyle.h"
15#include "StdString.h"
16#include "Style.h"
17
18#include <TCanvas.h>
19namespace Hal {
20 const int Painter::kHtmlBit = 0;
21 const int Painter::kGridBit = 1;
22
23 Painter::Painter() {
24 fCommonData.fCanvases = new std::vector<TCanvas*>();
25 fCommonData.fPads = new std::vector<std::vector<TVirtualPad*>>();
26 }
27
28 Painter::~Painter() {
30 for (auto x : fSubPainters)
31 delete x;
32 }
33
35 painter->CleanCommonData();
36 painter->fOwnPad = kFALSE;
37 painter->fParent = this;
38 painter->fCommonData = fCommonData;
39 fSubPainters.push_back(painter);
40 }
41
43 if (!HasParent()) {
44 if (!fPainted) {
46 fOwnPad = kTRUE;
47 }
48 LockPad();
49 TryPaint();
50 UpdateAllPads();
51 UnlockPad();
52 } else {
53 auto grand = GetAncestor();
54 if (grand->fPainted) {
55 grand->TryPaint();
56 grand->UpdateAllPads();
57 } else {
58 grand->Paint();
59 }
60 }
61 }
62
63 void Painter::SetFlag(Int_t bit, Bool_t state) {
64 if (state)
65 SETBIT(fDrawFlags, bit);
66 else
67 CLRBIT(fDrawFlags, bit);
68 }
69
70 void Painter::MakeCanvasPads(Int_t x, Int_t y, Int_t canvasNo) {
71 auto dividePads = [&]() {
72 auto canva = GetCanvas(canvasNo);
73 canva->Divide(x, y);
74 int count = 0;
75 for (int i = 0; i < x; i++) {
76 for (int j = 0; j < y; j++) {
77 (*fCommonData.fPads)[canvasNo].push_back(canva->cd(++count));
78 }
79 }
80 };
81 LockPad();
82 if (fCommonData.fCanvases->size() > canvasNo) {
83 dividePads();
84 } else {
85 for (int i = fCommonData.fCanvases->size(); i <= canvasNo; i++) {
86 auto newCanv = new TCanvas();
87 fCommonData.fCanvases->push_back(newCanv);
88 std::vector<TVirtualPad*> pads;
89 pads.push_back(newCanv);
90 fCommonData.fPads->push_back(pads);
91 }
92 dividePads();
93 }
94 UnlockPad();
95 }
96
97 void Painter::UpdateAllPads() {
98 if (!fOwnPad) return;
99 for (auto pads : *fCommonData.fPads) {
100 for (unsigned int i = 1; i < pads.size(); i++) {
101 auto pad = pads[i];
102 if (fPadStyle) { fPadStyle->Apply(pad); }
103 pad->Modified(kTRUE);
104 pad->Update();
105 }
106 }
107 }
108
109 void Painter::TryPaint() {
110 if (fPainted) {
111 InnerRepaint();
112 } else {
113 if (!CheckPads() && HasParent()) {
114 HalCoutDebug("Cannot paint this object due to failing pads dependencies");
115 return;
116 }
117 fPainted = kTRUE;
118 InnerPaint();
119 }
120 for (auto x : fSubPainters)
121 x->TryPaint();
122 }
123
124 void Painter::SetOption(TString option) {
125 if (Hal::Std::FindParam(option, "skip")) return;
126 if (Hal::Std::FindParam(option, "default!")) {
128 return;
129 }
130 if (Hal::Std::FindParam(option, "grid")) {
131 Hal::PadStyle style;
132 style.SetGridx(1);
133 style.SetGridy(1);
134 SetGlobalPadStyle(style);
135 }
136 if (Hal::Std::FindParam(option, "default")) { SetDefaultFlag(); }
137 ULong64_t defFlags = 0;
138 if (Hal::Std::FindParam(option, "keep", kTRUE)) { defFlags = fDrawFlags; }
139 if (Hal::Std::FindParam(option, "html", kTRUE)) {
140 SETBIT(defFlags, kHtmlBit);
141 if (defFlags != fDrawFlags) {
142 fOptionsChanged = kTRUE;
143 fDrawFlags = defFlags;
144 return;
145 }
146 } else {
147 CLRBIT(defFlags, kHtmlBit);
148 }
149 auto newOpt = SetOptionInternal(option, defFlags);
150 if (newOpt != fDrawFlags) {
151 fOptionsChanged = kTRUE;
152 fDrawFlags = newOpt;
153 }
154 }
155
156 Bool_t Painter::HasParent() const {
157 if (fParent) return kTRUE;
158 return kFALSE;
159 }
160
161 Bool_t Painter::GetPatterns(TString opt, TString flag, std::vector<double>& vals) const {
162 auto vec = Hal::Std::ExplodeString(opt, ',');
163 if (vec.size() == 0) return kFALSE;
164 TString match = Form("%s=", flag.Data());
165 if (!vec[0].Contains(match)) return kFALSE;
166 vec[0].ReplaceAll(match, "");
167 for (auto i : vec) {
168 double val = i.Atof();
169 vals.push_back(val);
170 }
171 return kTRUE;
172 }
173
174 void Painter::ContitionalPattern(TString& option, TString pattern, ULong64_t& drawOpt, Int_t bit, Bool_t remove) const {
175 Int_t flag = Hal::Std::FindParam2(option, pattern, remove);
176 if (flag == 1) SETBIT(drawOpt, bit);
177 if (flag == -1) CLRBIT(drawOpt, bit);
178 }
179
180 void Painter::LockPad() { fTempPad = gPad; }
181
183 gPad = fTempPad;
184 if (gPad) gPad->cd();
185 }
186
187 void Painter::GotoPad(Int_t no, Int_t canvasNo) { (*fCommonData.fPads)[canvasNo][no]->cd(); }
188
189 void Painter::ClearCanvas(Int_t canvasNo) {
190 for (auto canv : (*fCommonData.fCanvases)) {
191 canv->Clear();
192 (*fCommonData.fPads)[canvasNo].clear();
193 }
194 }
195
196 TVirtualPad* Painter::GetPad(Int_t index, Int_t canvasNo) const {
197 if (index < 0) index = 0;
198 return (*fCommonData.fPads)[canvasNo][index];
199 }
200
201 Painter* Painter::GetAncestor() const {
202 if (HasParent()) { return GetParent(); }
203 return GetAncestor();
204 }
205
206 void Painter::SetGlobalPadStyle(Hal::PadStyle& pad) { fPadStyle = new Hal::PadStyle(pad); }
207
208 Bool_t Painter::CanvasExist(Int_t canvasNo) const {
209 if (fCommonData.fCanvases->size() > canvasNo) return kTRUE;
210 return kFALSE;
211 }
212
214 if (!fOwnPad) return;
215 if (fCommonData.fCanvases) {
216 for (auto canv : *fCommonData.fCanvases)
217 delete canv;
218 delete fCommonData.fCanvases;
219 fCommonData.fCanvases = nullptr;
220 }
221 if (fCommonData.fPads) {
222 delete fCommonData.fPads;
223 fCommonData.fPads = nullptr;
224 }
225 }
226
227
228 void Painter::ResetFewBits(ULong64_t& flag, std::initializer_list<Int_t> bits, Int_t set) const {
229 for (auto ibit : bits)
230 CLRBIT(flag, ibit);
231 if (set >= 0) SETBIT(flag, set);
232 }
233
234} /* namespace Hal */
virtual void InnerPaint()
Definition Painter.h:176
void CleanCommonData()
Definition Painter.cxx:213
virtual void SetDefaultFlag()
Definition Painter.h:105
Bool_t CanvasExist(Int_t canvasNo=0) const
Definition Painter.cxx:208
TCanvas * GetCanvas(Int_t canvasNo=0) const
Definition Painter.h:227
void UnlockPad()
Definition Painter.cxx:182
void LockPad()
Definition Painter.cxx:180
void SetFlag(Int_t bit, Bool_t state)
Definition Painter.cxx:63
void AddPainter(Painter *painter)
Definition Painter.cxx:34
virtual void MakePadsAndCanvases()=0
void ResetFewBits(ULong64_t &flag, std::initializer_list< Int_t > bits, Int_t set=-1) const
Definition Painter.cxx:228
void MakeCanvasPads(Int_t x=1, Int_t y=1, Int_t canvasNo=0)
Definition Painter.cxx:70
Painter * GetParent() const
Definition Painter.h:221
Bool_t HasParent() const
Definition Painter.cxx:156
virtual Bool_t CheckPads() const
Definition Painter.h:111
virtual void InnerRepaint()
Definition Painter.h:172
void Paint()
Definition Painter.cxx:42
Bool_t fOptionsChanged
Definition Painter.h:68
void ContitionalPattern(TString &option, TString pattern, ULong64_t &drawOpt, Int_t bit, Bool_t remove=kTRUE) const
Definition Painter.cxx:174
virtual void SetOption(TString option)
Definition Painter.cxx:124
virtual ULong64_t SetOptionInternal(TString opt, ULong64_t prev=0)=0
TVirtualPad * GetPad(Int_t index, Int_t canvasNo=0) const
Definition Painter.cxx:196
void GotoPad(Int_t no, Int_t canvasNo=0)
Definition Painter.cxx:187
Bool_t GetPatterns(TString opt, TString flag, std::vector< double > &vals) const
Definition Painter.cxx:161
void ClearCanvas(Int_t canvas)
Definition Painter.cxx:189