KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_generated_table_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23
24#include <board.h>
25#include <board_commit.h>
28#include <confirm.h>
29#include <pcb_base_edit_frame.h>
30#include <pcb_generated_table.h>
32#include <widgets/wx_grid.h>
33
34#include <wx/grid.h>
35
36
37namespace
38{
39
40enum COLUMN_GRID_COL
41{
42 COL_SHOW = 0,
44 COL_HEADING,
45 COL_ALIGN,
47};
48
49
50// What the column reports, whatever the user has retitled its heading to
51wxString columnLabel( const GENERATED_TABLE_SCHEMA& aSchema, int aId )
52{
53 const GENERATED_TABLE_COLUMN_DEF* def = aSchema.Find( aId );
54
55 if( !def )
56 return wxEmptyString;
57
58 return wxGetTranslation( wxString( def->m_Label ? def->m_Label : def->m_Heading ) );
59}
60
61
62wxArrayString alignChoices()
63{
64 wxArrayString choices;
65 choices.Add( _( "Left" ) );
66 choices.Add( _( "Center" ) );
67 choices.Add( _( "Right" ) );
68
69 return choices;
70}
71
72} // namespace
73
74
76 PCB_GENERATED_TABLE* aTable,
79 m_frame( aFrame ),
80 m_table( aTable ),
81 m_options( nullptr ),
84{
85 SetTitle( wxString::Format( _( "%s Properties" ), m_table->GetFriendlyName() ) );
86
87 // Not merely "not copper". Silkscreen, mask, paste, adhesive, Edge.Cuts, Margin and
88 // courtyard are manufacturing inputs this would corrupt rather than document
89 m_LayerSelectionCtrl->SetNotAllowedLayerSet( ~DocumentationLayers() );
90 m_LayerSelectionCtrl->SetLayersHotkeys( false );
91 m_LayerSelectionCtrl->SetBoardFrame( m_frame );
92 m_LayerSelectionCtrl->Resync();
93
94 m_columnGrid->SetColLabelValue( COL_SHOW, _( "Show" ) );
95 m_columnGrid->SetColLabelValue( COL_NAME, _( "Column" ) );
96 m_columnGrid->SetColLabelValue( COL_HEADING, _( "Heading" ) );
97 m_columnGrid->SetColLabelValue( COL_ALIGN, _( "Align" ) );
98 m_columnGrid->SetColLabelValue( COL_WIDTH, _( "Min Width" ) );
99
100 m_columnGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
101 m_columnGrid->SetUnitsProvider( m_frame );
102 m_columnGrid->SetAutoEvalCols( { COL_WIDTH } );
103
105
106 if( aOptions )
107 {
108 // The page, not the dialog, since wx insists a sizer's windows are children of its window
109 m_options = aOptions( m_columnsPanel );
110 m_options->m_GetFormat = [this]()
111 {
112 return getFormat();
113 };
114 m_options->m_SetFormat = [this]( const GENERATED_TABLE_FORMAT& aFormat )
115 {
116 setFormat( aFormat );
117 };
118 m_optionsSizer->Add( m_options, 0, wxEXPAND, 0 );
119 }
120
123}
124
125
127{
128 if( m_columnGrid->GetNumberRows() )
129 m_columnGrid->DeleteRows( 0, m_columnGrid->GetNumberRows() );
130
131 m_columnGrid->AppendRows( static_cast<int>( m_rows.size() ) );
132
133 const wxArrayString aligns = alignChoices();
134
135 for( size_t row = 0; row < m_rows.size(); ++row )
136 {
137 const int r = static_cast<int>( row );
138 const GENERATED_TABLE_COLUMN& col = m_rows[row].m_Column;
139
140 m_columnGrid->SetCellRenderer( r, COL_SHOW, new wxGridCellBoolRenderer );
141 m_columnGrid->SetCellEditor( r, COL_SHOW, new wxGridCellBoolEditor );
142 m_columnGrid->SetCellValue( r, COL_SHOW, m_rows[row].m_Shown ? wxT( "1" ) : wxEmptyString );
143
144 m_columnGrid->SetCellValue( r, COL_NAME, columnLabel( m_table->Schema(), col.m_Id ) );
145 m_columnGrid->SetReadOnly( r, COL_NAME );
146
147 m_columnGrid->SetCellValue( r, COL_HEADING, col.m_Heading );
148
149 m_columnGrid->SetCellEditor( r, COL_ALIGN, new wxGridCellChoiceEditor( aligns ) );
150 m_columnGrid->SetCellValue( r, COL_ALIGN, aligns[static_cast<int>( col.m_Align )] );
151
152 // Empty rather than zero, since zero means the column sizes itself to its text
153 m_columnGrid->SetOptionalUnitValue( r, COL_WIDTH,
154 col.m_Width > 0 ? std::optional<int>( col.m_Width ) : std::nullopt );
155 }
156
157 m_columnGrid->AutoSizeColumns();
159}
160
161
163{
164 if( !m_columnGrid->CommitPendingChanges() )
165 return false;
166
167 const wxArrayString aligns = alignChoices();
168
169 for( size_t row = 0; row < m_rows.size(); ++row )
170 {
171 const int r = static_cast<int>( row );
172 GENERATED_TABLE_COLUMN& col = m_rows[row].m_Column;
173
174 m_rows[row].m_Shown = !m_columnGrid->GetCellValue( r, COL_SHOW ).IsEmpty();
175 col.m_Heading = m_columnGrid->GetCellValue( r, COL_HEADING );
176
177 const int align = aligns.Index( m_columnGrid->GetCellValue( r, COL_ALIGN ) );
178
179 if( align != wxNOT_FOUND )
180 col.m_Align = static_cast<GENERATED_TABLE_ALIGN>( align );
181
182 col.m_Width = std::clamp( m_columnGrid->GetOptionalUnitValue( r, COL_WIDTH ).value_or( 0 ), 0,
184 }
185
186 return true;
187}
188
189
190void DIALOG_GENERATED_TABLE_PROPERTIES::setColumns( const std::vector<GENERATED_TABLE_COLUMN>& aColumns )
191{
192 m_rows.clear();
193
194 for( const GENERATED_TABLE_COLUMN& col : aColumns )
195 m_rows.push_back( { col, col.m_Visible, true } );
196
197 for( const GENERATED_TABLE_COLUMN_DEF& def : m_table->Schema().Defs() )
198 {
199 const bool present = std::any_of( aColumns.begin(), aColumns.end(),
200 [&def]( const GENERATED_TABLE_COLUMN& aCol )
201 {
202 return aCol.m_Id == def.m_Id;
203 } );
204
205 if( !present )
206 m_rows.push_back( { m_table->Schema().DefaultColumn( def.m_Id ), false, false } );
207 }
208
210}
211
212
213std::vector<GENERATED_TABLE_COLUMN> DIALOG_GENERATED_TABLE_PROPERTIES::shownColumns() const
214{
215 std::vector<GENERATED_TABLE_COLUMN> columns = placedColumns();
216
217 std::erase_if( columns,
218 []( const GENERATED_TABLE_COLUMN& aColumn )
219 {
220 return !aColumn.m_Visible;
221 } );
222
223 return columns;
224}
225
226
227std::vector<GENERATED_TABLE_COLUMN> DIALOG_GENERATED_TABLE_PROPERTIES::placedColumns() const
228{
229 std::vector<GENERATED_TABLE_COLUMN> columns;
230
231 // One the table never had and nobody ticked stays out, rather than every table carrying
232 // every column the schema knows
233 for( const COLUMN_ROW& row : m_rows )
234 {
235 if( !row.m_Shown && !row.m_Placed )
236 continue;
237
238 GENERATED_TABLE_COLUMN& col = columns.emplace_back( row.m_Column );
239 col.m_Visible = row.m_Shown;
240 }
241
242 return columns;
243}
244
245
246std::optional<GENERATED_TABLE_FORMAT> DIALOG_GENERATED_TABLE_PROPERTIES::getFormat()
247{
248 if( !harvestColumnGrid() )
249 return std::nullopt;
250
252 format.m_Columns = shownColumns();
253 format.m_Units = static_cast<GENERATED_TABLE_UNITS>( m_unitsCtrl->GetSelection() );
254 format.m_Precision = m_precisionCtrl->GetValue();
255
256 return format;
257}
258
259
261{
262 setColumns( aFormat.m_Columns );
263 m_unitsCtrl->SetSelection( static_cast<int>( aFormat.m_Units ) );
264 m_precisionCtrl->SetValue( aFormat.m_Precision );
265}
266
267
269{
270 if( !wxDialog::TransferDataToWindow() )
271 return false;
272
273 // After DIALOG_SHIM has restored the last page, which is otherwise whatever the user
274 // happened to leave open the time before
275 m_notebook->SetSelection( 0 );
276
277 m_LayerSelectionCtrl->SetLayerSelection( m_table->GetLayer() );
278 m_cbLocked->SetValue( m_table->IsLocked() );
279
280 m_unitsCtrl->SetSelection( static_cast<int>( m_table->GetUnits() ) );
281 m_precisionCtrl->SetValue( m_table->GetPrecision() );
282
283 setColumns( m_table->AllColumns() );
284
285 if( m_options )
286 m_options->TransferToWindow( *m_table );
287
288 m_borderCheckbox->SetValue( m_table->StrokeExternal() );
289 m_headerBorder->SetValue( m_table->StrokeHeaderSeparator() );
290 m_rowSeparators->SetValue( m_table->StrokeRows() );
291 m_colSeparators->SetValue( m_table->StrokeColumns() );
292
293 if( m_table->GetBorderStroke().GetWidth() >= 0 )
294 m_borderWidth.SetValue( m_table->GetBorderStroke().GetWidth() );
295
296 if( m_table->GetSeparatorsStroke().GetWidth() >= 0 )
297 m_separatorsWidth.SetValue( m_table->GetSeparatorsStroke().GetWidth() );
298
299 m_borderWidth.Enable( m_table->StrokeExternal() || m_table->StrokeHeaderSeparator() );
300 m_separatorsWidth.Enable( m_table->StrokeRows() || m_table->StrokeColumns() );
301
302 return true;
303}
304
305
307{
308 if( !harvestColumnGrid() )
309 return false;
310
311 if( !wxDialog::TransferDataFromWindow() )
312 return false;
313
314 std::vector<GENERATED_TABLE_COLUMN> columns = shownColumns();
315
316 if( columns.empty() )
317 {
318 DisplayError( this, _( "A table needs at least one column." ) );
319 return false;
320 }
321
322 if( !m_table->Schema().Validate( columns ) )
323 {
324 DisplayError( this, _( "The columns are too wide in total." ) );
325 return false;
326 }
327
328 BOARD* board = m_frame->GetBoard();
329 BOARD_COMMIT commit( m_frame );
331
332 commit.Modify( m_table );
333
334 if( m_options && !m_options->TransferFromWindow( *m_table, refresh ) )
335 {
336 commit.Revert();
337 return false;
338 }
339
340 m_table->SetLayer( ToLAYER_ID( m_LayerSelectionCtrl->GetLayerSelection() ) );
341 m_table->SetLocked( m_cbLocked->GetValue() );
342
343 m_table->SetUnits( static_cast<GENERATED_TABLE_UNITS>( m_unitsCtrl->GetSelection() ) );
344 m_table->SetPrecision( m_precisionCtrl->GetValue() );
345
346 m_table->SetColumns( placedColumns() );
347
348 m_table->SetStrokeExternal( m_borderCheckbox->GetValue() );
349 m_table->SetStrokeHeaderSeparator( m_headerBorder->GetValue() );
350 m_table->SetStrokeRows( m_rowSeparators->GetValue() );
351 m_table->SetStrokeColumns( m_colSeparators->GetValue() );
352
353 const auto strokeWidth =
354 []( bool aStroked, UNIT_BINDER& aWidth )
355 {
356 return aStroked ? std::max( 0, aWidth.GetIntValue() ) : -1;
357 };
358
359 STROKE_PARAMS border = m_table->GetBorderStroke();
360 border.SetWidth( strokeWidth( m_borderCheckbox->GetValue() || m_headerBorder->GetValue(), m_borderWidth ) );
361 m_table->SetBorderStroke( border );
362
363 STROKE_PARAMS separators = m_table->GetSeparatorsStroke();
364 separators.SetWidth( strokeWidth( m_rowSeparators->GetValue() || m_colSeparators->GetValue(), m_separatorsWidth ) );
365 m_table->SetSeparatorsStroke( separators );
366
367 // Committed, or the table reads fresh while board state it was built against is still pending
368 m_table->RebuildCells( *board, &refresh );
369 refresh.Commit();
370
371 commit.Push( wxString::Format( _( "Edit %s" ), m_table->GetFriendlyName() ), SKIP_CONNECTIVITY );
372
373 return true;
374}
375
376
378{
379 const int cols = m_columnGrid->GetNumberCols();
380 const int width = m_columnGrid->GetClientSize().x;
381
382 if( cols < 1 || width < 1 )
383 return;
384
385 // The checkbox and choice columns keep the width they auto-sized to. Whatever is left
386 // over goes to the two text columns, so the grid has no unused strip down its side
387 int fixed = 0;
388
389 for( int col = 0; col < cols; ++col )
390 {
391 if( col != COL_NAME && col != COL_HEADING )
392 fixed += m_columnGrid->GetColSize( col );
393 }
394
395 const int spare = width - fixed;
396
397 if( spare < 2 )
398 return;
399
400 m_columnGrid->SetColSize( COL_NAME, spare / 2 );
401 m_columnGrid->SetColSize( COL_HEADING, spare - spare / 2 );
402}
403
404
406{
408 aEvent.Skip();
409}
410
411
413{
414 const int defaultWidth =
415 m_frame->GetDesignSettings().GetLineThickness( ToLAYER_ID( m_LayerSelectionCtrl->GetLayerSelection() ) );
416
417 const bool border = m_borderCheckbox->GetValue() || m_headerBorder->GetValue();
418 const bool separators = m_rowSeparators->GetValue() || m_colSeparators->GetValue();
419
420 if( border && m_borderWidth.GetValue() < 0 )
421 m_borderWidth.SetValue( defaultWidth );
422
423 if( separators && m_separatorsWidth.GetValue() < 0 )
424 m_separatorsWidth.SetValue( defaultWidth );
425
426 m_borderWidth.Enable( border );
427 m_separatorsWidth.Enable( separators );
428}
429
430
432{
433 if( !harvestColumnGrid() )
434 return;
435
436 const int row = m_columnGrid->GetGridCursorRow();
437 const int target = row + aDelta;
438
439 if( row < 0 || target < 0 || target >= static_cast<int>( m_rows.size() ) )
440 return;
441
442 std::swap( m_rows[row], m_rows[target] );
443
445 m_columnGrid->SetGridCursor( target, m_columnGrid->GetGridCursorCol() );
446 m_columnGrid->SelectRow( target );
447}
448
449
451{
452 moveColumn( -1 );
453}
454
455
457{
458 moveColumn( 1 );
459}
460
461
463{
464 setColumns( m_table->Schema().Defaults() );
465}
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
void Revert() override
Revert the commit by restoring the modified items state.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
DIALOG_GENERATED_TABLE_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Table Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void setColumns(const std::vector< GENERATED_TABLE_COLUMN > &aColumns)
Show aColumns checked and in order, then every other schema column unchecked so turning one back on d...
std::vector< GENERATED_TABLE_COLUMN > placedColumns() const
The columns the table has, or is given now, in order and marked shown or hidden.
bool harvestColumnGrid()
Read the grid back into m_rows.
void onMoveUp(wxCommandEvent &aEvent) override
void onResetColumns(wxCommandEvent &aEvent) override
DIALOG_GENERATED_TABLE_PROPERTIES(PCB_BASE_EDIT_FRAME *aFrame, PCB_GENERATED_TABLE *aTable, GENERATED_TABLE_OPTIONS_FACTORY aOptions)
aOptions may be empty. When given, the panel is created on the dialog and put below the column grid
void setFormat(const GENERATED_TABLE_FORMAT &aFormat)
void fillColumnGrid()
Fill the column grid from m_rows.
std::vector< GENERATED_TABLE_COLUMN > shownColumns() const
void onMoveDown(wxCommandEvent &aEvent) override
void onBorderChecked(wxCommandEvent &aEvent) override
std::optional< GENERATED_TABLE_FORMAT > getFormat()
std::vector< COLUMN_ROW > m_rows
Every known column, the table's own first in the table's order, then any it has never had.
void fitColumnGrid()
Share the grid's width out across its columns so none of it goes unused.
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
One pass of rebuilding tables.
The columns one generator (the drill chart, a stackup table, ...) can produce, and the units and prec...
const GENERATED_TABLE_COLUMN_DEF * Find(int aId) const
Common, abstract interface for edit frames.
A table whose cell text is derived from the board and rebuilt whenever it goes stale.
Simple container to manage line stroke parameters.
void SetWidth(int aWidth)
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
std::function< GENERATED_TABLE_OPTIONS_PANEL *(wxWindow *aParent)> GENERATED_TABLE_OPTIONS_FACTORY
The options panel must be created with its real parent, since a wxPanel cannot be reparented reliably...
#define _(s)
LSET DocumentationLayers()
Layers a generated table or drill map may live on.
GENERATED_TABLE_UNITS
constexpr int GENERATED_TABLE_MAX_COLUMN_WIDTH
A column can be no wider than this.
GENERATED_TABLE_ALIGN
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
#define SKIP_CONNECTIVITY
Definition sch_commit.h:41
@ COL_WIDTH
The column a schema knows how to build, independent of whether any particular table shows it.
A column as a generated table actually carries it: what a row's cell in that column holds and how it ...
GENERATED_TABLE_ALIGN m_Align
What the shared dialog edits that a template also carries.
std::vector< GENERATED_TABLE_COLUMN > m_Columns
Shown columns only, in table order.