"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/fieldmetergraph.cc" (11 Jul 2020, 4911 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fieldmetergraph.cc" see the Fossies "Dox" file reference documentation.

    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 
   12 // In order to use the FieldMeterGraph class in place of a FieldMeter class in
   13 // a meter file (say, cpumeter.cc), make the following changes:
   14 //   1.  Change cpumeter.h to include fieldmetergraph.h instead of
   15 //       fieldmeter.h
   16 //   2.  Change CPUMeter to inherit from FieldMeterGraph, rather than
   17 //       FieldMeter.
   18 //   3.  Change the constructor call to use FieldMeterGraph(), rather than
   19 //       FieldMeter().
   20 //   4.  Make the meter call FieldMeterGraph::checkResources(),
   21 //       to pick up graphNumCols resource.
   22 //   5.  Make the checkResources () function in the meter set the
   23 //       useGraph_ variable according to the, e.g., xosview*cpuGraph resource.
   24 
   25 #include "fieldmetergraph.h"
   26 #include <stdio.h>
   27 
   28 
   29 FieldMeterGraph::FieldMeterGraph( XOSView *parent,
   30                 int numfields, const char *title,
   31                 const char *legend, int docaptions, int dolegends,
   32   int dousedlegends )
   33 : FieldMeterDecay (parent, numfields, title, legend, docaptions,
   34   dolegends, dousedlegends)
   35 {
   36 
   37     useGraph_ = 0;
   38     heightfield_ = NULL;
   39     lastWinState = XOSView::OBSCURED;
   40 
   41     // set number of columns to a reasonable default in case we can't
   42     // find the resource
   43     setNumCols( 100 );
   44 
   45 }
   46 
   47 FieldMeterGraph::~FieldMeterGraph( void )
   48 {
   49     delete [] heightfield_;
   50 }
   51 
   52 void FieldMeterGraph::drawfields( int mandatory )
   53 {
   54     int i,j;
   55     enum XOSView::windowVisibilityState currWinState;
   56 
   57     if( !useGraph_ )
   58     {
   59         // Call FieldMeterDecay code if this meter should not be
   60         // drawn as a graph
   61         FieldMeterDecay::drawfields( mandatory );
   62         return;
   63     }
   64 
   65     if( total_ <= 0.0 )
   66         return;
   67 
   68     // allocate memory for height field graph storage
   69     // note: this is done here as it is not certain that both
   70     // numfields_ and graphNumCols_ are defined in the constructor
   71     if( heightfield_ == NULL )
   72     {
   73         if( numfields_ > 0 && graphNumCols_ > 0 )
   74         {
   75             heightfield_ = new double [numfields_*graphNumCols_];
   76 
   77             for( i = 0; i < graphNumCols_; i++ )
   78             {
   79                 for( j = 0; j < numfields_; j++ )
   80                 {
   81                     if( j < numfields_-1 )
   82                         heightfield_[i*numfields_+j] = 0.0;
   83                     else
   84                         heightfield_[i*numfields_+j] = 1.0;
   85                 }
   86             }
   87         }
   88     }
   89 
   90     // check current position here and slide graph if necessary
   91     if( graphpos_ >= graphNumCols_ )
   92     {
   93         for( i = 0; i < graphNumCols_-1; i++ )
   94         {
   95             for( j = 0; j < numfields_; j++ )
   96             {
   97                 heightfield_[i*numfields_+j] = heightfield_[(i+1)*numfields_+j];
   98             }
   99         }
  100         graphpos_ = graphNumCols_ - 1;
  101     }
  102 
  103     // get current values to be plotted
  104     for( i = 0; i < numfields_; i++ )
  105     {
  106         double a = fields_[i] / total_;
  107         if( a <= 0.0 )
  108             a = 0.0;
  109         if( a >= 1.0 )
  110             a = 1.0;
  111         heightfield_[graphpos_*numfields_+i] = a;
  112     }
  113 
  114     currWinState = parent_->getWindowVisibilityState();
  115 
  116     // Try to avoid having to redraw everything.
  117     if (!mandatory && currWinState == XOSView::FULLY_VISIBLE && currWinState == lastWinState)
  118     {
  119         // scroll area
  120         int col_width = width_/graphNumCols_;
  121         if( col_width < 1 )
  122         {
  123             col_width = 1;
  124         }
  125 
  126         int sx = x_ + col_width;
  127         int swidth = width_ - col_width;
  128         int sheight = height_ + 1;
  129         if( sx > x_ && swidth > 0 && sheight > 0 )
  130             parent_->copyArea( sx, y_, swidth, sheight, x_, y_ );
  131         drawBar( graphNumCols_ - 1 );
  132     } else {
  133         // need to draw entire graph for some reason
  134         for( i = 0; i < graphNumCols_; i++ ) {
  135             drawBar( i );
  136         }
  137     }
  138 
  139     lastWinState = currWinState;
  140     graphpos_++;
  141     parent_->setStippleN(0);    //  Restore all-bits stipple.
  142     if ( dousedlegends_ )
  143     {
  144         drawused( mandatory );
  145     }
  146 }
  147 
  148 
  149 void FieldMeterGraph::drawBar( int i )
  150 {
  151     int j;
  152     int y = y_ + height_;
  153     int x = x_ + i*width_/graphNumCols_;
  154     int barwidth = (x_ + (i+1)*width_/graphNumCols_)-x;
  155 
  156     if( barwidth>0 )
  157     {
  158         int barheight;
  159         for( j = 0 ; j < numfields_; j++ )
  160         {
  161             /*  Round up, by adding 0.5 before
  162             *  converting to an int.  */
  163             barheight = (int)((heightfield_[i*numfields_+j]*height_)+0.5);
  164 
  165             parent_->setForeground( colors_[j] );
  166             parent_->setStippleN(j%4);
  167 
  168             if( barheight > (y-y_) )
  169                 barheight = (y-y_);
  170 
  171             // hack to ensure last field always reaches top of graph area
  172             if( j == numfields_-1 )
  173                 barheight = (y-y_);
  174 
  175             y -= barheight;
  176             if( barheight>0 )
  177                 parent_->drawFilledRectangle( x, y, barwidth, barheight );
  178         }
  179     }
  180 }
  181 void FieldMeterGraph::checkResources( void )
  182 {
  183   FieldMeterDecay::checkResources();
  184 
  185   const char *ptr = parent_->getResource( "graphNumCols" );
  186   if( ptr )
  187   {
  188     int i;
  189     if( sscanf( ptr, "%d", &i ) == 1 )
  190     {
  191         if( i>0 )
  192         {
  193             setNumCols( i );
  194         }
  195     }
  196   }
  197 }
  198 void FieldMeterGraph::setNumCols( int n )
  199 {
  200     graphNumCols_ = n;
  201     graphpos_ = graphNumCols_-1;
  202 
  203     if( heightfield_ )
  204         delete [] heightfield_;
  205     heightfield_ = NULL;
  206 
  207 }