KiCad PCB EDA Suite
grid_menu.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) 2015 CERN
5  * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <tool/grid_menu.h>
27 #include <id.h>
28 #include <eda_draw_frame.h>
29 #include <settings/app_settings.h>
30 #include <tool/actions.h>
31 #include <bitmaps.h>
32 #include <base_units.h>
33 
34 using namespace std::placeholders;
35 
37  ACTION_MENU( true ),
38  m_parent( aParent )
39 {
40  UpdateTitle();
42 
43  APP_SETTINGS_BASE* settings = m_parent->config();
44  wxArrayString gridsList;
45  int i = ID_POPUP_GRID_START;
46 
47  BuildChoiceList( &gridsList, settings, m_parent );
48 
49  for( const wxString& grid : gridsList )
50  Append( i++, grid, wxEmptyString, wxITEM_CHECK );
51 }
52 
53 
54 OPT_TOOL_EVENT GRID_MENU::eventHandler( const wxMenuEvent& aEvent )
55 {
56  OPT_TOOL_EVENT event( ACTIONS::gridPreset.MakeEvent() );
57  event->SetParameter( (intptr_t) aEvent.GetId() - ID_POPUP_GRID_START );
58  return event;
59 }
60 
61 
63 {
64  SetTitle( _( "Grid" ) );
65 }
66 
67 
69 {
70  APP_SETTINGS_BASE* settings = m_parent->config();
71  unsigned int current = settings->m_Window.grid.last_size_idx;
72  wxArrayString gridsList;
73 
74  GRID_MENU::BuildChoiceList( &gridsList, settings, m_parent );
75 
76  for( unsigned int i = 0; i < GetMenuItemCount(); ++i )
77  {
78  wxMenuItem* menuItem = FindItemByPosition( i );
79 
80  menuItem->SetItemLabel( gridsList[ i ] ); // Refresh label in case units have changed
81  menuItem->Check( i == current ); // Refresh checkmark
82  }
83 }
84 
85 void GRID_MENU::BuildChoiceList( wxArrayString* aGridsList, APP_SETTINGS_BASE* aCfg,
86  EDA_DRAW_FRAME* aParent )
87 {
88  wxString msg;
89 
90  EDA_UNITS primaryUnit;
91  EDA_UNITS secondaryUnit;
92 
93  aParent->GetUnitPair( primaryUnit, secondaryUnit );
94 
95  for( const wxString& gridSize : aCfg->m_Window.grid.sizes )
96  {
97  int val = (int) ValueFromString( EDA_UNITS::MILLIMETRES, gridSize );
98 
99  msg.Printf( _( "Grid: %s (%s)" ),
100  MessageTextFromValue( primaryUnit, val ),
101  MessageTextFromValue( secondaryUnit, val ) );
102 
103  aGridsList->Add( msg );
104  }
105 
106  if( !aCfg->m_Window.grid.user_grid_x.empty() )
107  {
108  int val = (int) ValueFromString( EDA_UNITS::INCHES, aCfg->m_Window.grid.user_grid_x );
109 
110  msg.Printf( _( "User grid: %s (%s)" ),
111  MessageTextFromValue( primaryUnit, val ),
112  MessageTextFromValue( secondaryUnit, val ) );
113 
114  aGridsList->Add( msg );
115  }
116 }
void UpdateTitle() override
Used by some menus to just-in-time translate their titles.
Definition: grid_menu.cpp:62
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
wxString user_grid_x
Definition: app_settings.h:53
Implementation of conversion functions that require both schematic and board internal units.
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
Definition: action_menu.cpp:73
OPT_TOOL_EVENT eventHandler(const wxMenuEvent &aEvent) override
Event handler stub.
Definition: grid_menu.cpp:54
The base class for create windows for drawing purpose.
std::vector< wxString > sizes
Definition: app_settings.h:52
EDA_DRAW_FRAME * m_parent
Definition: grid_menu.h:52
static TOOL_ACTION gridPreset
Definition: actions.h:139
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:411
GRID_SETTINGS grid
Definition: app_settings.h:89
#define _(s)
EDA_UNITS
Definition: eda_units.h:38
WINDOW_SETTINGS m_Window
Definition: app_settings.h:181
void SetTitle(const wxString &aTitle) override
Set title for the menu.
Definition: action_menu.cpp:87
static void BuildChoiceList(wxArrayString *aGridsList, APP_SETTINGS_BASE *aCfg, EDA_DRAW_FRAME *aParent)
Definition: grid_menu.cpp:85
void update() override
Update menu state stub.
Definition: grid_menu.cpp:68
void GetUnitPair(EDA_UNITS &aPrimaryUnit, EDA_UNITS &aSecondaryUnits)
Get the pair or units in current use.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:548
GRID_MENU(EDA_DRAW_FRAME *aParent)
Definition: grid_menu.cpp:36