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