-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngularMeter.cpp
327 lines (229 loc) · 7.91 KB
/
AngularMeter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/////////////////////////////////////////////////////////////////////////////
// Name: AngularMeter.cpp
// Purpose: wxIndustrialControls Library
// Author: Marco Cavallini <m.cavallini AT koansoftware.com>
// Modified by:
// Copyright: (C)2004-2006 Copyright by Koan s.a.s. - www.koansoftware.com
// Licence: KWIC License http://www.koansoftware.com/kwic/kwic-license.htm
/////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
//#include "kprec.h" //#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <wx/image.h>
#include <math.h>
#include <wx/log.h>
#include "AngularMeter.h"
#include <wx/event.h>
//IMPLEMENT_DYNAMIC_CLASS(kwxAngularMeter, wxWindow)
BEGIN_EVENT_TABLE(kwxAngularMeter,wxWindow)
EVT_PAINT(kwxAngularMeter::OnPaint)
END_EVENT_TABLE()
kwxAngularMeter::kwxAngularMeter(wxWindow* parent,
const wxWindowID id,
const wxPoint& pos,
const wxSize& size)
: wxWindow(parent, id, pos, size, 0)
{
if (parent)
SetBackgroundColour(parent->GetBackgroundColour());
else
SetBackgroundColour(*wxLIGHT_GREY);
//SetSize(size);
SetAutoLayout(TRUE);
Refresh();
m_id = id;
//valori di default
m_nScaledVal = 0; //gradi
m_nRealVal = 0;
m_nTick = 0; //numero tacche
m_nSec = 1; //default numero settori
m_nRangeStart = 0;
m_nRangeEnd = 220;
m_nAngleStart = -20;
m_nAngleEnd = 200;
m_aSectorColor[0] = *wxWHITE;
// m_cBackColour = *wxLIGHT_GREY;
m_cBackColour = GetBackgroundColour() ; //default sfondo applicazione
m_cNeedleColour = *wxRED; //indicatore
m_cBorderColour = GetBackgroundColour() ;
m_dPI = 4.0 * atan(1.0);
m_Font = *wxSWISS_FONT; //font
m_bDrawCurrent = true ;
membitmap = new wxBitmap(size.GetWidth(), size.GetHeight()) ;
/////////////// TODO : Test for BMP image loading /////////////////
/*
m_pPreviewBmp = NULL ;
wxBitmap bitmap( 300, 300 );
wxImage image = bitmap.ConvertToImage();
image.Destroy();
if ( !image.LoadFile( "thumbnail.bmp", wxBITMAP_TYPE_BMP ) )
wxLogError(wxT("Can't load BMP image"));
else
m_pPreviewBmp = new wxBitmap( image );
*/
}
kwxAngularMeter::~kwxAngularMeter()
{
delete membitmap;
}
void kwxAngularMeter::SetValue(int val)
{
int deltarange = m_nRangeEnd - m_nRangeStart;
// int rangezero = deltarange - m_nRangeStart;
int deltaangle = m_nAngleEnd - m_nAngleStart;
double coeff = (double)deltaangle / (double)deltarange;
m_nScaledVal = (int)((double)(val - m_nRangeStart) * coeff);
m_nRealVal = val;
Refresh();
}
void kwxAngularMeter::OnPaint(wxPaintEvent &WXUNUSED(event))
{
wxPaintDC old_dc(this);
int w,h ;
GetClientSize(&w,&h);
/////////////////
// Create a memory DC
wxMemoryDC dc;
dc.SelectObject(*membitmap);
dc.SetBackground(*wxTheBrushList->FindOrCreateBrush(m_cBackColour,wxSOLID));
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cBackColour,wxSOLID));
dc.Clear();
/*
if (m_pPreviewBmp && m_pPreviewBmp->Ok())
dc.DrawBitmap( *m_pPreviewBmp, 1, 1 );
*/
///////////////////
//Rettangolo
dc.SetPen(*wxThePenList->FindOrCreatePen(m_cBorderColour, 1, wxSOLID));
dc.DrawRectangle(0,0,w,h);
//settori
DrawSectors(dc) ;
//tacche
if (m_nTick > 0)
DrawTicks(dc);
//indicatore lancetta
DrawNeedle(dc);
//testo valore
if (m_bDrawCurrent)
{
wxString valuetext;
valuetext.Printf(wxT("%d"),m_nRealVal);
dc.SetFont(m_Font);
dc.DrawText(valuetext, (w / 2) - 10, (h / 2) + 10);
}
// We can now draw into the memory DC...
// Copy from this DC to another DC.
old_dc.Blit(0, 0, w, h, &dc, 0, 0);
}
void kwxAngularMeter::DrawNeedle(wxDC &dc)
{
//indicatore triangolare
double dxi,dyi, val;
wxPoint ppoint[6];
int w, h ;
GetClientSize(&w,&h);
dc.SetPen(*wxThePenList->FindOrCreatePen(m_cNeedleColour, 1,wxSOLID));
val = (m_nScaledVal + m_nAngleStart) * m_dPI / 180; //radianti parametro angolo
dyi = sin(val - 90) * 2; //coordinate base sinistra
dxi = cos(val - 90) * 2;
ppoint[0].x = (w / 2) - dxi; //base sinistra
ppoint[0].y = (h / 2) - dyi;
dxi = cos(val) * ((h / 2) - 4); //coordinate punta indicatore
dyi = sin(val) * ((h / 2) - 4);
ppoint[2].x = (w / 2) - dxi; //punta
ppoint[2].y = (h / 2) - dyi;
dxi = cos(val + 90) * 2; //coordinate base destra
dyi = sin(val + 90) * 2;
ppoint[4].x = (w / 2) - dxi; //base destra
ppoint[4].y = (h / 2) - dyi;
ppoint[5].x = ppoint[0].x; //ritorno base sinistra
ppoint[5].y = ppoint[0].y;
//////////////////////////
val = (m_nScaledVal + m_nAngleStart + 1) * m_dPI / 180;
dxi = cos(val) * ((h / 2) - 10); //coordinate medio destra
dyi = sin(val) * ((h / 2) - 10);
ppoint[3].x = (w / 2) - dxi; //base media destra
ppoint[3].y = (h / 2) - dyi;
val = (m_nScaledVal + m_nAngleStart - 1) * m_dPI / 180;
dxi = cos(val) * ((h / 2) - 10); //coordinate medio sinistra
dyi = sin(val) * ((h / 2) - 10);
ppoint[1].x = (w / 2) - dxi; //base media sinistra
ppoint[1].y = (h / 2) - dyi;
/////////////////////////
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cNeedleColour,wxSOLID));
dc.DrawPolygon(6, ppoint, 0, 0, wxODDEVEN_RULE);
//cerchio indicatore
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(*wxWHITE,wxSOLID));
dc.DrawCircle(w / 2, h / 2, 4);
}
void kwxAngularMeter::DrawSectors(wxDC &dc)
{
double starc,endarc;
int secount,dx,dy;
int w,h ;
double val;
GetClientSize(&w,&h);
//arco -> settori
dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 1, wxSOLID));
starc = m_nAngleStart;
endarc = starc + ((m_nAngleEnd - m_nAngleStart) / (double)m_nSec);
//dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(*wxRED,wxSOLID));
for(secount=0;secount<m_nSec;secount++)
{
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_aSectorColor[secount],wxSOLID));
dc.DrawEllipticArc(0,0,w,h,180 - endarc,180 - starc);
//dc.DrawEllipticArc(0,0,w,h,0,180);
starc = endarc;
endarc += ((m_nAngleEnd - m_nAngleStart) / (double)m_nSec);
}
val = (m_nAngleStart * m_dPI) / 180.0;
dx = cos(val) * h / 2.0;
dy = sin(val) * h / 2.0;
dc.DrawLine(w / 2, h / 2, (w / 2) - dx, (h / 2) - dy); //linea sinistra
val = (m_nAngleEnd * m_dPI) / 180.0;
dx = cos(val) * h / 2.0;
dy = sin(val) * h / 2.0;
dc.DrawLine(w / 2, h / 2, (w / 2) - dx, (h / 2) - dy); //linea destra
}
void kwxAngularMeter::DrawTicks(wxDC &dc)
{
double intervallo = (m_nAngleEnd - m_nAngleStart) / (m_nTick + 1.0);
double valint = intervallo + m_nAngleStart;
double tx, ty;
double val;
double dx, dy;
int n, w, h;
int tw, th;
wxString s;
GetClientSize(&w, &h);
for(n = 0;n < m_nTick;n++)
{
val=(valint * m_dPI) / 180;
//wxLogTrace("v: %f",valint);
dx = cos(val) * (h/2); //punto sul cerchio
dy = sin(val) * (h/2);
tx = cos(val) * ((h / 2) - 10); //punto nel cerchio
ty = sin(val) * ((h / 2) - 10);
dc.DrawLine((w / 2) - tx, (h / 2) - ty, (w / 2) - dx, (h / 2) - dy);
int deltarange = m_nRangeEnd - m_nRangeStart;
int deltaangle = m_nAngleEnd - m_nAngleStart;
double coeff = (double)deltaangle / (double)deltarange;
int rightval = (int)(((valint - (double)m_nAngleStart)) / coeff) + m_nRangeStart;
s.Printf(wxT("%d"), rightval);
dc.GetTextExtent(s, &tw, &th);
val = ((valint - 4) * m_dPI) / 180; //angolo spostato
tx = cos(val) * ((h / 2) - 12); //punto testo
ty = sin(val) * ((h / 2) - 12);
dc.SetFont(m_Font);
dc.DrawRotatedText(s,(w / 2) - tx, (h / 2) - ty, 90 - valint);
valint = valint + intervallo;
}
}
void kwxAngularMeter::SetSectorColor(int nSector, wxColour colour)
{
m_aSectorColor[nSector] = colour;
}