forked from hills/xosview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meter.cc
113 lines (98 loc) · 2.61 KB
/
meter.cc
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
//
// Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
//
// This file may be distributed under terms of the GPL
//
#include "meter.h"
#include <string.h>
Meter::Meter( XOSView *parent, const char *title, const char *legend,
int docaptions, int dolegends, int dousedlegends ) {
title_ = legend_ = NULL;
Meter::title( title );
Meter::legend( legend );
parent_ = parent;
docaptions_ = docaptions;
dolegends_ = dolegends;
dousedlegends_ = dousedlegends;
priority_ = 1;
counter_ = 0;
resize( parent->xoff(), parent->newypos(), parent->width() - 10, 10 );
}
Meter::~Meter( void ){
if ( title_ )
delete[] title_;
if ( legend_ )
delete[] legend_;
}
void Meter::checkResources( void ){
textcolor_ = parent_->allocColor( parent_->getResource( "meterLabelColor") );
}
void Meter::title( const char *title ){
if ( title_ )
delete[] title_;
int len = strlen(title);
title_ = new char[len + 1];
memcpy( title_, title, len + 1 );
}
void Meter::legend( const char *legend ){
if ( legend_ )
delete[] legend_;
int len = strlen(legend);
legend_ = new char[len + 1];
memcpy( legend_, legend, len + 1 );
}
void Meter::resize( int x, int y, int width, int height ){
x_ = x;
y_ = y;
width_ = (width>=0) ? width : 0; // fix for cosmetical bug:
height_ = (height>=0) ? height : 0; // beware of values < 0 !
width_ &= ~1; // only allow even width_ values
}
double Meter::scaleValue( double value, char *scale, bool metric ){
double scaled = ( value < 0 ? -value : value );
if (scaled >= 999.5*1e15){
scale[0] = 'E';
scaled = value / ( metric ? 1e18 : 1ULL<<60 );
}
else if (scaled >= 999.5*1e12){
scale[0] = 'P';
scaled = value / ( metric ? 1e15 : 1ULL<<50 );
}
else if (scaled >= 999.5*1e9){
scale[0] = 'T';
scaled = value / ( metric ? 1e12 : 1ULL<<40 );
}
else if (scaled >= 999.5*1e6){
scale[0] = 'G';
scaled = value / ( metric ? 1e9 : 1UL<<30 );
}
else if (scaled >= 999.5*1e3){
scale[0] = 'M';
scaled = value / ( metric ? 1e6 : 1UL<<20 );
}
else if (scaled >= 999.5){
scale[0] = ( metric ? 'k' : 'K' );
scaled = value / ( metric ? 1e3 : 1UL<<10 );
}
else if (scaled < 0.9995 && metric){
if (scaled >= 0.9995/1e3){
scale[0] = 'm';
scaled = value * 1e3;
}
else if (scaled >= 0.9995/1e6){
scale[0] = '\265';
scaled = value * 1e6;
}
else {
scale[0] = 'n';
scaled = value * 1e9;
}
// add more if needed
}
else {
scale[0] = '\0';
scaled = value;
}
scale[1] = '\0';
return scaled;
}