KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_sheet_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) 2009 Wayne Stambaugh <[email protected]>
5 * Copyright The KiCad Developers, see CHANGELOG.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
26
27#include <kiface_base.h>
28#include <wx/string.h>
29#include <wx/log.h>
30#include <wx/tooltip.h>
31#include <common.h>
32#include <confirm.h>
33#include <kidialog.h>
34#include <validators.h>
35#include <wx_filename.h>
38#include <kiplatform/ui.h>
39#include <sch_commit.h>
40#include <sch_edit_frame.h>
41#include <sch_io/sch_io.h>
42#include <sch_sheet.h>
43#include <schematic.h>
44#include <bitmaps.h>
45#include <eeschema_settings.h>
47#include <trace_helpers.h>
49#include "wx/dcclient.h"
50#include "string_utils.h"
51
53 bool* aIsUndoable, bool* aClearAnnotationNewItems,
54 bool* aUpdateHierarchyNavigator,
55 wxString* aSourceSheetFilename ) :
57 m_frame( aParent ),
58 m_isUndoable( aIsUndoable ),
59 m_clearAnnotationNewItems( aClearAnnotationNewItems ),
60 m_updateHierarchyNavigator( aUpdateHierarchyNavigator ),
61 m_sourceSheetFilename( aSourceSheetFilename ),
62 m_borderWidth( aParent, m_borderWidthLabel, m_borderWidthCtrl, m_borderWidthUnits ),
63 m_dummySheet( *aSheet ),
64 m_dummySheetNameField( &m_dummySheet, FIELD_T::SHEET_NAME )
65{
66 m_sheet = aSheet;
67 m_fields = new FIELDS_GRID_TABLE( this, aParent, m_grid, m_sheet );
70
72 m_grid->PushEventHandler( new FIELDS_GRID_TRICKS( m_grid, this, { &aParent->Schematic() },
73 [&]( wxCommandEvent& aEvent )
74 {
75 OnAddField( aEvent );
76 } ) );
77 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
78
79 // Show/hide columns according to user's preference
80 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
81 {
82 m_grid->ShowHideColumns( cfg->m_Appearance.edit_sheet_visible_columns );
84 }
85
87 m_infoBar->ShowMessage( _( "Note: individual item colors overridden in Preferences." ) );
88
89 wxSize minSize = m_pageNumberTextCtrl->GetMinSize();
90 int minWidth = m_pageNumberTextCtrl->GetTextExtent( wxT( "XXX.XXX" ) ).GetWidth();
91
92 m_pageNumberTextCtrl->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
93
94 wxToolTip::Enable( true );
96
97 // Configure button logos
98 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
99 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
100 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
101 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
102
103 // Set font sizes
105 m_hierarchicalPath->SetFont( KIUI::GetSmallInfoFont( this ) );
106
107 // wxFormBuilder doesn't include this event...
108 m_grid->Connect( wxEVT_GRID_CELL_CHANGING,
110 nullptr, this );
111}
112
113
115{
116 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
117 {
118 cfg->m_Appearance.edit_sheet_visible_columns = m_grid->GetShownColumnsAsString();
119 cfg->m_Appearance.edit_sheet_width = GetSize().x;
120 cfg->m_Appearance.edit_sheet_height = GetSize().y;
121 }
122
123 // Prevents crash bug in wxGrid's d'tor
125
126 m_grid->Disconnect( wxEVT_GRID_CELL_CHANGING,
128 nullptr, this );
129
130 // Delete the GRID_TRICKS.
131 m_grid->PopEventHandler( true );
132}
133
134
136{
137 if( !wxDialog::TransferDataToWindow() )
138 return false;
139
140 // Push a copy of each field into m_updateFields
141 for( SCH_FIELD& field : m_sheet->GetFields() )
142 {
143 SCH_FIELD field_copy( field );
144
145#ifdef __WINDOWS__
146 // Filenames are stored using unix notation, so convert to Windows notation
147 if( field_copy.GetId() == FIELD_T::SHEET_FILENAME )
148 {
149 wxString filename = field_copy.GetText();
150 filename.Replace( wxT( "/" ), wxT( "\\" ) );
151 field_copy.SetText( filename );
152 }
153#endif
154
155 // change offset to be symbol-relative
156 field_copy.Offset( -m_sheet->GetPosition() );
157
158 m_fields->push_back( field_copy );
159 }
160
161 // notify the grid
162 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_fields->size() );
163 m_grid->ProcessTableMessage( msg );
165
166 // border width
168
169 // set up color swatches
170 KIGFX::COLOR4D borderColor = m_sheet->GetBorderColor();
171 KIGFX::COLOR4D backgroundColor = m_sheet->GetBackgroundColor();
172
173 m_borderSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
174 m_backgroundSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
175
176 m_borderSwatch->SetSwatchColor( borderColor, false );
177 m_backgroundSwatch->SetSwatchColor( backgroundColor, false );
178
182
184 instance.push_back( m_sheet );
185
186 m_pageNumberTextCtrl->ChangeValue( instance.GetPageNumber() );
187
191 m_cbDNP->SetValue( m_sheet->GetDNP() );
192
193 return true;
194}
195
196
198{
199 LIB_ID id;
200
201 if( !m_grid->CommitPendingChanges() || !m_grid->Validate() )
202 return false;
203
204 // Check for missing field names.
205 for( size_t i = 0; i < m_fields->size(); ++i )
206 {
207 SCH_FIELD& field = m_fields->at( i );
208
209 if( field.IsMandatory() )
210 continue;
211
212 if( field.GetName( false ).empty() && !field.GetText().empty() )
213 {
214 DisplayErrorMessage( this, _( "Fields must have a name." ) );
215
217 m_delayedFocusRow = (int) i;
218
219 return false;
220 }
221 }
222
223 return true;
224}
225
226
227static bool positioningChanged( const SCH_FIELD& a, const SCH_FIELD& b )
228{
229 if( a.GetPosition() != b.GetPosition() )
230 return true;
231
232 if( a.GetHorizJustify() != b.GetHorizJustify() )
233 return true;
234
235 if( a.GetVertJustify() != b.GetVertJustify() )
236 return true;
237
238 if( a.GetTextAngle() != b.GetTextAngle() )
239 return true;
240
241 return false;
242}
243
244
246{
249 {
250 return true;
251 }
252
255 {
256 return true;
257 }
258
259 return false;
260}
261
262
264{
265 wxCHECK( m_sheet && m_frame, false );
266
267 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
268 return false;
269
270 if( m_isUndoable )
271 *m_isUndoable = true;
272
273 // Sheet file names can be relative or absolute.
274 wxString sheetFileName = m_fields->GetField( FIELD_T::SHEET_FILENAME )->GetText();
275
276 // Ensure filepath is not empty. (In normal use will be caught by grid validators,
277 // but unedited data from existing files can be bad.)
278 if( sheetFileName.IsEmpty() )
279 {
280 DisplayError( this, _( "A sheet must have a valid file name." ) );
281 return false;
282 }
283
284 // Ensure the filename extension is OK. (In normal use will be caught by grid validators,
285 // but unedited data from existing files can be bad.)
286 sheetFileName = EnsureFileExtension( sheetFileName, FILEEXT::KiCadSchematicFileExtension );
287
288 // Ensure sheetFileName is legal
289 if( !IsFullFileNameValid( sheetFileName ) )
290 {
291 DisplayError( this, _( "A sheet must have a valid file name." ) );
292 return false;
293 }
294
295 wxFileName fn( sheetFileName );
296
297 wxString newRelativeFilename = fn.GetFullPath();
298
299 // Inside Eeschema, filenames are stored using unix notation
300 newRelativeFilename.Replace( wxT( "\\" ), wxT( "/" ) );
301
302 wxString oldFilename = m_sheet->GetField( FIELD_T::SHEET_FILENAME )->GetText();
303 oldFilename.Replace( wxT( "\\" ), wxT( "/" ) );
304
305 bool filename_changed = oldFilename != newRelativeFilename;
306
307 if( filename_changed || m_sheet->IsNew() )
308 {
309 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
310
311 wxCHECK( currentScreen, false );
312
313 bool clearFileName = false;
314
315 // This can happen for the root sheet when opening Eeschema in the stand alone mode.
316 if( currentScreen->GetFileName().IsEmpty() )
317 {
318 clearFileName = true;
319 currentScreen->SetFileName( m_frame->Prj().AbsolutePath( wxT( "noname.kicad_sch" ) ) );
320 }
321
322 wxFileName tmp( fn );
323 wxFileName screenFileName = currentScreen->GetFileName();
324
325 if( fn.IsAbsolute() && fn.MakeRelativeTo( screenFileName.GetPath() ) )
326 {
327 wxMessageDialog makeRelDlg( this, _( "Use relative path for sheet file?" ),
328 _( "Sheet File Path" ),
329 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION | wxCENTER );
330
331 makeRelDlg.SetExtendedMessage( _( "Using relative hierarchical sheet file name paths "
332 "improves schematic portability across systems and "
333 "platforms. Using absolute paths can result in "
334 "portability issues." ) );
335 makeRelDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Use Relative Path" ) ),
336 wxMessageDialog::ButtonLabel( _( "Use Absolute Path" ) ) );
337
338 if( makeRelDlg.ShowModal() == wxID_YES )
339 {
340 wxLogTrace( tracePathsAndFiles, "\n Converted absolute path: '%s'"
341 "\n to relative path: '%s'",
342 tmp.GetPath(),
343 fn.GetPath() );
344 m_fields->GetField( FIELD_T::SHEET_FILENAME )->SetText( fn.GetFullPath() );
345 newRelativeFilename = fn.GetFullPath();
346 }
347 }
348
349 if( !onSheetFilenameChanged( newRelativeFilename ) )
350 {
351 if( clearFileName )
352 currentScreen->SetFileName( wxEmptyString );
353 else
354 FindField( *m_fields, FIELD_T::SHEET_FILENAME )->SetText( oldFilename );
355
356 return false;
357 }
359 {
361 }
362
363 if( clearFileName )
364 currentScreen->SetFileName( wxEmptyString );
365
366 // One last validity check (and potential repair) just to be sure to be sure
367 SCH_SHEET_LIST repairedList;
368 repairedList.BuildSheetList( &m_frame->Schematic().Root(), true );
369 }
370
371 wxString newSheetname = m_fields->GetField( FIELD_T::SHEET_NAME )->GetText();
372
373 if( ( newSheetname != m_sheet->GetName() ) && m_updateHierarchyNavigator )
375
376 if( newSheetname.IsEmpty() )
377 newSheetname = _( "Untitled Sheet" );
378
379 m_fields->GetField( FIELD_T::SHEET_NAME )->SetText( newSheetname );
380
381 // change all field positions from relative to absolute
382 for( SCH_FIELD& m_field : *m_fields)
383 m_field.Offset( m_sheet->GetPosition() );
384
387
388 for( int ii = m_fields->GetNumberRows() - 1; ii >= 0; ii-- )
389 {
390 SCH_FIELD& field = m_fields->at( ii );
391
392 if( field.IsMandatory() )
393 continue;
394
395 const wxString& fieldName = field.GetCanonicalName();
396
397 if( field.IsEmpty() )
398 m_fields->erase( m_fields->begin() + ii );
399 else if( fieldName.IsEmpty() )
400 field.SetName( _( "untitled" ) );
401 }
402
404
406
407 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
408
409 if( colorSettings->GetOverrideSchItemColors()
412 {
413 wxPanel temp( this );
414 temp.Hide();
415 PANEL_EESCHEMA_COLOR_SETTINGS prefs( &temp );
416 wxString checkboxLabel = prefs.m_optOverrideColors->GetLabel();
417
418 KIDIALOG dlg( this, _( "Note: item colors are overridden in the current color theme." ),
420 dlg.ShowDetailedText( wxString::Format( _( "To see individual item colors uncheck '%s'\n"
421 "in Preferences > Schematic Editor > Colors." ),
422 checkboxLabel ) );
423 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
424 dlg.ShowModal();
425 }
426
429
433 m_sheet->SetDNP( m_cbDNP->GetValue() );
434
436
437 instance.push_back( m_sheet );
438
439 instance.SetPageNumber( m_pageNumberTextCtrl->GetValue() );
440
442
443 // Refresh all sheets in case ordering changed.
444 for( SCH_ITEM* item : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
445 m_frame->UpdateItem( item );
446
447 return true;
448}
449
450
451bool DIALOG_SHEET_PROPERTIES::onSheetFilenameChanged( const wxString& aNewFilename )
452{
453 wxString msg;
454 wxFileName sheetFileName( EnsureFileExtension( aNewFilename,
456
457 // Sheet file names are relative to the path of the current sheet. This allows for
458 // nesting of schematic files in subfolders. Screen file names are always absolute.
459 SCHEMATIC& schematic = m_frame->Schematic();
460 SCH_SHEET_LIST fullHierarchy = schematic.Hierarchy();
461 wxFileName screenFileName( sheetFileName );
462 wxFileName tmp( sheetFileName );
463 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
464
465 wxCHECK( currentScreen, false );
466
467 // SCH_SCREEN file names are always absolute.
468 wxFileName currentScreenFileName = currentScreen->GetFileName();
469
470 if( !screenFileName.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS,
471 currentScreenFileName.GetPath() ) )
472 {
473 msg = wxString::Format( _( "Cannot normalize new sheet schematic file path:\n"
474 "'%s'\n"
475 "against parent sheet schematic file path:\n"
476 "'%s'." ),
477 sheetFileName.GetPath(),
478 currentScreenFileName.GetPath() );
479 DisplayError( this, msg );
480 return false;
481 }
482
483 wxString newAbsoluteFilename = screenFileName.GetFullPath();
484
485 // Inside Eeschema, filenames are stored using unix notation
486 newAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
487
488 bool renameFile = false;
489 bool loadFromFile = false;
490 bool clearAnnotation = false;
491 bool isExistingSheet = false;
492 SCH_SCREEN* useScreen = nullptr;
493 SCH_SCREEN* oldScreen = nullptr;
494
495 // Search for a schematic file having the same filename already in use in the hierarchy
496 // or on disk, in order to reuse it.
497 if( !schematic.Root().SearchHierarchy( newAbsoluteFilename, &useScreen ) )
498 {
499 loadFromFile = wxFileExists( newAbsoluteFilename );
500
501 wxLogTrace( tracePathsAndFiles, "\n Sheet requested file '%s', %s",
502 newAbsoluteFilename,
503 loadFromFile ? "found" : "not found" );
504 }
505
506 if( m_sheet->GetScreen() == nullptr ) // New just created sheet.
507 {
509 newAbsoluteFilename ) )
510 return false;
511
512 if( useScreen || loadFromFile ) // Load from existing file.
513 {
514 clearAnnotation = true;
515
516 if( !IsOK( this, wxString::Format( _( "'%s' already exists." ),
517 sheetFileName.GetFullName() )
518 + wxT( "\n\n" )
519 + wxString::Format( _( "Link '%s' to this file?" ),
520 newAbsoluteFilename ) ) )
521 {
522 return false;
523 }
524 }
525 // If we are drawing a sheet from a design block/sheet import, we need to copy the
526 // sheet to the current directory.
527 else if( m_sourceSheetFilename && !m_sourceSheetFilename->IsEmpty() )
528 {
529 loadFromFile = true;
530
531 if( !wxCopyFile( *m_sourceSheetFilename, newAbsoluteFilename, false ) )
532 {
533 msg.Printf( _( "Failed to copy schematic file '%s' to destination '%s'." ),
534 currentScreenFileName.GetFullPath(), newAbsoluteFilename );
535
536 DisplayError( m_frame, msg );
537
538 return false;
539 }
540 }
541 else // New file.
542 {
543 m_frame->InitSheet( m_sheet, newAbsoluteFilename );
544 }
545 }
546 else // Existing sheet.
547 {
548 isExistingSheet = true;
549
551 newAbsoluteFilename ) )
552 return false;
553
554 // We are always using here a case insensitive comparison to avoid issues
555 // under Windows, although under Unix filenames are case sensitive.
556 // But many users create schematic under both Unix and Windows
557 // **
558 // N.B. 1: aSheet->GetFileName() will return a relative path
559 // aSheet->GetScreen()->GetFileName() returns a full path
560 //
561 // N.B. 2: newFilename uses the unix notation for separator.
562 // so we must use it also to compare the old and new filenames
563 wxString oldAbsoluteFilename = m_sheet->GetScreen()->GetFileName();
564 oldAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
565
566 if( newAbsoluteFilename.Cmp( oldAbsoluteFilename ) != 0 )
567 {
568 // Sheet file name changes cannot be undone.
569 if( m_isUndoable )
570 *m_isUndoable = false;
571
572 if( useScreen || loadFromFile ) // Load from existing file.
573 {
574 clearAnnotation = true;
575 oldScreen = m_sheet->GetScreen();
576
577 if( !IsOK( this, wxString::Format( _( "Change '%s' link from '%s' to '%s'?" ),
578 newAbsoluteFilename,
580 sheetFileName.GetFullName() )
581 + wxT( "\n\n" )
582 + _( "This action cannot be undone." ) ) )
583 {
584 return false;
585 }
586
587 if( loadFromFile )
588 m_sheet->SetScreen( nullptr );
589 }
590 else // Save to new file name.
591 {
592 if( m_sheet->GetScreenCount() > 1 )
593 {
594 if( !IsOK( this, wxString::Format( _( "Create new file '%s' with contents "
595 "of '%s'?" ),
596 sheetFileName.GetFullName(),
598 + wxT( "\n\n" )
599 + _( "This action cannot be undone." ) ) )
600 {
601 return false;
602 }
603 }
604
605 renameFile = true;
606 }
607 }
608
609 if( renameFile )
610 {
611 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
612
613 // If the associated screen is shared by more than one sheet, do not
614 // change the filename of the corresponding screen here.
615 // (a new screen will be created later)
616 // if it is not shared, update the filename
617 if( m_sheet->GetScreenCount() <= 1 )
618 m_sheet->GetScreen()->SetFileName( newAbsoluteFilename );
619
620 try
621 {
622 pi->SaveSchematicFile( newAbsoluteFilename, m_sheet, &schematic );
623 }
624 catch( const IO_ERROR& ioe )
625 {
626 msg = wxString::Format( _( "Error occurred saving schematic file '%s'." ),
627 newAbsoluteFilename );
628 DisplayErrorMessage( this, msg, ioe.What() );
629
630 msg = wxString::Format( _( "Failed to save schematic '%s'" ),
631 newAbsoluteFilename );
632 m_frame->SetMsgPanel( wxEmptyString, msg );
633 return false;
634 }
635
636 // If the associated screen is shared by more than one sheet, remove the
637 // screen and reload the file to a new screen. Failure to do this will trash
638 // the screen reference counting in complex hierarchies.
639 if( m_sheet->GetScreenCount() > 1 )
640 {
641 oldScreen = m_sheet->GetScreen();
642 m_sheet->SetScreen( nullptr );
643 loadFromFile = true;
644 }
645 }
646 }
647
648 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
649
650 if( useScreen )
651 {
652 // Create a temporary sheet for recursion testing to prevent a possible recursion error.
653 std::unique_ptr< SCH_SHEET> tmpSheet = std::make_unique<SCH_SHEET>( &schematic );
654 *tmpSheet->GetField( FIELD_T::SHEET_NAME ) = m_fields->GetField( FIELD_T::SHEET_NAME );
655 tmpSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( sheetFileName.GetFullPath() );
656 tmpSheet->SetScreen( useScreen );
657
658 // No need to check for valid library IDs if we are using an existing screen.
659 if( m_frame->CheckSheetForRecursion( tmpSheet.get(), &currentSheet ) )
660 return false;
661
662 // It's safe to set the sheet screen now.
663 m_sheet->SetScreen( useScreen );
664
665 SCH_SHEET_LIST sheetHierarchy( m_sheet ); // The hierarchy of the loaded file.
666
667 sheetHierarchy.AddNewSymbolInstances( currentSheet, m_frame->Prj().GetProjectName() );
668 sheetHierarchy.AddNewSheetInstances( currentSheet,
669 fullHierarchy.GetLastVirtualPageNumber() );
670 }
671 else if( loadFromFile )
672 {
673 bool restoreSheet = false;
674
675 if( isExistingSheet )
676 {
677 // Temporarily remove the sheet from the current schematic page so that recursion
678 // and symbol library link tests can be performed with the modified sheet settings.
679 restoreSheet = true;
680 currentSheet.LastScreen()->Remove( m_sheet );
681 }
682
683 if( !m_frame->LoadSheetFromFile( m_sheet, &currentSheet, newAbsoluteFilename, false, true )
684 || m_frame->CheckSheetForRecursion( m_sheet, &currentSheet ) )
685 {
686 if( restoreSheet )
687 {
688 // If we cleared the previous screen, restore it before returning to the user
689 if( oldScreen )
690 m_sheet->SetScreen( oldScreen );
691
692 currentSheet.LastScreen()->Append( m_sheet );
693 }
694
695 return false;
696 }
697
698 if( restoreSheet )
699 currentSheet.LastScreen()->Append( m_sheet );
700 }
701
703 *m_clearAnnotationNewItems = clearAnnotation;
704
705 // Rebuild the entire connection graph.
707
708 return true;
709}
710
711
713{
714 bool success = true;
715 wxGridCellEditor* editor = m_grid->GetCellEditor( event.GetRow(), event.GetCol() );
716 wxControl* control = editor->GetControl();
717
718 if( control && control->GetValidator() )
719 success = control->GetValidator()->Validate( control );
720
721 if( !success )
722 {
723 event.Veto();
724 m_delayedFocusRow = event.GetRow();
725 m_delayedFocusColumn = event.GetCol();
726 }
727
728 editor->DecRef();
729}
730
731
732void DIALOG_SHEET_PROPERTIES::OnAddField( wxCommandEvent& event )
733{
735 return;
736
737 SCH_FIELD newField( m_sheet, FIELD_T::SHEET_USER, GetUserFieldName( (int) m_fields->size(), DO_TRANSLATE ) );
738
739 newField.SetTextAngle( m_fields->GetField( FIELD_T::SHEET_NAME )->GetTextAngle() );
740 newField.SetVisible( false );
741 m_fields->push_back( newField );
742
743 // notify the grid
744 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
745 m_grid->ProcessTableMessage( msg );
746
747 m_grid->MakeCellVisible( m_fields->size() - 1, 0 );
748 m_grid->SetGridCursor( m_fields->size() - 1, 0 );
749
750 m_grid->EnableCellEditControl();
751 m_grid->ShowCellEditControl();
752}
753
754
755void DIALOG_SHEET_PROPERTIES::OnDeleteField( wxCommandEvent& event )
756{
757 wxArrayInt selectedRows = m_grid->GetSelectedRows();
758
759 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
760 selectedRows.push_back( m_grid->GetGridCursorRow() );
761
762 if( selectedRows.empty() )
763 return;
764
765 for( int row : selectedRows )
766 {
767 if( row < m_fields->GetMandatoryRowCount() )
768 {
769 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
771 return;
772 }
773 }
774
775 m_grid->CommitPendingChanges( true /* quiet mode */ );
776
777 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
778 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
779
780 for( int row : selectedRows )
781 {
782 m_grid->ClearSelection();
783 m_fields->erase( m_fields->begin() + row );
784
785 // notify the grid
786 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
787 m_grid->ProcessTableMessage( msg );
788
789 if( m_grid->GetNumberRows() > 0 )
790 {
791 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
792 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
793 }
794 }
795}
796
797
798void DIALOG_SHEET_PROPERTIES::OnMoveUp( wxCommandEvent& event )
799{
801 return;
802
803 int i = m_grid->GetGridCursorRow();
804
805 if( i > m_fields->GetMandatoryRowCount() )
806 {
807 SCH_FIELD tmp = m_fields->at( (unsigned) i );
808 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
809 m_fields->insert( m_fields->begin() + i - 1, tmp );
810 m_grid->ForceRefresh();
811
812 m_grid->SetGridCursor( i - 1, m_grid->GetGridCursorCol() );
813 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
814 }
815 else
816 {
817 wxBell();
818 }
819}
820
821
822void DIALOG_SHEET_PROPERTIES::OnMoveDown( wxCommandEvent& event )
823{
825 return;
826
827 int i = m_grid->GetGridCursorRow();
828
829 if( i >= m_fields->GetMandatoryRowCount() && i < m_grid->GetNumberRows() - 1 )
830 {
831 SCH_FIELD tmp = m_fields->at( (unsigned) i );
832 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
833 m_fields->insert( m_fields->begin() + i + 1, tmp );
834 m_grid->ForceRefresh();
835
836 m_grid->SetGridCursor( i + 1, m_grid->GetGridCursorCol() );
837 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
838 }
839 else
840 {
841 wxBell();
842 }
843}
844
845
847{
848 // Account for scroll bars
850
851 m_grid->AutoSizeColumn( 0 );
852 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
853
854 int fixedColsWidth = m_grid->GetColSize( 0 );
855
856 for( int i = 2; i < m_grid->GetNumberCols(); i++ )
857 fixedColsWidth += m_grid->GetColSize( i );
858
859 m_grid->SetColSize( 1, std::max( 120, width - fixedColsWidth ) );
860}
861
862
863void DIALOG_SHEET_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
864{
865 std::bitset<64> shownColumns = m_grid->GetShownColumns();
866
867 if( shownColumns != m_shownColumns )
868 {
869 m_shownColumns = shownColumns;
870
871 if( !m_grid->IsCellEditControlShown() )
873 }
874
875 // Propagate changes in sheetname to displayed hierarchical path
876 int sheetnameRow = m_fields->GetFieldRow( FIELD_T::SHEET_NAME );
877 wxString path = m_frame->GetCurrentSheet().PathHumanReadable( false );
878
879 if( path.Last() != '/' )
880 path.Append( '/' );
881
882 wxGridCellEditor* editor = m_grid->GetCellEditor( sheetnameRow, FDC_VALUE );
883 wxControl* control = editor->GetControl();
884 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
885 wxString sheetName;
886
887 if( textControl )
888 sheetName = textControl->GetValue();
889 else
890 sheetName = m_grid->GetCellValue( sheetnameRow, FDC_VALUE );
891
893 m_dummySheetNameField.SetText( sheetName );
895
896 editor->DecRef();
897
898 wxClientDC dc( m_hierarchicalPathLabel );
899 int width = m_sizerBottom->GetSize().x - m_stdDialogButtonSizer->GetSize().x
900 - m_hierarchicalPathLabel->GetSize().x
901 - 30;
902
903 path = wxControl::Ellipsize( path, dc, wxELLIPSIZE_START, width, wxELLIPSIZE_FLAGS_NONE );
904
905 if( m_hierarchicalPath->GetLabel() != path )
906 m_hierarchicalPath->SetLabel( path );
907
908 // Handle a delayed focus
909 if( m_delayedFocusRow >= 0 )
910 {
911 m_grid->SetFocus();
912 m_grid->MakeCellVisible( m_delayedFocusRow, m_delayedFocusColumn );
914
915 m_grid->EnableCellEditControl( true );
916 m_grid->ShowCellEditControl();
917
920 }
921}
922
923
924void DIALOG_SHEET_PROPERTIES::OnSizeGrid( wxSizeEvent& event )
925{
926 auto new_size = event.GetSize();
927
928 if( m_size != new_size )
929 {
930 m_size = new_size;
931
933 }
934
935 // Always propagate for a grid repaint (needed if the height changes, as well as width)
936 event.Skip();
937}
938
939
940void DIALOG_SHEET_PROPERTIES::OnInitDlg( wxInitDialogEvent& event )
941{
943
944 // Now all widgets have the size fixed, call FinishDialogSettings
946
947 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
948
949 if( cfg && cfg->m_Appearance.edit_sheet_width > 0 && cfg->m_Appearance.edit_sheet_height > 0 )
951
952}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Color settings are a bit different than most of the settings objects in that there can be more than o...
bool GetOverrideSchItemColors() const
COLOR4D GetColor(int aLayer) const
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
KIGFX::COLOR4D GetSwatchColor() const
void SetDefaultColor(const KIGFX::COLOR4D &aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
void SetSwatchBackground(const KIGFX::COLOR4D &aBackground)
Set the swatch background color.
Class DIALOG_SHEET_PROPERTIES_BASE.
wxStdDialogButtonSizer * m_stdDialogButtonSizer
void OnAddField(wxCommandEvent &event) override
void OnMoveDown(wxCommandEvent &event) override
void OnGridCellChanging(wxGridEvent &event)
void OnDeleteField(wxCommandEvent &event) override
bool onSheetFilenameChanged(const wxString &aNewFilename)
void OnSizeGrid(wxSizeEvent &event) override
void OnInitDlg(wxInitDialogEvent &event) override
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnMoveUp(wxCommandEvent &event) override
DIALOG_SHEET_PROPERTIES(SCH_EDIT_FRAME *aParent, SCH_SHEET *aSheet, bool *aIsUndoable, bool *aClearAnnotationNewItems, bool *aUpdateHierarchyNavigator, wxString *aSourceSheetFilename)
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...
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
bool IsNew() const
Definition: eda_item.h:123
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:144
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:97
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:596
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:197
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:386
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:200
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:299
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:241
SCH_FIELD * GetField(FIELD_T aFieldId)
int GetNumberRows() override
int GetMandatoryRowCount() const
int GetFieldRow(FIELD_T aFieldId)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:50
@ KD_WARNING
Definition: kidialog.h:53
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:160
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:372
Holds all the data relating to one schematic.
Definition: schematic.h:87
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:219
SCH_SHEET & Root() const
Definition: schematic.h:139
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Returns a pointer to the active color theme settings.
Schematic editor (Eeschema) main window.
bool AllowCaseSensitiveFileNameClashes(const wxString &aOldName, const wxString &aSchematicFileName)
Check aSchematicFileName for a potential file name case sensitivity clashes.
Definition: sheet.cpp:720
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void InitSheet(SCH_SHEET *aSheet, const wxString &aNewFilename)
Definition: sheet.cpp:110
bool LoadSheetFromFile(SCH_SHEET *aSheet, SCH_SHEET_PATH *aCurrentSheet, const wxString &aFileName, bool aSkipRecursionCheck=false, bool aSkipLibCheck=false)
Load a the KiCad schematic file aFileName into the sheet aSheet.
Definition: sheet.cpp:169
SCH_SHEET_PATH & GetCurrentSheet() const
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, PROGRESS_REPORTER *aProgressReporter=nullptr)
Generate the connection data for the entire schematic hierarchy.
SCHEMATIC & Schematic() const
bool CheckSheetForRecursion(SCH_SHEET *aSheet, SCH_SHEET_PATH *aCurrentSheet)
Verify that aSheet will not cause a recursion error in aCurrentSheet.
Definition: sheet.cpp:54
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false) override
Mark an item for refresh.
void TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
bool IsMandatory() const
Definition: sch_field.cpp:1366
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1344
FIELD_T GetId() const
Definition: sch_field.h:115
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1120
bool IsEmpty()
Return true if both the name and value of the field are empty.
Definition: sch_field.h:154
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1106
void SetName(const wxString &aName)
Definition: sch_field.cpp:1082
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:191
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1092
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition: sch_item.h:594
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:160
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition: sch_screen.h:112
const wxString & GetFileName() const
Definition: sch_screen.h:147
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:123
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
Definition: sch_screen.cpp:330
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void AddNewSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath, const wxString &aProjectName)
Attempt to add new symbol instances for all symbols in this list of sheet paths prefixed with aPrefix...
int GetLastVirtualPageNumber() const
void AddNewSheetInstances(const SCH_SHEET_PATH &aPrefixSheetPath, int aLastVirtualPageNumber)
void BuildSheetList(SCH_SHEET *aSheet, bool aCheckIntegrity)
Build the list of sheets and their sheet path from aSheet.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false) const
Return the sheet path in a human readable form made from the sheet names.
SCH_SCREEN * LastScreen()
wxString GetPageNumber() const
void SetPageNumber(const wxString &aPageNumber)
Set the sheet instance user definable page number.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:47
bool GetExcludedFromBoard() const override
Definition: sch_sheet.h:397
void SetBorderColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:125
void SetExcludedFromBoard(bool aExcludeFromBoard) override
Set or clear exclude from board netlist flag.
Definition: sch_sheet.h:396
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:321
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition: sch_sheet.h:87
bool GetExcludedFromBOM() const override
Definition: sch_sheet.h:391
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
Definition: sch_sheet.cpp:367
wxString GetName() const
Definition: sch_sheet.h:113
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:751
void SetBackgroundColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:128
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:116
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:415
void SetExcludedFromBOM(bool aExcludeFromBOM) override
Set or clear the exclude from schematic bill of materials flag.
Definition: sch_sheet.h:390
void SetFields(const std::vector< SCH_FIELD > &aFields)
Set multiple schematic fields.
Definition: sch_sheet.cpp:389
int GetScreenCount() const
Return the number of times the associated screen for the sheet is being used.
Definition: sch_sheet.cpp:160
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:137
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:124
void SetBorderWidth(int aWidth)
Definition: sch_sheet.h:122
bool GetExcludedFromSim() const override
Definition: sch_sheet.h:385
int GetBorderWidth() const
Definition: sch_sheet.h:121
void SetDNP(bool aDNP) override
Definition: sch_sheet.h:403
bool GetDNP() const override
Set or clear the 'Do Not Populate' flags.
Definition: sch_sheet.h:402
void SetExcludedFromSim(bool aExcludeFromSim) override
Set or clear the exclude from simulation flag.
Definition: sch_sheet.h:384
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:127
void SetBitmap(const wxBitmapBundle &aBmp)
int GetIntValue()
Definition: unit_binder.h:129
virtual void SetValue(long long int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:490
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid's SetTable() method with one which doesn't mess up the grid column widths when setting th...
Definition: wx_grid.cpp:273
void DestroyTable(wxGridTableBase *aTable)
Work-around for a bug in wxGrid which crashes when deleting the table if the cell edit control was no...
Definition: wx_grid.cpp:446
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:460
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:479
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:640
void ShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION) override
Show the info bar with the provided message and icon.
Definition: wx_infobar.cpp:154
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:427
The common library.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:251
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:169
This file is part of the common library.
static bool positioningChanged(const SCH_FIELD &a, const SCH_FIELD &b)
static bool positioningChanged(const SCH_FIELD &a, const SCH_FIELD &b)
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
@ FDC_NAME
@ FDC_VALUE
static const std::string KiCadSchematicFileExtension
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
This file is part of the common library.
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:478
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition: wxgtk/ui.cpp:252
KICOMMON_API wxFont GetSmallInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:162
const SCH_FIELD * FindField(const std::vector< SCH_FIELD > &aFields, FIELD_T aFieldId)
Definition: sch_field.h:358
@ AUTOPLACE_NONE
Definition: sch_item.h:70
@ GLOBAL_CLEANUP
Definition: schematic.h:76
bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ SHEET_FILENAME
wxLogTrace helper definitions.
@ SCH_SHEET_T
Definition: typeinfo.h:176
Custom text control validator definitions.
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39