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
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->SetupColumnAutosizer( 1 );
60 m_fieldsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
61}
62
63
65{
66 // Delete the GRID_TRICKS.
67 m_fieldsGrid->PopEventHandler( true );
68}
69
70
72{
73 m_textName->ChangeValue( m_designBlock->GetLibId().GetLibItemName() );
74 m_textKeywords->ChangeValue( m_designBlock->GetKeywords() );
75 m_textDescription->ChangeValue( m_designBlock->GetLibDescription() );
76
77 // Typical assignment operator does not work here because of the ordered_map
78 auto source = m_designBlock->GetFields();
79 m_fields.clear();
80
81 for( const auto& field : source )
82 {
83 m_fields[field.first] = field.second;
84 }
85
86 return TransferDataToGrid();
87}
88
89
91{
92 unsigned int illegalCh = LIB_ID::FindIllegalLibraryNameChar( m_textName->GetValue() );
93
94 // Also check for / in the name, since this is a path character which is illegal for
95 // design blocks and footprints but not symbols
96 if( illegalCh == 0 && m_textName->GetValue().Find( '/' ) != wxNOT_FOUND )
97 illegalCh = '/';
98
99 if( illegalCh )
100 {
101 wxString msg = wxString::Format( _( "Illegal character '%c' in name '%s'." ),
102 illegalCh,
103 m_textName->GetValue() );
104
105 wxMessageDialog errdlg( this, msg, _( "Error" ) );
106 errdlg.ShowModal();
107 return false;
108 }
109
110 m_designBlock->SetLibId( LIB_ID( m_designBlock->GetLibId().GetLibNickname(), m_textName->GetValue() ) );
111 m_designBlock->SetLibDescription( m_textDescription->GetValue() );
112 m_designBlock->SetKeywords( m_textKeywords->GetValue() );
113
114 return TransferDataFromGrid();
115}
116
117
119{
120 m_fieldsGrid->OnAddRow(
121 [&]() -> std::pair<int, int>
122 {
123 int row = m_fieldsGrid->GetNumberRows();
124 m_fieldsGrid->AppendRows( 1 );
125
126 m_fieldsGrid->SetCellValue( row, 0, _( "Untitled Field" ) );
127 //m_fieldsGrid->SetCellValue( row, 1, wxEmptyString );
128
129 // Set cell properties
130 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
131 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
132
133 return { row, 0 };
134 } );
135}
136
137
139{
140 m_fieldsGrid->OnDeleteRows(
141 [&]( int row )
142 {
143 m_fieldsGrid->DeleteRows( row );
144 } );
145}
146
147
149{
150 m_fieldsGrid->OnMoveRowUp(
151 [&]( int row )
152 {
153 m_fieldsGrid->SwapRows( row, row - 1 );
154 } );
155}
156
157
159{
160 m_fieldsGrid->OnMoveRowUp(
161 [&]( int row )
162 {
163 m_fieldsGrid->SwapRows( row, row + 1 );
164 } );
165}
166
167
169{
170 wxWindowUpdateLocker updateLock( m_fieldsGrid );
171
172 m_fieldsGrid->ClearRows();
173 m_fieldsGrid->AppendRows( m_fields.size() );
174
175 int row = 0;
176
177 for( const auto& [fieldName, fieldValue] : m_fields )
178 {
179 m_fieldsGrid->SetCellValue( row, 0, fieldName );
180 m_fieldsGrid->SetCellValue( row, 1, fieldValue );
181
182 // Set cell properties
183 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
184 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
185
186 row++;
187 }
188
189 return true;
190}
191
192
194{
195 if( !m_fieldsGrid->CommitPendingChanges() )
196 return false;
197
198 m_designBlock->GetFields().clear();
199
200 for( int row = 0; row < m_fieldsGrid->GetNumberRows(); row++ )
201 {
202 wxString fieldName = m_fieldsGrid->GetCellValue( row, 0 ).Strip();
203 fieldName.Replace( wxT( "\n" ), wxT( "" ), true ); // strip all newlines
204 fieldName.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
205
206 if( m_designBlock->GetFields().count( fieldName ) )
207 {
208 wxMessageBox( _( "Duplicate fields are not allowed." ) );
209 return false;
210 }
211
212 m_designBlock->GetFields()[fieldName] = m_fieldsGrid->GetCellValue( row, 1 );
213 }
214
215 return true;
216}
217
218
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
DIALOG_DESIGN_BLOCK_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Design Block Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void OnDeleteField(wxCommandEvent &aEvent) 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
#define _(s)