xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fieldmetergraph.cc
Go to the documentation of this file.
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 
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;
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 
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  {
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;
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 
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 
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 }
182 {
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 }
199 {
200  graphNumCols_ = n;
202 
203  if( heightfield_ )
204  delete [] heightfield_;
205  heightfield_ = NULL;
206 
207 }
virtual void drawfields(int mandatory=0)
void drawBar(int i)
void setNumCols(int n)
virtual void checkResources(void)
virtual void drawfields(int mandatory=0)
enum XOSView::windowVisibilityState lastWinState
double * heightfield_
FieldMeterGraph(XOSView *parent, int numfields, const char *title="", const char *legend="", int docaptions=0, int dolegends=0, int dousedlegends=0)
virtual ~FieldMeterGraph(void)
double total_
Definition: fieldmeter.h:41
virtual void checkResources(void)
Definition: fieldmeter.cc:54
void drawused(int mandatory)
Definition: fieldmeter.cc:171
int numfields_
Definition: fieldmeter.h:39
double * fields_
Definition: fieldmeter.h:40
unsigned long * colors_
Definition: fieldmeter.h:43
int x_
Definition: meter.h:53
int y_
Definition: meter.h:53
int dousedlegends_
Definition: meter.h:53
int width_
Definition: meter.h:53
XOSView * parent_
Definition: meter.h:52
int height_
Definition: meter.h:53
enum windowVisibilityState getWindowVisibilityState(void)
Definition: xosview.h:35
windowVisibilityState
Definition: xosview.h:34
@ FULLY_VISIBLE
Definition: xosview.h:34
@ OBSCURED
Definition: xosview.h:34
void copyArea(int src_x, int src_y, int width, int height, int dest_x, int dest_y)
Definition: xwin.h:71
void setForeground(unsigned long pixelvalue)
Definition: xwin.h:38
void setStippleN(int n)
Definition: xwin.h:48
const char * getResource(const char *name)
Definition: xwin.cc:362
void drawFilledRectangle(int x, int y, int width, int height)
Definition: xwin.h:67