INET Framework for OMNeT++/OMNEST
inet::CounterFigure Class Reference

#include <CounterFigure.h>

Inheritance diagram for inet::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
 
virtual const Point getSize () const 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)
 
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 PointgetLabelPos () 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()

inet::CounterFigure::CounterFigure ( const char *  name = nullptr)
42  : cGroupFigure(name)
43 {
44  addChildren();
45 }

◆ ~CounterFigure()

virtual inet::CounterFigure::~CounterFigure ( )
inlinevirtual
44 {}

Member Function Documentation

◆ addChildren()

void inet::CounterFigure::addChildren ( )
protected
397 {
398  backgroundFigure = new cRectangleFigure("background");
399  labelFigure = new cTextFigure("label");
400 
401  backgroundFigure->setFilled(true);
402  backgroundFigure->setFillColor(Color(INIT_BACKGROUND_COLOR));
403  backgroundFigure->setZoomLineWidth(true);
404 
405  labelFigure->setAnchor(ANCHOR_N);
406 
407  addFigure(backgroundFigure);
408  addFigure(labelFigure);
409 }

Referenced by CounterFigure().

◆ calculateBounds()

void inet::CounterFigure::calculateBounds ( )
protected
311 {
312  double rectWidth = getDigitFont().pointSize * DIGIT_WIDTH_PERCENT;
313  double rectHeight = getDigitFont().pointSize * DIGIT_HEIGHT_PERCENT;
314 
315  Rectangle bounds = backgroundFigure->getBounds();
316  backgroundFigure->setBounds(Rectangle(0, 0, 2 * PADDING + (rectWidth + DIGIT_PADDING) * digits.size(), rectHeight + 2 * PADDING));
317  Point pos = calculateRealPos(Point(bounds.x, bounds.y));
318  bounds = backgroundFigure->getBounds();
319  bounds.x = pos.x;
320  bounds.y = pos.y;
321  backgroundFigure->setBounds(bounds);
322 }

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

◆ calculateRealPos()

cFigure::Point inet::CounterFigure::calculateRealPos ( const Point pos)
protected
261 {
262  Rectangle bounds = backgroundFigure->getBounds();
263  Point position = pos;
264  switch (anchor) {
265  case cFigure::ANCHOR_CENTER:
266  position.x -= bounds.width / 2;
267  position.y -= bounds.height / 2;
268  break;
269 
270  case cFigure::ANCHOR_N:
271  position.x -= bounds.width / 2;
272  break;
273 
274  case cFigure::ANCHOR_E:
275  position.x -= bounds.width;
276  position.y -= bounds.height / 2;
277  break;
278 
279  case cFigure::ANCHOR_S:
280  case cFigure::ANCHOR_BASELINE_MIDDLE:
281  position.x -= bounds.width / 2;
282  position.y -= bounds.height;
283  break;
284 
285  case cFigure::ANCHOR_W:
286  position.y -= bounds.height / 2;
287  break;
288 
289  case cFigure::ANCHOR_NW:
290  break;
291 
292  case cFigure::ANCHOR_NE:
293  position.x -= bounds.width;
294  break;
295 
296  case cFigure::ANCHOR_SE:
297  case cFigure::ANCHOR_BASELINE_END:
298  position.x -= bounds.width;
299  position.y -= bounds.height;
300  break;
301 
302  case cFigure::ANCHOR_SW:
303  case cFigure::ANCHOR_BASELINE_START:
304  position.y -= bounds.width;
305  break;
306  }
307  return position;
308 }

Referenced by calculateBounds(), and setPos().

◆ getAllowedPropertyKeys()

const char ** inet::CounterFigure::getAllowedPropertyKeys ( ) const
overrideprotectedvirtual
363 {
364  static const char *keys[32];
365  if (!keys[0]) {
366  const char *localKeys[] = {
367  PKEY_BACKGROUND_COLOR, PKEY_DECIMAL_PLACES, PKEY_DIGIT_BACKGROUND_COLOR,
368  PKEY_DIGIT_BORDER_COLOR, PKEY_DIGIT_FONT, PKEY_DIGIT_COLOR, PKEY_LABEL, PKEY_LABEL_FONT,
369  PKEY_LABEL_COLOR, PKEY_INITIAL_VALUE, PKEY_POS, PKEY_ANCHOR, PKEY_LABEL_OFFSET, nullptr
370  };
371  concatArrays(keys, cGroupFigure::getAllowedPropertyKeys(), localKeys);
372  }
373  return keys;
374 }

