INET Framework for OMNeT++/OMNEST
LinearGaugeFigure Class Reference

#include <LinearGaugeFigure.h>

Inheritance diagram for LinearGaugeFigure:
inet::IIndicatorFigure

Public Member Functions

 LinearGaugeFigure (const char *name=nullptr)
 
virtual ~LinearGaugeFigure ()
 
virtual void setValue (int series, simtime_t timestamp, double value) override
 
const Rectangle & getBounds () const
 
void setBounds (const Rectangle &rect)
 
const Color & getBackgroundColor () const
 
void setBackgroundColor (const Color &color)
 
const Color & getNeedleColor () const
 
void setNeedleColor (const Color &color)
 
const char * getLabel () const
 
void setLabel (const char *text)
 
const int getLabelOffset () const
 
void setLabelOffset (int offset)
 
const Font & getLabelFont () const
 
void setLabelFont (const Font &font)
 
const Color & getLabelColor () const
 
void setLabelColor (const Color &color)
 
double getMinValue () const
 
void setMinValue (double value)
 
double getMaxValue () const
 
void setMaxValue (double value)
 
double getTickSize () const
 
void setTickSize (double value)
 
double getCornerRadius () const
 
void setCornerRadius (double radius)
 
- Public Member Functions inherited from inet::IIndicatorFigure
virtual ~IIndicatorFigure ()
 
virtual int getNumSeries () const
 
virtual void refreshDisplay ()
 

Protected Member Functions

virtual void parse (cProperty *property) override
 
virtual const char ** getAllowedPropertyKeys () const override
 
void addChildren ()
 
void setTickGeometry (cLineFigure *tick, int index)
 
void setNumberGeometry (cTextFigure *number, int index)
 
void setNeedleGeometry ()
 
void redrawTicks ()
 
void layout ()
 
void refresh ()
 

Private Attributes

cLineFigure * needle
 
cTextFigure * labelFigure
 
cRectangleFigure * backgroundFigure
 
cLineFigure * axisFigure
 
std::vector< cLineFigure * > tickFigures
 
std::vector< cTextFigure * > numberFigures
 
double min = 0
 
double max = 100
 
double tickSize = 10
 
double value = NaN
 
int numTicks = 0
 
double shifting = 0
 
int labelOffset = 10
 

Constructor & Destructor Documentation

LinearGaugeFigure::LinearGaugeFigure ( const char *  name = nullptr)
55  : cGroupFigure(name)
56 {
57  addChildren();
58 }
void addChildren()
Definition: LinearGaugeFigure.cc:246
LinearGaugeFigure::~LinearGaugeFigure ( )
virtual
61 {
62  // delete figures which is not in canvas
63  for (int i = numTicks; i < tickFigures.size(); ++i) {
64  delete tickFigures[i];
65  delete numberFigures[i];
66  }
67 }
std::vector< cTextFigure * > numberFigures
Definition: LinearGaugeFigure.h:35
std::vector< cLineFigure * > tickFigures
Definition: LinearGaugeFigure.h:34
int numTicks
Definition: LinearGaugeFigure.h:40

Member Function Documentation

void LinearGaugeFigure::addChildren ( )
protected

Referenced by LinearGaugeFigure().

247 {
248  backgroundFigure = new cRectangleFigure("background");
249  needle = new cLineFigure("needle");
250  labelFigure = new cTextFigure("label");
251  axisFigure = new cLineFigure("axis");
252 
253  backgroundFigure->setFilled(true);
254  backgroundFigure->setFillColor(Color("#b8afa6"));
255 
256  needle->setLineColor("red");
257  labelFigure->setAnchor(cFigure::ANCHOR_N);
258 
259  addFigure(backgroundFigure);
260  addFigure(axisFigure);
261  addFigure(needle);
262  addFigure(labelFigure);
263 }
cLineFigure * axisFigure
Definition: LinearGaugeFigure.h:33
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
Color
Definition: DiffservUtil.h:30
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
cLineFigure * needle
Definition: LinearGaugeFigure.h:30
const char ** LinearGaugeFigure::getAllowedPropertyKeys ( ) const
overrideprotectedvirtual
232 {
233  static const char *keys[32];
234  if (!keys[0]) {
235  const char *localKeys[] = {
236  PKEY_BACKGROUND_COLOR, PKEY_NEEDLE_COLOR, PKEY_LABEL, PKEY_LABEL_FONT,
237  PKEY_LABEL_COLOR, PKEY_MIN_VALUE, PKEY_MAX_VALUE, PKEY_TICK_SIZE,
238  PKEY_CORNER_RADIUS, PKEY_INITIAL_VALUE, PKEY_POS, PKEY_SIZE, PKEY_ANCHOR,
239  PKEY_BOUNDS, PKEY_LABEL_OFFSET, nullptr
240  };
241  concatArrays(keys, cGroupFigure::getAllowedPropertyKeys(), localKeys);
242  }
243  return keys;
244 }
std::vector< K > keys(const std::map< K, V > &m)
Definition: stlutils.h:96
const cFigure::Color & LinearGaugeFigure::getBackgroundColor ( ) const
81 {
82  return backgroundFigure->getFillColor();
83 }
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
const cFigure::Rectangle & LinearGaugeFigure::getBounds ( ) const

