Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TDiamond.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Rene Brun 22/06/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 <cstdlib>
13
14#include <iostream>
15#include "TROOT.h"
16#include "TDiamond.h"
17#include "TVirtualPad.h"
18#include "TBoxInteractive.h"
19#include "TCanvasImp.h"
20#include "TMath.h"
21
22
23/** \class TDiamond
24\ingroup BasicGraphics
25
26Draw a Diamond.
27
28A diamond is defined by:
29
30- Its central left coordinates x1,y1
31- Its top central coordinates x2,y2
32
33A diamond has line attributes (see TAttLine) and fill area attributes (see TAttFill).
34
35Like for the class TPaveText, a TDiamond may have one or more line(s) of text inside.
36
37Begin_Macro(source)
38../../../tutorials/visualisation/graphics/diamond.C
39End_Macro
40*/
41
42////////////////////////////////////////////////////////////////////////////////
43/// Diamond default constructor.
44
48
49////////////////////////////////////////////////////////////////////////////////
50/// Diamond standard constructor.
51
56
57////////////////////////////////////////////////////////////////////////////////
58/// Diamond destructor.
59
63
64////////////////////////////////////////////////////////////////////////////////
65/// Copy constructor.
66
67TDiamond::TDiamond(const TDiamond &diamond) : TPaveText(diamond)
68{
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Compute distance from point px,py to a diamond.
73///
74/// Compute the closest distance of approach from point px,py to the
75/// edges of this diamond.
76/// The distance is computed in pixels units.
77
82
83////////////////////////////////////////////////////////////////////////////////
84/// Draw this diamond with its current attributes.
85
91
93 public:
95
96 void PaintOutline(TVirtualPad &parent) override
97 {
98 Double_t xd[5] = { (newX1 + newX2) / 2, newX1, (newX1 + newX2) / 2, newX2, (newX1 + newX2) / 2 };
99 Double_t yd[5] = { newY2, (newY1 + newY2)/2, newY1, (newY1 + newY2)/2, newY2 };
100
101 // "i" is interactive painting, "diamond" is id
102 parent.PaintPolyLine(5, xd, yd, "idiamond");
103 }
104};
105
106
107////////////////////////////////////////////////////////////////////////////////
108/// Execute action corresponding to one event.
109///
110/// This member function is called when a Diamond object is clicked.
111///
112/// If the mouse is clicked inside the diamond, the diamond is moved.
113///
114/// If the mouse is clicked on the 4 tops (pL,pR,pTop,pBot), the diamond is
115/// rescaled.
116
118{
119 if (!gPad || !gPad->IsEditable())
120 return;
121
122 auto &parent = *gPad;
123
124 auto inter = parent.GetInteractive<TDiamondInteractive>(this);
125
126 auto setNewValues = [&inter, this]() {
127 SetX1(inter->newX1);
128 SetX2(inter->newX2);
129 SetY1(inter->newY1);
130 SetY2(inter->newY2);
131 };
132
133 switch (event) {
134
135 case kArrowKeyPress:
136 case kButton1Down:
137 inter = parent.MakeInteractive<TDiamondInteractive>(this, kFALSE, GetX1(), GetY1(), GetX2(), GetY2());
138 // No break !!!
139
140 case kMouseMotion: {
141
143 if (!inter) inter = &dummy;
144 inter->CalcPixelCoord(parent, GetX1(), GetY1(), GetX2(), GetY2());
145
146 if (!inter->SelectDiamondCorner(px, py)) {
147 // refuse interactive changes
148 parent.FreeInteractive(this);
149 } else {
150 inter->SetCursor(parent, event == kButton1Down);
151 fResizing = inter->IsResizing() && (event != kMouseMotion);
152 }
153
154 break;
155 }
156
157 case kArrowKeyRelease:
158 case kButton1Motion: {
159
160 if (!inter)
161 return;
162
163 if (!inter->ProcessMouseMove(parent, px, py))
164 return;
165
166 inter->ApplyChanges(parent);
167
168 if (inter->IsOpaque(parent)) {
169 setNewValues();
170 parent.ShowGuidelines(this, event, inter->GetGuideChar(), true);
171 parent.Modified(kTRUE);
172 }
173
174 break;
175 }
176
177 case kButton1Up:
178
179 if (inter && inter->IsOpaque(parent))
180 parent.ShowGuidelines(this, event);
181
182 if (gROOT->IsEscaped()) {
183 gROOT->SetEscape(kFALSE);
184 if (inter && inter->IsOpaque(parent)) {
185 SetX1(inter->oldX1);
186 SetY1(inter->oldY1);
187 SetX2(inter->oldX2);
188 SetY2(inter->oldY2);
189 }
190 } else if (inter && !inter->IsOpaque(parent) && (inter->newX1 != inter->newX2)) {
191 setNewValues();
192 }
193
194 parent.Modified();
195 parent.FreeInteractive(this); // delete interactive object
197
198 break;
199
200 case kButton1Locate:
201 // Sergey: code is never used, has to be removed in ROOT7
202 ExecuteEvent(kButton1Down, px, py);
203
204 while (true) {
205 px = py = 0;
206 event = parent.GetCanvasImp()->RequestLocator(px, py);
207
209
210 if (event != -1) { // button is released
211 ExecuteEvent(kButton1Up, px, py);
212 return;
213 }
214 }
215 }
216}
217
218////////////////////////////////////////////////////////////////////////////////
219/// Return 1 if the point (x,y) is inside the polygon defined by
220/// the diamond 0 otherwise.
221
223{
224
225 Double_t xd[4], yd[4];
226
227 xd[0] = fX1;
228 yd[0] = (fY2 + fY1) / 2.;
229 xd[1] = (fX2 + fX1) / 2.;
230 yd[1] = fY1;
231 xd[2] = fX2;
232 yd[2] = yd[0];
233 xd[3] = xd[1];
234 yd[3] = fY2;
235
236 return (Int_t)TMath::IsInside(x, y, 4, xd, yd);
237}
238
239////////////////////////////////////////////////////////////////////////////////
240/// Paint this diamond with its current attributes.
241
243{
244 if (!gPad) return;
245 Double_t x[7],y[7],depx,depy;
246 Double_t x1 = fX1;
247 Double_t y1 = fY1;
248 Double_t x2 = fX2;
249 Double_t y2 = fY2;
250 Int_t fillstyle = GetFillStyle();
251 Int_t fillcolor = GetFillColor();
252 Int_t linecolor = GetLineColor();
253 if (fBorderSize) {
254 Double_t wy = gPad->PixeltoY(0) - gPad->PixeltoY(fBorderSize);
255 Double_t wx = gPad->PixeltoX(fBorderSize) - gPad->PixeltoX(0);
256 // Draw the frame top right
257 if (y2-y1>x2-x1) {
258 depx = wx;
259 depy = 0;
260 }
261 else if (y2-y1<x2-x1) {
262 depx = 0;
263 depy = -wy;
264 }
265 else {
266 depx = wx;
267 depy = -wy;
268 }
269 x[0] = x[2] = (x1+x2)/2+depx;
270 x[1] = x2+depx;
271 x[3] = x1+depx;
272 y[0] = y2+depy;
273 y[2] = y1+depy;
274 y[1] = y[3] =(y1+y2)/2+depy;
275 x[4] = x[0]; y[4] = y[0];
276 SetFillStyle(fillstyle);
277 SetFillColor(linecolor);
278 TAttFill::Modify(); //Change fill area attributes only if necessary
279 gPad->PaintFillArea(4,x,y);
280 }
281 x[0] = x[2] = (x1+x2)/2;
282 x[1] = x2;
283 x[3] = x1;
284 y[0] = y2;
285 y[2] = y1;
286 y[1] = y[3] = (y1+y2)/2;
287 x[4] = x[0]; y[4] =y[0];
288 SetLineColor(linecolor);
289 SetFillColor(fillcolor);
290 TAttLine::Modify(); //Change line attributes only if necessary
291 TAttFill::Modify(); //Change fill area attributes only if necessary
292 gPad->PaintFillArea(4,x,y);
293 gPad->PaintPolyLine(5,x,y);
294
295 // Paint list of primitives (test,etc)
297}
298
299////////////////////////////////////////////////////////////////////////////////
300/// Save primitive as a C++ statement(s) on output stream out.
301
302void TDiamond::SavePrimitive(std::ostream &out, Option_t *option)
303{
304 SavePrimitiveConstructor(out, Class(), "diamond", TString::Format("%g, %g, %g, %g", fX1, fY1, fX2, fY2));
305
306 SaveFillAttributes(out, "diamond", -1, -1);
307 SaveLineAttributes(out, "diamond", 1, 1, 1);
308 SaveTextAttributes(out, "diamond", 11, 0, 1, 62, 0.05);
309
310 SaveLines(out, "diamond", kTRUE);
311
312 SavePrimitiveDraw(out, "diamond", option);
313}
@ kMouseMotion
Definition Buttons.h:23
@ kArrowKeyRelease
Definition Buttons.h:21
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kArrowKeyPress
Definition Buttons.h:21
@ kButton1Down
Definition Buttons.h:17
@ kButton1Locate
Definition Buttons.h:22
@ kDiamond
Definition Buttons.h:37
cudaEvent_t event
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
Option_t Option_t option
Option_t Option_t SetFillStyle
Option_t Option_t SetLineColor
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 SetFillColor
Option_t Option_t TPoint TPoint const char y1
#define gROOT
Definition TROOT.h:417
#define gPad
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:32
virtual Style_t GetFillStyle() const
Return the fill area style.
Definition TAttFill.h:33
virtual void Modify()
Change current fill area attributes if necessary.
Definition TAttFill.cxx:212
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:240
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:36
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:246
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:289
virtual void SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef=12, Float_t angdef=0, Int_t coldef=1, Int_t fondef=61, Float_t sizdef=1)
Save text attributes as C++ statement(s) on output stream out.
Definition TAttText.cxx:399
void CalcPixelCoord(const TVirtualPad &parent, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Calculate pixel coordinates.
TBoxInteractive(Bool_t bx=kTRUE, Double_t x1=0., Double_t y1=0., Double_t x2=0., Double_t y2=0.)
Constructor.
Double_t GetX1() const
Definition TBox.h:51
Double_t fX1
X of 1st point.
Definition TBox.h:28
Double_t GetX2() const
Definition TBox.h:52
Double_t GetY1() const
Definition TBox.h:53
Double_t GetY2() const
Definition TBox.h:54
Double_t fY2
Y of 2nd point.
Definition TBox.h:31
Double_t fX2
X of 2nd point.
Definition TBox.h:30
Double_t fY1
Y of 1st point.
Definition TBox.h:29
Bool_t fResizing
! True if box is being resized
Definition TBox.h:32
void PaintOutline(TVirtualPad &parent) override
Paint object outline.
Definition TDiamond.cxx:96
Draw a Diamond.
Definition TDiamond.h:17
TDiamond()
Diamond default constructor.
Definition TDiamond.cxx:45
void Draw(Option_t *option="") override
Draw this diamond with its current attributes.
Definition TDiamond.cxx:86
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TDiamond.cxx:117
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TDiamond.cxx:302
void Paint(Option_t *option="") override
Paint this diamond with its current attributes.
Definition TDiamond.cxx:242
Int_t IsInside(Double_t x, Double_t y) const override
Return 1 if the point (x,y) is inside the polygon defined by the diamond 0 otherwise.
Definition TDiamond.cxx:222
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a diamond.
Definition TDiamond.cxx:78
~TDiamond() override
Diamond destructor.
Definition TDiamond.cxx:60
static TClass * Class()
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
Computes distance from point (px,py) to the object.
Definition TObject.cxx:283
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:844
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:776
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
virtual void SaveLines(std::ostream &out, const char *name, Bool_t)
Save lines of this pavetext as C++ statements on output stream out.
virtual void PaintPrimitives(Int_t mode)
Paint list of primitives in this pavetext.
void SetX2(Double_t x2) override
Set the X2 value.
Definition TPave.cxx:670
Int_t fBorderSize
window box bordersize in pixels
Definition TPave.h:26
void SetX1(Double_t x1) override
Set the X1 value.
Definition TPave.cxx:657
void SetY1(Double_t y1) override
Set the Y1 value.
Definition TPave.cxx:683
void SetY2(Double_t y2) override
Set the Y2 value.
Definition TPave.cxx:696
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:2460
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:53
virtual void PaintPolyLine(Int_t n, Float_t *x, Float_t *y, Option_t *option="")=0
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Bool_t IsInside(T xp, T yp, Int_t np, T *x, T *y)
Function which returns kTRUE if point xp,yp lies inside the polygon defined by the np points in array...
Definition TMath.h:1326