◆ getAnchor()

cFigure::Anchor inet::CounterFigure::getAnchor ( ) const
245 {
246  return anchor;
247 }

◆ getBackgroundColor()

const cFigure::Color & inet::CounterFigure::getBackgroundColor ( ) const
48 {
49  return backgroundFigure->getFillColor();
50 }

◆ getDecimalPlaces()

int inet::CounterFigure::getDecimalPlaces ( ) const
58 {
59  return digits.size();
60 }

◆ getDigitBackgroundColor()

cFigure::Color inet::CounterFigure::getDigitBackgroundColor ( ) const
95 {
96  return digits.size() ? digits[0].bounds->getFillColor() : Color(INIT_DIGIT_BACKGROUND_COLOR);
97 }

Referenced by setDecimalPlaces().

◆ getDigitBorderColor()

cFigure::Color inet::CounterFigure::getDigitBorderColor ( ) const
106 {
107  return digits.size() ? digits[0].bounds->getLineColor() : Color(INIT_DIGIT_BORDER_COLOR);
108 }

◆ getDigitColor()

cFigure::Color inet::CounterFigure::getDigitColor ( ) const
131 {
132  return digits.size() ? digits[0].text->getColor() : Color(INIT_DIGIT_TEXT_COLOR);
133 }

◆ getDigitFont()

cFigure::Font inet::CounterFigure::getDigitFont ( ) const
117 {
118  return digits.size() ? digits[0].text->getFont() : Font(INIT_FONT_NAME, INIT_FONT_SIZE, cFigure::FONT_BOLD);
119 }

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

◆ getLabel()

const char * inet::CounterFigure::getLabel ( ) const
142 {
143  return labelFigure->getText();
144 }

◆ getLabelAnchor()

Anchor inet::CounterFigure::getLabelAnchor ( ) const

◆ getLabelColor()

const cFigure::Color & inet::CounterFigure::getLabelColor ( ) const
175 {
176  return labelFigure->getColor();
177 }

◆ getLabelFont()

const cFigure::Font & inet::CounterFigure::getLabelFont ( ) const
165 {
166  return labelFigure->getFont();
167 }

◆ getLabelOffset()

int inet::CounterFigure::getLabelOffset ( ) const
152 {
153  return labelOffset;
154 }

◆ getLabelPos()

const Point& inet::CounterFigure::getLabelPos ( ) const

◆ getPos()

cFigure::Point inet::CounterFigure::getPos ( ) const
185 {
186  Rectangle bounds = backgroundFigure->getBounds();
187  switch (anchor) {
188  case cFigure::ANCHOR_CENTER:
189  bounds.x += bounds.width / 2;
190  bounds.y += bounds.height / 2;
191  break;
192 
193  case cFigure::ANCHOR_N:
194  bounds.x += bounds.width / 2;
195  break;
196 
197  case cFigure::ANCHOR_E:
198  bounds.x += bounds.width;
199  bounds.y += bounds.height / 2;
200  break;
201 
202  case cFigure::ANCHOR_S:
203  case cFigure::ANCHOR_BASELINE_MIDDLE:
204  bounds.x += bounds.width / 2;
205  bounds.y += bounds.height;
206  break;
207 
208  case cFigure::ANCHOR_W:
209  bounds.y += bounds.height / 2;
210  break;
211 
212  case cFigure::ANCHOR_NW:
213  break;
214 
215  case cFigure::ANCHOR_NE:
216  bounds.x += bounds.width;
217  break;
218 
219  case cFigure::ANCHOR_SE:
220  case cFigure::ANCHOR_BASELINE_END:
221  bounds.x += bounds.width;
222  bounds.y += bounds.height;
223  break;
224 
225  case cFigure::ANCHOR_SW:
226  case cFigure::ANCHOR_BASELINE_START:
227  bounds.y += bounds.width;
228  break;
229  }
230  return Point(bounds.x, bounds.y);
231 }

