KiCad PCB EDA Suite
ui_common.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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <wx/wx.h>
21 #include <widgets/ui_common.h>
22 
23 #include <algorithm>
24 
25 
27 {
28  // This is the value used in (most) wxFB dialogs
29  return 5;
30 }
31 
32 
33 #define BADGE_SIZE 20
34 #define BADGE_FONT_SIZE 10
35 
36 wxBitmap MakeBadge( SEVERITY aStyle, int aCount, wxWindow* aWindow, int aDepth )
37 {
38  wxSize size( BADGE_SIZE, BADGE_SIZE );
39  wxBitmap bitmap( size );
40  wxBrush brush;
41  wxMemoryDC badgeDC;
42  wxColour badgeColour;
43  wxColour textColour;
44  wxColour backColour;
45  int fontSize = BADGE_FONT_SIZE;
46 
47  if( aCount > 99 )
48  fontSize--;
49 
50  badgeDC.SelectObject( bitmap );
51 
52  brush.SetStyle( wxBRUSHSTYLE_SOLID );
53 
54  backColour = aWindow->GetParent()->GetBackgroundColour();
55 
56  // Each level inside staticBoxes is darkened by 210
57  for( int i = 1; i < aDepth; ++i )
58  backColour = backColour.MakeDisabled( 210 );
59 
60  brush.SetColour( backColour );
61  badgeDC.SetBackground( brush );
62  badgeDC.Clear();
63 
64  if( aCount == 0 )
65  return bitmap;
66 
67  switch( aStyle )
68  {
69  case RPT_SEVERITY_ERROR:
70  badgeColour = *wxRED;
71  textColour = *wxWHITE;
72  break;
74  badgeColour = *wxYELLOW;
75  textColour = *wxBLACK;
76  break;
78  badgeColour = *wxGREEN;
79  textColour = *wxWHITE;
80  break;
82  case RPT_SEVERITY_INFO:
83  default:
84  badgeColour = *wxLIGHT_GREY;
85  textColour = *wxBLACK;
86  break;
87  }
88 
89  brush.SetStyle( wxBRUSHSTYLE_SOLID );
90  brush.SetColour( badgeColour );
91  badgeDC.SetBrush( brush );
92  badgeDC.SetPen( wxPen( badgeColour, 0 ) );
93  badgeDC.DrawCircle( size.x / 2 - 1, size.y / 2, ( std::max( size.x, size.y ) / 2 ) - 1 );
94 
95  wxFont font( fontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
96  badgeDC.SetFont( font );
97 
98  wxString text = wxString::Format( wxT( "%d" ), aCount );
99  wxSize textExtent = badgeDC.GetTextExtent( text );
100 
101  badgeDC.SetTextForeground( textColour );
102  badgeDC.DrawText( text, size.x / 2 - textExtent.x / 2 - 1, size.y / 2 - textExtent.y / 2 );
103 
104  return bitmap;
105 }
106 
107 
108 SEVERITY SeverityFromString( const wxString& aSeverity )
109 {
110  if( aSeverity == wxT( "warning" ) )
111  return RPT_SEVERITY_WARNING;
112  else if( aSeverity == wxT( "ignore" ) )
113  return RPT_SEVERITY_IGNORE;
114  else
115  return RPT_SEVERITY_ERROR;
116 }
117 
118 
119 wxString SeverityToString( const SEVERITY& aSeverity )
120 {
121  if( aSeverity == RPT_SEVERITY_IGNORE )
122  return wxT( "ignore" );
123  else if( aSeverity == RPT_SEVERITY_WARNING )
124  return wxT( "warning" );
125  else
126  return wxT( "error" );
127 }
#define BADGE_SIZE
Definition: ui_common.cpp:33
SEVERITY
Definition: ui_common.h:45
int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition: ui_common.cpp:26
#define BADGE_FONT_SIZE
Definition: ui_common.cpp:34
Functions to provide common constants and other functions to assist in making a consistent UI.
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:119
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxBitmap MakeBadge(SEVERITY aStyle, int aCount, wxWindow *aWindow, int aDepth)
Definition: ui_common.cpp:36
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:108