KiCad PCB EDA Suite
NUMBER_BADGE Class Reference

A simple UI element that puts a number on top of a colored rounded rectangle with a fill color that shows the severity of the reports the number is counting (e.g. More...

#include <number_badge.h>

Inheritance diagram for NUMBER_BADGE:

Public Member Functions

 NUMBER_BADGE (wxWindow *aParent, wxWindowID aId, const wxPoint &aPos, const wxSize &aSize, int aStyles)
 Create a number badge with 10pt font and a maximum number of 1000. More...
 
void UpdateNumber (int aNumber, SEVERITY aSeverity)
 Update the number displayed on the badge. More...
 
void SetMaximumNumber (int aMax)
 Set the maximum number to be shown on the badge. More...
 
void SetTextSize (int aSize)
 Set the text size to use on the badge. More...
 

Protected Member Functions

void computeSize ()
 Helper function to compute the size of the badge. More...
 
void onPaint (wxPaintEvent &aEvt)
 Handler that actually paints the badge and the text. More...
 

Protected Attributes

int m_textSize
 
int m_maxNumber
 
int m_currentNumber
 
bool m_showBadge
 
wxColour m_badgeColour
 
wxColour m_textColour
 

Detailed Description

A simple UI element that puts a number on top of a colored rounded rectangle with a fill color that shows the severity of the reports the number is counting (e.g.

green, yellow, red). This badge will also automatically truncate the displayed number to the set maximum and display "+" at the end to represent it is truncated.

Definition at line 38 of file number_badge.h.

Constructor & Destructor Documentation

◆ NUMBER_BADGE()

NUMBER_BADGE::NUMBER_BADGE ( wxWindow *  aParent,
wxWindowID  aId,
const wxPoint &  aPos,
const wxSize &  aSize,
int  aStyles 
)

Create a number badge with 10pt font and a maximum number of 1000.

Definition at line 30 of file number_badge.cpp.

31  :
32  wxPanel( aParent, aId, aPos, aSize, aStyles ),
33  m_textSize( 10 ),
34  m_maxNumber( 1000 ),
35  m_currentNumber( 0 ),
36  m_showBadge( false )
37 {
38  computeSize();
39  Bind( wxEVT_PAINT, &NUMBER_BADGE::onPaint, this );
40 }
void computeSize()
Helper function to compute the size of the badge.
void onPaint(wxPaintEvent &aEvt)
Handler that actually paints the badge and the text.
int m_currentNumber
Definition: number_badge.h:91
bool m_showBadge
Definition: number_badge.h:92

References computeSize(), and onPaint().

Member Function Documentation

◆ computeSize()

void NUMBER_BADGE::computeSize ( )
protected

Helper function to compute the size of the badge.

Definition at line 132 of file number_badge.cpp.

133 {
134  wxClientDC dc( this );
135 
136  wxString test = wxString::Format( wxT( "%d" ), m_maxNumber );
137  int len = test.length();
138 
139  // Determine the size using the string "-999+" where the - on the front is for spacing from
140  // the start of the rectangle so the number isn't close to the curved edge.
141  test = "-";
142  test.Pad( len, '9' );
143  test += "+";
144 
145  dc.SetFont( wxFont( m_textSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, BADGE_FONTWEIGHT ) );
146  wxSize size = dc.GetTextExtent( test );
147 
148  size.y *= PLATFORM_FUDGE_Y;
149  size.x = std::max<int>( size.x * PLATFORM_FUDGE_X, size.y );
150 
151  SetMinSize( size );
152  SetSize( size );
153 }
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References Format(), m_maxNumber, and m_textSize.

Referenced by NUMBER_BADGE(), SetMaximumNumber(), and SetTextSize().

◆ onPaint()

void NUMBER_BADGE::onPaint ( wxPaintEvent &  aEvt)
protected

Handler that actually paints the badge and the text.

Definition at line 156 of file number_badge.cpp.

157 {
158  // The drawing rectangle
159  wxSize clientSize = GetSize();
160  wxPaintDC dc( this );
161  wxString text;
162  wxBrush brush;
163 
164  // Give the badge a transparent background to show the panel underneath
165  dc.SetBackground( *wxTRANSPARENT_BRUSH );
166  dc.Clear();
167 
168  // We always draw a transparent background, but only draw the badge when it is needed
169  if( !m_showBadge )
170  return;
171 
172  // The rectangle the color is drawn in needs to be shrunk by 1px on each axis because for some
173  // reason it seems to be padded out by 1px and is cutoff otherwise.
174  wxRect rect( wxPoint( 0, 0 ), clientSize - wxSize( 1, 1 ) );
175 
176  brush.SetStyle( wxBRUSHSTYLE_SOLID );
177  brush.SetColour( m_badgeColour );
178  dc.SetBrush( brush );
179  dc.SetPen( wxPen( m_badgeColour, 0 ) );
180  dc.DrawRoundedRectangle( rect, rect.height / 2 );
181 
182  // Cap the number displayed and add the "+" to the end if required
184  text = wxString::Format( wxT( "%d+" ), m_maxNumber );
185  else
186  text = wxString::Format( wxT( "%d" ), m_currentNumber );
187 
188  dc.SetFont( wxFont( m_textSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, BADGE_FONTWEIGHT ) );
189  dc.SetTextForeground( m_textColour );
190  dc.DrawLabel( text, wxRect( wxPoint( 0, 0 ), clientSize ), wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
191 }
int m_currentNumber
Definition: number_badge.h:91
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxColour m_textColour
Definition: number_badge.h:94
wxColour m_badgeColour
Definition: number_badge.h:93
bool m_showBadge
Definition: number_badge.h:92

References Format(), m_badgeColour, m_currentNumber, m_maxNumber, m_showBadge, m_textColour, m_textSize, and text.

Referenced by NUMBER_BADGE().

◆ SetMaximumNumber()

void NUMBER_BADGE::SetMaximumNumber ( int  aMax)

Set the maximum number to be shown on the badge.

Any numbers greater than this will be displayed as the maximum number followed by "+".

Parameters
aMaxis the maximum number

Definition at line 98 of file number_badge.cpp.

99 {
100  m_maxNumber = aMax;
101  computeSize();
102 }
void computeSize()
Helper function to compute the size of the badge.

References computeSize(), and m_maxNumber.

Referenced by DIALOG_ERC::DIALOG_ERC(), DIALOG_FOOTPRINT_CHECKER::updateDisplayedCounts(), and DIALOG_DRC::updateDisplayedCounts().

◆ SetTextSize()

void NUMBER_BADGE::SetTextSize ( int  aSize)

Set the text size to use on the badge.

Parameters
aSizeis the text size (in pt) to use on the badge

Definition at line 105 of file number_badge.cpp.

106 {
107  m_textSize = aSize;
108  computeSize();
109 }
void computeSize()
Helper function to compute the size of the badge.

References computeSize(), and m_textSize.

◆ UpdateNumber()

void NUMBER_BADGE::UpdateNumber ( int  aNumber,
SEVERITY  aSeverity 
)

Update the number displayed on the badge.

Severity to badge color mapping:

  • RPT_SEVERITY_ERROR = red badge
  • RPT_SEVERITY_WARNING = yellow badge
  • RPT_SEVERITY_ACTION = green badge
  • RPT_SEVERITY_EXCLUSION = light grey badge
  • RPT_SEVERITY_INFO = light grey badge
Parameters
aNumberis the new number to display.
aSeverityis the new severity of the badge.

Definition at line 43 of file number_badge.cpp.

44 {
45  m_showBadge = true;
46  m_currentNumber = aNumber;
47 
48  // Choose the colors of the badge rectangle and font
49  if( aNumber < 0 )
50  {
51  m_showBadge = false;
52  }
53  else if( aNumber == 0 )
54  {
55  if( aSeverity == RPT_SEVERITY_ERROR || aSeverity == RPT_SEVERITY_WARNING )
56  {
57  m_badgeColour = KIGFX::COLOR4D( GREEN ).ToColour();
58  m_textColour = *wxWHITE;
59  }
60  else
61  {
62  m_showBadge = false;
63  }
64  }
65  else
66  {
67  switch( aSeverity )
68  {
69  case RPT_SEVERITY_ERROR:
70  m_badgeColour = *wxRED;
71  m_textColour = *wxWHITE;
72  break;
73 
75  m_badgeColour = *wxYELLOW;
76  m_textColour = *wxBLACK;
77  break;
78 
80  m_badgeColour = KIGFX::COLOR4D( GREEN ).ToColour();
81  m_textColour = *wxWHITE;
82  break;
83 
85  case RPT_SEVERITY_INFO:
86  default:
87  m_badgeColour = *wxLIGHT_GREY;
88  m_textColour = *wxBLACK;
89  break;
90  }
91  }
92 
93  // Force the badge UI to refresh so the new number and color is displayed
94  Refresh();
95 }
int m_currentNumber
Definition: number_badge.h:91
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
Definition: color4d.h:57
wxColour m_textColour
Definition: number_badge.h:94
wxColour m_badgeColour
Definition: number_badge.h:93
bool m_showBadge
Definition: number_badge.h:92
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98

References GREEN, m_badgeColour, m_currentNumber, m_showBadge, m_textColour, Refresh(), RPT_SEVERITY_ACTION, RPT_SEVERITY_ERROR, RPT_SEVERITY_EXCLUSION, RPT_SEVERITY_INFO, and RPT_SEVERITY_WARNING.

Referenced by WX_HTML_REPORT_PANEL::updateBadges(), DIALOG_FOOTPRINT_CHECKER::updateDisplayedCounts(), DIALOG_DRC::updateDisplayedCounts(), and DIALOG_ERC::updateDisplayedCounts().

Member Data Documentation

◆ m_badgeColour

wxColour NUMBER_BADGE::m_badgeColour
protected

Definition at line 93 of file number_badge.h.

Referenced by onPaint(), and UpdateNumber().

◆ m_currentNumber

int NUMBER_BADGE::m_currentNumber
protected

Definition at line 91 of file number_badge.h.

Referenced by onPaint(), and UpdateNumber().

◆ m_maxNumber

int NUMBER_BADGE::m_maxNumber
protected

Definition at line 89 of file number_badge.h.

Referenced by computeSize(), onPaint(), and SetMaximumNumber().

◆ m_showBadge

bool NUMBER_BADGE::m_showBadge
protected

Definition at line 92 of file number_badge.h.

Referenced by onPaint(), and UpdateNumber().

◆ m_textColour

wxColour NUMBER_BADGE::m_textColour
protected

Definition at line 94 of file number_badge.h.

Referenced by onPaint(), and UpdateNumber().

◆ m_textSize

int NUMBER_BADGE::m_textSize
protected

Definition at line 88 of file number_badge.h.

Referenced by computeSize(), onPaint(), and SetTextSize().


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