KiCad PCB EDA Suite
Loading...
Searching...
No Matches
fields_grid_table.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) 2018-2023 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <kiway.h>
25#include <kiway_player.h>
26#include <dialog_shim.h>
27#include <fields_grid_table.h>
28#include <sch_base_frame.h>
29#include <sch_field.h>
30#include <sch_label.h>
31#include <sch_validators.h>
32#include <validators.h>
33#include <sch_edit_frame.h>
34#include <symbol_library.h>
35#include <schematic.h>
36#include <template_fieldnames.h>
41#include "eda_doc.h"
43#include "font/fontconfig.h"
45#include <wx/settings.h>
46#include <string_utils.h>
48#include <pgm_base.h>
49
50
51enum
52{
55};
56
57
58#define DEFAULT_FONT_NAME _( "Default Font" )
59
60
61static wxString netList( SCH_SYMBOL* aSymbol, SCH_SHEET_PATH& aSheetPath )
62{
63 /*
64 * Symbol netlist format:
65 * library:footprint
66 * reference
67 * value
68 * pinName,netName,pinFunction,pinType
69 * pinName,netName,pinFunction,pinType
70 * ...
71 */
72 wxString netlist;
73
74 netlist << EscapeString( aSymbol->GetFootprintFieldText( true, &aSheetPath, false ), CTX_LINE ) << wxS( "\r" );
75 netlist << EscapeString( aSymbol->GetRef( &aSheetPath ), CTX_LINE ) << wxS( "\r" );
76 netlist << EscapeString( aSymbol->GetValueFieldText( true, &aSheetPath, false ), CTX_LINE );
77
78 for( SCH_PIN* pin : aSymbol->GetPins( &aSheetPath ) )
79 {
80 netlist << wxS( "\r" );
81 netlist << EscapeString( pin->GetNumber(), CTX_CSV ) << wxS( "," );
82 netlist << EscapeString( pin->GetDefaultNetName( aSheetPath ), CTX_CSV ) << wxS( "," );
83 netlist << EscapeString( pin->GetName(), CTX_CSV ) << wxS( "," );
84 netlist << EscapeString( pin->GetCanonicalElectricalTypeName(), CTX_CSV );
85 }
87 return netlist;
89
90
91template <class T>
93 WX_GRID* aGrid, LIB_SYMBOL* aSymbol ) :
94 m_frame( aFrame ),
95 m_dialog( aDialog ),
96 m_grid( aGrid ),
97 m_parentType( SCH_SYMBOL_T ),
98 m_mandatoryFieldCount( MANDATORY_FIELDS ),
99 m_part( aSymbol ),
100 m_fieldNameValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_NAME ),
101 m_referenceValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), REFERENCE_FIELD ),
102 m_valueValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), VALUE_FIELD ),
103 m_libIdValidator(),
104 m_urlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
105 m_nonUrlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
106 m_filepathValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), SHEETFILENAME )
108 initGrid( aGrid );
109}
112template <class T>
114 WX_GRID* aGrid, SCH_SYMBOL* aSymbol ) :
115 m_frame( aFrame ),
116 m_dialog( aDialog ),
117 m_grid( aGrid ),
118 m_parentType( SCH_SYMBOL_T ),
119 m_mandatoryFieldCount( MANDATORY_FIELDS ),
120 m_part( aSymbol->GetLibSymbolRef().get() ),
121 m_symbolNetlist( netList( aSymbol, aFrame->GetCurrentSheet() ) ),
122 m_fieldNameValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_NAME ),
123 m_referenceValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), REFERENCE_FIELD ),
124 m_valueValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), VALUE_FIELD ),
125 m_libIdValidator(),
126 m_urlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
127 m_nonUrlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
128 m_filepathValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), SHEETFILENAME )
129{
130 initGrid( aGrid );
131}
132
133
134template <class T>
136 WX_GRID* aGrid, SCH_SHEET* aSheet ) :
137 m_frame( aFrame ),
138 m_dialog( aDialog ),
139 m_grid( aGrid ),
140 m_parentType( SCH_SHEET_T ),
141 m_mandatoryFieldCount( SHEET_MANDATORY_FIELDS ),
142 m_part( nullptr ),
143 m_fieldNameValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_NAME ),
144 m_referenceValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), SHEETNAME_V ),
145 m_valueValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), VALUE_FIELD ),
146 m_libIdValidator(),
147 m_urlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
148 m_nonUrlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
149 m_filepathValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), SHEETFILENAME_V )
150{
151 initGrid( aGrid );
152}
153
154
155template <class T>
157 WX_GRID* aGrid, SCH_LABEL_BASE* aLabel ) :
158 m_frame( aFrame ),
159 m_dialog( aDialog ),
160 m_grid( aGrid ),
161 m_parentType( SCH_LABEL_LOCATE_ANY_T ),
162 m_mandatoryFieldCount( aLabel->GetMandatoryFieldCount() ),
163 m_part( nullptr ),
164 m_fieldNameValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_NAME ),
165 m_referenceValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), 0 ),
166 m_valueValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), 0 ),
167 m_libIdValidator(),
168 m_urlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
169 m_nonUrlValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), FIELD_VALUE ),
170 m_filepathValidator( aFrame->IsType( FRAME_SCH_SYMBOL_EDITOR ), 0 )
171{
172 initGrid( aGrid );
173}
174
175
176template <class T>
178{
179 // Build the various grid cell attributes.
180 // NOTE: validators and cellAttrs are member variables to get the destruction order
181 // right. wxGrid is VERY cranky about this.
182
183 m_readOnlyAttr = new wxGridCellAttr;
184 m_readOnlyAttr->SetReadOnly( true );
185
186 m_fieldNameAttr = new wxGridCellAttr;
188 nameEditor->SetValidator( m_fieldNameValidator );
189 m_fieldNameAttr->SetEditor( nameEditor );
190
191 m_referenceAttr = new wxGridCellAttr;
192 GRID_CELL_TEXT_EDITOR* referenceEditor = new GRID_CELL_TEXT_EDITOR();
193 referenceEditor->SetValidator( m_referenceValidator );
194 m_referenceAttr->SetEditor( referenceEditor );
195
196 m_valueAttr = new wxGridCellAttr;
197 GRID_CELL_TEXT_EDITOR* valueEditor = new GRID_CELL_TEXT_EDITOR();
198 valueEditor->SetValidator( m_valueValidator );
199 m_valueAttr->SetEditor( valueEditor );
200
201 m_footprintAttr = new wxGridCellAttr;
202 GRID_CELL_FPID_EDITOR* fpIdEditor = new GRID_CELL_FPID_EDITOR( m_dialog, m_symbolNetlist );
203 fpIdEditor->SetValidator( m_libIdValidator );
204 m_footprintAttr->SetEditor( fpIdEditor );
205
206 m_urlAttr = new wxGridCellAttr;
207 GRID_CELL_URL_EDITOR* urlEditor = new GRID_CELL_URL_EDITOR( m_dialog,
208 m_frame->Prj().SchSearchS() );
209 urlEditor->SetValidator( m_urlValidator );
210 m_urlAttr->SetEditor( urlEditor );
211
212 m_nonUrlAttr = new wxGridCellAttr;
213 GRID_CELL_TEXT_EDITOR* nonUrlEditor = new GRID_CELL_TEXT_EDITOR();
214 nonUrlEditor->SetValidator( m_nonUrlValidator );
215 m_nonUrlAttr->SetEditor( nonUrlEditor );
216
217 m_curdir = m_frame->Prj().GetProjectPath();
218 m_filepathAttr = new wxGridCellAttr;
219
220 // Create a wild card using wxFileDialog syntax.
221 wxString wildCard( _( "Schematic Files" ) );
222 std::vector<std::string> exts;
223 exts.push_back( KiCadSchematicFileExtension );
224 wildCard += AddFileExtListToFilter( exts );
225
226 auto filepathEditor = new GRID_CELL_PATH_EDITOR( m_dialog, aGrid, &m_curdir, wildCard );
227 filepathEditor->SetValidator( m_filepathValidator );
228 m_filepathAttr->SetEditor( filepathEditor );
229
230 m_boolAttr = new wxGridCellAttr;
231 m_boolAttr->SetRenderer( new wxGridCellBoolRenderer() );
232 m_boolAttr->SetEditor( new wxGridCellBoolEditor() );
233 m_boolAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
234
235 wxArrayString vAlignNames;
236 vAlignNames.Add( _( "Top" ) );
237 vAlignNames.Add( _( "Center" ) );
238 vAlignNames.Add( _( "Bottom" ) );
239 m_vAlignAttr = new wxGridCellAttr;
240 m_vAlignAttr->SetEditor( new wxGridCellChoiceEditor( vAlignNames ) );
241 m_vAlignAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_BOTTOM );
242
243 wxArrayString hAlignNames;
244 hAlignNames.Add( _( "Left" ) );
245 hAlignNames.Add(_( "Center" ) );
246 hAlignNames.Add(_( "Right" ) );
247 m_hAlignAttr = new wxGridCellAttr;
248 m_hAlignAttr->SetEditor( new wxGridCellChoiceEditor( hAlignNames ) );
249 m_hAlignAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_BOTTOM );
250
251 wxArrayString orientationNames;
252 orientationNames.Add( _( "Horizontal" ) );
253 orientationNames.Add(_( "Vertical" ) );
254 m_orientationAttr = new wxGridCellAttr;
255 m_orientationAttr->SetEditor( new wxGridCellChoiceEditor( orientationNames ) );
256 m_orientationAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_BOTTOM );
257
258 SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
259 wxArrayString existingNetclasses;
260
261 if( editFrame )
262 {
263 // Load the combobox with existing existingNetclassNames
264 PROJECT_FILE& projectFile = editFrame->Prj().GetProjectFile();
265 const std::shared_ptr<NET_SETTINGS>& settings = projectFile.NetSettings();
266
267 existingNetclasses.push_back( settings->m_DefaultNetClass->GetName() );
268
269 for( const auto& [ name, netclass ] : settings->m_NetClasses )
270 existingNetclasses.push_back( name );
271 }
272
273 m_netclassAttr = new wxGridCellAttr;
274 m_netclassAttr->SetEditor( new GRID_CELL_COMBOBOX( existingNetclasses ) );
275
276 wxArrayString fonts;
277 std::vector<std::string> fontNames;
278 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
279
280 for( const std::string& name : fontNames )
281 fonts.Add( wxString( name ) );
282
283 fonts.Sort();
284 fonts.Insert( KICAD_FONT_NAME, 0 );
285 fonts.Insert( DEFAULT_FONT_NAME, 0 );
286
287 m_fontAttr = new wxGridCellAttr;
288 m_fontAttr->SetEditor( new GRID_CELL_COMBOBOX( fonts ) );
289
290 m_colorAttr = new wxGridCellAttr;
291 m_colorAttr->SetRenderer( new GRID_CELL_COLOR_RENDERER( m_dialog ) );
292 m_colorAttr->SetEditor( new GRID_CELL_COLOR_SELECTOR( m_dialog, aGrid ) );
293
294 m_eval = std::make_unique<NUMERIC_EVALUATOR>( m_frame->GetUserUnits() );
295
296 m_frame->Bind( EDA_EVT_UNITS_CHANGED, &FIELDS_GRID_TABLE<T>::onUnitsChanged, this );
297}
298
299
300template <class T>
302{
303 m_readOnlyAttr->DecRef();
304 m_fieldNameAttr->DecRef();
305 m_boolAttr->DecRef();
306 m_referenceAttr->DecRef();
307 m_valueAttr->DecRef();
308 m_footprintAttr->DecRef();
309 m_urlAttr->DecRef();
310 m_nonUrlAttr->DecRef();
311 m_filepathAttr->DecRef();
312 m_vAlignAttr->DecRef();
313 m_hAlignAttr->DecRef();
314 m_orientationAttr->DecRef();
315 m_netclassAttr->DecRef();
316 m_fontAttr->DecRef();
317 m_colorAttr->DecRef();
318
319 m_frame->Unbind( EDA_EVT_UNITS_CHANGED, &FIELDS_GRID_TABLE<T>::onUnitsChanged, this );
320}
321
322
323template <class T>
324void FIELDS_GRID_TABLE<T>::onUnitsChanged( wxCommandEvent& aEvent )
325{
326 if( GetView() )
327 GetView()->ForceRefresh();
328
329 aEvent.Skip();
330}
331
332
333template <class T>
335{
336 switch( aCol )
337 {
338 case FDC_NAME: return _( "Name" );
339 case FDC_VALUE: return _( "Value" );
340 case FDC_SHOWN: return _( "Show" );
341 case FDC_SHOW_NAME: return _( "Show Name" );
342 case FDC_H_ALIGN: return _( "H Align" );
343 case FDC_V_ALIGN: return _( "V Align" );
344 case FDC_ITALIC: return _( "Italic" );
345 case FDC_BOLD: return _( "Bold" );
346 case FDC_TEXT_SIZE: return _( "Text Size" );
347 case FDC_ORIENTATION: return _( "Orientation" );
348 case FDC_POSX: return _( "X Position" );
349 case FDC_POSY: return _( "Y Position" );
350 case FDC_FONT: return _( "Font" );
351 case FDC_COLOR: return _( "Color" );
352 case FDC_ALLOW_AUTOPLACE: return _( "Allow Autoplacement" );
353 default: wxFAIL; return wxEmptyString;
354 }
355}
356
357
358template <class T>
359bool FIELDS_GRID_TABLE<T>::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
360{
361 switch( aCol )
362 {
363 case FDC_NAME:
364 case FDC_VALUE:
365 case FDC_H_ALIGN:
366 case FDC_V_ALIGN:
367 case FDC_TEXT_SIZE:
368 case FDC_ORIENTATION:
369 case FDC_POSX:
370 case FDC_POSY:
371 case FDC_FONT:
372 case FDC_COLOR:
373 return aTypeName == wxGRID_VALUE_STRING;
374
375 case FDC_SHOWN:
376 case FDC_SHOW_NAME:
377 case FDC_ITALIC:
378 case FDC_BOLD:
380 return aTypeName == wxGRID_VALUE_BOOL;
381
382 default:
383 wxFAIL;
384 return false;
385 }
386}
387
388
389template <class T>
390bool FIELDS_GRID_TABLE<T>::CanSetValueAs( int aRow, int aCol, const wxString& aTypeName )
391{
392 return CanGetValueAs( aRow, aCol, aTypeName );
393}
394
395
396template <class T>
397wxGridCellAttr* FIELDS_GRID_TABLE<T>::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind )
398{
399 wxGridCellAttr* tmp;
400
401 switch( aCol )
402 {
403 case FDC_NAME:
404 if( aRow < m_mandatoryFieldCount )
405 {
406 tmp = m_fieldNameAttr->Clone();
407 tmp->SetReadOnly( true );
408 return tmp;
409 }
410 else
411 {
412 m_fieldNameAttr->IncRef();
413 return m_fieldNameAttr;
414 }
415
416 case FDC_VALUE:
417 if( m_parentType == SCH_SYMBOL_T && aRow == REFERENCE_FIELD )
418 {
419 m_referenceAttr->IncRef();
420 return m_referenceAttr;
421 }
422 else if( m_parentType == SCH_SYMBOL_T && aRow == VALUE_FIELD )
423 {
424 m_valueAttr->IncRef();
425 return m_valueAttr;
426 }
427 else if( m_parentType == SCH_SYMBOL_T && aRow == FOOTPRINT_FIELD )
428 {
429 m_footprintAttr->IncRef();
430 return m_footprintAttr;
431 }
432 else if( m_parentType == SCH_SYMBOL_T && aRow == DATASHEET_FIELD )
433 {
434 m_urlAttr->IncRef();
435 return m_urlAttr;
436 }
437 else if( m_parentType == SCH_SHEET_T && aRow == SHEETNAME )
438 {
439 m_referenceAttr->IncRef();
440 return m_referenceAttr;
441 }
442 else if( m_parentType == SCH_SHEET_T && aRow == SHEETFILENAME )
443 {
444 m_filepathAttr->IncRef();
445 return m_filepathAttr;
446 }
447 else if( ( m_parentType == SCH_LABEL_LOCATE_ANY_T )
448 && this->at( (size_t) aRow ).GetCanonicalName() == wxT( "Netclass" ) )
449 {
450 m_netclassAttr->IncRef();
451 return m_netclassAttr;
452 }
453 else
454 {
455 wxString fn = GetValue( aRow, FDC_NAME );
456
457 SCHEMATIC_SETTINGS* settings = m_frame->Prj().GetProjectFile().m_SchematicSettings;
458
459 const TEMPLATE_FIELDNAME* templateFn =
460 settings ? settings->m_TemplateFieldNames.GetFieldName( fn ) : nullptr;
461
462 if( templateFn && templateFn->m_URL )
463 {
464 m_urlAttr->IncRef();
465 return m_urlAttr;
466 }
467 else
468 {
469 m_nonUrlAttr->IncRef();
470 return m_nonUrlAttr;
471 }
472 }
473
474 case FDC_TEXT_SIZE:
475 case FDC_POSX:
476 case FDC_POSY:
477 return nullptr;
478
479 case FDC_H_ALIGN:
480 m_hAlignAttr->IncRef();
481 return m_hAlignAttr;
482
483 case FDC_V_ALIGN:
484 m_vAlignAttr->IncRef();
485 return m_vAlignAttr;
486
487 case FDC_ORIENTATION:
488 m_orientationAttr->IncRef();
489 return m_orientationAttr;
490
491 case FDC_SHOWN:
492 case FDC_SHOW_NAME:
493 case FDC_ITALIC:
494 case FDC_BOLD:
496 m_boolAttr->IncRef();
497 return m_boolAttr;
498
499 case FDC_FONT:
500 m_fontAttr->IncRef();
501 return m_fontAttr;
502
503 case FDC_COLOR:
504 m_colorAttr->IncRef();
505 return m_colorAttr;
506
507 default:
508 wxFAIL;
509 return nullptr;
510 }
511}
512
513
514template <class T>
515wxString FIELDS_GRID_TABLE<T>::GetValue( int aRow, int aCol )
516{
517 wxCHECK( aRow < GetNumberRows(), wxEmptyString );
518
519 wxGrid* grid = GetView();
520 const T& field = this->at( (size_t) aRow );
521
522 if( grid->GetGridCursorRow() == aRow && grid->GetGridCursorCol() == aCol
523 && grid->IsCellEditControlShown() )
524 {
525 auto it = m_evalOriginal.find( { aRow, aCol } );
526
527 if( it != m_evalOriginal.end() )
528 return it->second;
529 }
530
531 switch( aCol )
532 {
533 case FDC_NAME:
534 // Use default field names for mandatory and system fields because they are translated
535 // according to the current locale
536 if( m_parentType == SCH_SYMBOL_T )
537 {
538 if( aRow < m_mandatoryFieldCount )
540 else
541 return field.GetName( false );
542 }
543 else if( m_parentType == SCH_SHEET_T )
544 {
545 if( aRow < m_mandatoryFieldCount )
546 return SCH_SHEET::GetDefaultFieldName( aRow );
547 else
548 return field.GetName( false );
549 }
550 else if( m_parentType == SCH_LABEL_LOCATE_ANY_T )
551 {
552 return SCH_LABEL_BASE::GetDefaultFieldName( field.GetCanonicalName(), false );
553 }
554 else
555 {
556 wxFAIL_MSG( wxS( "Unhandled field owner type." ) );
557 return field.GetName( false );
558 }
559
560 case FDC_VALUE:
561 return UnescapeString( field.GetText() );
562
563 case FDC_SHOWN:
564 return StringFromBool( field.IsVisible() );
565
566 case FDC_SHOW_NAME:
567 return StringFromBool( field.IsNameShown() );
568
569 case FDC_H_ALIGN:
570 switch ( field.GetHorizJustify() )
571 {
572 case GR_TEXT_H_ALIGN_LEFT: return _( "Left" );
573 case GR_TEXT_H_ALIGN_CENTER: return _( "Center" );
574 case GR_TEXT_H_ALIGN_RIGHT: return _( "Right" );
575 }
576
577 break;
578
579 case FDC_V_ALIGN:
580 switch ( field.GetVertJustify() )
581 {
582 case GR_TEXT_V_ALIGN_TOP: return _( "Top" );
583 case GR_TEXT_V_ALIGN_CENTER: return _( "Center" );
584 case GR_TEXT_V_ALIGN_BOTTOM: return _( "Bottom" );
585 }
586
587 break;
588
589 case FDC_ITALIC:
590 return StringFromBool( field.IsItalic() );
591
592 case FDC_BOLD:
593 return StringFromBool( field.IsBold() );
594
595 case FDC_TEXT_SIZE:
596 return m_frame->StringFromValue( field.GetTextHeight(), true );
597
598 case FDC_ORIENTATION:
599 if( field.GetTextAngle().IsHorizontal() )
600 return _( "Horizontal" );
601 else
602 return _( "Vertical" );
603
604 case FDC_POSX:
605 return m_frame->StringFromValue( field.GetTextPos().x, true );
606
607 case FDC_POSY:
608 return m_frame->StringFromValue( field.GetTextPos().y, true );
609
610 case FDC_FONT:
611 if( field.GetFont() )
612 return field.GetFont()->GetName();
613 else
614 return DEFAULT_FONT_NAME;
615
616 case FDC_COLOR:
617 return field.GetTextColor().ToCSSString();
618
620 return StringFromBool( field.CanAutoplace() );
621
622 default:
623 // we can't assert here because wxWidgets sometimes calls this without checking
624 // the column type when trying to see if there's an overflow
625 break;
626 }
627
628 return wxT( "bad wxWidgets!" );
629}
630
631
632template <class T>
633bool FIELDS_GRID_TABLE<T>::GetValueAsBool( int aRow, int aCol )
634{
635 wxCHECK( aRow < GetNumberRows(), false );
636 const T& field = this->at( (size_t) aRow );
637
638 switch( aCol )
639 {
640 case FDC_SHOWN: return field.IsVisible();
641 case FDC_SHOW_NAME: return field.IsNameShown();
642 case FDC_ITALIC: return field.IsItalic();
643 case FDC_BOLD: return field.IsBold();
644 case FDC_ALLOW_AUTOPLACE: return field.CanAutoplace();
645 default:
646 wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
647 return false;
648 }
649}
650
651
652template <class T>
653void FIELDS_GRID_TABLE<T>::SetValue( int aRow, int aCol, const wxString &aValue )
654{
655 wxCHECK( aRow < GetNumberRows(), /*void*/ );
656 T& field = this->at( (size_t) aRow );
657 VECTOR2I pos;
658 wxString value = aValue;
659
660 switch( aCol )
661 {
662 case FDC_TEXT_SIZE:
663 case FDC_POSX:
664 case FDC_POSY:
665 m_eval->SetDefaultUnits( m_frame->GetUserUnits() );
666
667 if( m_eval->Process( value ) )
668 {
669 m_evalOriginal[ { aRow, aCol } ] = value;
670 value = m_eval->Result();
671 }
672
673 break;
674
675 default:
676 break;
677 }
678
679 switch( aCol )
680 {
681 case FDC_NAME:
682 field.SetName( value );
683 break;
684
685 case FDC_VALUE:
686 {
687 if( m_parentType == SCH_SHEET_T && aRow == SHEETFILENAME )
688 {
690 }
691 else if( m_parentType == LIB_SYMBOL_T && aRow == VALUE_FIELD )
692 {
693 value = EscapeString( value, CTX_LIBID );
694 }
695
696 field.SetText( value );
697 break;
698 }
699
700 case FDC_SHOWN:
701 field.SetVisible( BoolFromString( value ) );
702 break;
703
704 case FDC_SHOW_NAME:
705 field.SetNameShown( BoolFromString( value ) );
706 break;
707
708 case FDC_H_ALIGN:
709 if( value == _( "Left" ) )
710 field.SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
711 else if( value == _( "Center" ) )
712 field.SetHorizJustify( GR_TEXT_H_ALIGN_CENTER );
713 else if( value == _( "Right" ) )
714 field.SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
715 else
716 wxFAIL_MSG( wxT( "unknown horizontal alignment: " ) + value );
717
718 break;
719
720 case FDC_V_ALIGN:
721 if( value == _( "Top" ) )
722 field.SetVertJustify( GR_TEXT_V_ALIGN_TOP );
723 else if( value == _( "Center" ) )
724 field.SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
725 else if( value == _( "Bottom" ) )
726 field.SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
727 else
728 wxFAIL_MSG( wxT( "unknown vertical alignment: " ) + value);
729
730 break;
731
732 case FDC_ITALIC:
733 field.SetItalic( BoolFromString( value ) );
734 break;
735
736 case FDC_BOLD:
737 field.SetBold( BoolFromString( value ) );
738 break;
739
740 case FDC_TEXT_SIZE:
741 field.SetTextSize( VECTOR2I( m_frame->ValueFromString( value ),
742 m_frame->ValueFromString( value ) ) );
743 break;
744
745 case FDC_ORIENTATION:
746 if( value == _( "Horizontal" ) )
747 field.SetTextAngle( ANGLE_HORIZONTAL );
748 else if( value == _( "Vertical" ) )
749 field.SetTextAngle( ANGLE_VERTICAL );
750 else
751 wxFAIL_MSG( wxT( "unknown orientation: " ) + value );
752
753 break;
754
755 case FDC_POSX:
756 case FDC_POSY:
757 pos = field.GetTextPos();
758
759 if( aCol == FDC_POSX )
760 pos.x = m_frame->ValueFromString( value );
761 else
762 pos.y = m_frame->ValueFromString( value );
763
764 field.SetTextPos( pos );
765 break;
766
767 case FDC_FONT:
768 if( value == DEFAULT_FONT_NAME )
769 field.SetFont( nullptr );
770 else if( value == KICAD_FONT_NAME )
771 field.SetFont( KIFONT::FONT::GetFont( wxEmptyString, field.IsBold(), field.IsItalic() ) );
772 else
773 field.SetFont( KIFONT::FONT::GetFont( aValue, field.IsBold(), field.IsItalic() ) );
774
775 break;
776
777 case FDC_COLOR:
778 field.SetTextColor( wxColor( value ) );
779 break;
780
782 field.SetCanAutoplace( BoolFromString( value ) );
783 break;
784
785 default:
786 wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a string value" ), aCol ) );
787 break;
788 }
789
790 m_dialog->OnModify();
791
792 GetView()->Refresh();
793}
794
795
796template <class T>
797void FIELDS_GRID_TABLE<T>::SetValueAsBool( int aRow, int aCol, bool aValue )
798{
799 wxCHECK( aRow < GetNumberRows(), /*void*/ );
800 T& field = this->at( (size_t) aRow );
801
802 switch( aCol )
803 {
804 case FDC_SHOWN:
805 field.SetVisible( aValue );
806 break;
807
808 case FDC_SHOW_NAME:
809 field.SetNameShown( aValue );
810 break;
811
812 case FDC_ITALIC:
813 field.SetItalic( aValue );
814 break;
815
816 case FDC_BOLD:
817 field.SetBold( aValue );
818 break;
819
821 field.SetCanAutoplace( aValue );
822 break;
823
824 default:
825 wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
826 break;
827 }
828
829 m_dialog->OnModify();
830}
831
832
833// Explicit Instantiations
834
835template class FIELDS_GRID_TABLE<SCH_FIELD>;
836template class FIELDS_GRID_TABLE<LIB_FIELD>;
837
838
839void FIELDS_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
840{
841 if( m_grid->GetGridCursorRow() == FOOTPRINT_FIELD && m_grid->GetGridCursorCol() == FDC_VALUE )
842 {
843 menu.Append( MYID_SELECT_FOOTPRINT, _( "Select Footprint..." ),
844 _( "Browse for footprint" ) );
845 menu.AppendSeparator();
846 }
847 else if( m_grid->GetGridCursorRow() == DATASHEET_FIELD && m_grid->GetGridCursorCol() == FDC_VALUE )
848 {
849 menu.Append( MYID_SHOW_DATASHEET, _( "Show Datasheet" ),
850 _( "Show datasheet in browser" ) );
851 menu.AppendSeparator();
852 }
853
854 GRID_TRICKS::showPopupMenu( menu, aEvent );
855}
856
857
858void FIELDS_GRID_TRICKS::doPopupSelection( wxCommandEvent& event )
859{
860 if( event.GetId() == MYID_SELECT_FOOTPRINT )
861 {
862 // pick a footprint using the footprint picker.
863 wxString fpid = m_grid->GetCellValue( FOOTPRINT_FIELD, FDC_VALUE );
865
866 if( frame->ShowModal( &fpid, m_dlg ) )
867 m_grid->SetCellValue( FOOTPRINT_FIELD, FDC_VALUE, fpid );
868
869 frame->Destroy();
870 }
871 else if (event.GetId() == MYID_SHOW_DATASHEET )
872 {
873 wxString datasheet_uri = m_grid->GetCellValue( DATASHEET_FIELD, FDC_VALUE );
874 GetAssociatedDocument( m_dlg, datasheet_uri, &m_dlg->Prj(), m_dlg->Prj().SchSearchS() );
875 }
876 else
877 {
879 }
880}
881
882
883template <class T>
884wxString FIELDS_GRID_TABLE<T>::StringFromBool( bool aValue ) const
885{
886 if( aValue )
887 return wxT( "1" );
888 else
889 return wxT( "0" );
890}
891
892
893template <class T>
894bool FIELDS_GRID_TABLE<T>::BoolFromString( wxString aValue ) const
895{
896 if( aValue == wxS( "1" ) )
897 {
898 return true;
899 }
900 else if( aValue == wxS( "0" ) )
901 {
902 return false;
903 }
904 else
905 {
906 wxFAIL_MSG( wxString::Format( "string '%s' can't be converted to boolean correctly and "
907 "will be perceived as FALSE", aValue ) );
908 return false;
909 }
910}
const char * name
Definition: DXF_plotter.cpp:56
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
wxString GetValue(int aRow, int aCol) override
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
bool GetValueAsBool(int aRow, int aCol) override
void onUnitsChanged(wxCommandEvent &aEvent)
wxString GetColLabelValue(int aCol) override
void SetValue(int aRow, int aCol, const wxString &aValue) override
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
FIELDS_GRID_TABLE(DIALOG_SHIM *aDialog, SCH_BASE_FRAME *aFrame, WX_GRID *aGrid, LIB_SYMBOL *aSymbol)
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) override
wxString StringFromBool(bool aValue) const
void SetValueAsBool(int aRow, int aCol, bool aValue) override
bool BoolFromString(wxString aValue) const
void initGrid(WX_GRID *aGrid)
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent) override
virtual void doPopupSelection(wxCommandEvent &event) override
Editor for wxGrid cells that adds a file/folder browser to the grid input field.
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Definition: validators.h:47
virtual void SetValidator(const wxValidator &validator) override
Definition: validators.cpp:46
virtual void doPopupSelection(wxCommandEvent &event)
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
WX_GRID * m_grid
I don't own the grid, but he owns me.
Definition: grid_tricks.h:125
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:137
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:66
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:432
Define a library symbol object.
Definition: lib_symbol.h:99
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
std::shared_ptr< NET_SETTINGS > & NetSettings()
Definition: project_file.h:96
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:149
These settings were stored in SCH_BASE_FRAME previously.
TEMPLATES m_TemplateFieldNames
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Schematic editor (Eeschema) main window.
static const wxString GetDefaultFieldName(const wxString &aName, bool aUseDefaultName)
Definition: sch_label.cpp:184
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslated=true)
Definition: sch_sheet.cpp:54
Schematic symbol object.
Definition: sch_symbol.h:81
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:698
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve a list of the SCH_PINs for the given sheet path.
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:875
const wxString GetValueFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
Definition: sch_symbol.cpp:859
const TEMPLATE_FIELDNAME * GetFieldName(const wxString &aName)
Search for aName in the template field name list.
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition: common.cpp:380
#define _(s)
static constexpr EDA_ANGLE & ANGLE_HORIZONTAL
Definition: eda_angle.h:425
static constexpr EDA_ANGLE & ANGLE_VERTICAL
Definition: eda_angle.h:426
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:74
This file is part of the common library.
#define DEFAULT_FONT_NAME
static wxString netList(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH &aSheetPath)
@ MYID_SHOW_DATASHEET
@ MYID_SELECT_FOOTPRINT
@ FDC_TEXT_SIZE
@ FDC_BOLD
@ FDC_POSY
@ FDC_ITALIC
@ FDC_SHOW_NAME
@ FDC_NAME
@ FDC_H_ALIGN
@ FDC_COLOR
@ FDC_SHOWN
@ FDC_ALLOW_AUTOPLACE
@ FDC_VALUE
@ FDC_POSX
@ FDC_ORIENTATION
@ FDC_V_ALIGN
@ FDC_FONT
FONTCONFIG * Fontconfig()
Definition: fontconfig.cpp:54
@ FRAME_FOOTPRINT_VIEWER_MODAL
Definition: frame_type.h:43
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ GRIDTRICKS_FIRST_CLIENT_ID
Definition: grid_tricks.h:48
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
const std::string KiCadSchematicFileExtension
#define KICAD_FONT_NAME
see class PGM_BASE
@ SHEET_MANDATORY_FIELDS
The first 2 are mandatory, and must be instantiated in SCH_SHEET.
Definition: sch_sheet.h:49
@ SHEETNAME
Definition: sch_sheet.h:45
@ SHEETFILENAME
Definition: sch_sheet.h:46
Definitions of control validators for schematic dialogs.
#define SHEETFILENAME_V
#define FIELD_NAME
#define SHEETNAME_V
#define FIELD_VALUE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LINE
Definition: string_utils.h:60
@ CTX_LIBID
Definition: string_utils.h:55
@ CTX_CSV
Definition: string_utils.h:61
Hold a name of a symbol's field, field value, and default visibility.
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI=false)
Return a default symbol field name for field aFieldNdx for all components.
Definition for symbol library class.
#define DO_TRANSLATE
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ MANDATORY_FIELDS
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
@ LIB_SYMBOL_T
Definition: typeinfo.h:188
@ SCH_SYMBOL_T
Definition: typeinfo.h:146
@ SCH_SHEET_T
Definition: typeinfo.h:148
@ SCH_LABEL_LOCATE_ANY_T
Definition: typeinfo.h:165
Custom text control validator definitions.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588
Definition of file extensions used in Kicad.