KiCad PCB EDA Suite
panel_package.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) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <wx/dcclient.h>
22 #include <math/util.h>
23 
24 #include "panel_package.h"
25 
26 PANEL_PACKAGE::PANEL_PACKAGE( wxWindow* parent, const ActionCallback& aCallback,
27  const PACKAGE_VIEW_DATA& aData ) :
28  PANEL_PACKAGE_BASE( parent ),
29  m_actionCallback( aCallback ),
30  m_data( aData )
31 {
32  // Propagate clicks on static elements to the panel handler.
33  m_name->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( PANEL_PACKAGE::OnClick ), NULL, this );
34  m_desc->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( PANEL_PACKAGE::OnClick ), NULL, this );
35  m_bitmap->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( PANEL_PACKAGE::OnClick ), NULL, this );
36 
37  wxColour bgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
38  m_desc->SetBackgroundColour( bgColor );
39  m_name->SetBackgroundColour( bgColor );
40  m_bitmap->SetBackgroundColour( bgColor );
41 
42  m_name->SetFont( m_name->GetFont().Bold() );
43 
44  m_name->SetLabel( m_data.package.name );
45  m_bitmap->SetBitmap( *m_data.bitmap );
46 
47  // Set min width to 0 otherwise wxStaticLabel really doesn't want to shrink on resize
48  m_desc->SetMinSize( wxSize( 0, -1 ) );
49 
50  m_minHeight = GetMinHeight();
51 
52  wxSizeEvent dummy;
53  OnSize( dummy );
54 
56 }
57 
58 
59 void PANEL_PACKAGE::OnSize( wxSizeEvent& event )
60 {
61  Layout();
62 
63  int nameLineHeight = m_name->GetTextExtent( "X" ).GetHeight();
64  double descLineHeight = m_desc->GetTextExtent( "X" ).GetHeight() * 1.2 /* leading */;
65 
66  m_desc->SetLabel( m_data.package.description );
67  m_desc->Wrap( m_desc->GetClientSize().GetWidth() - 10 );
68  descLineHeight = wxSplit( m_desc->GetLabel(), '\n' ).size() * descLineHeight;
69 
70  wxSize minSize = GetMinSize();
71  minSize.y = std::max( nameLineHeight + KiROUND( descLineHeight ) + 15, m_minHeight );
72  SetMinSize( minSize );
73 
74  Layout();
75 }
76 
77 
79 {
80  m_data.state = aState;
81 
82  switch( aState )
83  {
85  m_button->SetLabel( _( "Install" ) );
86  m_button->Enable();
87  break;
89  m_button->SetLabel( _( "Install" ) );
90  m_button->Disable();
91  break;
93  m_button->SetLabel( _( "Uninstall" ) );
94  m_button->Enable();
95  break;
97  m_button->SetLabel( _( "Pending install" ) );
98  m_button->Disable();
99  break;
101  m_button->SetLabel( _( "Pending uninstall" ) );
102  m_button->Disable();
103  break;
104  }
105 
106  // Relayout to change button size to fit the label.
107  wxSizeEvent dummy;
108  OnSize( dummy );
109 }
110 
111 
112 void PANEL_PACKAGE::OnButtonClicked( wxCommandEvent& event )
113 {
114  if( m_data.state == PPS_AVAILABLE )
115  {
116  wxString version = GetPreferredVersion();
117 
118  if( version.IsEmpty() )
119  return;
120 
121  m_actionCallback( m_data, PPA_INSTALL, version );
122  }
123  else
124  {
126  }
127 }
128 
129 
130 void PANEL_PACKAGE::SetSelectCallback( const std::function<void()>& aCallback )
131 {
132  m_selectCallback = aCallback;
133 }
134 
135 
136 void PANEL_PACKAGE::OnClick( wxMouseEvent& event )
137 {
139 }
140 
141 
142 void PANEL_PACKAGE::OnPaint( wxPaintEvent& event )
143 {
144  wxRect rect( wxPoint( 1, 1 ), GetClientSize() - wxSize( 1, 1 ) );
145  wxPaintDC dc( this );
146  dc.SetBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK ) );
147  dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER ), 1 ) );
148 
149  if( m_selected )
150  {
151  rect.Deflate( 1 );
152  dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT ), 3 ) );
153  }
154 
155  dc.DrawRectangle( rect );
156 }
157 
158 
159 void PANEL_PACKAGE::SetSelected( bool aSelected )
160 {
161  m_selected = aSelected;
162  Refresh();
163 }
164 
165 
167 {
168  // Versions are already presorted in descending order
169 
170  // Find last stable compatible version
171  auto ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(),
172  []( const PACKAGE_VERSION& ver )
173  {
174  return ver.compatible && ver.status == PVS_STABLE;
175  } );
176 
177  // If not found then find any compatible version
178  if( ver_it == m_data.package.versions.end() )
179  {
180  ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(),
181  []( const PACKAGE_VERSION& ver )
182  {
183  return ver.compatible;
184  } );
185  }
186 
187  if( ver_it == m_data.package.versions.end() )
188  return wxEmptyString; // Shouldn't happen
189 
190  return ver_it->version;
191 }
192 
193 
< Package version metadata Package metadata
Definition: pcm_data.h:73
wxStaticBitmap * m_bitmap
void OnPaint(wxPaintEvent &event) override
std::function< void()> m_selectCallback
Definition: panel_package.h:87
void SetState(PCM_PACKAGE_STATE aState)
Called when anywhere on the panel is clicked (except install button)
wxString description
Definition: pcm_data.h:96
const int minSize
Push and Shove router track width and via size dialog.
const PCM_PACKAGE package
Definition: panel_package.h:32
const ActionCallback & m_actionCallback
Definition: panel_package.h:89
< Collection of data relevant to the package display panel Callback for (un)install button
Definition: panel_package.h:30
wxStaticText * m_name
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
void SetSelected(bool aSelected)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
#define _(s)
wxBitmap * bitmap
Definition: panel_package.h:33
wxString name
Definition: pcm_data.h:95
void OnClick(wxMouseEvent &event) override
void SetSelectCallback(const std::function< void()> &aCallback)
Marks panel as selected.
std::vector< PACKAGE_VERSION > versions
Definition: pcm_data.h:105
void OnButtonClicked(wxCommandEvent &event) override
Changes state of the (un)install button.
PCM_PACKAGE_STATE
Definition: pcm.h:52
Class PANEL_PACKAGE_BASE.
PCM_PACKAGE_STATE state
Definition: panel_package.h:34
void OnSize(wxSizeEvent &event) override
Get preferred version. If criteria are not met, return wxEmptyString.
wxString current_version
Definition: panel_package.h:37
PACKAGE_VIEW_DATA m_data
Definition: panel_package.h:90
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
std::function< void(const PACKAGE_VIEW_DATA &aData, PCM_PACKAGE_ACTION aAction, const wxString &aVersion)> ActionCallback
Definition: panel_package.h:53
PANEL_PACKAGE(wxWindow *parent, const ActionCallback &aCallback, const PACKAGE_VIEW_DATA &aData)
Sets callback for OnClick action.
wxString GetPreferredVersion() const
wxStaticText * m_desc