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 ),
63 m_dummySheet( *aSheet ),
65{
66 m_sheet = aSheet;
67 m_fields = new FIELDS_GRID_TABLE( this, aParent, m_grid, m_sheet );
70
71 m_grid->SetTable( m_fields );
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 );
83 m_shownColumns = m_grid->GetShownColumns();
84 }
85
86 if( m_frame->GetColorSettings()->GetOverrideSchItemColors() )
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
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, wxGridEventHandler( DIALOG_SHEET_PROPERTIES::OnGridCellChanging ),
109 nullptr, this );
110
112}
113
114
116{
117 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
118 cfg->m_Appearance.edit_sheet_visible_columns = m_grid->GetShownColumnsAsString();
119
120 // Prevents crash bug in wxGrid's d'tor
121 m_grid->DestroyTable( m_fields );
122
123 m_grid->Disconnect( wxEVT_GRID_CELL_CHANGING, wxGridEventHandler( DIALOG_SHEET_PROPERTIES::OnGridCellChanging ),
124 nullptr, this );
125
126 // Delete the GRID_TRICKS.
127 m_grid->PopEventHandler( true );
128}
129
130
132{
133 if( !wxDialog::TransferDataToWindow() )
134 return false;
135
136 // Push a copy of each field into m_updateFields
137 for( SCH_FIELD& field : m_sheet->GetFields() )
138 {
139 SCH_FIELD field_copy( field );
140
141#ifdef __WINDOWS__
142 // Filenames are stored using unix notation, so convert to Windows notation
143 if( field_copy.GetId() == FIELD_T::SHEET_FILENAME )
144 {
145 wxString filename = field_copy.GetText();
146 filename.Replace( wxT( "/" ), wxT( "\\" ) );
147 field_copy.SetText( filename );
148 }
149#endif
150
151 // change offset to be symbol-relative
152 field_copy.Offset( -m_sheet->GetPosition() );
153
154 m_fields->push_back( field_copy );
155 }
156
157 // notify the grid
158 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_fields->size() );
159 m_grid->ProcessTableMessage( msg );
160
161 // border width
162 m_borderWidth.SetValue( m_sheet->GetBorderWidth() );
163
164 // set up color swatches
165 KIGFX::COLOR4D borderColor = m_sheet->GetBorderColor();
166 KIGFX::COLOR4D backgroundColor = m_sheet->GetBackgroundColor();
167
168 m_borderSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
169 m_backgroundSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
170
171 m_borderSwatch->SetSwatchColor( borderColor, false );
172 m_backgroundSwatch->SetSwatchColor( backgroundColor, false );
173
174 KIGFX::COLOR4D canvas = m_frame->GetColorSettings()->GetColor( LAYER_SCHEMATIC_BACKGROUND );
175 m_borderSwatch->SetSwatchBackground( canvas );
176 m_backgroundSwatch->SetSwatchBackground( canvas );
177
178 SCH_SHEET_PATH instance = m_frame->GetCurrentSheet();
179 instance.push_back( m_sheet );
180
181 m_pageNumberTextCtrl->ChangeValue( instance.GetPageNumber() );
182
183 m_cbExcludeFromSim->SetValue( m_sheet->GetExcludedFromSim() );
184 m_cbExcludeFromBom->SetValue( m_sheet->GetExcludedFromBOM() );
185 m_cbExcludeFromBoard->SetValue( m_sheet->GetExcludedFromBoard() );
186 m_cbDNP->SetValue( m_sheet->GetDNP() );
187
188 return true;
189}
190
191
193{
194 if( !m_grid->CommitPendingChanges() || !m_grid->Validate() )
195 return false;
196
197 // Check for missing field names.
198 for( size_t i = 0; i < m_fields->size(); ++i )
199 {
200 SCH_FIELD& field = m_fields->at( i );
201
202 if( field.IsMandatory() )
203 continue;
204
205 if( field.GetName( false ).empty() && !field.GetText().empty() )
206 {
207 DisplayErrorMessage( this, _( "Fields must have a name." ) );
208
210 m_delayedFocusRow = (int) i;
211
212 return false;
213 }
214 }
215
216 return true;
217}
218
219
220static bool positioningChanged( const SCH_FIELD& a, const SCH_FIELD& b )
221{
222 if( a.GetPosition() != b.GetPosition() )
223 return true;
224
225 if( a.GetHorizJustify() != b.GetHorizJustify() )
226 return true;
227
228 if( a.GetVertJustify() != b.GetVertJustify() )
229 return true;
230
231 if( a.GetTextAngle() != b.GetTextAngle() )
232 return true;
233
234 return false;
235}
236
237
239{
241 return true;
242
244 return true;
245
246 return false;
247}
248
249
251{
252 wxCHECK( m_sheet && m_frame, false );
253
254 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
255 return false;
256
257 if( m_isUndoable )
258 *m_isUndoable = true;
259
260 // Sheet file names can be relative or absolute.
261 wxString sheetFileName = m_fields->GetField( FIELD_T::SHEET_FILENAME )->GetText();
262
263 // Ensure filepath is not empty. (In normal use will be caught by grid validators,
264 // but unedited data from existing files can be bad.)
265 if( sheetFileName.IsEmpty() )
266 {
267 DisplayError( this, _( "A sheet must have a valid file name." ) );
268 return false;
269 }
270
271 // Ensure the filename extension is OK. (In normal use will be caught by grid validators,
272 // but unedited data from existing files can be bad.)
273 sheetFileName = EnsureFileExtension( sheetFileName, FILEEXT::KiCadSchematicFileExtension );
274
275 // Ensure sheetFileName is legal
276 if( !IsFullFileNameValid( sheetFileName ) )
277 {
278 DisplayError( this, _( "A sheet must have a valid file name." ) );
279 return false;
280 }
281
282 wxFileName fn( sheetFileName );
283
284 wxString newRelativeFilename = fn.GetFullPath();
285
286 // Inside Eeschema, filenames are stored using unix notation
287 newRelativeFilename.Replace( wxT( "\\" ), wxT( "/" ) );
288
289 wxString oldFilename = m_sheet->GetField( FIELD_T::SHEET_FILENAME )->GetText();
290 oldFilename.Replace( wxT( "\\" ), wxT( "/" ) );
291
292 bool filename_changed = oldFilename != newRelativeFilename;
293
294 if( filename_changed || m_sheet->IsNew() )
295 {
296 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
297
298 wxCHECK( currentScreen, false );
299
300 bool clearFileName = false;
301
302 // This can happen for the root sheet when opening Eeschema in the stand alone mode.
303 if( currentScreen->GetFileName().IsEmpty() )
304 {
305 clearFileName = true;
306 currentScreen->SetFileName( m_frame->Prj().AbsolutePath( wxT( "noname.kicad_sch" ) ) );
307 }
308
309 wxFileName tmp( fn );
310 wxFileName screenFileName = currentScreen->GetFileName();
311
312 if( fn.IsAbsolute() && fn.MakeRelativeTo( screenFileName.GetPath() ) )
313 {
314 wxMessageDialog makeRelDlg( this, _( "Use relative path for sheet file?" ), _( "Sheet File Path" ),
315 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION | wxCENTER );
316
317 makeRelDlg.SetExtendedMessage( _( "Using relative hierarchical sheet file name paths improves "
318 "schematic portability across systems and platforms. Using "
319 "absolute paths can result in portability issues." ) );
320 makeRelDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Use Relative Path" ) ),
321 wxMessageDialog::ButtonLabel( _( "Use Absolute Path" ) ) );
322
323 if( makeRelDlg.ShowModal() == wxID_YES )
324 {
325 wxLogTrace( tracePathsAndFiles, "\n Converted absolute path: '%s'"
326 "\n to relative path: '%s'",
327 tmp.GetPath(),
328 fn.GetPath() );
329 m_fields->GetField( FIELD_T::SHEET_FILENAME )->SetText( fn.GetFullPath() );
330 newRelativeFilename = fn.GetFullPath();
331 }
332 }
333
334 if( !onSheetFilenameChanged( newRelativeFilename ) )
335 {
336 if( clearFileName )
337 currentScreen->SetFileName( wxEmptyString );
338 else
340
341 return false;
342 }
344 {
346 }
347
348 if( clearFileName )
349 currentScreen->SetFileName( wxEmptyString );
350
351 // One last validity check (and potential repair) just to be sure to be sure
352 SCH_SHEET_LIST repairedList;
353 repairedList.BuildSheetList( &m_frame->Schematic().Root(), true );
354 }
355
356 wxString newSheetname = m_fields->GetField( FIELD_T::SHEET_NAME )->GetText();
357
358 if( ( newSheetname != m_sheet->GetName() ) && m_updateHierarchyNavigator )
360
361 if( newSheetname.IsEmpty() )
362 newSheetname = _( "Untitled Sheet" );
363
364 m_fields->GetField( FIELD_T::SHEET_NAME )->SetText( newSheetname );
365
366 // change all field positions from relative to absolute
367 for( SCH_FIELD& m_field : *m_fields)
368 m_field.Offset( m_sheet->GetPosition() );
369
371 m_sheet->SetFieldsAutoplaced( AUTOPLACE_NONE );
372
373 for( int ii = m_fields->GetNumberRows() - 1; ii >= 0; ii-- )
374 {
375 SCH_FIELD& field = m_fields->at( ii );
376
377 if( field.IsMandatory() )
378 continue;
379
380 const wxString& fieldName = field.GetCanonicalName();
381
382 if( field.IsEmpty() )
383 m_fields->erase( m_fields->begin() + ii );
384 else if( fieldName.IsEmpty() )
385 field.SetName( _( "untitled" ) );
386 }
387
388 m_sheet->SetFields( *m_fields );
389
390 m_sheet->SetBorderWidth( m_borderWidth.GetIntValue() );
391
392 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
393
394 if( colorSettings->GetOverrideSchItemColors()
395 && ( m_sheet->GetBorderColor() != m_borderSwatch->GetSwatchColor() ||
396 m_sheet->GetBackgroundColor() != m_backgroundSwatch->GetSwatchColor() ) )
397 {
398 wxPanel temp( this );
399 temp.Hide();
400 PANEL_EESCHEMA_COLOR_SETTINGS prefs( &temp );
401 wxString checkboxLabel = prefs.m_optOverrideColors->GetLabel();
402
403 KIDIALOG dlg( this, _( "Note: item colors are overridden in the current color theme." ),
405 dlg.ShowDetailedText( wxString::Format( _( "To see individual item colors uncheck '%s'\n"
406 "in Preferences > Schematic Editor > Colors." ),
407 checkboxLabel ) );
408 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
409 dlg.ShowModal();
410 }
411
412 m_sheet->SetBorderColor( m_borderSwatch->GetSwatchColor() );
413 m_sheet->SetBackgroundColor( m_backgroundSwatch->GetSwatchColor() );
414
415 m_sheet->SetExcludedFromSim( m_cbExcludeFromSim->GetValue() );
416 m_sheet->SetExcludedFromBOM( m_cbExcludeFromBom->GetValue() );
417 m_sheet->SetExcludedFromBoard( m_cbExcludeFromBoard->GetValue() );
418 m_sheet->SetDNP( m_cbDNP->GetValue() );
419
420 SCH_SHEET_PATH instance = m_frame->GetCurrentSheet();
421
422 instance.push_back( m_sheet );
423
424 instance.SetPageNumber( m_pageNumberTextCtrl->GetValue() );
425
426 m_frame->TestDanglingEnds();
427
428 // Refresh all sheets in case ordering changed.
429 for( SCH_ITEM* item : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
430 m_frame->UpdateItem( item );
431
432 return true;
433}
434
435
436bool DIALOG_SHEET_PROPERTIES::onSheetFilenameChanged( const wxString& aNewFilename )
437{
438 wxString msg;
439 wxFileName sheetFileName( EnsureFileExtension( aNewFilename, FILEEXT::KiCadSchematicFileExtension ) );
440
441 // Sheet file names are relative to the path of the current sheet. This allows for
442 // nesting of schematic files in subfolders. Screen file names are always absolute.
443 SCHEMATIC& schematic = m_frame->Schematic();
444 SCH_SHEET_LIST fullHierarchy = schematic.Hierarchy();
445 wxFileName screenFileName( sheetFileName );
446 wxFileName tmp( sheetFileName );
447 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
448
449 wxCHECK( currentScreen, false );
450
451 // SCH_SCREEN file names are always absolute.
452 wxFileName currentScreenFileName = currentScreen->GetFileName();
453
454 if( !screenFileName.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS, currentScreenFileName.GetPath() ) )
455 {
456 msg = wxString::Format( _( "Cannot normalize new sheet schematic file path:\n"
457 "'%s'\n"
458 "against parent sheet schematic file path:\n"
459 "'%s'." ),
460 sheetFileName.GetPath(),
461 currentScreenFileName.GetPath() );
462 DisplayError( this, msg );
463 return false;
464 }
465
466 wxString newAbsoluteFilename = screenFileName.GetFullPath();
467
468 // Inside Eeschema, filenames are stored using unix notation
469 newAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
470
471 bool renameFile = false;
472 bool loadFromFile = false;
473 bool clearAnnotation = false;
474 bool isExistingSheet = false;
475 SCH_SCREEN* useScreen = nullptr;
476 SCH_SCREEN* oldScreen = nullptr;
477
478 // Search for a schematic file having the same filename already in use in the hierarchy
479 // or on disk, in order to reuse it.
480 if( !schematic.Root().SearchHierarchy( newAbsoluteFilename, &useScreen ) )
481 {
482 loadFromFile = wxFileExists( newAbsoluteFilename );
483
484 wxLogTrace( tracePathsAndFiles, "\n Sheet requested file '%s', %s",
485 newAbsoluteFilename,
486 loadFromFile ? "found" : "not found" );
487 }
488
489 if( m_sheet->GetScreen() == nullptr ) // New just created sheet.
490 {
491 if( !m_frame->AllowCaseSensitiveFileNameClashes( m_sheet->GetFileName(), newAbsoluteFilename ) )
492 return false;
493
494 if( useScreen || loadFromFile ) // Load from existing file.
495 {
496 clearAnnotation = true;
497
498 if( !IsOK( this, wxString::Format( _( "'%s' already exists." ), sheetFileName.GetFullName() )
499 + wxT( "\n\n" )
500 + wxString::Format( _( "Link '%s' to this file?" ), newAbsoluteFilename ) ) )
501 {
502 return false;
503 }
504 }
505 // If we are drawing a sheet from a design block/sheet import, we need to copy the
506 // sheet to the current directory.
507 else if( m_sourceSheetFilename && !m_sourceSheetFilename->IsEmpty() )
508 {
509 loadFromFile = true;
510
511 if( !wxCopyFile( *m_sourceSheetFilename, newAbsoluteFilename, false ) )
512 {
513 msg.Printf( _( "Failed to copy schematic file '%s' to destination '%s'." ),
514 currentScreenFileName.GetFullPath(),
515 newAbsoluteFilename );
516
517 DisplayError( m_frame, msg );
518
519 return false;
520 }
521 }
522 else // New file.
523 {
524 m_frame->InitSheet( m_sheet, newAbsoluteFilename );
525 }
526 }
527 else // Existing sheet.
528 {
529 isExistingSheet = true;
530
531 if( !m_frame->AllowCaseSensitiveFileNameClashes( m_sheet->GetFileName(), newAbsoluteFilename ) )
532 return false;
533
534 // We are always using here a case insensitive comparison to avoid issues
535 // under Windows, although under Unix filenames are case sensitive.
536 // But many users create schematic under both Unix and Windows
537 // **
538 // N.B. 1: aSheet->GetFileName() will return a relative path
539 // aSheet->GetScreen()->GetFileName() returns a full path
540 //
541 // N.B. 2: newFilename uses the unix notation for separator.
542 // so we must use it also to compare the old and new filenames
543 wxString oldAbsoluteFilename = m_sheet->GetScreen()->GetFileName();
544 oldAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
545
546 if( newAbsoluteFilename.Cmp( oldAbsoluteFilename ) != 0 )
547 {
548 // Sheet file name changes cannot be undone.
549 if( m_isUndoable )
550 *m_isUndoable = false;
551
552 if( useScreen || loadFromFile ) // Load from existing file.
553 {
554 clearAnnotation = true;
555 oldScreen = m_sheet->GetScreen();
556
557 if( !IsOK( this, wxString::Format( _( "Change '%s' link from '%s' to '%s'?" ),
558 newAbsoluteFilename,
559 m_sheet->GetFileName(),
560 sheetFileName.GetFullName() )
561 + wxT( "\n\n" )
562 + _( "This action cannot be undone." ) ) )
563 {
564 return false;
565 }
566
567 if( loadFromFile )
568 m_sheet->SetScreen( nullptr );
569 }
570 else // Save to new file name.
571 {
572 if( m_sheet->GetScreenCount() > 1 )
573 {
574 if( !IsOK( this, wxString::Format( _( "Create new file '%s' with contents of '%s'?" ),
575 sheetFileName.GetFullName(),
576 m_sheet->GetFileName() )
577 + wxT( "\n\n" )
578 + _( "This action cannot be undone." ) ) )
579 {
580 return false;
581 }
582 }
583
584 renameFile = true;
585 }
586 }
587
588 if( renameFile )
589 {
590 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
591
592 // If the associated screen is shared by more than one sheet, do not
593 // change the filename of the corresponding screen here.
594 // (a new screen will be created later)
595 // if it is not shared, update the filename
596 if( m_sheet->GetScreenCount() <= 1 )
597 m_sheet->GetScreen()->SetFileName( newAbsoluteFilename );
598
599 try
600 {
601 pi->SaveSchematicFile( newAbsoluteFilename, m_sheet, &schematic );
602 }
603 catch( const IO_ERROR& ioe )
604 {
605 msg = wxString::Format( _( "Error occurred saving schematic file '%s'." ), newAbsoluteFilename );
606 DisplayErrorMessage( this, msg, ioe.What() );
607
608 msg = wxString::Format( _( "Failed to save schematic '%s'" ), newAbsoluteFilename );
609 m_frame->SetMsgPanel( wxEmptyString, msg );
610 return false;
611 }
612
613 // If the associated screen is shared by more than one sheet, remove the
614 // screen and reload the file to a new screen. Failure to do this will trash
615 // the screen reference counting in complex hierarchies.
616 if( m_sheet->GetScreenCount() > 1 )
617 {
618 oldScreen = m_sheet->GetScreen();
619 m_sheet->SetScreen( nullptr );
620 loadFromFile = true;
621 }
622 }
623 }
624
625 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
626
627 if( useScreen )
628 {
629 // Create a temporary sheet for recursion testing to prevent a possible recursion error.
630 std::unique_ptr< SCH_SHEET> tmpSheet = std::make_unique<SCH_SHEET>( &schematic );
631 *tmpSheet->GetField( FIELD_T::SHEET_NAME ) = m_fields->GetField( FIELD_T::SHEET_NAME );
632 tmpSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( sheetFileName.GetFullPath() );
633 tmpSheet->SetScreen( useScreen );
634
635 // No need to check for valid library IDs if we are using an existing screen.
636 if( m_frame->CheckSheetForRecursion( tmpSheet.get(), &currentSheet ) )
637 return false;
638
639 // It's safe to set the sheet screen now.
640 m_sheet->SetScreen( useScreen );
641
642 SCH_SHEET_LIST sheetHierarchy( m_sheet ); // The hierarchy of the loaded file.
643
644 sheetHierarchy.AddNewSymbolInstances( currentSheet, m_frame->Prj().GetProjectName() );
645 sheetHierarchy.AddNewSheetInstances( currentSheet, fullHierarchy.GetLastVirtualPageNumber() );
646 }
647 else if( loadFromFile )
648 {
649 bool restoreSheet = false;
650
651 if( isExistingSheet )
652 {
653 // Temporarily remove the sheet from the current schematic page so that recursion
654 // and symbol library link tests can be performed with the modified sheet settings.
655 restoreSheet = true;
656 currentSheet.LastScreen()->Remove( m_sheet );
657 }
658
659 if( !m_frame->LoadSheetFromFile( m_sheet, &currentSheet, newAbsoluteFilename, false, true )
660 || m_frame->CheckSheetForRecursion( m_sheet, &currentSheet ) )
661 {
662 if( restoreSheet )
663 {
664 // If we cleared the previous screen, restore it before returning to the user
665 if( oldScreen )
666 m_sheet->SetScreen( oldScreen );
667
668 currentSheet.LastScreen()->Append( m_sheet );
669 }
670
671 return false;
672 }
673
674 if( restoreSheet )
675 currentSheet.LastScreen()->Append( m_sheet );
676 }
677
679 *m_clearAnnotationNewItems = clearAnnotation;
680
681 // Rebuild the entire connection graph.
682 m_frame->RecalculateConnections( nullptr, GLOBAL_CLEANUP );
683
684 return true;
685}
686
687
689{
690 bool success = true;
691 wxGridCellEditor* editor = m_grid->GetCellEditor( event.GetRow(), event.GetCol() );
692 wxControl* control = editor->GetControl();
693
694 if( control && control->GetValidator() )
695 success = control->GetValidator()->Validate( control );
696
697 if( !success )
698 {
699 event.Veto();
700 m_delayedFocusRow = event.GetRow();
701 m_delayedFocusColumn = event.GetCol();
702 }
703
704 editor->DecRef();
705}
706
707
708void DIALOG_SHEET_PROPERTIES::OnAddField( wxCommandEvent& event )
709{
710 m_grid->OnAddRow(
711 [&]() -> std::pair<int, int>
712 {
714
715 newField.SetTextAngle( m_fields->GetField( FIELD_T::SHEET_NAME )->GetTextAngle() );
716 newField.SetVisible( false );
717 m_fields->push_back( newField );
718
719 // notify the grid
720 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
721 m_grid->ProcessTableMessage( msg );
722 return { m_fields->size() - 1, FDC_NAME };
723 } );
724}
725
726
727void DIALOG_SHEET_PROPERTIES::OnDeleteField( wxCommandEvent& event )
728{
729 m_grid->OnDeleteRows(
730 [&]( int row )
731 {
732 if( row < m_fields->GetMandatoryRowCount() )
733 {
734 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
735 m_fields->GetMandatoryRowCount() ) );
736 return false;
737 }
738
739 return true;
740 },
741 [&]( int row )
742 {
743 m_fields->erase( m_fields->begin() + row );
744
745 // notify the grid
746 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
747 m_grid->ProcessTableMessage( msg );
748 } );
749}
750
751
752void DIALOG_SHEET_PROPERTIES::OnMoveUp( wxCommandEvent& event )
753{
754 m_grid->OnMoveRowUp(
755 [&]( int row )
756 {
757 return row > m_fields->GetMandatoryRowCount();
758 },
759 [&]( int row )
760 {
761 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row - 1 ) );
762 m_grid->ForceRefresh();
763 } );
764}
765
766
767void DIALOG_SHEET_PROPERTIES::OnMoveDown( wxCommandEvent& event )
768{
769 m_grid->OnMoveRowUp(
770 [&]( int row )
771 {
772 return row >= m_fields->GetMandatoryRowCount();
773 },
774 [&]( int row )
775 {
776 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row + 1 ) );
777 m_grid->ForceRefresh();
778 } );
779}
780
781
782void DIALOG_SHEET_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
783{
784 std::bitset<64> shownColumns = m_grid->GetShownColumns();
785
786 if( shownColumns != m_shownColumns )
787 {
788 m_shownColumns = shownColumns;
789
790 if( !m_grid->IsCellEditControlShown() )
791 m_grid->SetGridWidthsDirty();
792 }
793
794 // Propagate changes in sheetname to displayed hierarchical path
795 int sheetnameRow = m_fields->GetFieldRow( FIELD_T::SHEET_NAME );
796 wxString path = m_frame->GetCurrentSheet().PathHumanReadable( false );
797
798 if( path.Last() != '/' )
799 path.Append( '/' );
800
801 wxGridCellEditor* editor = m_grid->GetCellEditor( sheetnameRow, FDC_VALUE );
802 wxControl* control = editor->GetControl();
803 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
804 wxString sheetName;
805
806 if( textControl )
807 sheetName = textControl->GetValue();
808 else
809 sheetName = m_grid->GetCellValue( sheetnameRow, FDC_VALUE );
810
811 m_dummySheet.SetFields( *m_fields );
812 m_dummySheetNameField.SetText( sheetName );
813 path += m_dummySheetNameField.GetShownText( false );
814
815 editor->DecRef();
816
817 wxClientDC dc( m_hierarchicalPathLabel );
818 int width = m_sizerBottom->GetSize().x - m_stdDialogButtonSizer->GetSize().x
819 - m_hierarchicalPathLabel->GetSize().x
820 - 30;
821
822 path = wxControl::Ellipsize( path, dc, wxELLIPSIZE_START, width, wxELLIPSIZE_FLAGS_NONE );
823
824 if( m_hierarchicalPath->GetLabel() != path )
825 m_hierarchicalPath->SetLabel( path );
826
827 // Handle a delayed focus
828 if( m_delayedFocusRow >= 0 )
829 {
830 m_grid->SetFocus();
831 m_grid->MakeCellVisible( m_delayedFocusRow, m_delayedFocusColumn );
833
834 m_grid->EnableCellEditControl( true );
835 m_grid->ShowCellEditControl();
836
839 }
840}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
Color settings are a bit different than most of the settings objects in that there can be more than o...
bool GetOverrideSchItemColors() const
wxStdDialogButtonSizer * m_stdDialogButtonSizer
DIALOG_SHEET_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Sheet Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxCAPTION|wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxMINIMIZE_BOX|wxRESIZE_BORDER|wxSYSTEM_MENU)
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 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...
const EDA_ANGLE & GetTextAngle() const
Definition eda_text.h:147
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:607
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:200
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:397
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:203
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:310
SCH_FIELD * GetField(FIELD_T aFieldId)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:42
@ KD_WARNING
Definition kidialog.h:47
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:55
int ShowModal() override
Definition kidialog.cpp:93
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
Holds all the data relating to one schematic.
Definition schematic.h:88
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET & Root() const
Definition schematic.h:140
Schematic editor (Eeschema) main window.
SCHEMATIC & Schematic() const
bool IsMandatory() const
VECTOR2I GetPosition() const override
FIELD_T GetId() const
Definition sch_field.h:116
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
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).
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
const wxString & GetFileName() const
Definition sch_screen.h:152
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
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...
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
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
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:429
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
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:488
KICOMMON_API wxFont GetSmallInfoFont(wxWindow *aWindow)
const SCH_FIELD * FindField(const std::vector< SCH_FIELD > &aFields, FIELD_T aFieldId)
Definition sch_field.h:362
@ AUTOPLACE_NONE
Definition sch_item.h:69
@ GLOBAL_CLEANUP
Definition schematic.h:77
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...
wxLogTrace helper definitions.
@ SCH_SHEET_T
Definition typeinfo.h:179
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