KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <stambaughw@gmail.com>
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 ), FIELD_T::SHEET_NAME, &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, so convert to Windows notation
150 if( field_copy.GetId() == FIELD_T::SHEET_FILENAME )
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
249{
252 {
253 return true;
254 }
255
258 {
259 return true;
260 }
261
262 return false;
263}
264
265
267{
268 wxCHECK( m_sheet && m_frame, false );
269
270 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
271 return false;
272
273 if( m_isUndoable )
274 *m_isUndoable = true;
275
276 // Sheet file names can be relative or absolute.
277 wxString sheetFileName = m_fields->GetField( FIELD_T::SHEET_FILENAME )->GetText();
278
279 // Ensure filepath is not empty. (In normal use will be caught by grid validators,
280 // but unedited data from existing files can be bad.)
281 if( sheetFileName.IsEmpty() )
282 {
283 DisplayError( this, _( "A sheet must have a valid file name." ) );
284 return false;
285 }
286
287 // Ensure the filename extension is OK. (In normal use will be caught by grid validators,
288 // but unedited data from existing files can be bad.)
289 sheetFileName = EnsureFileExtension( sheetFileName, FILEEXT::KiCadSchematicFileExtension );
290
291 // Ensure sheetFileName is legal
292 if( !IsFullFileNameValid( sheetFileName ) )
293 {
294 DisplayError( this, _( "A sheet must have a valid file name." ) );
295 return false;
296 }
297
298 wxFileName fn( sheetFileName );
299
300 wxString newRelativeFilename = fn.GetFullPath();
301
302 // Inside Eeschema, filenames are stored using unix notation
303 newRelativeFilename.Replace( wxT( "\\" ), wxT( "/" ) );
304
305 wxString oldFilename = m_sheet->GetField( FIELD_T::SHEET_FILENAME )->GetText();
306 oldFilename.Replace( wxT( "\\" ), wxT( "/" ) );
307
308 bool filename_changed = oldFilename != newRelativeFilename;
309
310 if( filename_changed || m_sheet->IsNew() )
311 {
312 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
313
314 wxCHECK( currentScreen, false );
315
316 bool clearFileName = false;
317
318 // This can happen for the root sheet when opening Eeschema in the stand alone mode.
319 if( currentScreen->GetFileName().IsEmpty() )
320 {
321 clearFileName = true;
322 currentScreen->SetFileName( m_frame->Prj().AbsolutePath( wxT( "noname.kicad_sch" ) ) );
323 }
324
325 wxFileName tmp( fn );
326 wxFileName screenFileName = currentScreen->GetFileName();
327
328 if( fn.IsAbsolute() && fn.MakeRelativeTo( screenFileName.GetPath() ) )
329 {
330 wxMessageDialog makeRelDlg( this, _( "Use relative path for sheet file?" ),
331 _( "Sheet File Path" ),
332 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION | wxCENTER );
333
334 makeRelDlg.SetExtendedMessage( _( "Using relative hierarchical sheet file name paths "
335 "improves schematic portability across systems and "
336 "platforms. Using absolute paths can result in "
337 "portability issues." ) );
338 makeRelDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Use Relative Path" ) ),
339 wxMessageDialog::ButtonLabel( _( "Use Absolute Path" ) ) );
340
341 if( makeRelDlg.ShowModal() == wxID_YES )
342 {
343 wxLogTrace( tracePathsAndFiles, "\n Converted absolute path: '%s'"
344 "\n to relative path: '%s'",
345 tmp.GetPath(),
346 fn.GetPath() );
347 m_fields->GetField( FIELD_T::SHEET_FILENAME )->SetText( fn.GetFullPath() );
348 newRelativeFilename = fn.GetFullPath();
349 }
350 }
351
352 if( !onSheetFilenameChanged( newRelativeFilename ) )
353 {
354 if( clearFileName )
355 currentScreen->SetFileName( wxEmptyString );
356 else
357 FindField( *m_fields, FIELD_T::SHEET_FILENAME )->SetText( oldFilename );
358
359 return false;
360 }
362 {
364 }
365
366 if( clearFileName )
367 currentScreen->SetFileName( wxEmptyString );
368
369 // One last validity check (and potential repair) just to be sure to be sure
370 SCH_SHEET_LIST repairedList;
371 repairedList.BuildSheetList( &m_frame->Schematic().Root(), true );
372 }
373
374 wxString newSheetname = m_fields->GetField( FIELD_T::SHEET_NAME )->GetText();
375
376 if( ( newSheetname != m_sheet->GetName() ) && m_updateHierarchyNavigator )
378
379 if( newSheetname.IsEmpty() )
380 newSheetname = _( "Untitled Sheet" );
381
382 m_fields->GetField( FIELD_T::SHEET_NAME )->SetText( newSheetname );
383
384 // change all field positions from relative to absolute
385 for( SCH_FIELD& m_field : *m_fields)
386 m_field.Offset( m_sheet->GetPosition() );
387
390
391 for( int ii = m_fields->GetNumberRows() - 1; ii >= 0; ii-- )
392 {
393 SCH_FIELD& field = m_fields->at( ii );
394
395 if( field.IsMandatory() )
396 continue;
397
398 const wxString& fieldName = field.GetCanonicalName();
399
400 if( field.IsEmpty() )
401 m_fields->erase( m_fields->begin() + ii );
402 else if( fieldName.IsEmpty() )
403 field.SetName( _( "untitled" ) );
404 }
405
407
409
410 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
411
412 if( colorSettings->GetOverrideSchItemColors()
415 {
416 wxPanel temp( this );
417 temp.Hide();
418 PANEL_EESCHEMA_COLOR_SETTINGS prefs( &temp );
419 wxString checkboxLabel = prefs.m_optOverrideColors->GetLabel();
420
421 KIDIALOG dlg( this, _( "Note: item colors are overridden in the current color theme." ),
423 dlg.ShowDetailedText( wxString::Format( _( "To see individual item colors uncheck '%s'\n"
424 "in Preferences > Schematic Editor > Colors." ),
425 checkboxLabel ) );
426 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
427 dlg.ShowModal();
428 }
429
432
436 m_sheet->SetDNP( m_cbDNP->GetValue() );
437
439
440 instance.push_back( m_sheet );
441
442 instance.SetPageNumber( m_pageNumberTextCtrl->GetValue() );
443
445
446 // Refresh all sheets in case ordering changed.
447 for( SCH_ITEM* item : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) )
448 m_frame->UpdateItem( item );
449
450 return true;
451}
452
453
454bool DIALOG_SHEET_PROPERTIES::onSheetFilenameChanged( const wxString& aNewFilename )
455{
456 wxString msg;
457 wxFileName sheetFileName( EnsureFileExtension( aNewFilename,
459
460 // Sheet file names are relative to the path of the current sheet. This allows for
461 // nesting of schematic files in subfolders. Screen file names are always absolute.
462 SCHEMATIC& schematic = m_frame->Schematic();
463 SCH_SHEET_LIST fullHierarchy = schematic.Hierarchy();
464 wxFileName screenFileName( sheetFileName );
465 wxFileName tmp( sheetFileName );
466 SCH_SCREEN* currentScreen = m_frame->GetCurrentSheet().LastScreen();
467
468 wxCHECK( currentScreen, false );
469
470 // SCH_SCREEN file names are always absolute.
471 wxFileName currentScreenFileName = currentScreen->GetFileName();
472
473 if( !screenFileName.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS,
474 currentScreenFileName.GetPath() ) )
475 {
476 msg = wxString::Format( _( "Cannot normalize new sheet schematic file path:\n"
477 "'%s'\n"
478 "against parent sheet schematic file path:\n"
479 "'%s'." ),
480 sheetFileName.GetPath(),
481 currentScreenFileName.GetPath() );
482 DisplayError( this, msg );
483 return false;
484 }
485
486 wxString newAbsoluteFilename = screenFileName.GetFullPath();
487
488 // Inside Eeschema, filenames are stored using unix notation
489 newAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
490
491 bool renameFile = false;
492 bool loadFromFile = false;
493 bool clearAnnotation = false;
494 bool isExistingSheet = false;
495 SCH_SCREEN* useScreen = nullptr;
496 SCH_SCREEN* oldScreen = nullptr;
497
498 // Search for a schematic file having the same filename already in use in the hierarchy
499 // or on disk, in order to reuse it.
500 if( !schematic.Root().SearchHierarchy( newAbsoluteFilename, &useScreen ) )
501 {
502 loadFromFile = wxFileExists( newAbsoluteFilename );
503
504 wxLogTrace( tracePathsAndFiles, "\n Sheet requested file '%s', %s",
505 newAbsoluteFilename,
506 loadFromFile ? "found" : "not found" );
507 }
508
509 if( m_sheet->GetScreen() == nullptr ) // New just created sheet.
510 {
512 newAbsoluteFilename ) )
513 return false;
514
515 if( useScreen || loadFromFile ) // Load from existing file.
516 {
517 clearAnnotation = true;
518
519 if( !IsOK( this, wxString::Format( _( "'%s' already exists." ),
520 sheetFileName.GetFullName() )
521 + wxT( "\n\n" )
522 + wxString::Format( _( "Link '%s' to this file?" ),
523 newAbsoluteFilename ) ) )
524 {
525 return false;
526 }
527 }
528 // If we are drawing a sheet from a design block/sheet import, we need to copy the
529 // sheet to the current directory.
530 else if( m_sourceSheetFilename && !m_sourceSheetFilename->IsEmpty() )
531 {
532 loadFromFile = true;
533
534 if( !wxCopyFile( *m_sourceSheetFilename, newAbsoluteFilename, false ) )
535 {
536 msg.Printf( _( "Failed to copy schematic file '%s' to destination '%s'." ),
537 currentScreenFileName.GetFullPath(), newAbsoluteFilename );
538
539 DisplayError( m_frame, msg );
540
541 return false;
542 }
543 }
544 else // New file.
545 {
546 m_frame->InitSheet( m_sheet, newAbsoluteFilename );
547 }
548 }
549 else // Existing sheet.
550 {
551 isExistingSheet = true;
552
554 newAbsoluteFilename ) )
555 return false;
556
557 // We are always using here a case insensitive comparison to avoid issues
558 // under Windows, although under Unix filenames are case sensitive.
559 // But many users create schematic under both Unix and Windows
560 // **
561 // N.B. 1: aSheet->GetFileName() will return a relative path
562 // aSheet->GetScreen()->GetFileName() returns a full path
563 //
564 // N.B. 2: newFilename uses the unix notation for separator.
565 // so we must use it also to compare the old and new filenames
566 wxString oldAbsoluteFilename = m_sheet->GetScreen()->GetFileName();
567 oldAbsoluteFilename.Replace( wxT( "\\" ), wxT( "/" ) );
568
569 if( newAbsoluteFilename.Cmp( oldAbsoluteFilename ) != 0 )
570 {
571 // Sheet file name changes cannot be undone.
572 if( m_isUndoable )
573 *m_isUndoable = false;
574
575 if( useScreen || loadFromFile ) // Load from existing file.
576 {
577 clearAnnotation = true;
578 oldScreen = m_sheet->GetScreen();
579
580 if( !IsOK( this, wxString::Format( _( "Change '%s' link from '%s' to '%s'?" ),
581 newAbsoluteFilename,
583 sheetFileName.GetFullName() )
584 + wxT( "\n\n" )
585 + _( "This action cannot be undone." ) ) )
586 {
587 return false;
588 }
589
590 if( loadFromFile )
591 m_sheet->SetScreen( nullptr );
592 }
593 else // Save to new file name.
594 {
595 if( m_sheet->GetScreenCount() > 1 )
596 {
597 if( !IsOK( this, wxString::Format( _( "Create new file '%s' with contents "
598 "of '%s'?" ),
599 sheetFileName.GetFullName(),
601 + wxT( "\n\n" )
602 + _( "This action cannot be undone." ) ) )
603 {
604 return false;
605 }
606 }
607
608 renameFile = true;
609 }
610 }
611
612 if( renameFile )
613 {
614 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
615
616 // If the associated screen is shared by more than one sheet, do not
617 // change the filename of the corresponding screen here.
618 // (a new screen will be created later)
619 // if it is not shared, update the filename
620 if( m_sheet->GetScreenCount() <= 1 )
621 m_sheet->GetScreen()->SetFileName( newAbsoluteFilename );
622
623 try
624 {
625 pi->SaveSchematicFile( newAbsoluteFilename, m_sheet, &schematic );
626 }
627 catch( const IO_ERROR& ioe )
628 {
629 msg = wxString::Format( _( "Error occurred saving schematic file '%s'." ),
630 newAbsoluteFilename );
631 DisplayErrorMessage( this, msg, ioe.What() );
632
633 msg = wxString::Format( _( "Failed to save schematic '%s'" ),
634 newAbsoluteFilename );
635 m_frame->SetMsgPanel( wxEmptyString, msg );
636 return false;
637 }
638
639 // If the associated screen is shared by more than one sheet, remove the
640 // screen and reload the file to a new screen. Failure to do this will trash
641 // the screen reference counting in complex hierarchies.
642 if( m_sheet->GetScreenCount() > 1 )
643 {
644 oldScreen = m_sheet->GetScreen();
645 m_sheet->SetScreen( nullptr );
646 loadFromFile = true;
647 }
648 }
649 }
650
651 SCH_SHEET_PATH& currentSheet = m_frame->GetCurrentSheet();
652
653 if( useScreen )
654 {
655 // Create a temporary sheet for recursion testing to prevent a possible recursion error.
656 std::unique_ptr< SCH_SHEET> tmpSheet = std::make_unique<SCH_SHEET>( &schematic );
657 *tmpSheet->GetField( FIELD_T::SHEET_NAME ) = m_fields->GetField( FIELD_T::SHEET_NAME );
658 tmpSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( sheetFileName.GetFullPath() );
659 tmpSheet->SetScreen( useScreen );
660
661 // No need to check for valid library IDs if we are using an existing screen.
662 if( m_frame->CheckSheetForRecursion( tmpSheet.get(), &currentSheet ) )
663 return false;
664
665 // It's safe to set the sheet screen now.
666 m_sheet->SetScreen( useScreen );
667
668 SCH_SHEET_LIST sheetHierarchy( m_sheet ); // The hierarchy of the loaded file.
669
670 sheetHierarchy.AddNewSymbolInstances( currentSheet, m_frame->Prj().GetProjectName() );
671 sheetHierarchy.AddNewSheetInstances( currentSheet,
672 fullHierarchy.GetLastVirtualPageNumber() );
673 }
674 else if( loadFromFile )
675 {
676 bool restoreSheet = false;
677
678 if( isExistingSheet )
679 {
680 // Temporarily remove the sheet from the current schematic page so that recursion
681 // and symbol library link tests can be performed with the modified sheet settings.
682 restoreSheet = true;
683 currentSheet.LastScreen()->Remove( m_sheet );
684 }
685
686 if( !m_frame->LoadSheetFromFile( m_sheet, &currentSheet, newAbsoluteFilename, false, true )
687 || m_frame->CheckSheetForRecursion( m_sheet, &currentSheet ) )
688 {
689 if( restoreSheet )
690 {
691 // If we cleared the previous screen, restore it before returning to the user
692 if( oldScreen )
693 m_sheet->SetScreen( oldScreen );
694
695 currentSheet.LastScreen()->Append( m_sheet );
696 }
697
698 return false;
699 }
700
701 if( restoreSheet )
702 currentSheet.LastScreen()->Append( m_sheet );
703 }
704
706 *m_clearAnnotationNewItems = clearAnnotation;
707
708 // Rebuild the entire connection graph.
710
711 return true;
712}
713
714
716{
717 bool success = true;
718 wxGridCellEditor* editor = m_grid->GetCellEditor( event.GetRow(), event.GetCol() );
719 wxControl* control = editor->GetControl();
720
721 if( control && control->GetValidator() )
722 success = control->GetValidator()->Validate( control );
723
724 if( !success )
725 {
726 event.Veto();
727 m_delayedFocusRow = event.GetRow();
728 m_delayedFocusColumn = event.GetCol();
729 }
730
731 editor->DecRef();
732}
733
734
735void DIALOG_SHEET_PROPERTIES::OnAddField( wxCommandEvent& event )
736{
738 return;
739
740 SCH_FIELD newField( { 0, 0 }, FIELD_T::SHEET_USER, m_sheet,
741 GetUserFieldName( (int) m_fields->size(), DO_TRANSLATE ) );
742
743 newField.SetTextAngle( m_fields->GetField( FIELD_T::SHEET_NAME )->GetTextAngle() );
744
745 m_fields->push_back( newField );
746
747 // notify the grid
748 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
749 m_grid->ProcessTableMessage( msg );
750
751 m_grid->MakeCellVisible( m_fields->size() - 1, 0 );
752 m_grid->SetGridCursor( m_fields->size() - 1, 0 );
753
754 m_grid->EnableCellEditControl();
755 m_grid->ShowCellEditControl();
756}
757
758
759void DIALOG_SHEET_PROPERTIES::OnDeleteField( wxCommandEvent& event )
760{
761 wxArrayInt selectedRows = m_grid->GetSelectedRows();
762
763 if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
764 selectedRows.push_back( m_grid->GetGridCursorRow() );
765
766 if( selectedRows.empty() )
767 return;
768
769 for( int row : selectedRows )
770 {
771 if( row < m_fields->GetMandatoryRowCount() )
772 {
773 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
775 return;
776 }
777 }
778
779 m_grid->CommitPendingChanges( true /* quiet mode */ );
780
781 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
782 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
783
784 for( int row : selectedRows )
785 {
786 m_fields->erase( m_fields->begin() + row );
787
788 // notify the grid
789 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
790 m_grid->ProcessTableMessage( msg );
791
792 if( m_grid->GetNumberRows() > 0 )
793 {
794 m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
795 m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
796 }
797 }
798}
799
800
801void DIALOG_SHEET_PROPERTIES::OnMoveUp( wxCommandEvent& event )
802{
804 return;
805
806 int i = m_grid->GetGridCursorRow();
807
808 if( i > m_fields->GetMandatoryRowCount() )
809 {
810 SCH_FIELD tmp = m_fields->at( (unsigned) i );
811 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
812 m_fields->insert( m_fields->begin() + i - 1, tmp );
813 m_grid->ForceRefresh();
814
815 m_grid->SetGridCursor( i - 1, m_grid->GetGridCursorCol() );
816 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
817 }
818 else
819 {
820 wxBell();
821 }
822}
823
824
825void DIALOG_SHEET_PROPERTIES::OnMoveDown( wxCommandEvent& event )
826{
828 return;
829
830 int i = m_grid->GetGridCursorRow();
831
832 if( i >= m_fields->GetMandatoryRowCount() && i < m_grid->GetNumberRows() - 1 )
833 {
834 SCH_FIELD tmp = m_fields->at( (unsigned) i );
835 m_fields->erase( m_fields->begin() + i, m_fields->begin() + i + 1 );
836 m_fields->insert( m_fields->begin() + i + 1, tmp );
837 m_grid->ForceRefresh();
838
839 m_grid->SetGridCursor( i + 1, m_grid->GetGridCursorCol() );
840 m_grid->MakeCellVisible( m_grid->GetGridCursorRow(), m_grid->GetGridCursorCol() );
841 }
842 else
843 {
844 wxBell();
845 }
846}
847
848
850{
851 // Account for scroll bars
853
854 m_grid->AutoSizeColumn( 0 );
855 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
856
857 int fixedColsWidth = m_grid->GetColSize( 0 );
858
859 for( int i = 2; i < m_grid->GetNumberCols(); i++ )
860 fixedColsWidth += m_grid->GetColSize( i );
861
862 m_grid->SetColSize( 1, std::max( 120, width - fixedColsWidth ) );
863}
864
865
866void DIALOG_SHEET_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
867{
868 std::bitset<64> shownColumns = m_grid->GetShownColumns();
869
870 if( shownColumns != m_shownColumns )
871 {
872 m_shownColumns = shownColumns;
873
874 if( !m_grid->IsCellEditControlShown() )
876 }
877
878 // Propagate changes in sheetname to displayed hierarchical path
879 int sheetnameRow = m_fields->GetFieldRow( FIELD_T::SHEET_NAME );
880 wxString path = m_frame->GetCurrentSheet().PathHumanReadable( false );
881
882 if( path.Last() != '/' )
883 path.Append( '/' );
884
885 wxGridCellEditor* editor = m_grid->GetCellEditor( sheetnameRow, FDC_VALUE );
886 wxControl* control = editor->GetControl();
887 wxTextEntry* textControl = dynamic_cast<wxTextEntry*>( control );
888 wxString sheetName;
889
890 if( textControl )
891 sheetName = textControl->GetValue();
892 else
893 sheetName = m_grid->GetCellValue( sheetnameRow, FDC_VALUE );
894
896 m_dummySheetNameField.SetText( sheetName );
898
899 editor->DecRef();
900
901 wxClientDC dc( m_hierarchicalPathLabel );
902 int width = m_sizerBottom->GetSize().x - m_stdDialogButtonSizer->GetSize().x
903 - m_hierarchicalPathLabel->GetSize().x
904 - 30;
905
906 path = wxControl::Ellipsize( path, dc, wxELLIPSIZE_START, width, wxELLIPSIZE_FLAGS_NONE );
907
908 if( m_hierarchicalPath->GetLabel() != path )
909 m_hierarchicalPath->SetLabel( path );
910
911 // Handle a delayed focus
912 if( m_delayedFocusRow >= 0 )
913 {
914 m_grid->SetFocus();
915 m_grid->MakeCellVisible( m_delayedFocusRow, m_delayedFocusColumn );
917
918 m_grid->EnableCellEditControl( true );
919 m_grid->ShowCellEditControl();
920
923 }
924}
925
926
927void DIALOG_SHEET_PROPERTIES::OnSizeGrid( wxSizeEvent& event )
928{
929 auto new_size = event.GetSize();
930
931 if( m_size != new_size )
932 {
933 m_size = new_size;
934
936 }
937
938 // Always propagate for a grid repaint (needed if the height changes, as well as width)
939 event.Skip();
940}
941
942
943void DIALOG_SHEET_PROPERTIES::OnInitDlg( wxInitDialogEvent& event )
944{
946
947 // Now all widgets have the size fixed, call FinishDialogSettings
949
950 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
951
952 if( cfg && cfg->m_Appearance.edit_sheet_width > 0 && cfg->m_Appearance.edit_sheet_height > 0 )
954
955}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Color settings are a bit different than most of the settings objects in that there can be more than o...
bool GetOverrideSchItemColors() const
COLOR4D GetColor(int aLayer) const
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
KIGFX::COLOR4D GetSwatchColor() const
void SetDefaultColor(const KIGFX::COLOR4D &aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
void SetSwatchBackground(const KIGFX::COLOR4D &aBackground)
Set the swatch background color.
Class DIALOG_SHEET_PROPERTIES_BASE.
wxStdDialogButtonSizer * m_stdDialogButtonSizer
void OnAddField(wxCommandEvent &event) override
void OnMoveDown(wxCommandEvent &event) override
void OnGridCellChanging(wxGridEvent &event)
void OnDeleteField(wxCommandEvent &event) override
bool onSheetFilenameChanged(const wxString &aNewFilename)
void OnSizeGrid(wxSizeEvent &event) override
void OnInitDlg(wxInitDialogEvent &event) override
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnMoveUp(wxCommandEvent &event) override
DIALOG_SHEET_PROPERTIES(SCH_EDIT_FRAME *aParent, SCH_SHEET *aSheet, bool *aIsUndoable, bool *aClearAnnotationNewItems, bool *aUpdateHierarchyNavigator, wxString *aSourceSheetFilename)
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
bool IsNew() const
Definition: eda_item.h:113
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:589
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:292
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:241
SCH_FIELD * GetField(FIELD_T aFieldId)
int GetNumberRows() override
int GetMandatoryRowCount() const
int GetFieldRow(FIELD_T aFieldId)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h: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:69
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:208
SCH_SHEET & Root() const
Definition: schematic.h:117
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Returns a pointer to the active color theme settings.
Schematic editor (Eeschema) main window.
bool AllowCaseSensitiveFileNameClashes(const wxString &aOldName, const wxString &aSchematicFileName)
Check aSchematicFileName for a potential file name case sensitivity clashes.
Definition: sheet.cpp:720
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void InitSheet(SCH_SHEET *aSheet, const wxString &aNewFilename)
Definition: sheet.cpp:110
bool LoadSheetFromFile(SCH_SHEET *aSheet, SCH_SHEET_PATH *aCurrentSheet, const wxString &aFileName, bool aSkipRecursionCheck=false, bool aSkipLibCheck=false)
Load a the KiCad schematic file aFileName into the sheet aSheet.
Definition: sheet.cpp:169
SCH_SHEET_PATH & GetCurrentSheet() const
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.
bool IsMandatory() const
Definition: sch_field.cpp:1336
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1314
FIELD_T GetId() const
Definition: sch_field.h:124
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:1098
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:1084
void SetName(const wxString &aName)
Definition: sch_field.cpp:1059
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:192
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1069
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
void SetFieldsAutoplaced(AUTOPLACE_ALGO aAlgo)
Definition: sch_item.h:566
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:155
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition: sch_screen.h:112
const wxString & GetFileName() const
Definition: sch_screen.h:147
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:120
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
Definition: sch_screen.cpp:325
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void AddNewSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath, const wxString &aProjectName)
Attempt to add new symbol instances for all symbols in this list of sheet paths prefixed with aPrefix...
int GetLastVirtualPageNumber() const
void AddNewSheetInstances(const SCH_SHEET_PATH &aPrefixSheetPath, int aLastVirtualPageNumber)
void BuildSheetList(SCH_SHEET *aSheet, bool aCheckIntegrity)
Build the list of sheets and their sheet path from aSheet.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false) const
Return the sheet path in a human readable form made from the sheet names.
SCH_SCREEN * LastScreen()
wxString GetPageNumber() const
void SetPageNumber(const wxString &aPageNumber)
Set the sheet instance user definable page number.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:47
bool GetExcludedFromBOM() const
Definition: sch_sheet.h:391
void SetBorderColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:125
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:321
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition: sch_sheet.h:87
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
Definition: sch_sheet.cpp:369
wxString GetName() const
Definition: sch_sheet.h:113
void SetExcludedFromBOM(bool aExcludeFromBOM)
Set or clear the exclude from schematic bill of materials flag.
Definition: sch_sheet.h:390
bool GetExcludedFromBoard() const
Definition: sch_sheet.h:397
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:753
void SetBackgroundColor(KIGFX::COLOR4D aColor)
Definition: sch_sheet.h:128
void SetDNP(bool aDNP)
Definition: sch_sheet.h:403
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:116
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:415
void SetFields(const std::vector< SCH_FIELD > &aFields)
Set multiple schematic fields.
Definition: sch_sheet.cpp:391
int GetScreenCount() const
Return the number of times the associated screen for the sheet is being used.
Definition: sch_sheet.cpp:160
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:137
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:124
void SetBorderWidth(int aWidth)
Definition: sch_sheet.h:122
bool GetExcludedFromSim() const override
Definition: sch_sheet.h:385
int GetBorderWidth() const
Definition: sch_sheet.h:121
void SetExcludedFromBoard(bool aExcludeFromBoard)
Set or clear exclude from board netlist flag.
Definition: sch_sheet.h:396
void SetExcludedFromSim(bool aExcludeFromSim) override
Set or clear the exclude from simulation flag.
Definition: sch_sheet.h:384
bool GetDNP() const
Set or clear the 'Do Not Populate' flags.
Definition: sch_sheet.h:402
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:127
void SetBitmap(const wxBitmapBundle &aBmp)
int GetIntValue()
Definition: unit_binder.h:129
virtual void SetValue(long long int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:494
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:275
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:449
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:464
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:483
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:644
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: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:249
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:170
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:477
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:156
@ GLOBAL_CLEANUP
const SCH_FIELD * FindField(const std::vector< SCH_FIELD > &aFields, FIELD_T aFieldId)
Definition: sch_field.h:367
@ AUTOPLACE_NONE
Definition: sch_item.h:70
bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ SHEET_FILENAME
wxLogTrace helper definitions.
@ SCH_SHEET_T
Definition: typeinfo.h:174
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