◆ getSize()

virtual const Point inet::CounterFigure::getSize ( ) const
inlineoverridevirtual

Implements inet::IIndicatorFigure.

48 { return backgroundFigure->getBounds().getSize(); }

◆ layout()

void inet::CounterFigure::layout ( )
protected
377 {
378  Rectangle bounds = backgroundFigure->getBounds();
379 
380  // Add frame
381  bounds.x += PADDING + DIGIT_PADDING / 2;
382 
383  for (uint32_t i = 0; i < digits.size(); ++i) {
384  double rectWidth = getDigitFont().pointSize * DIGIT_WIDTH_PERCENT;
385  double rectHeight = getDigitFont().pointSize * DIGIT_HEIGHT_PERCENT;
386  double x = bounds.x + (rectWidth + DIGIT_PADDING) * i;
387  double y = bounds.y + PADDING;
388 
389  digits[i].bounds->setBounds(Rectangle(x, y, rectWidth, rectHeight));
390  digits[i].text->setPosition(digits[i].bounds->getBounds().getCenter());
391  }
392 
393  labelFigure->setPosition(Point(bounds.x + bounds.width / 2, bounds.y + bounds.height + labelOffset));
394 }

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

◆ parse()

void inet::CounterFigure::parse ( cProperty *  property)
overrideprotectedvirtual
325 {
326  cGroupFigure::parse(property);
327 
328  const char *s;
329 
330  setPos(parsePoint(property, PKEY_POS, 0));
331 
332  if ((s = property->getValue(PKEY_BACKGROUND_COLOR)) != nullptr)
333  setBackgroundColor(parseColor(s));
334  if ((s = property->getValue(PKEY_ANCHOR)) != nullptr)
335  setAnchor(parseAnchor(s));
336  if ((s = property->getValue(PKEY_DECIMAL_PLACES)) != nullptr)
338  else
339  setDecimalPlaces(INIT_DECIMAL_PLACES);
340  if ((s = property->getValue(PKEY_DIGIT_FONT)) != nullptr)
341  setDigitFont(parseFont(s));
342  if ((s = property->getValue(PKEY_DIGIT_BACKGROUND_COLOR)) != nullptr)
343  setDigitBackgroundColor(parseColor(s));
344  if ((s = property->getValue(PKEY_DIGIT_BORDER_COLOR)) != nullptr)
345  setDigitBorderColor(Color(parseColor(s)));
346  if ((s = property->getValue(PKEY_DIGIT_COLOR)) != nullptr)
347  setDigitColor(parseColor(s));
348  if ((s = property->getValue(PKEY_LABEL)) != nullptr)
349  setLabel(s);
350  if ((s = property->getValue(PKEY_LABEL_OFFSET)) != nullptr)
351  setLabelOffset(atoi(s));
352  if ((s = property->getValue(PKEY_LABEL_FONT)) != nullptr)
353  setLabelFont(parseFont(s));
354  if ((s = property->getValue(PKEY_LABEL_COLOR)) != nullptr)
355  setLabelColor(parseColor(s));
356  if ((s = property->getValue(PKEY_INITIAL_VALUE)) != nullptr)
357  setValue(0, simTime(), utils::atod(s));
358 
359  refresh();
360 }

◆ refresh()

void inet::CounterFigure::refresh ( )
protected
423 {
424  // update displayed number
425  int max = std::pow(10, digits.size());
426  if (std::isnan(value))
427  for (Digit digit : digits)
428  digit.text->setText("");
429 
430  else if (value >= max || value < 0)
431  for (Digit digit : digits)
432  digit.text->setText("*");
433 
434  else {
435  int pow = 1;
436  for (int i = digits.size() - 1; i >= 0; --i) {
437  char buf[32];
438  pow *= 10;
439  int actValue = ((int)value % pow) / (pow / 10);
440  sprintf(buf, "%d", actValue);
441  digits[i].text->setText(buf);
442  }
443  }
444 }

Referenced by parse(), and setValue().

◆ setAnchor()

void inet::CounterFigure::setAnchor ( Anchor  anchor)
250 {
251  if (anchor != this->anchor) {
252  this->anchor = anchor;
253 
254  calculateBounds();
255  layout();
256  }
257 }

Referenced by parse().

◆ setBackgroundColor()

void inet::CounterFigure::setBackgroundColor ( const Color color)
53 {
54  backgroundFigure->setFillColor(color);
55 }

Referenced by parse().

◆ setDecimalPlaces()

void inet::CounterFigure::setDecimalPlaces ( int  radius)
63 {
64  ASSERT(number > 0);
65  if (digits.size() != (unsigned int)number) {
66  if (digits.size() > (unsigned int)number)
67  // Remove unnecessary figures from canvas
68  for (int i = digits.size() - 1; i > number - 1; --i) {
69  delete removeFigure(digits[i].bounds);
70  delete removeFigure(digits[i].text);
71  digits.pop_back();
72  }
73  else
74  // Add figure to canvas if it's necessary
75  while (digits.size() < (unsigned int)number) {
76  Digit digit(new cRectangleFigure(), new cTextFigure());
77  digit.bounds->setFilled(true);
78  digit.bounds->setFillColor(getDigitBackgroundColor());
79  digit.bounds->setZoomLineWidth(true);
80  digit.text->setAnchor(ANCHOR_CENTER);
81  digit.text->setFont(getDigitFont());
82 
83  addFigure(digit.bounds);
84  addFigure(digit.text);
85 
86  digits.push_back(digit);
87  }
88 
90  layout();
91  }
92 }

Referenced by parse().

◆ setDigitBackgroundColor()

void inet::CounterFigure::setDigitBackgroundColor ( const Color color)
100 {
101  for (Digit digit : digits)
102  digit.bounds->setFillColor(color);
103 }

Referenced by parse().

◆ setDigitBorderColor()

void inet::CounterFigure::setDigitBorderColor ( const Color color)
111 {
112  for (Digit digit : digits)
113  digit.bounds->setLineColor(color);
114 }

Referenced by parse().

◆ setDigitColor()

void inet::CounterFigure::setDigitColor ( const Color color)
136 {
137  for (Digit digit : digits)
138  digit.text->setColor(color);
139 }

Referenced by parse().

◆ setDigitFont()

void inet::CounterFigure::setDigitFont ( const Font &  font)
122 {
123  for (Digit digit : digits)
124  digit.text->setFont(font);
125 
126  calculateBounds();
127  layout();
128 }

Referenced by parse().

◆ setLabel()

void inet::CounterFigure::setLabel ( const char *  text)
147 {
148  labelFigure->setText(text);
149 }

Referenced by parse().

◆ setLabelAnchor()

void inet::CounterFigure::setLabelAnchor ( Anchor  anchor)

◆ setLabelColor()

void inet::CounterFigure::setLabelColor ( const Color color)
180 {
181  labelFigure->setColor(color);
182 }

Referenced by parse().

◆ setLabelFont()

void inet::CounterFigure::setLabelFont ( const Font &  font)
170 {
171  labelFigure->setFont(font);
172 }

Referenced by parse().

◆ setLabelOffset()

void inet::CounterFigure::setLabelOffset ( int  offset)
157 {
158  if (labelOffset != offset) {
159  labelOffset = offset;
160  labelFigure->setPosition(Point(backgroundFigure->getBounds().x + backgroundFigure->getBounds().width / 2, backgroundFigure->getBounds().y + backgroundFigure->getBounds().height + labelOffset));
161  }
162 }

Referenced by parse().

◆ setLabelPos()

void inet::CounterFigure::setLabelPos ( const Point pos)

◆ setPos()

void inet::CounterFigure::setPos ( const Point bounds)
234 {
235  Rectangle bounds = backgroundFigure->getBounds();
236  Point backgroundPos = calculateRealPos(pos);
237  bounds.x = backgroundPos.x;
238  bounds.y = backgroundPos.y;
239 
240  backgroundFigure->setBounds(bounds);
241  layout();
242 }

Referenced by parse().

◆ setValue()

void inet::CounterFigure::setValue ( int  series,
simtime_t  timestamp,
double  value 
)
overridevirtual

