KiCad PCB EDA Suite
Loading...
Searching...
No Matches
number_badge.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <gal/color4d.h>
22#include <fmt/format.h>
23#include <algorithm>
24#include <kiplatform/ui.h>
25
26NUMBER_BADGE::NUMBER_BADGE( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
27 const wxSize& aSize, int aStyles ) :
28 wxPanel( aParent, aId, aPos, aSize, aStyles ),
29 m_textSize( 10 ),
30 m_maxNumber( 1000 ),
31 m_currentNumber( 0 ),
32 m_showBadge( false )
33{
35 Bind( wxEVT_PAINT, &NUMBER_BADGE::onPaint, this );
36}
37
38
39void NUMBER_BADGE::UpdateNumber( int aNumber, SEVERITY aSeverity )
40{
41 m_showBadge = true;
42 m_currentNumber = aNumber;
43
44 // Choose the colors of the badge rectangle and font
45 if( aNumber < 0 )
46 {
47 m_showBadge = false;
48 }
49 else if( aNumber == 0 )
50 {
51 if( aSeverity == RPT_SEVERITY_ERROR || aSeverity == RPT_SEVERITY_WARNING )
52 {
54 m_textColour = *wxWHITE;
55 }
56 else
57 {
58 m_showBadge = false;
59 }
60 }
61 else
62 {
63 switch( aSeverity )
64 {
66 m_badgeColour = KIPLATFORM::UI::IsDarkTheme() ? wxColour( 240, 64, 64 ) : *wxRED;
67 m_textColour = *wxWHITE;
68 break;
69
71 m_badgeColour = *wxYELLOW;
72 m_textColour = *wxBLACK;
73 break;
74
77 m_textColour = *wxWHITE;
78 break;
79
82 default:
83 m_badgeColour = *wxLIGHT_GREY;
84 m_textColour = *wxBLACK;
85 break;
86 }
87 }
88
90
91 // Force the badge UI to refresh so the new number and color is displayed
92 Refresh();
93}
94
95
97{
98 m_maxNumber = aMax;
99}
100
101
103{
104 m_textSize = aSize;
105 computeSize();
106}
107
108
109// OSX has prevalent badges in the application bar at the bottom of the screen so we try to
110// match those. Other platforms may also need tweaks to spacing, fontweight, etc.
111#if defined( __WXMAC__ )
112#define BADGE_FONTWEIGHT wxFONTWEIGHT_NORMAL
113#define PLATFORM_FUDGE_X 0.92
114#define PLATFORM_FUDGE_Y 1.6
115#else
116#define BADGE_FONTWEIGHT wxFONTWEIGHT_BOLD
117#define PLATFORM_FUDGE_X 1.0
118#define PLATFORM_FUDGE_Y 1.0
119#endif
120
121
123{
124 wxClientDC dc( this );
125
126 wxString test = fmt::format( "{}", m_currentNumber );
127 int len = test.length();
128
129 // Determine the size using the string "m999{+}" where the 'm' on the front serves as a margin
130 // so the number isn't too close to the curved edge.
131 test = wxS( "m" );
132 test.Pad( len, '9' );
133
135 test += wxS( "+" );
136
137 dc.SetFont( wxFont( m_textSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, BADGE_FONTWEIGHT ) );
138 wxSize size = dc.GetTextExtent( test );
139
140 size.y *= PLATFORM_FUDGE_Y;
141 size.x = std::max<int>( size.x * PLATFORM_FUDGE_X, size.y );
142
143 SetMinSize( size );
144 SetSize( size );
145}
146
147
148void NUMBER_BADGE::onPaint( wxPaintEvent& aEvt )
149{
150 // The drawing rectangle
151 wxSize clientSize = GetSize();
152 wxPaintDC dc( this );
153 wxString text;
154 wxBrush brush;
155
156 // Give the badge a transparent background to show the panel underneath
157 dc.SetBackground( *wxTRANSPARENT_BRUSH );
158 dc.Clear();
159
160 // We always draw a transparent background, but only draw the badge when it is needed
161 if( !m_showBadge )
162 return;
163
164 // The rectangle the color is drawn in needs to be shrunk by 1px on each axis because for some
165 // reason it seems to be padded out by 1px and is cutoff otherwise.
166 wxRect rect( wxPoint( 0, 0 ), clientSize - wxSize( 1, 1 ) );
167
168 brush.SetStyle( wxBRUSHSTYLE_SOLID );
169 brush.SetColour( m_badgeColour );
170 dc.SetBrush( brush );
171 dc.SetPen( wxPen( m_badgeColour, 0 ) );
172 dc.DrawRoundedRectangle( rect, rect.height / 2 );
173
174 // Cap the number displayed and add the "+" to the end if required
176 text = fmt::format( "{}+", m_maxNumber );
177 else
178 text = fmt::format( "{}", m_currentNumber );
179
180 dc.SetFont( wxFont( m_textSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, BADGE_FONTWEIGHT ) );
181 dc.SetTextForeground( m_textColour );
182 dc.DrawLabel( text, wxRect( wxPoint( 0, 0 ), clientSize ),
183 wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
184}
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxColour ToColour() const
Definition color4d.cpp:221
void SetMaximumNumber(int aMax)
Set the maximum number to be shown on the badge.
wxColour m_badgeColour
void UpdateNumber(int aNumber, SEVERITY aSeverity)
Update the number displayed on the badge.
void computeSize()
Helper function to compute the size of the badge.
void SetTextSize(int aSize)
Set the text size to use on the badge.
wxColour m_textColour
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.
void onPaint(wxPaintEvent &aEvt)
Handler that actually paints the badge and the text.
@ GREEN
Definition color4d.h:53
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:50
#define BADGE_FONTWEIGHT
#define PLATFORM_FUDGE_X
#define PLATFORM_FUDGE_Y
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION