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#include <confirm.h>
34
35#include <design_block.h>
36
38 DESIGN_BLOCK* aDesignBlock,
39 bool aDisableName ) :
40 DIALOG_DESIGN_BLOCK_PROPERTIES_BASE( aParent ), m_designBlock( aDesignBlock )
41{
42 m_textName->SetEditable( !aDisableName );
43
44 wxToolTip::Enable( true );
46
47 // Configure button logos
52
53 m_fieldsGrid->SetUseNativeColLabels();
54
55 m_fieldsGrid->PushEventHandler( new GRID_TRICKS( m_fieldsGrid,
56 [this]( wxCommandEvent& aEvent )
57 {
58 OnAddField( aEvent );
59 } ) );
60 m_fieldsGrid->SetupColumnAutosizer( 1 );
61 m_fieldsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
62}
63
64
66{
67 // Delete the GRID_TRICKS.
68 m_fieldsGrid->PopEventHandler( true );
69}
70
71
73{
74 m_textName->ChangeValue( m_designBlock->GetLibId().GetLibItemName() );
75 m_textKeywords->ChangeValue( m_designBlock->GetKeywords() );
76 m_textDescription->ChangeValue( m_designBlock->GetLibDescription() );
77
78 // Typical assignment operator does not work here because of the ordered_map
79 auto source = m_designBlock->GetFields();
80 m_fields.clear();
81
82 for( const auto& field : source )
83 {
84 m_fields[field.first] = field.second;
85 }
86
87 return TransferDataToGrid();
88}
89
90
92{
93 unsigned int illegalCh = LIB_ID::FindIllegalLibraryNameChar( m_textName->GetValue() );
94
95 // Also check for / in the name, since this is a path character which is illegal for
96 // design blocks and footprints but not symbols
97 if( illegalCh == 0 && m_textName->GetValue().Find( '/' ) != wxNOT_FOUND )
98 illegalCh = '/';
99
100 if( illegalCh )
101 {
102 wxString msg = wxString::Format( _( "Illegal character '%c' in name '%s'." ),
103 illegalCh,
104 m_textName->GetValue() );
105
106 KICAD_MESSAGE_DIALOG errdlg( this, msg, _( "Error" ) );
107 errdlg.ShowModal();
108 return false;
109 }
110
111 m_designBlock->SetLibId( LIB_ID( m_designBlock->GetLibId().GetLibNickname(), m_textName->GetValue() ) );
112 m_designBlock->SetLibDescription( m_textDescription->GetValue() );
113 m_designBlock->SetKeywords( m_textKeywords->GetValue() );
114
115 return TransferDataFromGrid();
116}
117
118
120{
121 m_fieldsGrid->OnAddRow(
122 [&]() -> std::pair<int, int>
123 {
124 int row = m_fieldsGrid->GetNumberRows();
125 m_fieldsGrid->AppendRows( 1 );
126
127 m_fieldsGrid->SetCellValue( row, 0, _( "Untitled Field" ) );
128 //m_fieldsGrid->SetCellValue( row, 1, wxEmptyString );
129
130 // Set cell properties
131 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
132 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
133
134 return { row, 0 };
135 } );
136}
137
138
140{
141 m_fieldsGrid->OnDeleteRows(
142 [&]( int row )
143 {
144 m_fieldsGrid->DeleteRows( row );
145 } );
146}
147
148
150{
151 m_fieldsGrid->OnMoveRowUp(
152 [&]( int row )
153 {
154 m_fieldsGrid->SwapRows( row, row - 1 );
155 } );
156}
157
158
160{
161 m_fieldsGrid->OnMoveRowUp(
162 [&]( int row )
163 {
164 m_fieldsGrid->SwapRows( row, row + 1 );
165 } );
166}
167
168
170{
171 wxWindowUpdateLocker updateLock( m_fieldsGrid );
172
173 m_fieldsGrid->ClearRows();
174 m_fieldsGrid->AppendRows( m_fields.size() );
175
176 int row = 0;
177
178 for( const auto& [fieldName, fieldValue] : m_fields )
179 {
180 m_fieldsGrid->SetCellValue( row, 0, fieldName );
181 m_fieldsGrid->SetCellValue( row, 1, fieldValue );
182
183 // Set cell properties
184 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
185 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
186
187 row++;
188 }
189
190 return true;
191}
192
193
195{
196 if( !m_fieldsGrid->CommitPendingChanges() )
197 return false;
198
199 m_designBlock->GetFields().clear();
200
201 for( int row = 0; row < m_fieldsGrid->GetNumberRows(); row++ )
202 {
203 wxString fieldName = m_fieldsGrid->GetCellValue( row, 0 ).Strip();
204 fieldName.Replace( wxT( "\n" ), wxT( "" ), true ); // strip all newlines
205 fieldName.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
206
207 if( m_designBlock->GetFields().count( fieldName ) )
208 {
209 wxMessageBox( _( "Duplicate fields are not allowed." ) );
210 return false;
211 }
212
213 m_designBlock->GetFields()[fieldName] = m_fieldsGrid->GetCellValue( row, 1 );
214 }
215
216 return true;
217}
218
219
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
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:52
#define _(s)