INET Framework for OMNeT++/OMNEST
CounterFigure Class Reference

#include <CounterFigure.h>

Inheritance diagram for CounterFigure:
inet::IIndicatorFigure

Classes

struct  Digit
 

Public Member Functions

 CounterFigure (const char *name=nullptr)
 
virtual ~CounterFigure ()
 
virtual void setValue (int series, simtime_t timestamp, double value) override
 
const ColorgetBackgroundColor () const
 
void setBackgroundColor (const Color &color)
 
int getDecimalPlaces () const
 
void setDecimalPlaces (int radius)
 
Color getDigitBackgroundColor () const
 
void setDigitBackgroundColor (const Color &color)
 
Color getDigitBorderColor () const
 
void setDigitBorderColor (const Color &color)
 
Font getDigitFont () const
 
void setDigitFont (const Font &font)
 
Color getDigitColor () const
 
void setDigitColor (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 ColorgetLabelColor () const
 
void setLabelColor (const Color &color)
 
const Point & getLabelPos () const
 
void setLabelPos (const Point &pos)
 
Anchor getLabelAnchor () const
 
void setLabelAnchor (Anchor anchor)
 
Point getPos () const
 
void setPos (const Point &bounds)
 
Anchor getAnchor () const
 
void setAnchor (Anchor anchor)
 
- 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
 
Point calculateRealPos (const Point &pos)
 
void calculateBounds ()
 
void addChildren ()
 
void refresh ()
 
void layout ()
 

Private Attributes

cRectangleFigure * backgroundFigure
 
std::vector< Digitdigits
 
cTextFigure * labelFigure
 
double value = NaN
 
int labelOffset = 10
 
Anchor anchor = ANCHOR_NW
 

Constructor & Destructor Documentation

CounterFigure::CounterFigure ( const char *  name = nullptr)
53  : cGroupFigure(name)
54 {
55  addChildren();
56 }
void addChildren()
Definition: CounterFigure.cc:408
virtual CounterFigure::~CounterFigure ( )
inlinevirtual
57 {};

Member Function Documentation

void CounterFigure::addChildren ( )
protected

Referenced by CounterFigure().

409 {
410  backgroundFigure = new cRectangleFigure("background");
411  labelFigure = new cTextFigure("label");
412 
413  backgroundFigure->setFilled(true);
414  backgroundFigure->setFillColor(Color(INIT_BACKGROUND_COLOR));
415  backgroundFigure->setZoomLineWidth(true);
416 
417  labelFigure->setAnchor(ANCHOR_N);
418 
419  addFigure(backgroundFigure);
420  addFigure(labelFigure);
421 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
Color
Definition: DiffservUtil.h:30
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
void CounterFigure::calculateBounds ( )
protected

Referenced by setAnchor(), setDecimalPlaces(), and setDigitFont().

322 {
323  double rectWidth = getDigitFont().pointSize * DIGIT_WIDTH_PERCENT;
324  double rectHeight = getDigitFont().pointSize * DIGIT_HEIGHT_PERCENT;
325 
326  Rectangle bounds = backgroundFigure->getBounds();
327  backgroundFigure->setBounds(Rectangle(0, 0, 2 * PADDING + (rectWidth + DIGIT_PADDING) * digits.size(),
328  rectHeight + 2 * PADDING));
329  Point pos = calculateRealPos(Point(bounds.x, bounds.y));
330  bounds = backgroundFigure->getBounds();
331  bounds.x = pos.x;
332  bounds.y = pos.y;
333  backgroundFigure->setBounds(bounds);
334 }
std::vector< Digit > digits
Definition: CounterFigure.h:39
Point calculateRealPos(const Point &pos)
Definition: CounterFigure.cc:271
Font getDigitFont() const
Definition: CounterFigure.cc:127
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
cFigure::Point CounterFigure::calculateRealPos ( const Point &  pos)
protected

Referenced by calculateBounds(), and setPos().

272 {
273  Rectangle bounds = backgroundFigure->getBounds();
274  Point position = pos;
275  switch (anchor) {
276  case cFigure::ANCHOR_CENTER:
277  position.x -= bounds.width / 2;
278  position.y -= bounds.height / 2;
279  break;
280 
281  case cFigure::ANCHOR_N:
282  position.x -= bounds.width / 2;
283  break;
284 
285  case cFigure::ANCHOR_E:
286  position.x -= bounds.width;
287  position.y -= bounds.height / 2;
288  break;
289 
290  case cFigure::ANCHOR_S:
291  case cFigure::ANCHOR_BASELINE_MIDDLE:
292  position.x -= bounds.width / 2;
293  position.y -= bounds.height;
294  break;
295 
296  case cFigure::ANCHOR_W:
297  position.y -= bounds.height / 2;
298  break;
299 
300  case cFigure::ANCHOR_NW:
301  break;
302 
303  case cFigure::ANCHOR_NE:
304  position.x -= bounds.width;
305  break;
306 
307  case cFigure::ANCHOR_SE:
308  case cFigure::ANCHOR_BASELINE_END:
309  position.x -= bounds.width;
310  position.y -= bounds.height;
311  break;
312 
313  case cFigure::ANCHOR_SW:
314  case cFigure::ANCHOR_BASELINE_START:
315  position.y -= bounds.width;
316  break;
317  }
318  return position;
319 }
Anchor anchor
Definition: CounterFigure.h:44
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
const char ** CounterFigure::getAllowedPropertyKeys ( ) const
overrideprotectedvirtual
375 {
376  static const char *keys[32];
377  if (!keys[0]) {
378  const char *localKeys[] = {
379  PKEY_BACKGROUND_COLOR, PKEY_DECIMAL_PLACES, PKEY_DIGIT_BACKGROUND_COLOR,
380  PKEY_DIGIT_BORDER_COLOR, PKEY_DIGIT_FONT, PKEY_DIGIT_COLOR, PKEY_LABEL, PKEY_LABEL_FONT,
381  PKEY_LABEL_COLOR, PKEY_INITIAL_VALUE, PKEY_POS, PKEY_ANCHOR, PKEY_LABEL_OFFSET, nullptr
382  };
383  concatArrays(keys, cGroupFigure::getAllowedPropertyKeys(), localKeys);
384  }
385  return keys;
386 }
std::vector< K > keys(const std::map< K, V > &m)
Definition: stlutils.h:96
cFigure::Anchor CounterFigure::getAnchor ( ) const
256 {
257  return anchor;
258 }
Anchor anchor
Definition: CounterFigure.h:44
const cFigure::Color & CounterFigure::getBackgroundColor ( ) const
59 {
60  return backgroundFigure->getFillColor();
61 }
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
int CounterFigure::getDecimalPlaces ( ) const
69 {
70  return digits.size();
71 }
std::vector< Digit > digits
Definition: CounterFigure.h:39
cFigure::Color CounterFigure::getDigitBackgroundColor ( ) const

Referenced by setDecimalPlaces().

106 {
107  return digits.size() ? digits[0].bounds->getFillColor() : Color(INIT_DIGIT_BACKGROUND_COLOR);
108 }
Color
Definition: DiffservUtil.h:30
std::vector< Digit > digits
Definition: CounterFigure.h:39
cFigure::Color CounterFigure::getDigitBorderColor ( ) const
117 {
118  return digits.size() ? digits[0].bounds->getLineColor() : Color(INIT_DIGIT_BORDER_COLOR);
119 }
Color
Definition: DiffservUtil.h:30
std::vector< Digit > digits
Definition: CounterFigure.h:39
cFigure::Color CounterFigure::getDigitColor ( ) const
142 {
143  return digits.size() ? digits[0].text->getColor() : Color(INIT_DIGIT_TEXT_COLOR);
144 }
Color
Definition: DiffservUtil.h:30
std::vector< Digit > digits
Definition: CounterFigure.h:39
cFigure::Font CounterFigure::getDigitFont ( ) const

Referenced by calculateBounds(), layout(), and setDecimalPlaces().

128 {
129  return digits.size() ? digits[0].text->getFont() : Font(INIT_FONT_NAME, INIT_FONT_SIZE, cFigure::FONT_BOLD);
130 }
std::vector< Digit > digits
Definition: CounterFigure.h:39
const char * CounterFigure::getLabel ( ) const
153 {
154  return labelFigure->getText();
155 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
Anchor CounterFigure::getLabelAnchor ( ) const
const cFigure::Color & CounterFigure::getLabelColor ( ) const
186 {
187  return labelFigure->getColor();
188 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
const cFigure::Font & CounterFigure::getLabelFont ( ) const
176 {
177  return labelFigure->getFont();
178 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
const int CounterFigure::getLabelOffset ( ) const
163 {
164  return labelOffset;
165 }
int labelOffset
Definition: CounterFigure.h:43
const Point& CounterFigure::getLabelPos ( ) const
cFigure::Point CounterFigure::getPos ( ) const
196 {
197  Rectangle bounds = backgroundFigure->getBounds();
198  switch (anchor) {
199  case cFigure::ANCHOR_CENTER:
200  bounds.x += bounds.width / 2;
201  bounds.y += bounds.height / 2;
202  break;
203 
204  case cFigure::ANCHOR_N:
205  bounds.x += bounds.width / 2;
206  break;
207 
208  case cFigure::ANCHOR_E:
209  bounds.x += bounds.width;
210  bounds.y += bounds.height / 2;
211  break;
212 
213  case cFigure::ANCHOR_S:
214  case cFigure::ANCHOR_BASELINE_MIDDLE:
215  bounds.x += bounds.width / 2;
216  bounds.y += bounds.height;
217  break;
218 
219  case cFigure::ANCHOR_W:
220  bounds.y += bounds.height / 2;
221  break;
222 
223  case cFigure::ANCHOR_NW:
224  break;
225 
226  case cFigure::ANCHOR_NE:
227  bounds.x += bounds.width;
228  break;
229 
230  case cFigure::ANCHOR_SE:
231  case cFigure::ANCHOR_BASELINE_END:
232  bounds.x += bounds.width;
233  bounds.y += bounds.height;
234  break;
235 
236  case cFigure::ANCHOR_SW:
237  case cFigure::ANCHOR_BASELINE_START:
238  bounds.y += bounds.width;
239  break;
240  }
241  return Point(bounds.x, bounds.y);
242 }
Anchor anchor
Definition: CounterFigure.h:44
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
void CounterFigure::layout ( )
protected

Referenced by setAnchor(), setDecimalPlaces(), setDigitFont(), and setPos().

389 {
390  Rectangle bounds = backgroundFigure->getBounds();
391 
392  // Add frame
393  bounds.x += PADDING + DIGIT_PADDING / 2;
394 
395  for (int i = 0; i < digits.size(); ++i) {
396  double rectWidth = getDigitFont().pointSize * DIGIT_WIDTH_PERCENT;
397  double rectHeight = getDigitFont().pointSize * DIGIT_HEIGHT_PERCENT;
398  double x = bounds.x + (rectWidth + DIGIT_PADDING) * i;
399  double y = bounds.y + PADDING;
400 
401  digits[i].bounds->setBounds(Rectangle(x, y, rectWidth, rectHeight));
402  digits[i].text->setPosition(digits[i].bounds->getBounds().getCenter());
403  }
404 
405  labelFigure->setPosition(Point(bounds.x + bounds.width / 2, bounds.y + bounds.height + labelOffset));
406 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
int labelOffset
Definition: CounterFigure.h:43
std::vector< Digit > digits
Definition: CounterFigure.h:39
Font getDigitFont() const
Definition: CounterFigure.cc:127
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
void CounterFigure::parse ( cProperty *  property)
overrideprotectedvirtual
337 {
338  cGroupFigure::parse(property);
339 
340  const char *s;
341 
342  setPos(parsePoint(property, PKEY_POS, 0));
343 
344  if ((s = property->getValue(PKEY_BACKGROUND_COLOR)) != nullptr)
345  setBackgroundColor(parseColor(s));
346  if ((s = property->getValue(PKEY_ANCHOR)) != nullptr)
347  setAnchor(parseAnchor(s));
348  if ((s = property->getValue(PKEY_DECIMAL_PLACES)) != nullptr)
350  else
351  setDecimalPlaces(INIT_DECIMAL_PLACES);
352  if ((s = property->getValue(PKEY_DIGIT_FONT)) != nullptr)
353  setDigitFont(parseFont(s));
354  if ((s = property->getValue(PKEY_DIGIT_BACKGROUND_COLOR)) != nullptr)
355  setDigitBackgroundColor(parseColor(s));
356  if ((s = property->getValue(PKEY_DIGIT_BORDER_COLOR)) != nullptr)
357  setDigitBorderColor(Color(parseColor(s)));
358  if ((s = property->getValue(PKEY_DIGIT_COLOR)) != nullptr)
359  setDigitColor(parseColor(s));
360  if ((s = property->getValue(PKEY_LABEL)) != nullptr)
361  setLabel(s);
362  if ((s = property->getValue(PKEY_LABEL_OFFSET)) != nullptr)
363  setLabelOffset(atoi(s));
364  if ((s = property->getValue(PKEY_LABEL_FONT)) != nullptr)
365  setLabelFont(parseFont(s));
366  if ((s = property->getValue(PKEY_LABEL_COLOR)) != nullptr)
367  setLabelColor(parseColor(s));
368  if ((s = property->getValue(PKEY_INITIAL_VALUE)) != nullptr)
369  setValue(0, simTime(), utils::atod(s));
370 
371  refresh();
372 }
void setDecimalPlaces(int radius)
Definition: CounterFigure.cc:73
void setDigitColor(const Color &color)
Definition: CounterFigure.cc:146
void setAnchor(Anchor anchor)
Definition: CounterFigure.cc:260
void setBackgroundColor(const Color &color)
Definition: CounterFigure.cc:63
void setLabelFont(const Font &font)
Definition: CounterFigure.cc:180
void refresh()
Definition: CounterFigure.cc:434
double atod(const char *s)
Converts string to double.
Definition: INETUtils.cc:38
void setLabelColor(const Color &color)
Definition: CounterFigure.cc:190
void setDigitFont(const Font &font)
Definition: CounterFigure.cc:132
void setDigitBackgroundColor(const Color &color)
Definition: CounterFigure.cc:110
void setPos(const Point &bounds)
Definition: CounterFigure.cc:244
Color
Definition: DiffservUtil.h:30
void setLabel(const char *text)
Definition: CounterFigure.cc:157
value< double, units::s > s
Definition: Units.h:1049
void setDigitBorderColor(const Color &color)
Definition: CounterFigure.cc:121
virtual void setValue(int series, simtime_t timestamp, double value) override
Definition: CounterFigure.cc:423
void setLabelOffset(int offset)
Definition: CounterFigure.cc:167
void CounterFigure::refresh ( )
protected

Referenced by parse(), and setValue().

435 {
436  // update displayed number
437  int max = std::pow(10, digits.size());
438  if (std::isnan(value))
439  for (Digit digit : digits)
440  digit.text->setText("");
441 
442  else if (value >= max || value < 0)
443  for (Digit digit : digits)
444  digit.text->setText("*");
445 
446  else {
447  int pow = 1;
448  for (int i = digits.size() - 1; i >= 0; --i) {
449  char buf[32];
450  pow *= 10;
451  int actValue = ((int)value % pow) / (pow / 10);
452  sprintf(buf, "%d", actValue);
453  digits[i].text->setText(buf);
454  }
455  }
456 }
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
double value
Definition: CounterFigure.h:42
std::vector< Digit > digits
Definition: CounterFigure.h:39
void CounterFigure::setAnchor ( Anchor  anchor)

Referenced by parse().

261 {
262  if (anchor != this->anchor) {
263  this->anchor = anchor;
264 
265  calculateBounds();
266  layout();
267  }
268 }
Anchor anchor
Definition: CounterFigure.h:44
void layout()
Definition: CounterFigure.cc:388
void calculateBounds()
Definition: CounterFigure.cc:321
void CounterFigure::setBackgroundColor ( const Color color)

Referenced by parse().

64 {
65  backgroundFigure->setFillColor(color);
66 }
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
void CounterFigure::setDecimalPlaces ( int  radius)

Referenced by parse().

74 {
75  ASSERT(number > 0);
76  if (digits.size() != number) {
77  if (digits.size() > number)
78  // Remove unnecessary figures from canvas
79  for (int i = digits.size() - 1; i > number - 1; --i) {
80  delete removeFigure(digits[i].bounds);
81  delete removeFigure(digits[i].text);
82  digits.pop_back();
83  }
84  else
85  // Add figure to canvas if it's necessary
86  while (digits.size() < number) {
87  Digit digit(new cRectangleFigure(), new cTextFigure());
88  digit.bounds->setFilled(true);
89  digit.bounds->setFillColor(getDigitBackgroundColor());
90  digit.bounds->setZoomLineWidth(true);
91  digit.text->setAnchor(ANCHOR_CENTER);
92  digit.text->setFont(getDigitFont());
93 
94  addFigure(digit.bounds);
95  addFigure(digit.text);
96 
97  digits.push_back(digit);
98  }
99 
100  calculateBounds();
101  layout();
102  }
103 }
Color getDigitBackgroundColor() const
Definition: CounterFigure.cc:105
void layout()
Definition: CounterFigure.cc:388
std::vector< Digit > digits
Definition: CounterFigure.h:39
void calculateBounds()
Definition: CounterFigure.cc:321
Font getDigitFont() const
Definition: CounterFigure.cc:127
void CounterFigure::setDigitBackgroundColor ( const Color color)

Referenced by parse().

111 {
112  for (Digit digit : digits)
113  digit.bounds->setFillColor(color);
114 }
std::vector< Digit > digits
Definition: CounterFigure.h:39
void CounterFigure::setDigitBorderColor ( const Color color)

Referenced by parse().

122 {
123  for (Digit digit : digits)
124  digit.bounds->setLineColor(color);
125 }
std::vector< Digit > digits
Definition: CounterFigure.h:39
void CounterFigure::setDigitColor ( const Color color)

Referenced by parse().

147 {
148  for (Digit digit : digits)
149  digit.text->setColor(color);
150 }
std::vector< Digit > digits
Definition: CounterFigure.h:39
void CounterFigure::setDigitFont ( const Font &  font)

Referenced by parse().

133 {
134  for (Digit digit : digits)
135  digit.text->setFont(font);
136 
137  calculateBounds();
138  layout();
139 }
void layout()
Definition: CounterFigure.cc:388
std::vector< Digit > digits
Definition: CounterFigure.h:39
void calculateBounds()
Definition: CounterFigure.cc:321
void CounterFigure::setLabel ( const char *  text)

Referenced by parse().

158 {
159  labelFigure->setText(text);
160 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
void CounterFigure::setLabelAnchor ( Anchor  anchor)
void CounterFigure::setLabelColor ( const Color color)

Referenced by parse().

191 {
192  labelFigure->setColor(color);
193 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
void CounterFigure::setLabelFont ( const Font &  font)

Referenced by parse().

181 {
182  labelFigure->setFont(font);
183 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
void CounterFigure::setLabelOffset ( int  offset)

Referenced by parse().

168 {
169  if(labelOffset != offset) {
170  labelOffset = offset;
171  labelFigure->setPosition(Point(backgroundFigure->getBounds().x + backgroundFigure->getBounds().width / 2, backgroundFigure->getBounds().y + backgroundFigure->getBounds().height + labelOffset));
172  }
173 }
cTextFigure * labelFigure
Definition: CounterFigure.h:40
int labelOffset
Definition: CounterFigure.h:43
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
void CounterFigure::setLabelPos ( const Point &  pos)
void CounterFigure::setPos ( const Point &  bounds)

Referenced by parse().

245 {
246  Rectangle bounds = backgroundFigure->getBounds();
247  Point backgroundPos = calculateRealPos(pos);
248  bounds.x = backgroundPos.x;
249  bounds.y = backgroundPos.y;
250 
251  backgroundFigure->setBounds(bounds);
252  layout();
253 }
void layout()
Definition: CounterFigure.cc:388
Point calculateRealPos(const Point &pos)
Definition: CounterFigure.cc:271
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:38
void CounterFigure::setValue ( int  series,
simtime_t  timestamp,
double  value 
)
overridevirtual

Implements inet::IIndicatorFigure.

Referenced by parse().

424 {
425  ASSERT(series == 0);
426 
427  // Note: we currently ignore timestamp
428  if (value != newValue) {
429  value = newValue;
430  refresh();
431  }
432 }
double value
Definition: CounterFigure.h:42
void refresh()
Definition: CounterFigure.cc:434

Member Data Documentation

Anchor CounterFigure::anchor = ANCHOR_NW
private
cRectangleFigure* CounterFigure::backgroundFigure
private
cTextFigure* CounterFigure::labelFigure
private
int CounterFigure::labelOffset = 10
private
double CounterFigure::value = NaN
private

Referenced by refresh(), and setValue().


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