Implements inet::IIndicatorFigure.

412 {
413  ASSERT(series == 0);
414 
415  // Note: we currently ignore timestamp
416  if (value != newValue) {
417  value = newValue;
418  refresh();
419  }
420 }

Referenced by parse().

Member Data Documentation

◆ anchor

Anchor inet::CounterFigure::anchor = ANCHOR_NW
private

◆ backgroundFigure

cRectangleFigure* inet::CounterFigure::backgroundFigure
private

◆ digits

◆ labelFigure

cTextFigure* inet::CounterFigure::labelFigure
private

◆ labelOffset

int inet::CounterFigure::labelOffset = 10
private

◆ value

double inet::CounterFigure::value = NaN
private

Referenced by refresh(), and setValue().


The documentation for this class was generated from the following files:
inet::CounterFigure::refresh
void refresh()
Definition: CounterFigure.cc:422
inet::CounterFigure::calculateRealPos
Point calculateRealPos(const Point &pos)
Definition: CounterFigure.cc:260
inet::CounterFigure::setDigitBorderColor
void setDigitBorderColor(const Color &color)
Definition: CounterFigure.cc:110
inet::CounterFigure::setPos
void setPos(const Point &bounds)
Definition: CounterFigure.cc:233
inet::CounterFigure::setBackgroundColor
void setBackgroundColor(const Color &color)
Definition: CounterFigure.cc:52
inet::CounterFigure::setLabelOffset
void setLabelOffset(int offset)
Definition: CounterFigure.cc:156
inet::CounterFigure::setDigitFont
void setDigitFont(const Font &font)
Definition: CounterFigure.cc:121
inet::CounterFigure::setLabel
void setLabel(const char *text)
Definition: CounterFigure.cc:146
inet::units::values::s
value< double, units::s > s
Definition: Units.h:1235
inet::CounterFigure::backgroundFigure
cRectangleFigure * backgroundFigure
Definition: CounterFigure.h:25
inet::CounterFigure::getDigitFont
Font getDigitFont() const
Definition: CounterFigure.cc:116
inet::CounterFigure::anchor
Anchor anchor
Definition: CounterFigure.h:31
inet::utils::atod
double atod(const char *s)
Converts string to double.
Definition: INETUtils.cc:80
inet::CounterFigure::setValue
virtual void setValue(int series, simtime_t timestamp, double value) override
Definition: CounterFigure.cc:411
inet::CounterFigure::layout
void layout()
Definition: CounterFigure.cc:376
inet::CounterFigure::addChildren
void addChildren()
Definition: CounterFigure.cc:396
inet::keys
std::vector< K > keys(const std::map< K, V > &m)
Definition: stlutils.h:113
inet::CounterFigure::setDigitBackgroundColor
void setDigitBackgroundColor(const Color &color)
Definition: CounterFigure.cc:99
inet::CounterFigure::setDecimalPlaces
void setDecimalPlaces(int radius)
Definition: CounterFigure.cc:62
inet::CounterFigure::labelFigure
cTextFigure * labelFigure
Definition: CounterFigure.h:27
inet::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
inet::CounterFigure::getDigitBackgroundColor
Color getDigitBackgroundColor() const
Definition: CounterFigure.cc:94
inet::CounterFigure::value
double value
Definition: CounterFigure.h:29
inet::CounterFigure::setAnchor
void setAnchor(Anchor anchor)
Definition: CounterFigure.cc:249
inet::CounterFigure::labelOffset
int labelOffset
Definition: CounterFigure.h:30
inet::DiffservUtil::Color
Color
Definition: DiffservUtil.h:17
inet::CounterFigure::digits
std::vector< Digit > digits
Definition: CounterFigure.h:26
inet::CounterFigure::setLabelFont
void setLabelFont(const Font &font)
Definition: CounterFigure.cc:169
inet::CounterFigure::calculateBounds
void calculateBounds()
Definition: CounterFigure.cc:310
inet::CounterFigure::setLabelColor
void setLabelColor(const Color &color)
Definition: CounterFigure.cc:179
inet::CounterFigure::setDigitColor
void setDigitColor(const Color &color)
Definition: CounterFigure.cc:135