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( 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 = 0; i < m_fields->size(); ++i )
209 {
210 SCH_FIELD& field = m_fields->at( i );
211
212 if( field.IsMandatory() )
213 continue;
214
215 if( field.GetName( false ).empty() && !field.GetText().empty() )
216 {
217 DisplayErrorMessage( this, _( "Fields must have a name." ) );
218
220 m_delayedFocusRow = (int) i;
221
222 return false;
223 }
224 }
225
226 return true;
227}
228
229
230static bool positioningChanged( const SCH_FIELD& a, const SCH_FIELD& b )
231{
232 if( a.GetPosition() != b.GetPosition() )
233 return true;
234
235 if( a.GetHorizJustify() != b.GetHorizJustify() )
236 return true;
237
238 if( a.GetVertJustify() != b.GetVertJustify() )
239 return true;
240
241 if( a.GetTextAngle() != b.GetTextAngle() )
242 return true;
243
244 return false;
245}
246
247
248static bool positioningChanged( FIELDS_GRID_TABLE* a, std::vector<SCH_FIELD>& b )
249{
250 if( positioningChanged( a->at( SHEETNAME ), b.at( SHEETNAME ) ) )
251 return true;
252
253 if( positioningChanged( a->at( SHEETFILENAME ), b.at( SHEETFILENAME ) ) )
254 return true;
255
256 return false;
257}
258
259
261{
262 wxCHECK( m_sheet && m_frame, false );
263
264 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
265 return false;
266
267 if( m_isUndoable )
268 *m_isUndoable = true;
269
270 // Sheet file names can be relative or absolute.
271 wxString sheetFileName = m_fields->at( SHEETFILENAME ).GetText();
272
273 // Ensure filepath is not empty. (In normal use will be caught by grid validators,
274 // but unedited data from existing files can be bad.)
275 if( sheetFileName.IsEmpty() )
276 {
277 DisplayError( this, _( "A sheet must have a valid file name." ) );
278 return false;
279 }
280
281 // Ensure the filename extension is OK. (In normal use will be caught by grid validators,
282 // but unedited data from existing files can be bad.)
283 sheetFileName = EnsureFileExtension( sheetFileName, FILEEXT::KiCadSchematicFileExtension );
284
285 // Ensure sheetFileName is legal
286 if( !IsFullFileNameValid( sheetFileName ) )
287 {
288 DisplayError( this, _( "A sheet must have a valid file name." ) );
289 return false;
290 }
291
292 wxFileName fn( sheetFileName );
293
294 wxString newRelativeFilename = fn.GetFullPath();
295
296 // Inside Eeschema, filenames are stored using unix notation
297 newRelativeFilename.Replace( wxT( "\\" ), wxT( "/" ) );
298
299 wxString oldFilename = m_sheet->GetFields()[ SHEETFILENAME ].GetText();
300 oldFilename.Replace( wxT( "\\" ), wxT( "/" ) );
301
302 bool filename_changed = oldFilename != newRelativeFilename;
303
304 if( filename_changed || m_sheet->IsNew() )
305 {
306 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
307
308 wxCHECK( currentScreen, false );
309
310 bool clearFileName = false;
311
312 // This can happen for the root sheet when opening Eeschema in the stand alone mode.
313 if( currentScreen->GetFileName().IsEmpty() )
314 {
315 clearFileName = true;
316 currentScreen->SetFileName( m_frame->Prj().AbsolutePath( wxT( "noname.kicad_sch" ) ) );
317 }
318
319 wxFileName tmp( fn );
320 wxFileName screenFileName = currentScreen->GetFileName();
321
322 if( fn.IsAbsolute() && fn.MakeRelativeTo( screenFileName.GetPath() ) )
323 {
324 wxMessageDialog makeRelDlg( this, _( "Use relative path for sheet file?" ),
325 _( "Sheet File Path" ),
326 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION | wxCENTER );
327
328 makeRelDlg.SetExtendedMessage( _( "Using relative hierarchical sheet file name paths "
329 "improves schematic portability across systems and "
330 "platforms. Using absolute paths can result in "
331 "portability issues." ) );
332 makeRelDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Use Relative Path" ) ),
333 wxMessageDialog::ButtonLabel( _( "Use Absolute Path" ) ) );
334
335 if( makeRelDlg.ShowModal() == wxID_YES )
336 {
337 wxLogTrace( tracePathsAndFiles, "\n Converted absolute path: '%s'"
338 "\n to relative path: '%s'",
339 tmp.GetPath(),
340 fn.GetPath() );
341 m_fields->at( SHEETFILENAME ).SetText( fn.GetFullPath() );
342 newRelativeFilename = fn.GetFullPath();
343 }
344 }
345
346 if( !onSheetFilenameChanged( newRelativeFilename ) )
347 {
348 if( clearFileName )
349 currentScreen->SetFileName( wxEmptyString );
350
351 return false;
352 }
354 {
356 }
357
358 if( clearFileName )
359 currentScreen->SetFileName( wxEmptyString );
360
361 // One last validity check (and potential repair) just to be sure to be sure
362 SCH_SHEET_LIST repairedList;
363 repairedList.BuildSheetList( &m_frame->Schematic().Root(), true );
364 }
365
366 wxString newSheetname = m_fields->at( SHEETNAME ).GetText();
367
368 if( ( newSheetname != m_sheet->GetName() ) && m_updateHierarchyNavigator )
370
371 if( newSheetname.IsEmpty() )
372 newSheetname = _( "Untitled Sheet" );
373
374 m_fields->at( SHEETNAME ).SetText( newSheetname );
375
376 // change all field positions from relative to absolute
377 for( SCH_FIELD& m_field : *m_fields)
378 m_field.Offset( m_sheet->GetPosition() );
379
382
383 for( int ii = m_fields->GetNumberRows() - 1; ii >= 0; ii-- )
384 {
385 SCH_FIELD& field = m_fields->at( ii );
386
387 if( field.IsMandatory() )
388 continue;
389
390 const wxString& fieldName = field.GetCanonicalName();
391
392 if( field.IsEmpty() )
393 m_fields->erase( m_fields->begin() + ii );
394 else if( fieldName.IsEmpty() )
395 field.SetName( _( "untitled" ) );
396 }
397
399
401
402 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
403
404 if( colorSettings->GetOverrideSchItemColors()
407 {
408 wxPanel temp( this );
409 temp.Hide();
410 PANEL_EESCHEMA_COLOR_SETTINGS prefs( &temp );
411 wxString checkboxLabel = prefs.m_optOverrideColors->GetLabel();
412
413 KIDIALOG dlg( this, _( "Note: item colors are overridden in the current color theme." ),
415 dlg.ShowDetailedText( wxString::Format( _( "To see individual item colors uncheck '%s'\n"
416 "in Preferences > Schematic Editor > Colors." ),
417 checkboxLabel ) );
418 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
419 dlg.ShowModal();
420 }
421
424
428 m_sheet->SetDNP( m_cbDNP->GetValue() );
429
431
432 instance.push_back( m_sheet );
433
434 instance.SetPageNumber( m_pageNumberTextCtrl->GetValue() );
435
437
438 // Refresh all sheets in case ordering changed.
439 for( SCH_ITEM* item : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
440 m_frame->UpdateItem( item );
441
442 return true;
443}
444
445
446bool DIALOG_SHEET_PROPERTIES::onSheetFilenameChanged( const wxString& aNewFilename )
447{
448 wxString msg;
449 wxFileName sheetFileName( EnsureFileExtension( aNewFilename,
451
452 // Sheet file names are relative to the path of the current sheet. This allows for
453 // nesting of schematic files in subfolders. Screen file names are always absolute.
454 SCHEMATIC& schematic = m_frame->Schematic();
455 SCH_SHEET_LIST fullHierarchy = schematic.Hierarchy();
456 wxFileName screenFileName( sheetFileName );
457 wxFileName tmp( sheetFileName );
458 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
459
460 wxCHECK( currentScreen, false );
461
462 // SCH_SCREEN file names are always absolute.
463 wxFileName currentScreenFileName = currentScreen->GetFileName();
464
465 if( !screenFileName.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS,
466 currentScreenFileName.GetPath() ) )
467 {
468 msg = wxString::Format( _( "Cannot normalize new sheet schematic file path:\n"
469 "'%s'\n"
470 "against parent sheet schematic file path:\n"
471 "'%s'." ),
472 sheetFileName.GetPath(),
473 currentScreenFileName.GetPath() );
474 DisplayError( this, msg );
475 return false;
476 }
477
478 wxString newAbsoluteFilename = screenFileName.GetFullPath();
479
480 // Inside Eeschema, filenames are stored using unix notation
481 newAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
482
483 bool renameFile = false;
484 bool loadFromFile = false;
485 bool clearAnnotation = false;
486 bool isExistingSheet = false;
487 SCH_SCREEN* useScreen = nullptr;
488 SCH_SCREEN* oldScreen = nullptr;
489
490 // Search for a schematic file having the same filename already in use in the hierarchy
491 // or on disk, in order to reuse it.
492 if( !schematic.Root().SearchHierarchy( newAbsoluteFilename, &useScreen ) )
493 {
494 loadFromFile = wxFileExists( newAbsoluteFilename );
495
496 wxLogTrace( tracePathsAndFiles, "\n Sheet requested file '%s', %s",
497 newAbsoluteFilename,
498 loadFromFile ? "found" : "not found" );
499 }
500
501 if( m_sheet->GetScreen() == nullptr ) // New just created sheet.
502 {
504 newAbsoluteFilename ) )
505 return false;
506
507 if( useScreen || loadFromFile ) // Load from existing file.
508 {
509 clearAnnotation = true;
510
511 if( !IsOK( this, wxString::Format( _( "'%s' already exists." ),
512 sheetFileName.GetFullName() )
513 + wxT( "\n\n" )
514 + wxString::Format( _( "Link '%s' to this file?" ),
515 newAbsoluteFilename ) ) )
516 {
517 return false;
518 }
519 }
520 // If we are drawing a sheet from a design block/sheet import, we need to copy the
521 // sheet to the current directory.
522 else if( m_sourceSheetFilename && !m_sourceSheetFilename->IsEmpty() )
523 {
524 loadFromFile = true;
525
526 if( !wxCopyFile( *m_sourceSheetFilename, newAbsoluteFilename, false ) )
527 {
528 msg.Printf( _( "Failed to copy schematic file '%s' to destination '%s'." ),
529 currentScreenFileName.GetFullPath(), newAbsoluteFilename );
530
531 DisplayError( m_frame, msg );
532
533 return false;
534 }
535 }
536 else // New file.
537 {
538 m_frame->InitSheet( m_sheet, newAbsoluteFilename );
539 }
540 }
541 else // Existing sheet.
542 {
543 isExistingSheet = true;
544
546 newAbsoluteFilename ) )
547 return false;
548
549 // We are always using here a case insensitive comparison to avoid issues
550 // under Windows, although under Unix filenames are case sensitive.
551 // But many users create schematic under both Unix and Windows
552 // **
553 // N.B. 1: aSheet->GetFileName() will return a relative path
554 // aSheet->GetScreen()->GetFileName() returns a full path
555 //
556 // N.B. 2: newFilename uses the unix notation for separator.
557 // so we must use it also to compare the old and new filenames
558 wxString oldAbsoluteFilename = m_sheet->GetScreen()->GetFileName();
559 oldAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
560
561 if( newAbsoluteFilename.Cmp( oldAbsoluteFilename ) != 0 )
562 {
563 // Sheet file name changes cannot be undone.
564 if( m_isUndoable )
565 *m_isUndoable = false;
566
567 if( useScreen || loadFromFile ) // Load from existing file.
568 {
569 clearAnnotation = true;
570
571 if( !IsOK( this, wxString::Format( _( "Change '%s' link from '%s' to '%s'?" ),
572 newAbsoluteFilename,
574 sheetFileName.GetFullName() )
575 + wxT( "\n\n" )
576 + _( "This action cannot be undone." ) ) )
577 {
578 return false;
579 }
580
581 if( loadFromFile )
582 m_sheet->SetScreen( nullptr );
583 }
584 else // Save to new file name.
585 {
586 if( m_sheet->GetScreenCount() > 1 )
587 {
588 if( !IsOK( this, wxString::Format( _( "Create new file '%s' with contents "
589 "of '%s'?" ),
590 sheetFileName.GetFullName(),
592 + wxT( "\n\n" )
593 + _( "This action cannot be undone." ) ) )
594 {
595 return false;
596 }
597 }
598
599 renameFile = true;
600 }
601 }
602
603 if( renameFile )
604 {
605 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
606
607 // If the associated screen is shared by more than one sheet, do not
608 // change the filename of the corresponding screen here.
609 // (a new screen will be created later)
610 // if it is not shared, update the filename
611 if( m_sheet->GetScreenCount() <= 1 )
612 m_sheet->GetScreen()->SetFileName( newAbsoluteFilename );
613
614 try
615 {
616 pi->SaveSchematicFile( newAbsoluteFilename, m_sheet, &schematic );
617 }
618 catch( const IO_ERROR& ioe )
619 {
620 msg = wxString::Format( _( "Error occurred saving schematic file '%s'." ),
621 newAbsoluteFilename );
622 DisplayErrorMessage( this, msg, ioe.What() );
623
624 msg = wxString::Format( _( "Failed to save schematic '%s'" ),
625 newAbsoluteFilename );
626 m_frame->SetMsgPanel( wxEmptyString, msg );
627 return false;
628 }
629
630 // If the associated screen is shared by more than one sheet, remove the
631 // screen and reload the file to a new screen. Failure to do this will trash
632 // the screen reference counting in complex hierarchies.
633 if( m_sheet->GetScreenCount() > 1 )
634 {
635 oldScreen = m_sheet->GetScreen();
636 m_sheet->SetScreen( nullptr );
637 loadFromFile = true;
638 }
639 }
640 }
641
642 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
643
644 if( useScreen )
645 {
646 // Create a temporary sheet for recursion testing to prevent a possible recursion error.
647 std::unique_ptr< SCH_SHEET> tmpSheet = std::make_unique<SCH_SHEET>( &schematic );
648 tmpSheet->GetFields()[SHEETNAME] = m_fields->at( SHEETNAME );
649 tmpSheet->GetFields()[SHEETFILENAME].SetText( sheetFileName.GetFullPath() );
650 tmpSheet->SetScreen( useScreen );
651
652 // No need to check for valid library IDs if we are using an existing screen.
653 if( m_frame->CheckSheetForRecursion( tmpSheet.get(), &currentSheet ) )
654 return false;
655
656 // It's safe to set the sheet screen now.
657 m_sheet->SetScreen( useScreen );
658
659 SCH_SHEET_LIST sheetHierarchy( m_sheet ); // The hierarchy of the loaded file.
660
661 sheetHierarchy.AddNewSymbolInstances( currentSheet, m_frame->Prj().GetProjectName() );
662 sheetHierarchy.AddNewSheetInstances( currentSheet,
663 fullHierarchy.GetLastVirtualPageNumber() );
664 }
665 else if( loadFromFile )
666 {
667 bool restoreSheet = false;
668
669 if( isExistingSheet )
670 {
671 // Temporarily remove the sheet from the current schematic page so that recursion
672 // and symbol library link tests can be performed with the modified sheet settings.
673 restoreSheet = true;
674 currentSheet.LastScreen()->Remove( m_sheet );
675 }
676
677 if( !m_frame->LoadSheetFromFile( m_sheet, &currentSheet, newAbsoluteFilename, false, true )
678 || m_frame->CheckSheetForRecursion( m_sheet, &currentSheet ) )
679 {
680 if( restoreSheet )
681 {
682 // If we cleared the previous screen, restore it before returning to the user
683 if( oldScreen )
684 m_sheet->SetScreen( oldScreen );
685
686 currentSheet.LastScreen()->Append( m_sheet );
687 }
688
689 return false;
690 }
691
692 if( restoreSheet )
693 currentSheet.LastScreen()->Append( m_sheet );
694 }
695
697 *m_clearAnnotationNewItems = clearAnnotation;
698
699 // Rebuild the entire connection graph.
701
702 return true;
703}
704
705
707{
708 bool success = true;
709 wxGridCellEditor* editor = m_grid->GetCellEditor( event.GetRow(), event.GetCol() );
710 wxControl* control = editor->GetControl();
711 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
712
713 // Short-circuit the validator's more generic "can't be empty" message for the
714 // two mandatory fields:
715 if( event.GetRow() == SHEETNAME && event.GetCol() == FDC_VALUE )
716 {
717 if( textControl && textControl->IsEmpty() )
718 {
719 wxMessageBox( _( "A sheet must have a name." ) );
720 success = false;
721 }
722 }
723 else if( event.GetRow() == SHEETFILENAME && event.GetCol() == FDC_VALUE && textControl )
724 {
725 if( textControl->IsEmpty() )
726 {
727 wxMessageBox( _( "A sheet must have a file specified." ) );
728 success = false;
729 }
730 }
731
732 if( success && control && control->GetValidator() )
733 success = control->GetValidator()->Validate( control );
734
735 if( !success )
736 {
737 event.Veto();
738 m_delayedFocusRow = event.GetRow();
739 m_delayedFocusColumn = event.GetCol();
740 }
741
742 editor->DecRef();
743}
744
745
746void DIALOG_SHEET_PROPERTIES::OnAddField( wxCommandEvent& event )
747{
749 return;
750
751 int fieldID = m_fields->size();
752 SCH_FIELD newField( VECTOR2I( 0, 0 ), fieldID, m_sheet,
754
755 newField.SetTextAngle( m_fields->at( SHEETNAME ).GetTextAngle() );
756
757 m_fields->push_back( newField );
758
759 // notify the grid
760 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
761 m_grid->ProcessTableMessage( msg );
762
763 m_grid->MakeCellVisible( m_fields->size() - 1, 0 );
764 m_grid->SetGridCursor( m_fields->size() - 1, 0 );
765
766 m_grid->EnableCellEditControl();
767 m_grid->ShowCellEditControl();
768}
769
770
771void DIALOG_SHEET_PROPERTIES::OnDeleteField( wxCommandEvent& event )
772{
773 wxArrayInt selectedRows = m_grid->GetSelectedRows();
774
775 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
776 selectedRows.push_back( m_grid->GetGridCursorRow() );
777
778 if( selectedRows.empty() )
779 return;
780
781 for( int row : selectedRows )
782 {
783 if( row < m_fields->GetMandatoryRowCount() )
784 {
785 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
787 return;
788 }
789 }
790
791 m_grid->CommitPendingChanges( true /* quiet mode */ );
792
793 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
794 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
795
796 for( int row : selectedRows )
797 {
798 m_fields->erase( m_fields->begin() + row );
799
800 // notify the grid
801 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
802 m_grid->ProcessTableMessage( msg );
803
804 if( m_grid->GetNumberRows() > 0 )
805 {
806 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
807 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
808 }
809 }
810}
811
812
813void DIALOG_SHEET_PROPERTIES::OnMoveUp( wxCommandEvent& event )
814{
816 return;
817
818 int i = m_grid->GetGridCursorRow();
819
820 if( i > m_fields->GetMandatoryRowCount() )
821 {
822 SCH_FIELD tmp = m_fields->at( (unsigned) i );
823 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
824 m_fields->insert( m_fields->begin() + i - 1, tmp );
825 m_grid->ForceRefresh();
826
827 m_grid->SetGridCursor( i - 1, m_grid->GetGridCursorCol() );
828 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
829 }
830 else
831 {
832 wxBell();
833 }
834}
835
836
837void DIALOG_SHEET_PROPERTIES::OnMoveDown( wxCommandEvent& event )
838{
840 return;
841
842 int i = m_grid->GetGridCursorRow();
843
844 if( i >= m_fields->GetMandatoryRowCount() && i < m_grid->GetNumberRows() - 1 )
845 {
846 SCH_FIELD tmp = m_fields->at( (unsigned) i );
847 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
848 m_fields->insert( m_fields->begin() + i + 1, tmp );
849 m_grid->ForceRefresh();
850
851 m_grid->SetGridCursor( i + 1, m_grid->GetGridCursorCol() );
852 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
853 }
854 else
855 {
856 wxBell();
857 }
858}
859
860
862{
863 // Account for scroll bars
865
866 m_grid->AutoSizeColumn( 0 );
867 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
868
869 int fixedColsWidth = m_grid->GetColSize( 0 );
870
871 for( int i = 2; i < m_grid->GetNumberCols(); i++ )
872 fixedColsWidth += m_grid->GetColSize( i );
873
874 m_grid->SetColSize( 1, std::max( 120, width - fixedColsWidth ) );
875}
876
877
878void DIALOG_SHEET_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
879{
880 std::bitset<64> shownColumns = m_grid->GetShownColumns();
881
882 if( shownColumns != m_shownColumns )
883 {
884 m_shownColumns = shownColumns;
885
886 if( !m_grid->IsCellEditControlShown() )
888 }
889
890 // Propagate changes in sheetname to displayed hierarchical path
891 wxString path = m_frame->GetCurrentSheet().PathHumanReadable( false );
892
893 if( path.Last() != '/' )
894 path.Append( '/' );
895
896 wxGridCellEditor* editor = m_grid->GetCellEditor( SHEETNAME, FDC_VALUE );
897 wxControl* control = editor->GetControl();
898 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
899 wxString sheetName;
900
901 if( textControl )
902 sheetName = textControl->GetValue();
903 else
904 sheetName = m_grid->GetCellValue( SHEETNAME, FDC_VALUE );
905
907 m_dummySheetNameField.SetText( sheetName );
909
910 editor->DecRef();
911
912 wxClientDC dc( m_hierarchicalPathLabel );
913 int width = m_sizerBottom->GetSize().x - m_stdDialogButtonSizer->GetSize().x
914 - m_hierarchicalPathLabel->GetSize().x
915 - 30;
916
917 path = wxControl::Ellipsize( path, dc, wxELLIPSIZE_START, width, wxELLIPSIZE_FLAGS_NONE );
918
919 if( m_hierarchicalPath->GetLabel() != path )
920 m_hierarchicalPath->SetLabel( path );
921
922 // Handle a delayed focus
923 if( m_delayedFocusRow >= 0 )
924 {
925 m_grid->SetFocus();
926 m_grid->MakeCellVisible( m_delayedFocusRow, m_delayedFocusColumn );
928
929 m_grid->EnableCellEditControl( true );
930 m_grid->ShowCellEditControl();
931
934 }
935}
936
937
938void DIALOG_SHEET_PROPERTIES::OnSizeGrid( wxSizeEvent& event )
939{
940 auto new_size = event.GetSize();
941
942 if( m_size != new_size )
943 {
944 m_size = new_size;
945
947 }
948
949 // Always propagate for a grid repaint (needed if the height changes, as well as width)
950 event.Skip();
951}
952
953
954void DIALOG_SHEET_PROPERTIES::OnInitDlg( wxInitDialogEvent& event )
955{
957
958 // Now all widgets have the size fixed, call FinishDialogSettings
960
961 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
962
963 if( cfg && cfg->m_Appearance.edit_sheet_width > 0 && cfg->m_Appearance.edit_sheet_height > 0 )
965
966}
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.
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:587
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:290
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
int GetNumberRows() override
int GetMandatoryRowCount() const
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)
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:158
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:370
Holds all the data relating to one schematic.
Definition: schematic.h:82
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:130
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:728
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
bool IsMandatory() const
Definition: sch_field.cpp:1507
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1485
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:1254
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:1229
void SetName(const wxString &aName)
Definition: sch_field.cpp:1204
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:213
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1214
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:167
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition: sch_item.h:550
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:375
void SetBorderColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:118
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:305
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslated)
Definition: sch_sheet.cpp:59
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:92
wxString GetName() const
Definition: sch_sheet.h:106
void SetExcludedFromBOM(bool aExcludeFromBOM)
Set or clear the exclude from schematic bill of materials flag.
Definition: sch_sheet.h:374
bool GetExcludedFromBoard() const
Definition: sch_sheet.h:381
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:121
void SetDNP(bool aDNP)
Definition: sch_sheet.h:387
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:109
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:399
void SetFields(const std::vector< SCH_FIELD > &aFields)
Set multiple schematic fields.
Definition: sch_sheet.cpp:408
int GetScreenCount() const
Return the number of times the associated screen for the sheet is being used.
Definition: sch_sheet.cpp:193
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:170
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:117
void SetBorderWidth(int aWidth)
Definition: sch_sheet.h:115
bool GetExcludedFromSim() const override
Definition: sch_sheet.h:369
int GetBorderWidth() const
Definition: sch_sheet.h:114
void SetExcludedFromBoard(bool aExcludeFromBoard)
Set or clear exclude from board netlist flag.
Definition: sch_sheet.h:380
void SetExcludedFromSim(bool aExcludeFromSim) override
Set or clear the exclude from simulation flag.
Definition: sch_sheet.h:368
bool GetDNP() const
Set or clear the 'Do Not Populate' flaga.
Definition: sch_sheet.h:386
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:120
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:496
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:277
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:451
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:466
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:485
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
void ShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION) override
Show the info bar with the provided message and icon.
Definition: wx_infobar.cpp:153
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:425
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:440
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 GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:155
@ GLOBAL_CLEANUP
@ AUTOPLACE_NONE
Definition: sch_item.h:69
@ 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.
#define DO_TRANSLATE
wxLogTrace helper definitions.
@ SCH_SHEET_T
Definition: typeinfo.h:174
Custom text control validator definitions.
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39