Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TSlider.cxx
Go to the documentation of this file.
1// @(#)root/gpad:$Id$
2// Author: Rene Brun 23/11/96
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TROOT.h"
13#include "TSlider.h"
14#include "TSliderBox.h"
15
16#include <cstring>
17#include <iostream>
18
19
20/** \class TSlider
21\ingroup gpad
22
23A specialized TPad including a TSliderBox object.
24The TSliderBox can be moved in the pad.
25
26Slider drawing options include the possibility to change the slider
27starting and ending positions or only one of them.
28
29The current slider position can be retrieved via the functions
30TSlider::GetMinimum and TSlider::GetMaximum
31 These two functions return numbers in the range [0,1].
32
33If a method has been set (via TSlider::SetMethod), the expression is
34executed via the interpreter when the button 1 is released.
35
36 if no method has been set, and an object is referenced (TSlider::SetObject
37 has been called), while the slider is being moved/resized,
38 the object ExecuteEvent function is called.
39
40### Example 1 using TSlider::SetMethod
41
42#### macro xyslider.C
43
44~~~ {.cpp}
45void xyslider()
46{
47 // Example of macro featuring two sliders
48 TFile *f = new TFile("hsimple.root");
49 TH2F *hpxpy = (TH2F*)f->Get("hpxpy");
50 TCanvas *c1 = new TCanvas("c1");
51 TPad *pad = new TPad("pad","Color plot pad",0.1,0.1,0.98,0.98);
52 pad->Draw();
53 pad->cd();
54 hpxpy->Draw("COLZ");
55 c1->cd();
56
57 // Create two sliders in main canvas. When button1 will be released
58 // the macro action.C will be called.
59 TSlider *xslider = new TSlider("xslider","x",0.1,0.02,0.98,0.08);
60 xslider->SetMethod(".x action.C");
61 TSlider *yslider = new TSlider("yslider","y",0.02,0.1,0.06,0.98);
62 yslider->SetMethod(".x action.C");
63}
64~~~
65
66#### macro action.C
67
68~~~ {.cpp}
69void action()
70{
71 Int_t nx = hpxpy->GetXaxis()->GetNbins();
72 Int_t ny = hpxpy->GetYaxis()->GetNbins();
73 Int_t binxmin = nx*xslider->GetMinimum();
74 Int_t binxmax = nx*xslider->GetMaximum();
75 hpxpy->GetXaxis()->SetRange(binxmin,binxmax);
76 Int_t binymin = ny*yslider->GetMinimum();
77 Int_t binymax = ny*yslider->GetMaximum();
78 hpxpy->GetYaxis()->SetRange(binymin,binymax);
79 pad->cd();
80 hpxpy->Draw("COLZ");
81 c1->Update();
82}
83~~~
84
85The canvas and the sliders created in the above macro are shown in the picture
86below.
87
88\image html gpad_slider.png
89
90### Example 2 using TSlider::SetObject macro xyslider.C
91
92 Same example as above. Instead of TSlider::SetMethod:
93~~~ {.cpp}
94 Myclass *obj = new Myclass(); // Myclass derived from TObject
95 xslider->SetObject(obj);
96 yslider->SetObject(obj);
97~~~
98
99When the slider will be changed, MyClass::ExecuteEvent will be called with px=0
100and py = 0
101*/
102
103////////////////////////////////////////////////////////////////////////////////
104/// slider default constructor.
105
107{
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// Slider normal constructor.
112///
113/// x1,y1,x2,y2 are in pad user coordinates
114
116 :TPad(name,title,0.1,0.1,0.9,0.9,color,bordersize,bordermode)
117{
118 if (!gPad) return;
119
120 Double_t x1pad = gPad->GetX1();
121 Double_t x2pad = gPad->GetX2();
122 Double_t y1pad = gPad->GetY1();
123 Double_t y2pad = gPad->GetY2();
129 Range(0,0,1,1);
130
133
137 sbox->SetSlider(this);
139 AppendPad();
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// slider default destructor.
144
148
149////////////////////////////////////////////////////////////////////////////////
150/// Save primitive as a C++ statement(s) on output stream out
151
152void TSlider::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
153{
154 auto sbox = dynamic_cast<TSliderBox *>(fPrimitives->FindObject("TSliderBox"));
155
157 out, Class(), "slider",
158 TString::Format("\"%s\", \"%s\", %g, %g, %g, %g, %d, %d, %d", TString(GetName()).ReplaceSpecialCppChars().Data(),
159 TString(GetTitle()).ReplaceSpecialCppChars().Data(),
160 gPad->GetX1() + (gPad->GetX2() - gPad->GetX1()) * GetXlowNDC(),
161 gPad->GetY1() + (gPad->GetY2() - gPad->GetY1()) * GetYlowNDC(),
162 gPad->GetX1() + (gPad->GetX2() - gPad->GetX1()) * (GetXlowNDC() + GetWNDC()),
163 gPad->GetY1() + (gPad->GetY2() - gPad->GetY1()) * (GetYlowNDC() + GetHNDC()),
164 sbox ? sbox->GetFillColor() : GetFillColor(), GetBorderSize(), GetBorderMode()));
165
166 SaveFillAttributes(out, "slider", 16, 1001);
167 SaveLineAttributes(out, "slider", 1, 1, 1);
168
169 out << " slider->SetRange(" << fMinimum << ", " << fMaximum << ");\n";
170
172 if (cmethod.Length() > 0)
173 out << " slider->SetMethod(\"" << cmethod.ReplaceSpecialCppChars() << "\");\n";
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Set Slider range in [0,1]
178
180{
181 auto sbox = dynamic_cast<TSliderBox*>(fPrimitives->FindObject("TSliderBox"));
182
183 if (sbox) {
184 if (fAbsWNDC > fAbsHNDC) {
185 sbox->SetX1(xmin);
186 sbox->SetX2(xmax);
187 } else {
188 sbox->SetY1(xmin);
189 sbox->SetY2(xmax);
190 }
191 }
192 fMinimum = xmin;
193 fMaximum = xmax;
194 Modified();
195}
short Color_t
Color number (short)
Definition RtypesCore.h:99
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:110
float xmin
float ymin
float xmax
float ymax
#define gPad
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:31
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:238
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:274
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:575
void Add(TObject *obj) override
Definition TList.h:81
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:771
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:68
The most important graphics class in the ROOT system.
Definition TPad.h:28
Short_t GetBorderMode() const override
Definition TPad.h:200
Double_t fAbsWNDC
Absolute Width of pad along X in NDC.
Definition TPad.h:71
const char * GetTitle() const override
Returns title of object.
Definition TPad.h:262
void SetPad(const char *name, const char *title, Double_t xlow, Double_t ylow, Double_t xup, Double_t yup, Color_t color=35, Short_t bordersize=5, Short_t bordermode=-1) override
Set all pad parameters.
Definition TPad.cxx:6242
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TPad.cxx:5453
Double_t PixeltoX(Int_t px) override
Convert pixel to X coordinate.
Definition TPad.cxx:7478
void Modified(Bool_t flag=true) override
Mark pad modified Will be repainted when TCanvas::Update() will be called next time.
Definition TPad.cxx:7459
Double_t fAbsHNDC
Absolute Height of pad along Y in NDC.
Definition TPad.h:72
Short_t GetBorderSize() const override
Definition TPad.h:201
TList * fPrimitives
->List of primitives (subpads)
Definition TPad.h:107
Double_t GetXlowNDC() const override
Definition TPad.h:213
Double_t GetYlowNDC() const override
Definition TPad.h:214
Double_t PixeltoY(Int_t py) override
Convert pixel to Y coordinate.
Definition TPad.cxx:7487
Double_t GetWNDC() const override
Get width of pad along X in Normalized Coordinates (NDC)
Definition TPad.h:216
Double_t GetHNDC() const override
Get height of pad along Y in Normalized Coordinates (NDC)
Definition TPad.h:218
const char * GetName() const override
Returns name of object.
Definition TPad.h:261
The moving box in a TSlider.
Definition TSliderBox.h:20
TSlider()
slider default constructor.
Definition TSlider.cxx:106
static TClass * Class()
virtual const char * GetMethod() const
Definition TSlider.h:36
Double_t fMaximum
Slider maximum value in [0,1].
Definition TSlider.h:21
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TSlider.cxx:152
~TSlider() override
slider default destructor.
Definition TSlider.cxx:145
virtual void SetRange(Double_t xmin=0, Double_t xmax=1)
Set Slider range in [0,1].
Definition TSlider.cxx:179
Double_t fMinimum
Slider minimum value in [0,1].
Definition TSlider.h:20
Basic string class.
Definition TString.h:138
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384