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 <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
78 for( const auto& field : source )
79 {
80 m_fields[field.first] = field.second;
81 }
82
83 return TransferDataToGrid();
84}
85
86
88{
93
94 return TransferDataFromGrid();
95}
96
97
98void DIALOG_DESIGN_BLOCK_PROPERTIES::OnAddField( wxCommandEvent& event )
99{
101 return;
102
103 int row = m_fieldsGrid->GetNumberRows();
104
105 m_fieldsGrid->AppendRows( 1 );
106
107 m_fieldsGrid->SetCellValue( row, 0, _( "Untitled Field" ) );
108 //m_fieldsGrid->SetCellValue( row, 1, wxEmptyString );
109
110 // Set cell properties
111 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
112 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
113
114 // wx documentation is wrong, SetGridCursor does not make visible.
115 m_fieldsGrid->MakeCellVisible( row, 0 );
116 m_fieldsGrid->SetGridCursor( row, 0 );
117}
118
119
121{
123 return;
124
125 wxArrayInt selectedRows = m_fieldsGrid->GetSelectedRows();
126
127 if( selectedRows.empty() && m_fieldsGrid->GetGridCursorRow() >= 0 )
128 selectedRows.push_back( m_fieldsGrid->GetGridCursorRow() );
129
130 if( selectedRows.empty() )
131 return;
132
133 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
134 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
135
136 for( int row : selectedRows )
137 {
138 m_fieldsGrid->DeleteRows( row );
139
140 m_fieldsGrid->MakeCellVisible( std::max( 0, row - 1 ), m_fieldsGrid->GetGridCursorCol() );
141 m_fieldsGrid->SetGridCursor( std::max( 0, row - 1 ), m_fieldsGrid->GetGridCursorCol() );
142 }
143}
144
145
147{
149 return;
150
151 int row = m_fieldsGrid->GetGridCursorRow();
152
153 if( m_fieldsGrid->GetNumberRows() < 2 || row == 0 )
154 return;
155
156 // Swap the grid at row with the grid at row - 1
157 wxString temp0 = m_fieldsGrid->GetCellValue( row, 0 );
158 m_fieldsGrid->SetCellValue( row, 0, m_fieldsGrid->GetCellValue( row - 1, 0 ) );
159 m_fieldsGrid->SetCellValue( row - 1, 0, temp0 );
160
161 wxString temp1 = m_fieldsGrid->GetCellValue( row, 1 );
162 m_fieldsGrid->SetCellValue( row, 1, m_fieldsGrid->GetCellValue( row - 1, 1 ) );
163 m_fieldsGrid->SetCellValue( row - 1, 1, temp1 );
164
165 m_fieldsGrid->SetGridCursor( row - 1, 0 );
166}
167
168
170{
172 return;
173
174 int row = m_fieldsGrid->GetGridCursorRow();
175
176 if( m_fieldsGrid->GetNumberRows() < 2 || row == ( (int) m_fieldsGrid->GetNumberRows() - 1 ) )
177 return;
178
179 // Swap the grid at row with the grid at row + 1
180 wxString temp0 = m_fieldsGrid->GetCellValue( row, 0 );
181 m_fieldsGrid->SetCellValue( row, 0, m_fieldsGrid->GetCellValue( row + 1, 0 ) );
182 m_fieldsGrid->SetCellValue( row + 1, 0, temp0 );
183
184 wxString temp1 = m_fieldsGrid->GetCellValue( row, 1 );
185 m_fieldsGrid->SetCellValue( row, 1, m_fieldsGrid->GetCellValue( row + 1, 1 ) );
186 m_fieldsGrid->SetCellValue( row + 1, 1, temp1 );
187
188 m_fieldsGrid->SetGridCursor( row + 1, 0 );
189}
190
191
193{
194 m_fieldsGrid->Freeze();
195
197 m_fieldsGrid->AppendRows( m_fields.size() );
198
199 int row = 0;
200
201 for( const auto& [fieldName, fieldValue] : m_fields )
202 {
203 m_fieldsGrid->SetCellValue( row, 0, fieldName );
204 m_fieldsGrid->SetCellValue( row, 1, fieldValue );
205
206 // Set cell properties
207 m_fieldsGrid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
208 m_fieldsGrid->SetCellAlignment( row, 1, wxALIGN_LEFT, wxALIGN_CENTRE );
209
210 row++;
211 }
212
213 m_fieldsGrid->Thaw();
214
215 return true;
216}
217
218
220{
222 return false;
223
224 nlohmann::ordered_map<wxString, wxString> newFields;
225
226 for( int row = 0; row < m_fieldsGrid->GetNumberRows(); row++ )
227 {
228 wxString fieldName = m_fieldsGrid->GetCellValue( row, 0 ).Strip();
229 fieldName.Replace( wxT( "\n" ), wxT( "" ), true ); // strip all newlines
230 fieldName.Replace( wxT( " " ), wxT( " " ), true ); // double space to single
231
232 if( newFields.count( fieldName ) )
233 {
234 wxMessageBox( _( "Duplicate fields are not allowed." ) );
235 return false;
236 }
237
238 newFields[fieldName] = m_fieldsGrid->GetCellValue( row, 1 );
239 }
240
241 m_designBlock->SetFields( newFields );
242
243 return true;
244}
245
246
248{
249 if( aWidth <= 0 )
250 return;
251
252 // Account for scroll bars
253 aWidth -= ( m_fieldsGrid->GetSize().x - m_fieldsGrid->GetClientSize().x );
254
255 m_fieldsGrid->SetColSize( 1, aWidth - m_fieldsGrid->GetColSize( 0 ) );
256}
257
258
260{
261 AdjustGridColumns( event.GetSize().GetX() );
262
263 event.Skip();
264}
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:49
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 SetBitmap(const wxBitmapBundle &aBmp)
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:193
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
#define _(s)