Referenced by layout(), parse(), setLabelOffset(), setNeedleGeometry(), setNumberGeometry(), and setTickGeometry().

70 {
71  return backgroundFigure->getBounds();
72 }
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
double LinearGaugeFigure::getCornerRadius ( ) const
186 {
187  return backgroundFigure->getCornerRx();
188 }
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
const char * LinearGaugeFigure::getLabel ( ) const
101 {
102  return labelFigure->getText();
103 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
const cFigure::Color & LinearGaugeFigure::getLabelColor ( ) const
134 {
135  return labelFigure->getColor();
136 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
const cFigure::Font & LinearGaugeFigure::getLabelFont ( ) const
124 {
125  return labelFigure->getFont();
126 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
const int LinearGaugeFigure::getLabelOffset ( ) const
111 {
112  return labelOffset;
113 }
int labelOffset
Definition: LinearGaugeFigure.h:42
double LinearGaugeFigure::getMaxValue ( ) const
158 {
159  return max;
160 }
double max
Definition: LinearGaugeFigure.h:37
double LinearGaugeFigure::getMinValue ( ) const
144 {
145  return min;
146 }
double min
Definition: LinearGaugeFigure.h:36
const cFigure::Color & LinearGaugeFigure::getNeedleColor ( ) const
91 {
92  return needle->getLineColor();
93 }
cLineFigure * needle
Definition: LinearGaugeFigure.h:30
double LinearGaugeFigure::getTickSize ( ) const
172 {
173  return tickSize;
174 }
double tickSize
Definition: LinearGaugeFigure.h:38
void LinearGaugeFigure::layout ( )
protected

Referenced by setBounds().

372 {
373  backgroundFigure->setLineWidth(getBounds().height * BORDER_WIDTH_PERCENT);
374 
375  double y = getBounds().getCenter().y;
376  axisFigure->setLineWidth(getBounds().height * AXIS_WIDTH_PERCENT);
377  axisFigure->setStart(Point(getBounds().x + getBounds().width * FRAME_PERCENT + axisFigure->getLineWidth() / 2, y));
378  axisFigure->setEnd(Point(getBounds().x + getBounds().width * (1 - FRAME_PERCENT) + axisFigure->getLineWidth() / 2, y));
379 
380  for (int i = 0; i < numTicks; ++i) {
383  }
384 
386  labelFigure->setPosition(Point(getBounds().getCenter().x, getBounds().y + getBounds().height + labelOffset));
387 }
std::vector< cTextFigure * > numberFigures
Definition: LinearGaugeFigure.h:35
std::vector< cLineFigure * > tickFigures
Definition: LinearGaugeFigure.h:34
cLineFigure * axisFigure
Definition: LinearGaugeFigure.h:33
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
void setTickGeometry(cLineFigure *tick, int index)
Definition: LinearGaugeFigure.cc:275
void setNeedleGeometry()
Definition: LinearGaugeFigure.cc:299
void setNumberGeometry(cTextFigure *number, int index)
Definition: LinearGaugeFigure.cc:289
int numTicks
Definition: LinearGaugeFigure.h:40
int labelOffset
Definition: LinearGaugeFigure.h:42
const Rectangle & getBounds() const
Definition: LinearGaugeFigure.cc:69
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
void LinearGaugeFigure::parse ( cProperty *  property)
overrideprotectedvirtual
196 {
197  cGroupFigure::parse(property);
198 
199  const char *s;
200 
201  setBounds(parseBounds(property, getBounds()));
202 
203  // Set default
204  redrawTicks();
205 
206  if ((s = property->getValue(PKEY_BACKGROUND_COLOR)) != nullptr)
207  setBackgroundColor(parseColor(s));
208  if ((s = property->getValue(PKEY_NEEDLE_COLOR)) != nullptr)
209  setNeedleColor(parseColor(s));
210  if ((s = property->getValue(PKEY_LABEL)) != nullptr)
211  setLabel(s);
212  if ((s = property->getValue(PKEY_LABEL_OFFSET)) != nullptr)
213  setLabelOffset(atoi(s));
214  if ((s = property->getValue(PKEY_LABEL_FONT)) != nullptr)
215  setLabelFont(parseFont(s));
216  if ((s = property->getValue(PKEY_LABEL_COLOR)) != nullptr)
217  setLabelColor(parseColor(s));
218  // This must be initialize before min and max because it is possible to be too much unnecessary tick and number
219  if ((s = property->getValue(PKEY_TICK_SIZE)) != nullptr)
221  if ((s = property->getValue(PKEY_MIN_VALUE)) != nullptr)
223  if ((s = property->getValue(PKEY_MAX_VALUE)) != nullptr)
225  if ((s = property->getValue(PKEY_CORNER_RADIUS)) != nullptr)
227  if ((s = property->getValue(PKEY_INITIAL_VALUE)) != nullptr)
228  setValue(0, simTime(), utils::atod(s));
229 }
void redrawTicks()
Definition: LinearGaugeFigure.cc:322
void setCornerRadius(double radius)
Definition: LinearGaugeFigure.cc:190
void setNeedleColor(const Color &color)
Definition: LinearGaugeFigure.cc:95
void setLabelOffset(int offset)
Definition: LinearGaugeFigure.cc:115
void setTickSize(double value)
Definition: LinearGaugeFigure.cc:176
void setBackgroundColor(const Color &color)
Definition: LinearGaugeFigure.cc:85
double atod(const char *s)
Converts string to double.
Definition: INETUtils.cc:38
const Rectangle & getBounds() const
Definition: LinearGaugeFigure.cc:69
void setMinValue(double value)
Definition: LinearGaugeFigure.cc:148
void setLabelColor(const Color &color)
Definition: LinearGaugeFigure.cc:138
void setLabel(const char *text)
Definition: LinearGaugeFigure.cc:105
value< double, units::s > s
Definition: Units.h:1049
virtual void setValue(int series, simtime_t timestamp, double value) override
Definition: LinearGaugeFigure.cc:265
void setBounds(const Rectangle &rect)
Definition: LinearGaugeFigure.cc:74
void setLabelFont(const Font &font)
Definition: LinearGaugeFigure.cc:128
void setMaxValue(double value)
Definition: LinearGaugeFigure.cc:162
void LinearGaugeFigure::redrawTicks ( )
protected

Referenced by parse(), setMaxValue(), setMinValue(), and setTickSize().

323 {
324  ASSERT(tickFigures.size() == numberFigures.size());
325 
326  double fraction = std::abs(fmod(min / tickSize, 1));
327  shifting = tickSize * (min < 0 ? fraction : 1 - fraction);
328  // if fraction == 0 then shifting == tickSize therefore don't have to shift the ticks
329  if (shifting == tickSize)
330  shifting = 0;
331 
332  int prevNumTicks = numTicks;
333  numTicks = std::max(0.0, std::abs(max - min - shifting) / tickSize + 1);
334 
335  // Allocate ticks and numbers if needed
336  if (numTicks > tickFigures.size())
337  while (numTicks > tickFigures.size()) {
338  cLineFigure *tick = new cLineFigure();
339  cTextFigure *number = new cTextFigure();
340 
341  number->setAnchor(cFigure::ANCHOR_N);
342 
343  tickFigures.push_back(tick);
344  numberFigures.push_back(number);
345  }
346 
347  // Add or remove figures from canvas according to previous number of ticks
348  for (int i = numTicks; i < prevNumTicks; ++i) {
349  removeFigure(tickFigures[i]);
350  removeFigure(numberFigures[i]);
351  }
352  for (int i = prevNumTicks; i < numTicks; ++i) {
353  tickFigures[i]->insertBelow(needle);
354  numberFigures[i]->insertBelow(needle);
355  }
356 
357  for (int i = 0; i < numTicks; ++i) {
359 
360  double number = min + i * tickSize + shifting;
361  if (std::abs(number) < tickSize / 2)
362  number = 0;
363 
364  char buf[32];
365  sprintf(buf, "%g", number);
366  numberFigures[i]->setText(buf);
368  }
369 }
std::vector< cTextFigure * > numberFigures
Definition: LinearGaugeFigure.h:35
std::vector< cLineFigure * > tickFigures
Definition: LinearGaugeFigure.h:34
void setTickGeometry(cLineFigure *tick, int index)
Definition: LinearGaugeFigure.cc:275
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
void setNumberGeometry(cTextFigure *number, int index)
Definition: LinearGaugeFigure.cc:289
int numTicks
Definition: LinearGaugeFigure.h:40
double max
Definition: LinearGaugeFigure.h:37
double shifting
Definition: LinearGaugeFigure.h:41
double tickSize
Definition: LinearGaugeFigure.h:38
cLineFigure * needle
Definition: LinearGaugeFigure.h:30
double min
Definition: LinearGaugeFigure.h:36
void LinearGaugeFigure::refresh ( )
protected

Referenced by setMaxValue(), setMinValue(), setTickSize(), and setValue().

390 {
392 }
void setNeedleGeometry()
Definition: LinearGaugeFigure.cc:299
void LinearGaugeFigure::setBackgroundColor ( const Color &  color)

Referenced by parse().

86 {
87  backgroundFigure->setFillColor(color);
88 }
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
void LinearGaugeFigure::setBounds ( const Rectangle &  rect)

Referenced by parse().

75 {
76  backgroundFigure->setBounds(rect);
77  layout();
78 }
void layout()
Definition: LinearGaugeFigure.cc:371
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
void LinearGaugeFigure::setCornerRadius ( double  radius)

Referenced by parse().

191 {
192  backgroundFigure->setCornerRadius(radius);
193 }
cRectangleFigure * backgroundFigure
Definition: LinearGaugeFigure.h:32
void LinearGaugeFigure::setLabel ( const char *  text)

Referenced by parse().

106 {
107  labelFigure->setText(text);
108 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
void LinearGaugeFigure::setLabelColor ( const Color &  color)

Referenced by parse().

139 {
140  labelFigure->setColor(color);
141 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
void LinearGaugeFigure::setLabelFont ( const Font &  font)

Referenced by parse().

129 {
130  labelFigure->setFont(font);
131 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
void LinearGaugeFigure::setLabelOffset ( int  offset)

Referenced by parse().

116 {
117  if(labelOffset != offset) {
118  labelOffset = offset;
119  labelFigure->setPosition(Point(getBounds().getCenter().x, getBounds().y + getBounds().height + labelOffset));
120  }
121 }
cTextFigure * labelFigure
Definition: LinearGaugeFigure.h:31
int labelOffset
Definition: LinearGaugeFigure.h:42
const Rectangle & getBounds() const
Definition: LinearGaugeFigure.cc:69
void LinearGaugeFigure::setMaxValue ( double  value)

Referenced by parse().

163 {
164  if (max != value) {
165  max = value;
166  redrawTicks();
167  refresh();
168  }
169 }
void redrawTicks()
Definition: LinearGaugeFigure.cc:322
double value
Definition: LinearGaugeFigure.h:39
double max
Definition: LinearGaugeFigure.h:37
void refresh()
Definition: LinearGaugeFigure.cc:389
void LinearGaugeFigure::setMinValue ( double  value)

Referenced by parse().

149 {
150  if (min != value) {
151  min = value;
152  redrawTicks();
153  refresh();
154  }
155 }
void redrawTicks()
Definition: LinearGaugeFigure.cc:322
double value
Definition: LinearGaugeFigure.h:39
void refresh()
Definition: LinearGaugeFigure.cc:389
double min
Definition: LinearGaugeFigure.h:36
void LinearGaugeFigure::setNeedleColor ( const Color &  color)

Referenced by parse().

96 {
97  needle->setLineColor(color);
98 }
cLineFigure * needle
Definition: LinearGaugeFigure.h:30
void LinearGaugeFigure::setNeedleGeometry ( )
protected

Referenced by layout(), and refresh().

300 {
301  needle->setLineWidth(getBounds().height * NEEDLE_WIDTH_PERCENT);
302 
303  double x = axisFigure->getStart().x + axisFigure->getLineWidth() / 2;
304  double axisWidth = axisFigure->getEnd().x - axisFigure->getStart().x - axisFigure->getLineWidth();
305 
306  needle->setVisible(true);
307  if (std::isnan(value)) {
308  needle->setVisible(false);
309  return;
310  }
311  else if (value < min)
312  x -= getBounds().width * NEEDLE_OFFSET_PERCENT;
313  else if (value > max)
314  x = axisFigure->getEnd().x + getBounds().width * NEEDLE_OFFSET_PERCENT;
315  else
316  x += (value - min) * axisWidth / (max - min);
317 
318  needle->setStart(Point(x, axisFigure->getStart().y - getBounds().height * NEEDLE_HEIGHT_PERCENT / 2));
319  needle->setEnd(Point(x, axisFigure->getStart().y + getBounds().height * NEEDLE_HEIGHT_PERCENT / 2));
320 }
cLineFigure * axisFigure
Definition: LinearGaugeFigure.h:33
double value
Definition: LinearGaugeFigure.h:39
const Rectangle & getBounds() const
Definition: LinearGaugeFigure.cc:69
double max
Definition: LinearGaugeFigure.h:37
cLineFigure * needle
Definition: LinearGaugeFigure.h:30
double min
Definition: LinearGaugeFigure.h:36
void LinearGaugeFigure::setNumberGeometry ( cTextFigure *  number,
int  index 
)
protected

Referenced by layout(), and redrawTicks().

290 {
291  double axisWidth = axisFigure->getEnd().x - axisFigure->getStart().x - axisFigure->getLineWidth() / 2;
292  double x = axisFigure->getStart().x + axisFigure->getLineWidth() / 2
293  + axisWidth * (index * tickSize + shifting) / (max - min);
294  Point textPos = Point(x, axisFigure->getStart().y + getBounds().height * NUMBER_Y_PERCENT);
295  number->setPosition(textPos);
296  number->setFont(cFigure::Font("", getBounds().height * NUMBER_FONTSIZE_PERCENT, 0));
297 }
cLineFigure * axisFigure
Definition: LinearGaugeFigure.h:33
const Rectangle & getBounds() const
Definition: LinearGaugeFigure.cc:69
double max
Definition: LinearGaugeFigure.h:37
double shifting
Definition: LinearGaugeFigure.h:41
double tickSize
Definition: LinearGaugeFigure.h:38
double min
Definition: LinearGaugeFigure.h:36
void LinearGaugeFigure::setTickGeometry ( cLineFigure *  tick,
int  index 
)
protected

Referenced by layout(), and redrawTicks().

276 {
277  double axisWidth = axisFigure->getEnd().x - axisFigure->getStart().x - axisFigure->getLineWidth();
278  double x = axisFigure->getStart().x + axisFigure->getLineWidth() / 2
279  + axisWidth * (index * tickSize + shifting) / (max - min);
280  tick->setStart(Point(x, getBounds().getCenter().y));
281 
282  Point endPos = tick->getStart();
283  endPos.y -= !(index % 3) ? getBounds().height * TICK_SMALL_LENGTH_PERCENT :
284  getBounds().height * TICK_BIG_LENGTH_PERCENT;
285  tick->setEnd(endPos);
286  tick->setLineWidth(getBounds().height * AXIS_WIDTH_PERCENT);
287 }
cLineFigure * axisFigure
Definition: LinearGaugeFigure.h:33
const Rectangle & getBounds() const
Definition: LinearGaugeFigure.cc:69
double max
Definition: LinearGaugeFigure.h:37
double shifting
Definition: LinearGaugeFigure.h:41
double tickSize
Definition: LinearGaugeFigure.h:38
double min
Definition: LinearGaugeFigure.h:36
void LinearGaugeFigure::setTickSize ( double  value)

Referenced by parse().

177 {
178  if (tickSize != value) {
179  tickSize = value;
180  redrawTicks();
181  refresh();
182  }
183 }
void redrawTicks()
Definition: LinearGaugeFigure.cc:322
double value
Definition: LinearGaugeFigure.h:39
void refresh()
Definition: LinearGaugeFigure.cc:389
double tickSize
Definition: LinearGaugeFigure.h:38
void LinearGaugeFigure::setValue ( int  series,
simtime_t  timestamp,
double  value 
)
overridevirtual

Implements inet::IIndicatorFigure.

Referenced by parse().

266 {
267  ASSERT(series == 0);
268  // Note: we currently ignore timestamp
269  if (value != newValue) {
270  value = newValue;
271  refresh();
272  }
273 }
double value
Definition: LinearGaugeFigure.h:39
void refresh()
Definition: LinearGaugeFigure.cc:389

Member Data Documentation

cLineFigure* LinearGaugeFigure::axisFigure
private
cRectangleFigure* LinearGaugeFigure::backgroundFigure
private
cTextFigure* LinearGaugeFigure::labelFigure
private
int LinearGaugeFigure::labelOffset = 10
private
double LinearGaugeFigure::max = 100
private
double LinearGaugeFigure::min = 0
private
cLineFigure* LinearGaugeFigure::needle
private
std::vector<cTextFigure *> LinearGaugeFigure::numberFigures
private
int LinearGaugeFigure::numTicks = 0
private
double LinearGaugeFigure::shifting = 0
private
std::vector<cLineFigure *> LinearGaugeFigure::tickFigures
private
double LinearGaugeFigure::tickSize = 10
private
double LinearGaugeFigure::value = NaN
private

The documentation for this class was generated from the following files: