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 (C) 2020 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <gal/color4d.h>
26
27#include <algorithm>
28#include <kiplatform/ui.h>
29
30NUMBER_BADGE::NUMBER_BADGE( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
31 const wxSize& aSize, int aStyles ) :
32 wxPanel( aParent, aId, aPos, aSize, aStyles ),
33 m_textSize( 10 ),
34 m_maxNumber( 1000 ),
35 m_currentNumber( 0 ),
36 m_showBadge( false )
37{
39 Bind( wxEVT_PAINT, &NUMBER_BADGE::onPaint, this );
40}
41
42
43void NUMBER_BADGE::UpdateNumber( int aNumber, SEVERITY aSeverity )
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 {
58 m_textColour = *wxWHITE;
59 }
60 else
61 {
62 m_showBadge = false;
63 }
64 }
65 else
66 {
67 switch( aSeverity )
68 {
70 m_badgeColour = KIPLATFORM::UI::IsDarkTheme() ? wxColour( 240, 64, 64 ) : *wxRED;
71 m_textColour = *wxWHITE;
72 break;
73
75 m_badgeColour = *wxYELLOW;
76 m_textColour = *wxBLACK;
77 break;
78
81 m_textColour = *wxWHITE;
82 break;
83
86 default:
87 m_badgeColour = *wxLIGHT_GREY;
88 m_textColour = *wxBLACK;
89 break;
90 }
91 }
92
94
95 // Force the badge UI to refresh so the new number and color is displayed
96 Refresh();
97}
98
99
101{
102 m_maxNumber = aMax;
103}
104
105
107{
108 m_textSize = aSize;
109 computeSize();
110}
111
112
113// OSX has prevalent badges in the application bar at the bottom of the screen so we try to
114// match those. Other platforms may also need tweaks to spacing, fontweight, etc.
115#if defined( __WXMAC__ )
116#define BADGE_FONTWEIGHT wxFONTWEIGHT_NORMAL
117#define PLATFORM_FUDGE_X 0.92
118#define PLATFORM_FUDGE_Y 1.6
119#else
120#define BADGE_FONTWEIGHT wxFONTWEIGHT_BOLD
121#define PLATFORM_FUDGE_X 1.0
122#define PLATFORM_FUDGE_Y 1.0
123#endif
124
126{
127 wxClientDC dc( this );
128
129 wxString test = wxString::Format( wxT( "%d" ), m_currentNumber );
130 int len = test.length();
131
132 // Determine the size using the string "m999{+}" where the 'm' on the front serves as a margin
133 // so the number isn't too close to the curved edge.
134 test = wxS( "m" );
135 test.Pad( len, '9' );
136
138 test += wxS( "+" );
139
140 dc.SetFont( wxFont( m_textSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, BADGE_FONTWEIGHT ) );
141 wxSize size = dc.GetTextExtent( test );
142
143 size.y *= PLATFORM_FUDGE_Y;
144 size.x = std::max<int>( size.x * PLATFORM_FUDGE_X, size.y );
145
146 SetMinSize( size );
147 SetSize( size );
148}
149
150
151void NUMBER_BADGE::onPaint( wxPaintEvent& aEvt )
152{
153 // The drawing rectangle
154 wxSize clientSize = GetSize();
155 wxPaintDC dc( this );
156 wxString text;
157 wxBrush brush;
158
159 // Give the badge a transparent background to show the panel underneath
160 dc.SetBackground( *wxTRANSPARENT_BRUSH );
161 dc.Clear();
162
163 // We always draw a transparent background, but only draw the badge when it is needed
164 if( !m_showBadge )
165 return;
166
167 // The rectangle the color is drawn in needs to be shrunk by 1px on each axis because for some
168 // reason it seems to be padded out by 1px and is cutoff otherwise.
169 wxRect rect( wxPoint( 0, 0 ), clientSize - wxSize( 1, 1 ) );
170
171 brush.SetStyle( wxBRUSHSTYLE_SOLID );
172 brush.SetColour( m_badgeColour );
173 dc.SetBrush( brush );
174 dc.SetPen( wxPen( m_badgeColour, 0 ) );
175 dc.DrawRoundedRectangle( rect, rect.height / 2 );
176
177 // Cap the number displayed and add the "+" to the end if required
179 text = wxString::Format( wxT( "%d+" ), m_maxNumber );
180 else
181 text = wxString::Format( wxT( "%d" ), m_currentNumber );
182
183 dc.SetFont( wxFont( m_textSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, BADGE_FONTWEIGHT ) );
184 dc.SetTextForeground( m_textColour );
185 dc.DrawLabel( text, wxRect( wxPoint( 0, 0 ), clientSize ), wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
186}
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
wxColour ToColour() const
Definition: color4d.cpp:220
void SetMaximumNumber(int aMax)
Set the maximum number to be shown on the badge.
wxColour m_badgeColour
Definition: number_badge.h:93
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.
int m_currentNumber
Definition: number_badge.h:91
wxColour m_textColour
Definition: number_badge.h:94
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.
bool m_showBadge
Definition: number_badge.h:92
void onPaint(wxPaintEvent &aEvt)
Handler that actually paints the badge and the text.
@ GREEN
Definition: color4d.h:57
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: wxgtk/ui.cpp:48
#define BADGE_FONTWEIGHT
#define PLATFORM_FUDGE_X
#define PLATFORM_FUDGE_Y
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION