KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_design_block_properties.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) 2024 Mike Williams <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
27#include <wx/msgdlg.h>
28#include <wx/tooltip.h>
29#include <wx/wupdlock.h>
30#include <grid_tricks.h>
32#include <bitmaps.h>
33
34#include <design_block.h>
35
37 DESIGN_BLOCK* aDesignBlock,
38 bool aDisableName ) :
39 DIALOG_DESIGN_BLOCK_PROPERTIES_BASE( aParent ), m_designBlock( aDesignBlock )
40{
41 m_textName->SetEditable( !aDisableName );
42
43 wxToolTip::Enable( true );
45
46 // Configure button logos
47 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
48 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
49 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
50 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
51
52 m_fieldsGrid->SetUseNativeColLabels();
53
54 m_fieldsGrid->PushEventHandler( new GRID_TRICKS( m_fieldsGrid,
55 [this]( wxCommandEvent& aEvent )
56 {
57 OnAddField( aEvent );
58 } ) );
59 m_fieldsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
60}
61
62
64{
65 // Delete the GRID_TRICKS.
66 m_fieldsGrid->PopEventHandler( true );
67}
68
69
71{
75
76 // Typical assignment operator does not work here because of the ordered_map
77 auto source = m_designBlock->GetFields();
78 m_fields.clear();
79
80 for( const auto& field : source )
81 {
82 m_fields[field.first] = field.second;
83 }
84
85 return TransferDataToGrid();
86}
87
88
90{
91 unsigned int illegalCh = LIB_ID::FindIllegalLibraryNameChar( m_textName->GetValue() );
92
93 // Also check for / in the name, since this is a path character which is illegal for
94 // design blocks and footprints but not symbols
95 if( illegalCh == 0 && m_textName->GetValue().Find( '/' ) != wxNOT_FOUND )
96 illegalCh = '/';
97
98 if( illegalCh )
99 {
100 wxString msg = wxString::Format( _( "Illegal character '%c' in name '%s'." ),
101 illegalCh,
102 m_textName->GetValue() );
103
104 wxMessageDialog errdlg( this, msg, _( "Error" ) );
105 errdlg.ShowModal();
106 return false;
107 }
108
112
113 return TransferDataFromGrid();
114}
115
116
118{
120 [&]() -> std::pair<int, int>
121 {
122 int row = m_fieldsGrid->GetNumberRows();
123 m_fieldsGrid->AppendRows( 1 );
124
125 m_fieldsGrid->SetCellValue( row, 0, _( "Untitled Field" ) );
126 //m_fieldsGrid->SetCellValue( row, 1, wxEmptyString );
127
128 // Set cell properties
129 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
130 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
131
132 return { row, 0 };
133 } );
134}
135
136
138{
140 [&]( int row )
141 {
142 m_fieldsGrid->DeleteRows( row );
143 } );
144}
145
146
148{
150 [&]( int row )
151 {
152 m_fieldsGrid->SwapRows( row, row - 1 );
153 } );
154}
155
156
158{
160 [&]( int row )
161 {
162 m_fieldsGrid->SwapRows( row, row + 1 );
163 } );
164}
165
166
168{
169 wxWindowUpdateLocker updateLock( m_fieldsGrid );
170
172 m_fieldsGrid->AppendRows( m_fields.size() );
173
174 int row = 0;
175
176 for( const auto& [fieldName, fieldValue] : m_fields )
177 {
178 m_fieldsGrid->SetCellValue( row, 0, fieldName );
179 m_fieldsGrid->SetCellValue( row, 1, fieldValue );
180
181 // Set cell properties
182 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
183 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
184
185 row++;
186 }
187
188 return true;
189}
190
191
193{
195 return false;
196
197 m_designBlock->GetFields().clear();
198
199 for( int row = 0; row < m_fieldsGrid->GetNumberRows(); row++ )
200 {
201 wxString fieldName = m_fieldsGrid->GetCellValue( row, 0 ).Strip();
202 fieldName.Replace( wxT( "\n" ), wxT( "" ), true ); // strip all newlines
203 fieldName.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
204
205 if( m_designBlock->GetFields().count( fieldName ) )
206 {
207 wxMessageBox( _( "Duplicate fields are not allowed." ) );
208 return false;
209 }
210
211 m_designBlock->GetFields()[fieldName] = m_fieldsGrid->GetCellValue( row, 1 );
212 }
213
214 return true;
215}
216
217
219{
220 if( aWidth <= 0 )
221 return;
222
223 // Account for scroll bars
224 aWidth -= ( m_fieldsGrid->GetSize().x - m_fieldsGrid->GetClientSize().x );
225
226 m_fieldsGrid->SetColSize( 1, aWidth - m_fieldsGrid->GetColSize( 0 ) );
227}
228
229
231{
232 AdjustGridColumns( event.GetSize().GetX() );
233
234 event.Skip();
235}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
void SetLibDescription(const wxString &aDesc)
Definition: design_block.h:40
void SetKeywords(const wxString &aKeywords)
Definition: design_block.h:43
const wxString & GetKeywords() const
Definition: design_block.h:42
const wxString & GetLibDescription() const
Definition: design_block.h:39
void SetLibId(const LIB_ID &aName)
Definition: design_block.h:36
const LIB_ID & GetLibId() const
Definition: design_block.h:37
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
Definition: design_block.h:51
Class DIALOG_DESIGN_BLOCK_PROPERTIES_BASE.
void OnDeleteField(wxCommandEvent &aEvent) override
void OnSizeGrid(wxSizeEvent &event) override
void OnMoveFieldUp(wxCommandEvent &aEvent) override
void OnMoveFieldDown(wxCommandEvent &aEvent) override
void OnAddField(wxCommandEvent &aEvent) override
DIALOG_DESIGN_BLOCK_PROPERTIES(wxWindow *aParent, DESIGN_BLOCK *aDesignBlock, bool aDisableName=false)
nlohmann::ordered_map< wxString, wxString > m_fields
void SetupStandardButtons(std::map< int, wxString > aLabels={})
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
static unsigned FindIllegalLibraryNameChar(const UTF8 &aLibraryName)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:241
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
void SetBitmap(const wxBitmapBundle &aBmp)
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:766
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition: wx_grid.cpp:755
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:220
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
#define _(s)