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 (C) 1992-2024 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 <sch_edit_frame.h>
28#include <wx/msgdlg.h>
29#include <wx/tooltip.h>
30#include <grid_tricks.h>
32
33#include <design_block.h>
34
36 DESIGN_BLOCK* aDesignBlock ) :
37 DIALOG_DESIGN_BLOCK_PROPERTIES_BASE( aParent ), m_designBlock( aDesignBlock )
38{
39 if( !m_textName->IsEmpty() )
40 m_textName->SetEditable( false );
41
42 wxToolTip::Enable( true );
44
45 // Configure button logos
46 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
47 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
48 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
49 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
50
51 m_fieldsGrid->SetUseNativeColLabels();
52
53 m_fieldsGrid->PushEventHandler( new GRID_TRICKS( m_fieldsGrid, [this]( wxCommandEvent& aEvent )
54 {
55 OnAddField( aEvent );
56 } ) );
57 m_fieldsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
58}
59
60
62{
63 // Delete the GRID_TRICKS.
64 m_fieldsGrid->PopEventHandler( true );
65}
66
67
69{
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 for( const auto& field : source )
78 {
79 m_fields[field.first] = field.second;
80 }
81
82 return TransferDataToGrid();
83}
84
85
87{
92
93 return TransferDataFromGrid();
94}
95
96
97void DIALOG_DESIGN_BLOCK_PROPERTIES::OnAddField( wxCommandEvent& event )
98{
100 return;
101
102 int row = m_fieldsGrid->GetNumberRows();
103
104 m_fieldsGrid->AppendRows( 1 );
105
106 m_fieldsGrid->SetCellValue( row, 0, _( "Untitled Field" ) );
107 //m_fieldsGrid->SetCellValue( row, 1, wxEmptyString );
108
109 // Set cell properties
110 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
111 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
112
113 // wx documentation is wrong, SetGridCursor does not make visible.
114 m_fieldsGrid->MakeCellVisible( row, 0 );
115 m_fieldsGrid->SetGridCursor( row, 0 );
116}
117
118
120{
122 return;
123
124 wxArrayInt selectedRows = m_fieldsGrid->GetSelectedRows();
125
126 if( selectedRows.empty() && m_fieldsGrid->GetGridCursorRow() >= 0 )
127 selectedRows.push_back( m_fieldsGrid->GetGridCursorRow() );
128
129 if( selectedRows.empty() )
130 return;
131
132 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
133 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
134
135 for( int row : selectedRows )
136 {
137 m_fieldsGrid->DeleteRows( row );
138
139 m_fieldsGrid->MakeCellVisible( std::max( 0, row - 1 ), m_fieldsGrid->GetGridCursorCol() );
140 m_fieldsGrid->SetGridCursor( std::max( 0, row - 1 ), m_fieldsGrid->GetGridCursorCol() );
141 }
142}
143
144
146{
148 return;
149
150 int row = m_fieldsGrid->GetGridCursorRow();
151
152 if( m_fieldsGrid->GetNumberRows() < 2 || row == 0 )
153 return;
154
155 // Swap the grid at row with the grid at row - 1
156 wxString temp0 = m_fieldsGrid->GetCellValue( row, 0 );
157 m_fieldsGrid->SetCellValue( row, 0, m_fieldsGrid->GetCellValue( row - 1, 0 ) );
158 m_fieldsGrid->SetCellValue( row - 1, 0, temp0 );
159
160 wxString temp1 = m_fieldsGrid->GetCellValue( row, 1 );
161 m_fieldsGrid->SetCellValue( row, 1, m_fieldsGrid->GetCellValue( row - 1, 1 ) );
162 m_fieldsGrid->SetCellValue( row - 1, 1, temp1 );
163
164 m_fieldsGrid->SetGridCursor( row - 1, 0 );
165}
166
167
169{
171 return;
172
173 int row = m_fieldsGrid->GetGridCursorRow();
174
175 if( m_fieldsGrid->GetNumberRows() < 2 || row == ( (int) m_fieldsGrid->GetNumberRows() - 1 ) )
176 return;
177
178 // Swap the grid at row with the grid at row + 1
179 wxString temp0 = m_fieldsGrid->GetCellValue( row, 0 );
180 m_fieldsGrid->SetCellValue( row, 0, m_fieldsGrid->GetCellValue( row + 1, 0 ) );
181 m_fieldsGrid->SetCellValue( row + 1, 0, temp0 );
182
183 wxString temp1 = m_fieldsGrid->GetCellValue( row, 1 );
184 m_fieldsGrid->SetCellValue( row, 1, m_fieldsGrid->GetCellValue( row + 1, 1 ) );
185 m_fieldsGrid->SetCellValue( row + 1, 1, temp1 );
186
187 m_fieldsGrid->SetGridCursor( row + 1, 0 );
188}
189
190
192{
193 m_fieldsGrid->Freeze();
194
196 m_fieldsGrid->AppendRows( m_fields.size() );
197
198 int row = 0;
199
200 for( const auto& [fieldName, fieldValue] : m_fields )
201 {
202 m_fieldsGrid->SetCellValue( row, 0, fieldName );
203 m_fieldsGrid->SetCellValue( row, 1, fieldValue );
204
205 // Set cell properties
206 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
207 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
208
209 row++;
210 }
211
212 m_fieldsGrid->Thaw();
213
214 return true;
215}
216
217
219{
221 return false;
222
223 nlohmann::ordered_map<wxString, wxString> newFields;
224
225 for( int row = 0; row < m_fieldsGrid->GetNumberRows(); row++ )
226 {
227 wxString fieldName = m_fieldsGrid->GetCellValue( row, 0 ).Strip();
228 fieldName.Replace( wxT( "\n" ), wxT( "" ), true ); // strip all newlines
229 fieldName.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
230
231 if( newFields.count( fieldName ) )
232 {
233 wxMessageBox( _( "Duplicate fields are not allowed." ) );
234 return false;
235 }
236
237 newFields[fieldName] = m_fieldsGrid->GetCellValue( row, 1 );
238 }
239
240 m_designBlock->SetFields( newFields );
241
242 return true;
243}
244
245
247{
248 if( aWidth <= 0 )
249 return;
250
251 // Account for scroll bars
252 aWidth -= ( m_fieldsGrid->GetSize().x - m_fieldsGrid->GetClientSize().x );
253
254 m_fieldsGrid->SetColSize( 1, aWidth - m_fieldsGrid->GetColSize( 0 ) );
255}
256
257
259{
260 AdjustGridColumns( event.GetSize().GetX() );
261
262 event.Skip();
263}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
void SetLibDescription(const wxString &aDesc)
Definition: design_block.h:36
void SetKeywords(const wxString &aKeywords)
Definition: design_block.h:39
const wxString & GetKeywords() const
Definition: design_block.h:38
const wxString & GetLibDescription() const
Definition: design_block.h:35
void SetLibId(const LIB_ID &aName)
Definition: design_block.h:32
void SetFields(nlohmann::ordered_map< wxString, wxString > &aFields)
Definition: design_block.h:44
const LIB_ID & GetLibId() const
Definition: design_block.h:33
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
Definition: design_block.h:48
Class DIALOG_DESIGN_BLOCK_PROPERTIES_BASE.
void OnDeleteField(wxCommandEvent &aEvent) override
void OnSizeGrid(wxSizeEvent &event) override
DIALOG_DESIGN_BLOCK_PROPERTIES(SCH_EDIT_FRAME *aParent, DESIGN_BLOCK *aDesignBlock)
void OnMoveFieldUp(wxCommandEvent &aEvent) override
void OnMoveFieldDown(wxCommandEvent &aEvent) override
void OnAddField(wxCommandEvent &aEvent) override
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
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
Schematic editor (Eeschema) main window.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:184
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
#define _(s)