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 (C) 2014-2024 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( VECTOR2I( -1, -1 ), SHEETNAME, &m_dummySheet )
65{
66 m_sheet = aSheet;
67 m_fields = new FIELDS_GRID_TABLE( this, aParent, m_grid, m_sheet );
70
71 // Give a bit more room for combobox editors
72 m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
73
75 m_grid->PushEventHandler( new FIELDS_GRID_TRICKS( m_grid, this, &aParent->Schematic(),
76 [&]( wxCommandEvent& aEvent )
77 {
78 OnAddField( aEvent );
79 } ) );
80 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
81
82 // Show/hide columns according to user's preference
83 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
84 {
85 m_grid->ShowHideColumns( cfg->m_Appearance.edit_sheet_visible_columns );
87 }
88
90 m_infoBar->ShowMessage( _( "Note: individual item colors overridden in Preferences." ) );
91
92 wxSize minSize = m_pageNumberTextCtrl->GetMinSize();
93 int minWidth = m_pageNumberTextCtrl->GetTextExtent( wxT( "XXX.XXX" ) ).GetWidth();
94
95 m_pageNumberTextCtrl->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
96
97 wxToolTip::Enable( true );
99
100 // Configure button logos
101 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
102 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
103 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
104 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
105
106 // Set font sizes
107 m_hierarchicalPathLabel->SetFont( KIUI::GetInfoFont( this ) );
108 m_hierarchicalPath->SetFont( KIUI::GetInfoFont( this ) );
109
110 // wxFormBuilder doesn't include this event...
111 m_grid->Connect( wxEVT_GRID_CELL_CHANGING,
113 nullptr, this );
114}
115
116
118{
119 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
120 {
121 cfg->m_Appearance.edit_sheet_visible_columns = m_grid->GetShownColumnsAsString();
122 cfg->m_Appearance.edit_sheet_width = GetSize().x;
123 cfg->m_Appearance.edit_sheet_height = GetSize().y;
124 }
125
126 // Prevents crash bug in wxGrid's d'tor
128
129 m_grid->Disconnect( wxEVT_GRID_CELL_CHANGING,
131 nullptr, this );
132
133 // Delete the GRID_TRICKS.
134 m_grid->PopEventHandler( true );
135}
136
137
139{
140 if( !wxDialog::TransferDataToWindow() )
141 return false;
142
143 // Push a copy of each field into m_updateFields
144 for( SCH_FIELD& field : m_sheet->GetFields() )
145 {
146 SCH_FIELD field_copy( field );
147
148#ifdef __WINDOWS__
149 // Filenames are stored using unix notation
150 if( field_copy.GetId() == SHEETFILENAME )
151 {
152 wxString filename = field_copy.GetText();
153 filename.Replace( wxT( "/" ), wxT( "\\" ) );
154 field_copy.SetText( filename );
155 }
156#endif
157
158 // change offset to be symbol-relative
159 field_copy.Offset( -m_sheet->GetPosition() );
160
161 m_fields->push_back( field_copy );
162 }
163
164 // notify the grid
165 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_fields->size() );
166 m_grid->ProcessTableMessage( msg );
168
169 // border width
171
172 // set up color swatches
173 KIGFX::COLOR4D borderColor = m_sheet->GetBorderColor();
174 KIGFX::COLOR4D backgroundColor = m_sheet->GetBackgroundColor();
175
176 m_borderSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
177 m_backgroundSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
178
179 m_borderSwatch->SetSwatchColor( borderColor, false );
180 m_backgroundSwatch->SetSwatchColor( backgroundColor, false );
181
185
187 instance.push_back( m_sheet );
188
189 m_pageNumberTextCtrl->ChangeValue( instance.GetPageNumber() );
190
194 m_cbDNP->SetValue( m_sheet->GetDNP() );
195
196 return true;
197}
198
199
201{
202 LIB_ID id;
203
204 if( !m_grid->CommitPendingChanges() || !m_grid->Validate() )
205 return false;
206
207 // Check for missing field names.
208 for( size_t i = SHEET_MANDATORY_FIELDS; i < m_fields->size(); ++i )
209 {
210 SCH_FIELD& field = m_fields->at( i );
211
212 if( field.GetName( false ).empty() && !field.GetText().empty() )
213 {
214 DisplayErrorMessage( this, _( "Fields must have a name." ) );
215
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
245static bool positioningChanged( FIELDS_GRID_TABLE* a, std::vector<SCH_FIELD>& b )
246{
247 for( size_t i = 0; i < SHEET_MANDATORY_FIELDS; ++i )
248 {
249 if( positioningChanged( a->at( i ), b.at( i ) ) )
250 return true;
251 }
252
253 return false;
254}
255
256
258{
259 wxCHECK( m_sheet && m_frame, false );
260
261 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
262 return false;
263
264 SCH_COMMIT commit( m_frame );
265
266 commit.Modify( m_sheet, m_frame->GetScreen() );
267
268 if( m_isUndoable )
269 *m_isUndoable = true;
270
271 // Sheet file names can be relative or absolute.
272 wxString sheetFileName = m_fields->at( SHEETFILENAME ).GetText();
273
274 // Ensure filepath is not empty. (In normal use will be caught by grid validators,
275 // but unedited data from existing files can be bad.)
276 if( sheetFileName.IsEmpty() )
277 {
278 DisplayError( this, _( "A sheet must have a valid file name." ) );
279 return false;
280 }
281
282 // Ensure the filename extension is OK. (In normal use will be caught by grid validators,
283 // but unedited data from existing files can be bad.)
284 sheetFileName = EnsureFileExtension( sheetFileName, FILEEXT::KiCadSchematicFileExtension );
285
286 // Ensure sheetFileName is legal
287 if( !IsFullFileNameValid( sheetFileName ) )
288 {
289 DisplayError( this, _( "A sheet must have a valid file name." ) );
290 return false;
291 }
292
293 wxFileName fn( sheetFileName );
294
295 wxString newRelativeFilename = fn.GetFullPath();
296
297 // Inside Eeschema, filenames are stored using unix notation
298 newRelativeFilename.Replace( wxT( "\\" ), wxT( "/" ) );
299
300 wxString oldFilename = m_sheet->GetFields()[ SHEETFILENAME ].GetText();
301 oldFilename.Replace( wxT( "\\" ), wxT( "/" ) );
302
303 bool filename_changed = oldFilename != newRelativeFilename;
304
305 if( filename_changed || m_sheet->IsNew() )
306 {
307 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
308
309 wxCHECK( currentScreen, false );
310
311 bool clearFileName = false;
312
313 // This can happen for the root sheet when opening Eeschema in the stand alone mode.
314 if( currentScreen->GetFileName().IsEmpty() )
315 {
316 clearFileName = true;
317 currentScreen->SetFileName( m_frame->Prj().AbsolutePath( wxT( "noname.kicad_sch" ) ) );
318 }
319
320 wxFileName tmp( fn );
321 wxFileName screenFileName = currentScreen->GetFileName();
322
323 if( fn.IsAbsolute() && fn.MakeRelativeTo( screenFileName.GetPath() ) )
324 {
325 wxMessageDialog makeRelDlg( this, _( "Use relative path for sheet file?" ),
326 _( "Sheet File Path" ),
327 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION | wxCENTER );
328
329 makeRelDlg.SetExtendedMessage( _( "Using relative hierarchical sheet file name paths "
330 "improves schematic portability across systems and "
331 "platforms. Using absolute paths can result in "
332 "portability issues." ) );
333 makeRelDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Use Relative Path" ) ),
334 wxMessageDialog::ButtonLabel( _( "Use Absolute Path" ) ) );
335
336 if( makeRelDlg.ShowModal() == wxID_YES )
337 {
338 wxLogTrace( tracePathsAndFiles, "\n Converted absolute path: '%s'"
339 "\n to relative path: '%s'",
340 tmp.GetPath(),
341 fn.GetPath() );
342 m_fields->at( SHEETFILENAME ).SetText( fn.GetFullPath() );
343 newRelativeFilename = fn.GetFullPath();
344 }
345 }
346
347 if( !onSheetFilenameChanged( newRelativeFilename ) )
348 {
349 if( clearFileName )
350 currentScreen->SetFileName( wxEmptyString );
351
352 return false;
353 }
355 {
357 }
358
359 if( clearFileName )
360 currentScreen->SetFileName( wxEmptyString );
361
362 // One last validity check (and potential repair) just to be sure to be sure
363 SCH_SHEET_LIST repairedList;
364 repairedList.BuildSheetList( &m_frame->Schematic().Root(), true );
365 }
366
367 wxString newSheetname = m_fields->at( SHEETNAME ).GetText();
368
369 if( ( newSheetname != m_sheet->GetName() ) && m_updateHierarchyNavigator )
371
372 if( newSheetname.IsEmpty() )
373 newSheetname = _( "Untitled Sheet" );
374
375 m_fields->at( SHEETNAME ).SetText( newSheetname );
376
377 // change all field positions from relative to absolute
378 for( unsigned i = 0; i < m_fields->size(); ++i )
379 m_fields->at( i ).Offset( m_sheet->GetPosition() );
380
383
384 for( int ii = m_fields->GetNumberRows() - 1; ii >= SHEET_MANDATORY_FIELDS; ii-- )
385 {
386 SCH_FIELD& field = m_fields->at( ii );
387 const wxString& fieldName = field.GetCanonicalName();
388
389 if( field.IsEmpty() )
390 m_fields->erase( m_fields->begin() + ii );
391 else if( fieldName.IsEmpty() )
392 field.SetName( _( "untitled" ) );
393 }
394
396
398
399 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
400
401 if( colorSettings->GetOverrideSchItemColors()
404 {
405 wxPanel temp( this );
406 temp.Hide();
407 PANEL_EESCHEMA_COLOR_SETTINGS prefs( &temp );
408 wxString checkboxLabel = prefs.m_optOverrideColors->GetLabel();
409
410 KIDIALOG dlg( this, _( "Note: item colors are overridden in the current color theme." ),
412 dlg.ShowDetailedText( wxString::Format( _( "To see individual item colors uncheck '%s'\n"
413 "in Preferences > Schematic Editor > Colors." ),
414 checkboxLabel ) );
415 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
416 dlg.ShowModal();
417 }
418
421
425 m_sheet->SetDNP( m_cbDNP->GetValue() );
426
428
429 instance.push_back( m_sheet );
430
431 instance.SetPageNumber( m_pageNumberTextCtrl->GetValue() );
432
434
435 // Refresh all sheets in case ordering changed.
436 for( SCH_ITEM* item : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
437 m_frame->UpdateItem( item );
438
439 return true;
440}
441
442
443bool DIALOG_SHEET_PROPERTIES::onSheetFilenameChanged( const wxString& aNewFilename )
444{
445 wxString msg;
446 wxFileName sheetFileName( EnsureFileExtension( aNewFilename,
448 // Sheet file names are relative to the path of the current sheet. This allows for
449 // nesting of schematic files in subfolders. Screen file names are always absolute.
450 SCHEMATIC& schematic = m_frame->Schematic();
451 SCH_SHEET_LIST fullHierarchy = schematic.Hierarchy();
452 wxFileName screenFileName( sheetFileName );
453 wxFileName tmp( sheetFileName );
454 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
455
456 wxCHECK( currentScreen, false );
457
458 // SCH_SCREEN file names are always absolute.
459 wxFileName currentScreenFileName = currentScreen->GetFileName();
460
461 if( !screenFileName.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS,
462 currentScreenFileName.GetPath() ) )
463 {
464 msg = wxString::Format( _( "Cannot normalize new sheet schematic file path:\n"
465 "'%s'\n"
466 "against parent sheet schematic file path:\n"
467 "'%s'." ),
468 sheetFileName.GetPath(),
469 currentScreenFileName.GetPath() );
470 DisplayError( this, msg );
471 return false;
472 }
473
474 wxString newAbsoluteFilename = screenFileName.GetFullPath();
475
476 // Inside Eeschema, filenames are stored using unix notation
477 newAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
478
479 bool renameFile = false;
480 bool loadFromFile = false;
481 bool clearAnnotation = false;
482 bool isExistingSheet = false;
483 SCH_SCREEN* useScreen = nullptr;
484 SCH_SCREEN* oldScreen = nullptr;
485
486 // Search for a schematic file having the same filename already in use in the hierarchy
487 // or on disk, in order to reuse it.
488 if( !schematic.Root().SearchHierarchy( newAbsoluteFilename, &useScreen ) )
489 {
490 loadFromFile = wxFileExists( newAbsoluteFilename );
491
492 wxLogTrace( tracePathsAndFiles, "\n Sheet requested file '%s', %s",
493 newAbsoluteFilename,
494 loadFromFile ? "found" : "not found" );
495 }
496
497 if( m_sheet->GetScreen() == nullptr ) // New just created sheet.
498 {
499 if( !m_frame->AllowCaseSensitiveFileNameClashes( m_sheet->GetFileName(), newAbsoluteFilename ) )
500 return false;
501
502 if( useScreen || loadFromFile ) // Load from existing file.
503 {
504 clearAnnotation = true;
505
506 if( !IsOK( this, wxString::Format( _( "'%s' already exists." ),
507 sheetFileName.GetFullName() )
508 + wxT( "\n\n" )
509 + wxString::Format( _( "Link '%s' to this file?" ),
510 newAbsoluteFilename ) ) )
511 {
512 return false;
513 }
514 }
515 // If we are drawing a sheet from a design block/sheet import, we need to copy the sheet to the current directory.
516 else if( m_sourceSheetFilename && !m_sourceSheetFilename->IsEmpty() )
517 {
518 loadFromFile = true;
519
520 if( !wxCopyFile( *m_sourceSheetFilename, newAbsoluteFilename, false ) )
521 {
522 msg.Printf( _( "Failed to copy schematic file '%s' to destination '%s'." ),
523 currentScreenFileName.GetFullPath(), newAbsoluteFilename );
524
525 DisplayError( m_frame, msg );
526
527 return false;
528 }
529 }
530 else // New file.
531 {
532 m_frame->InitSheet( m_sheet, newAbsoluteFilename );
533 }
534 }
535 else // Existing sheet.
536 {
537 isExistingSheet = true;
538
539 if( !m_frame->AllowCaseSensitiveFileNameClashes( m_sheet->GetFileName(), newAbsoluteFilename ) )
540 return false;
541
542 // We are always using here a case insensitive comparison to avoid issues
543 // under Windows, although under Unix filenames are case sensitive.
544 // But many users create schematic under both Unix and Windows
545 // **
546 // N.B. 1: aSheet->GetFileName() will return a relative path
547 // aSheet->GetScreen()->GetFileName() returns a full path
548 //
549 // N.B. 2: newFilename uses the unix notation for separator.
550 // so we must use it also to compare the old and new filenames
551 wxString oldAbsoluteFilename = m_sheet->GetScreen()->GetFileName();
552 oldAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
553
554 if( newAbsoluteFilename.Cmp( oldAbsoluteFilename ) != 0 )
555 {
556 // Sheet file name changes cannot be undone.
557 if( m_isUndoable )
558 *m_isUndoable = false;
559
560 if( useScreen || loadFromFile ) // Load from existing file.
561 {
562 clearAnnotation = true;
563
564 if( !IsOK( this, wxString::Format( _( "Change '%s' link from '%s' to '%s'?" ),
565 newAbsoluteFilename,
567 sheetFileName.GetFullName() )
568 + wxT( "\n\n" )
569 + _( "This action cannot be undone." ) ) )
570 {
571 return false;
572 }
573
574 if( loadFromFile )
575 m_sheet->SetScreen( nullptr );
576 }
577 else // Save to new file name.
578 {
579 if( m_sheet->GetScreenCount() > 1 )
580 {
581 if( !IsOK( this, wxString::Format( _( "Create new file '%s' with contents "
582 "of '%s'?" ),
583 sheetFileName.GetFullName(),
585 + wxT( "\n\n" )
586 + _( "This action cannot be undone." ) ) )
587 {
588 return false;
589 }
590 }
591
592 renameFile = true;
593 }
594 }
595
596 if( renameFile )
597 {
598 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
599
600 // If the associated screen is shared by more than one sheet, do not
601 // change the filename of the corresponding screen here.
602 // (a new screen will be created later)
603 // if it is not shared, update the filename
604 if( m_sheet->GetScreenCount() <= 1 )
605 m_sheet->GetScreen()->SetFileName( newAbsoluteFilename );
606
607 try
608 {
609 pi->SaveSchematicFile( newAbsoluteFilename, m_sheet, &schematic );
610 }
611 catch( const IO_ERROR& ioe )
612 {
613 msg = wxString::Format( _( "Error occurred saving schematic file '%s'." ),
614 newAbsoluteFilename );
615 DisplayErrorMessage( this, msg, ioe.What() );
616
617 msg = wxString::Format( _( "Failed to save schematic '%s'" ),
618 newAbsoluteFilename );
619 m_frame->SetMsgPanel( wxEmptyString, msg );
620 return false;
621 }
622
623 // If the associated screen is shared by more than one sheet, remove the
624 // screen and reload the file to a new screen. Failure to do this will trash
625 // the screen reference counting in complex hierarchies.
626 if( m_sheet->GetScreenCount() > 1 )
627 {
628 oldScreen = m_sheet->GetScreen();
629 m_sheet->SetScreen( nullptr );
630 loadFromFile = true;
631 }
632 }
633 }
634
635 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
636
637 if( useScreen )
638 {
639 // Create a temporary sheet for recursion testing to prevent a possible recursion error.
640 std::unique_ptr< SCH_SHEET> tmpSheet = std::make_unique<SCH_SHEET>( &schematic );
641 tmpSheet->GetFields()[SHEETNAME] = m_fields->at( SHEETNAME );
642 tmpSheet->GetFields()[SHEETFILENAME].SetText( sheetFileName.GetFullPath() );
643 tmpSheet->SetScreen( useScreen );
644
645 // No need to check for valid library IDs if we are using an existing screen.
646 if( m_frame->CheckSheetForRecursion( tmpSheet.get(), &currentSheet ) )
647 return false;
648
649 // It's safe to set the sheet screen now.
650 m_sheet->SetScreen( useScreen );
651
652 SCH_SHEET_LIST sheetHierarchy( m_sheet ); // The hierarchy of the loaded file.
653
654 sheetHierarchy.AddNewSymbolInstances( currentSheet, m_frame->Prj().GetProjectName() );
655 sheetHierarchy.AddNewSheetInstances( currentSheet,
656 fullHierarchy.GetLastVirtualPageNumber() );
657 }
658 else if( loadFromFile )
659 {
660 bool restoreSheet = false;
661
662 if( isExistingSheet )
663 {
664 // Temporarily remove the sheet from the current schematic page so that recursion
665 // and symbol library link tests can be performed with the modified sheet settings.
666 restoreSheet = true;
667 currentSheet.LastScreen()->Remove( m_sheet );
668 }
669
670 if( !m_frame->LoadSheetFromFile( m_sheet, &currentSheet, newAbsoluteFilename )
671 || m_frame->CheckSheetForRecursion( m_sheet, &currentSheet ) )
672 {
673 if( restoreSheet )
674 {
675 // If we cleared the previous screen, restore it before returning to the user
676 if( oldScreen )
677 m_sheet->SetScreen( oldScreen );
678
679 currentSheet.LastScreen()->Append( m_sheet );
680 }
681
682 return false;
683 }
684
685 if( restoreSheet )
686 currentSheet.LastScreen()->Append( m_sheet );
687 }
688
690 *m_clearAnnotationNewItems = clearAnnotation;
691
692 // Rebuild the entire connection graph.
694
695 return true;
696}
697
698
700{
701 bool success = true;
702 wxGridCellEditor* editor = m_grid->GetCellEditor( event.GetRow(), event.GetCol() );
703 wxControl* control = editor->GetControl();
704 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
705
706 // Short-circuit the validator's more generic "can't be empty" message for the
707 // two mandatory fields:
708 if( event.GetRow() == SHEETNAME && event.GetCol() == FDC_VALUE )
709 {
710 if( textControl && textControl->IsEmpty() )
711 {
712 wxMessageBox( _( "A sheet must have a name." ) );
713 success = false;
714 }
715 }
716 else if( event.GetRow() == SHEETFILENAME && event.GetCol() == FDC_VALUE && textControl )
717 {
718 if( textControl->IsEmpty() )
719 {
720 wxMessageBox( _( "A sheet must have a file specified." ) );
721 success = false;
722 }
723 }
724
725 if( success && control && control->GetValidator() )
726 success = control->GetValidator()->Validate( control );
727
728 if( !success )
729 {
730 event.Veto();
731 m_delayedFocusRow = event.GetRow();
732 m_delayedFocusColumn = event.GetCol();
733 }
734
735 editor->DecRef();
736}
737
738
739void DIALOG_SHEET_PROPERTIES::OnAddField( wxCommandEvent& event )
740{
742 return;
743
744 int fieldID = m_fields->size();
745 SCH_FIELD newField( VECTOR2I( 0, 0 ), fieldID, m_sheet,
747
748 newField.SetTextAngle( m_fields->at( SHEETNAME ).GetTextAngle() );
749
750 m_fields->push_back( newField );
751
752 // notify the grid
753 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
754 m_grid->ProcessTableMessage( msg );
755
756 m_grid->MakeCellVisible( m_fields->size() - 1, 0 );
757 m_grid->SetGridCursor( m_fields->size() - 1, 0 );
758
759 m_grid->EnableCellEditControl();
760 m_grid->ShowCellEditControl();
761}
762
763
764void DIALOG_SHEET_PROPERTIES::OnDeleteField( wxCommandEvent& event )
765{
766 wxArrayInt selectedRows = m_grid->GetSelectedRows();
767
768 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
769 selectedRows.push_back( m_grid->GetGridCursorRow() );
770
771 if( selectedRows.empty() )
772 return;
773
774 for( int row : selectedRows )
775 {
776 if( row < SHEET_MANDATORY_FIELDS )
777 {
778 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
780 return;
781 }
782 }
783
784 m_grid->CommitPendingChanges( true /* quiet mode */ );
785
786 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
787 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
788
789 for( int row : selectedRows )
790 {
791 m_fields->erase( m_fields->begin() + row );
792
793 // notify the grid
794 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
795 m_grid->ProcessTableMessage( msg );
796
797 if( m_grid->GetNumberRows() > 0 )
798 {
799 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
800 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
801 }
802 }
803}
804
805
806void DIALOG_SHEET_PROPERTIES::OnMoveUp( wxCommandEvent& event )
807{
809 return;
810
811 int i = m_grid->GetGridCursorRow();
812
813 if( i > SHEET_MANDATORY_FIELDS )
814 {
815 SCH_FIELD tmp = m_fields->at( (unsigned) i );
816 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
817 m_fields->insert( m_fields->begin() + i - 1, tmp );
818 m_grid->ForceRefresh();
819
820 m_grid->SetGridCursor( i - 1, m_grid->GetGridCursorCol() );
821 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
822 }
823 else
824 {
825 wxBell();
826 }
827}
828
829
830void DIALOG_SHEET_PROPERTIES::OnMoveDown( wxCommandEvent& event )
831{
833 return;
834
835 int i = m_grid->GetGridCursorRow();
836
837 if( i >= SHEET_MANDATORY_FIELDS && i < m_grid->GetNumberRows() - 1 )
838 {
839 SCH_FIELD tmp = m_fields->at( (unsigned) i );
840 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
841 m_fields->insert( m_fields->begin() + i + 1, tmp );
842 m_grid->ForceRefresh();
843
844 m_grid->SetGridCursor( i + 1, m_grid->GetGridCursorCol() );
845 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
846 }
847 else
848 {
849 wxBell();
850 }
851}
852
853
855{
856 // Account for scroll bars
858
859 m_grid->AutoSizeColumn( 0 );
860 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
861
862 int fixedColsWidth = m_grid->GetColSize( 0 );
863
864 for( int i = 2; i < m_grid->GetNumberCols(); i++ )
865 fixedColsWidth += m_grid->GetColSize( i );
866
867 m_grid->SetColSize( 1, std::max( 120, width - fixedColsWidth ) );
868}
869
870
871void DIALOG_SHEET_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
872{
873 std::bitset<64> shownColumns = m_grid->GetShownColumns();
874
875 if( shownColumns != m_shownColumns )
876 {
877 m_shownColumns = shownColumns;
878
879 if( !m_grid->IsCellEditControlShown() )
881 }
882
883 // Propagate changes in sheetname to displayed hierarchical path
884 wxString path = m_frame->GetCurrentSheet().PathHumanReadable( false );
885
886 if( path.Last() != '/' )
887 path.Append( '/' );
888
889 wxGridCellEditor* editor = m_grid->GetCellEditor( SHEETNAME, FDC_VALUE );
890 wxControl* control = editor->GetControl();
891 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
892 wxString sheetName;
893
894 if( textControl )
895 sheetName = textControl->GetValue();
896 else
897 sheetName = m_grid->GetCellValue( SHEETNAME, FDC_VALUE );
898
900 m_dummySheetNameField.SetText( sheetName );
902
903 editor->DecRef();
904
905 wxClientDC dc( m_hierarchicalPathLabel );
906 int width = m_sizerBottom->GetSize().x - m_stdDialogButtonSizer->GetSize().x
907 - m_hierarchicalPathLabel->GetSize().x
908 - 30;
909
910 path = wxControl::Ellipsize( path, dc, wxELLIPSIZE_START, width, wxELLIPSIZE_FLAGS_NONE );
911
912 if( m_hierarchicalPath->GetLabel() != path )
913 m_hierarchicalPath->SetLabel( path );
914
915 // Handle a delayed focus
916 if( m_delayedFocusRow >= 0 )
917 {
918 m_grid->SetFocus();
919 m_grid->MakeCellVisible( m_delayedFocusRow, m_delayedFocusColumn );
921
922 m_grid->EnableCellEditControl( true );
923 m_grid->ShowCellEditControl();
924
927 }
928}
929
930
931void DIALOG_SHEET_PROPERTIES::OnSizeGrid( wxSizeEvent& event )
932{
933 auto new_size = event.GetSize();
934
935 if( m_size != new_size )
936 {
937 m_size = new_size;
938
940 }
941
942 // Always propagate for a grid repaint (needed if the height changes, as well as width)
943 event.Skip();
944}
945
946
947void DIALOG_SHEET_PROPERTIES::OnInitDlg( wxInitDialogEvent& event )
948{
950
951 // Now all widgets have the size fixed, call FinishDialogSettings
953
954 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
955
956 if( cfg && cfg->m_Appearance.edit_sheet_width > 0 && cfg->m_Appearance.edit_sheet_height > 0 )
958
959}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
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.
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
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:107
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:134
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:569
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:187
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:190
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:291
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
int GetNumberRows() override
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:43
@ KD_WARNING
Definition: kidialog.h:46
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
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:147
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:359
Holds all the data relating to one schematic.
Definition: schematic.h:77
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
SCH_SHEET & Root() const
Definition: schematic.h:125
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:722
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
SCHEMATIC & Schematic() const
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags)
Generate the connection data for the entire schematic hierarchy.
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.
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1486
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:1253
int GetId() const
Definition: sch_field.h:133
bool IsEmpty()
Return true if both the name and value of the field are empty.
Definition: sch_field.h:163
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1228
void SetName(const wxString &aName)
Definition: sch_field.cpp:1203
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:211
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1213
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
void ClearFieldsAutoplaced()
Definition: sch_item.h:551
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:153
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:108
const wxString & GetFileName() const
Definition: sch_screen.h:143
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:118
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
Definition: sch_screen.cpp:323
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:57
bool GetExcludedFromBOM() const
Definition: sch_sheet.h:376
void SetBorderColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:119
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:306
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslated=true)
Definition: sch_sheet.cpp:57
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:93
wxString GetName() const
Definition: sch_sheet.h:107
void SetExcludedFromBOM(bool aExcludeFromBOM)
Set or clear the exclude from schematic bill of materials flag.
Definition: sch_sheet.h:375
bool GetExcludedFromBoard() const
Definition: sch_sheet.h:382
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:778
void SetBackgroundColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:122
void SetDNP(bool aDNP)
Definition: sch_sheet.h:388
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:400
void SetFields(const std::vector< SCH_FIELD > &aFields)
Set multiple schematic fields.
Definition: sch_sheet.cpp:409
int GetScreenCount() const
Return the number of times the associated screen for the sheet is being used.
Definition: sch_sheet.cpp:196
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:173
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:118
void SetBorderWidth(int aWidth)
Definition: sch_sheet.h:116
bool GetExcludedFromSim() const override
Definition: sch_sheet.h:370
int GetBorderWidth() const
Definition: sch_sheet.h:115
void SetExcludedFromBoard(bool aExcludeFromBoard)
Set or clear exclude from board netlist flag.
Definition: sch_sheet.h:381
void SetExcludedFromSim(bool aExcludeFromSim) override
Set or clear the exclude from simulation flag.
Definition: sch_sheet.h:369
bool GetDNP() const
Set or clear the 'Do Not Populate' flaga.
Definition: sch_sheet.h:387
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:121
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:487
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:270
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:443
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:457
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:476
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
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:422
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:250
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
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:391
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition: wxgtk/ui.cpp:195
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:154
@ GLOBAL_CLEANUP
@ 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
bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
wxLogTrace helper definitions.
@ SCH_SHEET_T
Definition: typeinfo.h:174
Custom text control validator definitions.
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39