root/foundation-apps/grosview-maxx/fieldmetergraph.cc

Revision 2, 5.5 KB (checked in by emasson, 3 years ago)

initial import for the community edition

Line 
1//
2//  The original FieldMeter class is Copyright (c) 1994, 2006 by Mike Romberg
3//    ( mike.romberg@noaa.gov )
4//
5//  Modifications from FieldMeter class done in Oct. 1995
6//    by Brian Grayson ( bgrayson@netbsd.org )
7//
8//  Modifications from FieldMeterDecay class done in Oct. 1998
9//    by Scott McNab ( jedi@tartarus.uwa.edu.au )
10//
11// $Id: fieldmetergraph.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $
12//
13
14// In order to use the FieldMeterGraph class in place of a FieldMeter class in
15// a meter file (say, cpumeter.cc), make the following changes:
16//   1.  Change cpumeter.h to include fieldmetergraph.h instead of
17//       fieldmeter.h
18//   2.  Change CPUMeter to inherit from FieldMeterGraph, rather than
19//       FieldMeter.
20//   3.  Change the constructor call to use FieldMeterGraph(), rather than
21//       FieldMeter().
22//   4.  Make the meter call FieldMeterGraph::checkResources(),
23//       to pick up graphNumCols resource.
24//   5.  Make the checkResources () function in the meter set the
25//       useGraph_ variable according to the, e.g., xosview*cpuGraph resource.
26
27#ifdef HAVE_FSTREAM
28#include <fstream>
29#else
30#include <fstream.h>
31#endif
32#include <math.h>               //  For fabs()
33#include "general.h"
34#include "fieldmeter.h"
35#include "fieldmetergraph.h"
36#include "xosview.h"
37
38CVSID("$Id: fieldmetergraph.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $");
39CVSID_DOT_H(FIELDMETERGRAPH_H_CVSID);
40
41FieldMeterGraph::FieldMeterGraph( XOSView *parent,
42                int numfields, const char *title,
43                const char *legend, int docaptions, int dolegends,
44  int dousedlegends )
45: FieldMeterDecay (parent, numfields, title, legend, docaptions,
46  dolegends, dousedlegends)
47{
48
49        useGraph_ = 0;
50        heightfield_ = NULL;
51        firstTimeDrawn_ = 1;
52
53        // set number of columns to a reasonable default in case we can't
54        // find the resource
55        setNumCols( 100 );
56
57}
58
59FieldMeterGraph::~FieldMeterGraph( void )
60{
61        delete [] heightfield_;
62}
63
64void FieldMeterGraph::drawfields( int manditory )
65{
66        int i,j;
67
68        if( !useGraph_ )
69        {
70                // Call FieldMeterDecay code if this meter should not be
71                // drawn as a graph
72                FieldMeterDecay::drawfields( manditory );
73                return;
74        }
75
76        if( total_ <= 0.0 )
77                return;
78
79        // allocate memory for height field graph storage
80        // note: this is done here as it is not certain that both
81        // numfields_ and graphNumCols_ are defined in the constructor
82        if( heightfield_ == NULL )
83        {
84                if( numfields_ > 0 && graphNumCols_ > 0 )
85                {
86                        heightfield_ = new float [numfields_*graphNumCols_];
87
88                        for( i = 0; i < graphNumCols_; i++ )
89                        {
90                                for( j = 0; j < numfields_; j++ )
91                                {
92                                        if( j < numfields_-1 )
93                                                heightfield_[i*numfields_+j] = 0.0;
94                                        else
95                                                heightfield_[i*numfields_+j] = 1.0;
96                                }
97                        }
98                }
99        }
100
101        // check current position here and slide graph if necessary
102        if( graphpos_ >= graphNumCols_ )
103        {
104                for( i = 0; i < graphNumCols_-1; i++ )
105                {
106                        for( j = 0; j < numfields_; j++ )
107                        {
108                                heightfield_[i*numfields_+j] = heightfield_[(i+1)*numfields_+j];
109                        }
110                }
111                graphpos_ = graphNumCols_ - 1;
112        }
113
114        // get current values to be plotted
115        for( i = 0; i < numfields_; i++ )
116        {
117                float a = fields_[i] / total_;
118                if( a <= 0.0 )
119                        a = 0.0;
120                if( a >= 1.0 )
121                        a = 1.0;
122                heightfield_[graphpos_*numfields_+i] = a;
123        }
124
125        /*  For the first time, we need to draw everything, so
126         *  skip the optimized copyArea case.  Also, if we are
127         *  not fully visible, then the copy-area won't work
128         *  properly.  */
129        if( !firstTimeDrawn_ && parent_->hasBeenExposedAtLeastOnce() && !parent_->isExposed() && parent_->isFullyVisible() )
130        {
131                // scroll area
132                int col_width = width_/graphNumCols_;
133                if( col_width < 1 )
134                {
135                        col_width = 1;
136                }
137
138                int sx = x_ + col_width;
139                int swidth = width_ - col_width;
140                int sheight = height_ + 1;
141                if( sx > x_ && swidth > 0 && sheight > 0 )
142                        parent_->copyArea( sx, y_, swidth, sheight, x_, y_ );
143                drawBar( graphNumCols_ - 1 );
144        } else {
145                if (firstTimeDrawn_ &&
146                    parent_->isAtLeastPartiallyVisible() &&
147                    parent_->hasBeenExposedAtLeastOnce()) {
148                        XOSDEBUG("True exposure! %d\n", firstTimeDrawn_);
149                        firstTimeDrawn_ = 0;
150                }
151                else XOSDEBUG("Full draw:  isAtLeastPart %d, hasBeenExposed %d\n",
152                        parent_->isAtLeastPartiallyVisible(),
153                        parent_->hasBeenExposedAtLeastOnce());
154                // need to draw entire graph on expose event
155                for( i = 0; i < graphNumCols_; i++ ) {
156                        drawBar( i );
157                }
158        }
159
160        graphpos_++;
161    parent_->setStippleN(0);    //  Restore all-bits stipple.
162    if ( dousedlegends_ )
163    {
164        drawused( manditory );
165    }
166}
167void FieldMeterGraph::drawBar( int i )
168{
169        int j;
170        int y = y_ + height_;
171        int x = x_ + i*width_/graphNumCols_;
172        int barwidth = (x_ + (i+1)*width_/graphNumCols_)-x;
173
174        if( barwidth>0 )
175        {
176                int barheight;
177                for( j = 0 ; j < numfields_; j++ )
178                {
179                        /*  Round up, by adding 0.5 before
180                        *  converting to an int.  */
181                        barheight = (int)((heightfield_[i*numfields_+j]*height_)+0.5);
182
183                        parent_->setForeground( colors_[j] );
184                        parent_->setStippleN(j%4);
185
186                        if( barheight > (y-y_) )
187                                barheight = (y-y_);
188
189                        // hack to ensure last field always reaches top of graph area
190                        if( j == numfields_-1 )
191                                barheight = (y-y_);
192
193                        y -= barheight;
194                        if( barheight>0 )
195                                parent_->drawFilledRectangle( x, y, barwidth, barheight );
196                }
197        }
198}
199void FieldMeterGraph::checkResources( void )
200{
201  FieldMeterDecay::checkResources();
202
203  const char *ptr = parent_->getResource( "graphNumCols" );
204  if( ptr )
205  {
206    int i;
207        if( sscanf( ptr, "%d", &i ) == 1 )
208        {
209                if( i>0 )
210                {
211                        setNumCols( i );
212                }
213        }
214  }
215}
216void FieldMeterGraph::setNumCols( int n )
217{
218        graphNumCols_ = n;
219        graphpos_ = graphNumCols_-1;
220
221        if( heightfield_ )
222                delete [] heightfield_;
223        heightfield_ = NULL;
224
225}
Note: See TracBrowser for help on using the browser.