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
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()) return;
120
121 auto &parent = *gPad;
122
123 auto inter = dynamic_cast<TDiamondInteractive *>(parent.Interactive(this));
124
125 auto setNewValues = [&inter, this]() {
126 SetX1(inter->newX1);
127 SetX2(inter->newX2);
128 SetY1(inter->newY1);
129 SetY2(inter->newY2);
130 };
131
132 switch (event) {
133
134 case kArrowKeyPress:
135 case kButton1Down:
136
138 parent.Interactive(this, inter);
139
140 // No break !!!
141
142 case kMouseMotion: {
143
145 if (!inter) inter = &dummy;
146 inter->CalcPixelCoord(parent, GetX1(), GetY1(), GetX2(), GetY2());
147
148 if (!inter->SelectDiamondCorner(px, py)) {
149 // refuse interactive changes
150 parent.Interactive();
151 } else {
152 inter->SetCursor(parent, event == kButton1Down);
153 fResizing = inter->IsResizing() && (event != kMouseMotion);
154 }
155
156 break;
157 }
158
159 case kArrowKeyRelease:
160 case kButton1Motion: {
161
162 if (!inter)
163 return;
164
165 if (!inter->ProcessMouseMove(parent, px, py))
166 return;
167
168 inter->ApplyChanges(parent);
169
170 if (inter->IsOpaque(parent)) {
171 setNewValues();
172 parent.ShowGuidelines(this, event, inter->GetGuideChar(), true);
173 parent.Modified(kTRUE);
174 }
175
176 break;
177 }
178
179 case kButton1Up:
180
181 if (inter && inter->IsOpaque(parent))
182 parent.ShowGuidelines(this, event);
183
184 if (gROOT->IsEscaped()) {
185 gROOT->SetEscape(kFALSE);
186 if (inter && inter->IsOpaque(parent)) {
187 SetX1(inter->oldX1);
188 SetY1(inter->oldY1);
189 SetX2(inter->oldX2);
190 SetY2(inter->oldY2);
191 }
192 } else if (inter && !inter->IsOpaque(parent) && (inter->newX1 != inter->newX2)) {
193 setNewValues();
194 }
195
196 parent.Modified();
197 parent.Interactive(); // delete interactive object
199
200 break;
201
202 case kButton1Locate:
203 // Sergey: code is never used, has to be removed in ROOT7
204 ExecuteEvent(kButton1Down, px, py);
205
206 while (true) {
207 px = py = 0;
208 event = parent.GetCanvasImp()->RequestLocator(px, py);
209
211
212 if (event != -1) { // button is released
213 ExecuteEvent(kButton1Up, px, py);
214 return;
215 }
216 }
217 }
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Return 1 if the point (x,y) is inside the polygon defined by
222/// the diamond 0 otherwise.
223
225{
226
227 Double_t xd[4], yd[4];
228
229 xd[0] = fX1;
230 yd[0] = (fY2 + fY1) / 2.;
231 xd[1] = (fX2 + fX1) / 2.;
232 yd[1] = fY1;
233 xd[2] = fX2;
234 yd[2] = yd[0];
235 xd[3] = xd[1];
236 yd[3] = fY2;
237
238 return (Int_t)TMath::IsInside(x, y, 4, xd, yd);
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Paint this diamond with its current attributes.
243
245{
246 if (!gPad) return;
247 Double_t x[7],y[7],depx,depy;
248 Double_t x1 = fX1;
249 Double_t y1 = fY1;
250 Double_t x2 = fX2;
251 Double_t y2 = fY2;
255 if (fBorderSize) {
256 Double_t wy = gPad->PixeltoY(0) - gPad->PixeltoY(fBorderSize);
257 Double_t wx = gPad->PixeltoX(fBorderSize) - gPad->PixeltoX(0);
258 // Draw the frame top right
259 if (y2-y1>x2-x1) {
260 depx = wx;
261 depy = 0;
262 }
263 else if (y2-y1<x2-x1) {
264 depx = 0;
265 depy = -wy;
266 }
267 else {
268 depx = wx;
269 depy = -wy;
270 }
271 x[0] = x[2] = (x1+x2)/2+depx;
272 x[1] = x2+depx;
273 x[3] = x1+depx;
274 y[0] = y2+depy;
275 y[2] = y1+depy;
276 y[1] = y[3] =(y1+y2)/2+depy;
277 x[4] = x[0]; y[4] = y[0];
280 TAttFill::Modify(); //Change fill area attributes only if necessary
281 gPad->PaintFillArea(4,x,y);
282 }
283 x[0] = x[2] = (x1+x2)/2;
284 x[1] = x2;
285 x[3] = x1;
286 y[0] = y2;
287 y[2] = y1;
288 y[1] = y[3] = (y1+y2)/2;
289 x[4] = x[0]; y[4] =y[0];
292 TAttLine::Modify(); //Change line attributes only if necessary
293 TAttFill::Modify(); //Change fill area attributes only if necessary
294 gPad->PaintFillArea(4,x,y);
295 gPad->PaintPolyLine(5,x,y);
296
297 // Paint list of primitives (test,etc)
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// Save primitive as a C++ statement(s) on output stream out.
303
304void TDiamond::SavePrimitive(std::ostream &out, Option_t *option)
305{
306 SavePrimitiveConstructor(out, Class(), "diamond", TString::Format("%g, %g, %g, %g", fX1, fY1, fX2, fY2));
307
308 SaveFillAttributes(out, "diamond", -1, -1);
309 SaveLineAttributes(out, "diamond", 1, 1, 1);
310 SaveTextAttributes(out, "diamond", 11, 0, 1, 62, 0.05);
311
312 SaveLines(out, "diamond", kTRUE);
313
314 SavePrimitiveDraw(out, "diamond", option);
315}
@ 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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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:304
void Paint(Option_t *option="") override
Paint this diamond with its current attributes.
Definition TDiamond.cxx:244
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:224
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:51
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