| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #ifdef HAVE_IOSTREAM |
|---|
| 10 | #include <iostream> |
|---|
| 11 | #else |
|---|
| 12 | #include <iostream.h> |
|---|
| 13 | #endif |
|---|
| 14 | #ifdef HAVE_FSTREAM |
|---|
| 15 | #include <fstream> |
|---|
| 16 | #else |
|---|
| 17 | #include <fstream.h> |
|---|
| 18 | #endif |
|---|
| 19 | #include <stdlib.h> |
|---|
| 20 | #include "general.h" |
|---|
| 21 | #include "fieldmeter.h" |
|---|
| 22 | #include "xosview.h" |
|---|
| 23 | |
|---|
| 24 | CVSID("$Id: fieldmeter.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $"); |
|---|
| 25 | CVSID_DOT_H(FIELDMETER_H_CVSID); |
|---|
| 26 | |
|---|
| 27 | FieldMeter::FieldMeter( XOSView *parent, int numfields, const char *title, |
|---|
| 28 | const char *legend, int docaptions, int dolegends, |
|---|
| 29 | int dousedlegends ) |
|---|
| 30 | : Meter(parent, title, legend, docaptions, dolegends, dousedlegends){ |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | numWarnings_ = printedZeroTotalMesg_ = 0; |
|---|
| 35 | print_ = PERCENT; |
|---|
| 36 | used_ = 0; |
|---|
| 37 | lastused_ = -1; |
|---|
| 38 | fields_ = NULL; |
|---|
| 39 | colors_ = NULL; |
|---|
| 40 | lastvals_ = NULL; |
|---|
| 41 | lastx_ = NULL; |
|---|
| 42 | setNumFields(numfields); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void |
|---|
| 46 | FieldMeter::disableMeter ( ) |
|---|
| 47 | { |
|---|
| 48 | setNumFields(1); |
|---|
| 49 | setfieldcolor (0, "gray"); |
|---|
| 50 | Meter::legend ("Disabled"); |
|---|
| 51 | |
|---|
| 52 | total_ = 1.0; |
|---|
| 53 | fields_[0] = 1.0; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | FieldMeter::~FieldMeter( void ){ |
|---|
| 58 | delete[] fields_; |
|---|
| 59 | delete[] colors_; |
|---|
| 60 | delete[] lastvals_; |
|---|
| 61 | delete[] lastx_; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void FieldMeter::checkResources( void ){ |
|---|
| 65 | Meter::checkResources(); |
|---|
| 66 | usedcolor_ = parent_->allocColor( parent_->getResource( "usedLabelColor") ); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | void FieldMeter::SetUsedFormat ( const char * const fmt ) { |
|---|
| 71 | |
|---|
| 72 | if (!strncasecmp (fmt, "percent", 8)) |
|---|
| 73 | print_ = PERCENT; |
|---|
| 74 | else if (!strncasecmp (fmt, "autoscale", 10)) |
|---|
| 75 | print_ = AUTOSCALE; |
|---|
| 76 | else if (!strncasecmp (fmt, "float", 6)) |
|---|
| 77 | print_ = FLOAT; |
|---|
| 78 | else |
|---|
| 79 | { |
|---|
| 80 | fprintf (stderr, "Error: could not parse format of '%s'\n", fmt); |
|---|
| 81 | fprintf (stderr, " I expected one of 'percent', 'bytes', or 'float'\n"); |
|---|
| 82 | fprintf (stderr, " (Case-insensitive)\n"); |
|---|
| 83 | exit(1); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void FieldMeter::setUsed (float val, float total) |
|---|
| 88 | { |
|---|
| 89 | if (print_ == FLOAT) |
|---|
| 90 | used_ = val; |
|---|
| 91 | else if (print_ == PERCENT) |
|---|
| 92 | { |
|---|
| 93 | if (total != 0.0) |
|---|
| 94 | used_ = val / total * 100.0; |
|---|
| 95 | else |
|---|
| 96 | { |
|---|
| 97 | if (!printedZeroTotalMesg_) { |
|---|
| 98 | printedZeroTotalMesg_ = 1; |
|---|
| 99 | fprintf(stderr, "Warning: %s meter had a zero total " |
|---|
| 100 | "field! Would have caused a div-by-zero " |
|---|
| 101 | "exception.\n", name()); |
|---|
| 102 | } |
|---|
| 103 | used_ = 0.0; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | else if (print_ == AUTOSCALE) |
|---|
| 107 | used_ = val; |
|---|
| 108 | else { |
|---|
| 109 | fprintf (stderr, "Error in %s: I can't handle a " |
|---|
| 110 | "UsedType enum value of %d!\n", name(), print_); |
|---|
| 111 | exit(1); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void FieldMeter::reset( void ){ |
|---|
| 116 | for ( int i = 0 ; i < numfields_ ; i++ ) |
|---|
| 117 | lastvals_[i] = lastx_[i] = -1; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void FieldMeter::setfieldcolor( int field, const char *color ){ |
|---|
| 121 | colors_[field] = parent_->allocColor( color ); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | void FieldMeter::setfieldcolor( int field, unsigned long color ) { |
|---|
| 125 | colors_[field] = color; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void FieldMeter::draw( void ){ |
|---|
| 129 | |
|---|
| 130 | parent_->setForeground( parent_->foreground() ); |
|---|
| 131 | parent_->drawRectangle( x_ - 1, y_ - 1, width_ + 2, height_ + 2 ); |
|---|
| 132 | if ( dolegends_ ){ |
|---|
| 133 | parent_->setForeground( textcolor_ ); |
|---|
| 134 | |
|---|
| 135 | int offset; |
|---|
| 136 | if ( dousedlegends_ ) |
|---|
| 137 | offset = parent_->textWidth( "XXXXXXXXX" ); |
|---|
| 138 | else |
|---|
| 139 | offset = parent_->textWidth( "XXXXX" ); |
|---|
| 140 | |
|---|
| 141 | parent_->drawString( x_ - offset + 1, y_ + height_, title_ ); |
|---|
| 142 | if(docaptions_) |
|---|
| 143 | drawlegend(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | drawfields( 1 ); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void FieldMeter::drawlegend( void ){ |
|---|
| 150 | char *tmp1, *tmp2, buff[100]; |
|---|
| 151 | int n, x = x_; |
|---|
| 152 | |
|---|
| 153 | tmp1 = tmp2 = legend_; |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | for ( int i = 0 ; i < numfields_ ; i++ ){ |
|---|
| 168 | n = 0; |
|---|
| 169 | while ( (*tmp2 != '/') && (*tmp2 != '\0') ){ |
|---|
| 170 | tmp2++; |
|---|
| 171 | n++; |
|---|
| 172 | } |
|---|
| 173 | tmp2++; |
|---|
| 174 | strncpy( buff, tmp1, n ); |
|---|
| 175 | buff[n] = '\0'; |
|---|
| 176 | parent_->setStippleN(i%4); |
|---|
| 177 | parent_->setForeground( colors_[i] ); |
|---|
| 178 | parent_->drawString( x, y_ - 5, buff ); |
|---|
| 179 | x += parent_->textWidth( buff, n ); |
|---|
| 180 | parent_->setForeground( parent_->foreground() ); |
|---|
| 181 | if ( i != numfields_ - 1 ) |
|---|
| 182 | parent_->drawString( x, y_ - 5, "/" ); |
|---|
| 183 | x += parent_->textWidth( "/", 1 ); |
|---|
| 184 | tmp1 = tmp2; |
|---|
| 185 | } |
|---|
| 186 | parent_->setStippleN(0); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | void FieldMeter::drawused( int manditory ){ |
|---|
| 190 | if ( !manditory ) |
|---|
| 191 | if ( (lastused_ == used_) ) |
|---|
| 192 | return; |
|---|
| 193 | |
|---|
| 194 | parent_->setStippleN(0); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | parent_->setStippleN(0); |
|---|
| 202 | static int labfield = parent_->textWidth( "XXXXXX" ); |
|---|
| 203 | int xlu,ylu,xro,yro,xoffset; |
|---|
| 204 | char buf[10]; |
|---|
| 205 | char agpbuf[10]; |
|---|
| 206 | int f=2; |
|---|
| 207 | xoffset = width_ -5; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | if (print_ == PERCENT){ |
|---|
| 211 | snprintf( buf, 10, "%d%%", (int)used_ ); |
|---|
| 212 | } |
|---|
| 213 | else if (print_ == AUTOSCALE){ |
|---|
| 214 | char scale; |
|---|
| 215 | float scaled_used; |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | if (used_ >= 999.5*1000*1000*1000*1000*1000*1000) |
|---|
| 226 | {scale='E'; scaled_used = used_/1024/1024/1024/1024/1024/1024;} |
|---|
| 227 | else if (used_ >= 999.5*1000*1000*1000*1000) |
|---|
| 228 | {scale='P'; scaled_used = used_/1024/1024/1024/1024/1024;} |
|---|
| 229 | else if (used_ >= 999.5*1000*1000*1000) |
|---|
| 230 | {scale='T'; scaled_used = used_/1024/1024/1024/1024;} |
|---|
| 231 | else if (used_ >= 999.5*1000*1000) |
|---|
| 232 | {scale='G'; scaled_used = used_/1024/1024/1024;} |
|---|
| 233 | else if (used_ >= 999.5*1000) |
|---|
| 234 | {scale='M'; scaled_used = used_/1024/1024;} |
|---|
| 235 | else if (used_ >= 999.5) |
|---|
| 236 | {scale='K'; scaled_used = used_/1024;} |
|---|
| 237 | else {scale=' '; scaled_used = used_;} |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | if (scaled_used < 0) |
|---|
| 245 | snprintf (buf, 10, "-"); |
|---|
| 246 | else if (scaled_used == 0.0) |
|---|
| 247 | snprintf (buf, 10, "0"); |
|---|
| 248 | else if (scaled_used < 9.95) |
|---|
| 249 | |
|---|
| 250 | snprintf (buf, 10, "%.1f%c", scaled_used, scale); |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | else |
|---|
| 255 | snprintf (buf, 10, "%.0f%c", scaled_used, scale); |
|---|
| 256 | } |
|---|
| 257 | else { |
|---|
| 258 | snprintf( buf, 10, "%.1f", used_ ); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | xlu= x_ + xoffset - labfield; |
|---|
| 265 | ylu= y_ -5 - parent_->textHeight() ; |
|---|
| 266 | xro= labfield; |
|---|
| 267 | yro= parent_->textHeight(); |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | parent_->clear( xlu, ylu, xro, yro); |
|---|
| 276 | parent_->setForeground( 0); |
|---|
| 277 | parent_->drawFilledRectangle(xlu, ylu, xro, yro); |
|---|
| 278 | parent_->setForeground(0xaaaaaa); |
|---|
| 279 | parent_->drawString( xlu +16, y_ -7, buf ); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | lastused_ = used_; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | void FieldMeter::drawfields( int manditory ){ |
|---|
| 286 | int twidth, x = x_; |
|---|
| 287 | |
|---|
| 288 | if ( total_ == 0 ) |
|---|
| 289 | return; |
|---|
| 290 | |
|---|
| 291 | for ( int i = 0 ; i < numfields_ ; i++ ){ |
|---|
| 292 | |
|---|
| 293 | if (fields_[i] < 0.0) { |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | numWarnings_ ++; |
|---|
| 297 | if (numWarnings_ < 5) |
|---|
| 298 | fprintf(stderr, "Warning: meter %s had a negative " |
|---|
| 299 | "value of %f for field %d\n", name(), fields_[i], i); |
|---|
| 300 | if (numWarnings_ == 5) |
|---|
| 301 | fprintf(stderr, "Future warnings from the %s meter " |
|---|
| 302 | "will not be displayed.\n", name()); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | twidth = (int) ((width_ * (float) fields_[i]) / total_); |
|---|
| 306 | |
|---|
| 307 | if ( (i == numfields_ - 1) && ((x + twidth) != (x_ + width_)) ) |
|---|
| 308 | twidth = width_ + x_ - x; |
|---|
| 309 | |
|---|
| 310 | if ( manditory || (twidth != lastvals_[i]) || (x != lastx_[i]) ){ |
|---|
| 311 | parent_->setForeground( colors_[i] ); |
|---|
| 312 | parent_->setStippleN(i%4); |
|---|
| 313 | parent_->drawFilledRectangle( x, y_, twidth, height_ ); |
|---|
| 314 | parent_->setStippleN(0); |
|---|
| 315 | lastvals_[i] = twidth; |
|---|
| 316 | lastx_[i] = x; |
|---|
| 317 | |
|---|
| 318 | if ( dousedlegends_ ) |
|---|
| 319 | drawused( manditory ); |
|---|
| 320 | } |
|---|
| 321 | x += twidth; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void FieldMeter::checkevent( void ){ |
|---|
| 328 | drawfields(); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | void FieldMeter::setNumFields(int n){ |
|---|
| 332 | numfields_ = n; |
|---|
| 333 | delete[] fields_; |
|---|
| 334 | delete[] colors_; |
|---|
| 335 | delete[] lastvals_; |
|---|
| 336 | delete[] lastx_; |
|---|
| 337 | fields_ = new float[numfields_]; |
|---|
| 338 | colors_ = new unsigned long[numfields_]; |
|---|
| 339 | lastvals_ = new int[numfields_]; |
|---|
| 340 | lastx_ = new int[numfields_]; |
|---|
| 341 | |
|---|
| 342 | total_ = 0; |
|---|
| 343 | for ( int i = 0 ; i < numfields_ ; i++ ){ |
|---|
| 344 | fields_[i] = 0.0; |
|---|
| 345 | lastvals_[i] = lastx_[i] = 0; |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | } |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | bool FieldMeter::checkX(int x, int width) const { |
|---|
| 352 | if ((x < x_) || (x + width < x_) |
|---|
| 353 | || (x > x_ + width_) || (x + width > x_ + width_)){ |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | return false; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | return true; |
|---|
| 367 | } |
|---|