KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_label_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) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2013 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <template_fieldnames.h>
24#include <widgets/font_choice.h>
29#include <sch_edit_frame.h>
30#include <tool/tool_manager.h>
31#include <gr_text.h>
32#include <confirm.h>
33#include <schematic.h>
36#include <string_utils.h>
37#include <kiface_base.h>
38#include <sch_label.h>
39#include <sch_commit.h>
40
41
44 m_Parent( aParent ),
45 m_currentLabel( aLabel ),
46 m_activeTextEntry( nullptr ),
47 m_netNameValidator( true ),
48 m_fields( nullptr ),
50 m_helpWindow( nullptr ),
51 m_labelList( nullptr )
52{
53 COLOR_SETTINGS* colorSettings = m_Parent->GetColorSettings();
54 COLOR4D schematicBackground = colorSettings->GetColor( LAYER_SCHEMATIC_BACKGROUND );
55
56 m_fields = new FIELDS_GRID_TABLE( this, aParent, m_grid, m_currentLabel );
59
60 if( m_currentLabel->Type() == SCH_GLOBAL_LABEL_T || m_currentLabel->Type() == SCH_LABEL_T )
61 {
63 SetInitialFocus( m_valueCombo->GetTextCtrl() );
64
65 m_labelSingleLine->Show( false );
66 m_valueSingleLine->Show( false );
67 }
68 else if( m_currentLabel->Type() == SCH_HIER_LABEL_T )
69 {
72
73 m_labelCombo->Show( false );
74 m_valueCombo->Show( false );
75 }
76 else if( m_currentLabel->Type() == SCH_DIRECTIVE_LABEL_T )
77 {
80
81 m_labelSingleLine->Show( false );
82 m_valueSingleLine->Show( false );
83 m_labelCombo->Show( false );
84 m_valueCombo->Show( false );
85 m_syntaxHelp->Show( false );
86 m_textEntrySizer->Show( false );
87 m_labelCombo->Show( false );
88 m_valueCombo->Show( false );
89 m_cbMultiLine->Show( false );
90
91 m_textSizeLabel->SetLabel( _( "Pin length:" ) );
92 }
93
94 if( !aNew )
95 m_cbMultiLine->Show( false );
96
97 // multiline set of labels can be used only to create new labels
98 m_multilineAllowed = aNew && m_cbMultiLine->IsShown();
99
100 switch( m_currentLabel->Type() )
101 {
102 case SCH_GLOBAL_LABEL_T: SetTitle( _( "Global Label Properties" ) ); break;
103 case SCH_HIER_LABEL_T: SetTitle( _( "Hierarchical Label Properties" ) ); break;
104 case SCH_LABEL_T: SetTitle( _( "Label Properties" ) ); break;
105 case SCH_DIRECTIVE_LABEL_T: SetTitle( _( "Directive Label Properties" ) ); break;
106 case SCH_SHEET_PIN_T: SetTitle( _( "Hierarchical Sheet Pin Properties" ) ); break;
107 default: UNIMPLEMENTED_FOR( m_currentLabel->GetClass() ); break;
108 }
109
110 m_grid->SetTable( m_fields );
111 m_grid->PushEventHandler( new FIELDS_GRID_TRICKS( m_grid, this, {},
112 [&]( wxCommandEvent& aEvent )
113 {
114 OnAddField( aEvent );
115 } ) );
116 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
117 m_grid->ShowHideColumns( "0 1 2 3 4 5 6 7" );
118 m_shownColumns = m_grid->GetShownColumns();
119
120 // Configure button logos
125
126 m_separator1->SetIsSeparator();
127
128 m_bold->SetIsCheckButton();
130 m_italic->SetIsCheckButton();
132
133 m_separator2->SetIsSeparator();
134
135 m_spin0->SetIsRadioButton();
136 m_spin1->SetIsRadioButton();
137 m_spin2->SetIsRadioButton();
138 m_spin3->SetIsRadioButton();
139
140 m_separator3->SetIsSeparator();
141
142 m_textColorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
143 m_textColorSwatch->SetSwatchBackground( schematicBackground );
144
145 // Show/hide relevant controls
147 {
148 m_dot->Hide();
149 m_circle->Hide();
150 m_diamond->Hide();
151 m_rectangle->Hide();
152
157 }
158 else if( m_currentLabel->Type() == SCH_DIRECTIVE_LABEL_T )
159 {
160 m_input->Hide();
161 m_output->Hide();
162 m_bidirectional->Hide();
163 m_triState->Hide();
164 m_passive->Hide();
165
166 m_fontLabel->SetLabel( _( "Orientation:" ) );
167 m_fontCtrl->Hide();
168 m_separator1->Hide();
169 m_bold->Hide();
170 m_italic->Hide();
171 m_separator2->Hide();
176 m_separator3->Hide();
177
178 m_formattingGB->Detach( m_fontCtrl );
179 m_formattingGB->Detach( m_iconBar );
180 m_formattingGB->Add( m_iconBar, wxGBPosition( 0, 1 ), wxGBSpan( 1, 1 ), wxEXPAND | wxRIGHT, 5 );
181 }
182 else
183 {
184 m_shapeSizer->Show( false );
185
190 }
191
192 if( !m_currentLabel->AutoRotateOnPlacementSupported() )
193 {
194 m_autoRotate->Hide();
195 wxSizer* parentSizer = m_autoRotate->GetContainingSizer();
196 parentSizer->Detach( m_autoRotate );
197 parentSizer->Layout();
198 }
199
201
202 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient because the
203 // various versions have different controls so we want to store sizes for each version.
204 m_hash_key = TO_UTF8( GetTitle() );
205
206 m_spin0->Bind( wxEVT_BUTTON, &DIALOG_LABEL_PROPERTIES::onSpinButton, this );
207 m_spin1->Bind( wxEVT_BUTTON, &DIALOG_LABEL_PROPERTIES::onSpinButton, this );
208 m_spin2->Bind( wxEVT_BUTTON, &DIALOG_LABEL_PROPERTIES::onSpinButton, this );
209 m_spin3->Bind( wxEVT_BUTTON, &DIALOG_LABEL_PROPERTIES::onSpinButton, this );
210
211 // wxFormBuilder doesn't include this event...
212 m_grid->Connect( wxEVT_GRID_CELL_CHANGING,
213 wxGridEventHandler( DIALOG_LABEL_PROPERTIES::OnGridCellChanging ), nullptr,
214 this );
215
216 // Now all widgets have the size fixed, call FinishDialogSettings
218}
219
220
222{
223 // Prevents crash bug in wxGrid's d'tor
224 m_grid->DestroyTable( m_fields );
225
226 m_grid->Disconnect( wxEVT_GRID_CELL_CHANGING,
227 wxGridEventHandler( DIALOG_LABEL_PROPERTIES::OnGridCellChanging ), nullptr,
228 this );
229
230 // Delete the GRID_TRICKS.
231 m_grid->PopEventHandler( true );
232
233 if( m_helpWindow )
234 m_helpWindow->Destroy();
235}
236
237
239{
240 if( !wxDialog::TransferDataToWindow() )
241 return false;
242
243 // Respond to previously-saved state of multilable checkbox
244 wxCommandEvent dummy;
246
247 wxString text;
248
250 {
251 // show control characters in a human-readable format
252 text = UnescapeString( m_currentLabel->GetText() );
253
254 // show text variable cross-references in a human-readable format
255 text = m_currentLabel->Schematic()->ConvertKIIDsToRefs( text );
256 }
257
258 if( m_currentLabel->Type() == SCH_GLOBAL_LABEL_T || m_currentLabel->Type() == SCH_LABEL_T )
259 {
260 // Load the combobox with the existing labels of the same type
261 std::set<wxString> existingLabels;
262 SCH_SCREENS allScreens( m_Parent->Schematic().Root() );
263
264 for( SCH_SCREEN* screen = allScreens.GetFirst(); screen; screen = allScreens.GetNext() )
265 {
266 for( SCH_ITEM* item : screen->Items().OfType( m_currentLabel->Type() ) )
267 {
268 const SCH_LABEL_BASE* label = static_cast<const SCH_LABEL_BASE*>( item );
269 existingLabels.insert( UnescapeString( label->GetText() ) );
270 }
271
272 // Add global power labels from power symbols
273 if( m_currentLabel->Type() == SCH_GLOBAL_LABEL_T )
274 {
275 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_LOCATE_POWER_T ) )
276 {
277 const SCH_SYMBOL* power = static_cast<const SCH_SYMBOL*>( item );
278
279 // Ensure the symbol has the Power (i.e. equivalent to a global label
280 // before adding its value in list
281 if( power->IsSymbolLikePowerGlobalLabel() )
282 {
283 const SCH_FIELD* valueField = power->GetField( FIELD_T::VALUE );
284 existingLabels.insert( UnescapeString( valueField->GetText() ) );
285 }
286 }
287 }
288
289 // Add local power labels from power symbols
290 if( m_currentLabel->Type() == SCH_LABEL_T )
291 {
292 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_LOCATE_POWER_T ) )
293 {
294 const SCH_SYMBOL* power = static_cast<const SCH_SYMBOL*>( item );
295
296 // Ensure the symbol has the Power (i.e. equivalent to a local label
297 // before adding its value in list
298 if( power->IsSymbolLikePowerLocalLabel() )
299 {
300 const SCH_FIELD* valueField = power->GetField( FIELD_T::VALUE );
301 existingLabels.insert( UnescapeString( valueField->GetText() ) );
302 }
303 }
304 }
305 }
306
307 // Add bus aliases to label list
308 for( const std::shared_ptr<BUS_ALIAS>& busAlias : m_Parent->Schematic().GetAllBusAliases() )
309 existingLabels.insert( wxT( "{" ) + busAlias->GetName() + wxT( "}" ) );
310
311 for( const wxString& label : existingLabels )
312 m_existingLabelArray.push_back( label );
313
314 m_valueCombo->SetStringList( m_existingLabelArray );
315 m_valueCombo->SetSelectedString( text );
316 }
317 else if( m_activeTextEntry )
318 {
319 m_activeTextEntry->SetValue( text );
320 }
321
322 // Push a copy of each field into m_updateFields
323 for( SCH_FIELD& field : m_currentLabel->GetFields() )
324 {
325 SCH_FIELD field_copy( field );
326
327 // change offset to be symbol-relative
328 field_copy.Offset( -m_currentLabel->GetPosition() );
329
330 m_fields->push_back( field_copy );
331 }
332
333 // notify the grid
334 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, (int) m_fields->size() );
335 m_grid->ProcessTableMessage( msg );
336
337 if( m_shapeSizer->AreAnyItemsShown() )
338 {
339 switch( m_currentLabel->GetShape() )
340 {
341 case LABEL_FLAG_SHAPE::L_INPUT: m_input->SetValue( true ); break;
342 case LABEL_FLAG_SHAPE::L_OUTPUT: m_output->SetValue( true ); break;
343 case LABEL_FLAG_SHAPE::L_BIDI: m_bidirectional->SetValue( true ); break;
344 case LABEL_FLAG_SHAPE::L_TRISTATE: m_triState->SetValue( true ); break;
345 case LABEL_FLAG_SHAPE::L_UNSPECIFIED: m_passive->SetValue( true ); break;
346 case LABEL_FLAG_SHAPE::F_DOT: m_dot->SetValue( true ); break;
347 case LABEL_FLAG_SHAPE::F_ROUND: m_circle->SetValue( true ); break;
348 case LABEL_FLAG_SHAPE::F_DIAMOND: m_diamond->SetValue( true ); break;
349 case LABEL_FLAG_SHAPE::F_RECTANGLE: m_rectangle->SetValue( true ); break;
350 }
351 }
352
353 m_fontCtrl->SetFontSelection( m_currentLabel->GetFont() );
354
356 m_textSize.SetValue( static_cast<SCH_DIRECTIVE_LABEL*>( m_currentLabel )->GetPinLength() );
357 else
358 m_textSize.SetValue( m_currentLabel->GetTextWidth() );
359
360 m_bold->Check( m_currentLabel->IsBold() );
361 m_italic->Check( m_currentLabel->IsItalic() );
362 m_textColorSwatch->SetSwatchColor( m_currentLabel->GetTextColor(), false );
363
364 switch( m_currentLabel->GetSpinStyle() )
365 {
366 case SPIN_STYLE::RIGHT: m_spin0->Check( true ); break;
367 case SPIN_STYLE::LEFT: m_spin1->Check( true ); break;
368 case SPIN_STYLE::UP: m_spin2->Check( true ); break;
369 case SPIN_STYLE::BOTTOM: m_spin3->Check( true ); break;
370 }
371
372 if( m_currentLabel->AutoRotateOnPlacementSupported() )
373 m_autoRotate->SetValue( m_currentLabel->AutoRotateOnPlacement() );
374
375 // Recalculate layout after grid population
376 m_grid->Layout();
377 Layout();
378 GetSizer()->SetSizeHints( this );
379
380 wxSize minSize = GetMinSize();
381 wxSize curSize = GetSize();
382
383 if( curSize.x < minSize.x || curSize.y < minSize.y )
384 SetSize( wxSize( std::max( curSize.x, minSize.x ), std::max( curSize.y, minSize.y ) ) );
385
386 SendSizeEvent( wxSEND_EVENT_POST );
387
388 return true;
389}
390
391
392void DIALOG_LABEL_PROPERTIES::OnEnterKey( wxCommandEvent& aEvent )
393{
394 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
395}
396
397
399{
400 // If the key is WXK_RETURN/WXK_NUMPAD_ENTER because m_valueCombo is the source event, we do not skip
401 // the key event because the default action is to show the m_valueCombo dropdown list,
402 // and we only want to accept the entered string.
403 if(( aEvent.GetKeyCode() == WXK_RETURN ) || (aEvent.GetKeyCode() == WXK_NUMPAD_ENTER))
404 {
405 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
406 }
407 else if( aEvent.GetKeyCode() == WXK_SPACE )
408 {
409 // our FILTER_COMBOBOX uses the space as special command, not wanted here,
410 // to open the dropdown list of labels.
411 // So just add the space char to the current text, at current insertion point,
412 // and do not skip this event
413 m_activeTextEntry->WriteText( wxT(" " ) );
414 }
415 else
416 {
417 aEvent.Skip();
418 }
419}
420
421
423{
424 if( aEvent.GetKeyCode() == WXK_TAB )
425 {
426 if( aEvent.ShiftDown() )
427 {
428 m_textSizeCtrl->SetFocusFromKbd();
429 }
430 else if( !m_fields->empty() )
431 {
432 m_grid->SetFocusFromKbd();
433 m_grid->MakeCellVisible( 0, 0 );
434 m_grid->SetGridCursor( 0, 0 );
435 }
436 else
437 {
438 m_textSizeCtrl->SetFocusFromKbd();
439 }
440 }
441 else
442 {
443 aEvent.Skip();
444 }
445}
446
447
448static bool positioningChanged( const SCH_FIELD& a, const SCH_FIELD& b )
449{
450 return a.GetPosition() != b.GetPosition()
451 || a.GetHorizJustify() != b.GetHorizJustify()
452 || a.GetVertJustify() != b.GetVertJustify()
453 || a.GetTextAngle() != b.GetTextAngle();
454}
455
456
457static bool positioningChanged( FIELDS_GRID_TABLE* a, std::vector<SCH_FIELD>& b )
458{
459 for( size_t i = 0; i < a->size() && i < b.size(); ++i )
460 {
461 if( positioningChanged( a->at( i ), b.at( i ) ) )
462 return true;
463 }
464
465 return false;
466}
467
468
470{
471 if( !m_grid->CommitPendingChanges() )
472 return false;
473
474 if( !wxDialog::TransferDataFromWindow() )
475 return false;
476
477 // Don't allow text to disappear; it can be difficult to correct if you can't select it
478 if( !m_textSize.Validate( 0.01, 1000.0, EDA_UNITS::MM ) )
479 return false;
480
481 SCH_COMMIT commit( m_Parent );
482 wxString text;
483
484 /* save old text in undo list if not already in edit */
485 if( m_currentLabel->GetEditFlags() == 0 )
486 commit.Modify( m_currentLabel, m_Parent->GetScreen() );
487
488 m_Parent->GetCanvas()->Refresh();
489
491 {
492 // labels need escaping
494
495 // convert any text variable cross-references to their UUIDs
496 text = m_currentLabel->Schematic()->ConvertRefsToKIIDs( text );
497
498#ifdef __WXMAC__
499 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting
500 text.Replace( wxS( "\r" ), wxS( "\n" ) );
501#endif
502
503 wxString visibleText = text;
504 visibleText.Trim( false ).Trim( true );
505
506 if( visibleText.IsEmpty() )
507 {
508 DisplayError( this, _( "Label can not be empty." ) );
509 return false;
510 }
511
512 m_currentLabel->SetText( text );
513 }
514
515 // change all field positions from relative to absolute
516 for( SCH_FIELD& field : *m_fields )
517 {
518 field.Offset( m_currentLabel->GetPosition() );
519
520 if( field.GetCanonicalName() == wxT( "Netclass" ) )
521 {
522 field.SetLayer( LAYER_NETCLASS_REFS );
523 }
524 else if( field.GetId() == FIELD_T::INTERSHEET_REFS )
525 {
526 if( field.IsVisible() != m_Parent->Schematic().Settings().m_IntersheetRefsShow )
527 {
528 DisplayInfoMessage( this, _( "Intersheet reference visibility is controlled globally from "
529 "Schematic Setup > General > Formatting" ) );
530 }
531
532 field.SetLayer( LAYER_INTERSHEET_REFS );
533 }
534 else
535 {
536 field.SetLayer( LAYER_FIELDS );
537 }
538 }
539
540 if( positioningChanged( m_fields, m_currentLabel->GetFields() ) )
541 m_currentLabel->SetFieldsAutoplaced( AUTOPLACE_NONE );
542
543 for( int ii = m_fields->GetNumberRows() - 1; ii >= 0; ii-- )
544 {
545 SCH_FIELD& field = m_fields->at( ii );
546 const wxString& fieldName = field.GetCanonicalName();
547 const wxString& fieldText = field.GetText();
548
549 if( fieldName.IsEmpty() && fieldText.IsEmpty() )
550 {
551 // delete empty, unnamed fields
552 m_fields->erase( m_fields->begin() + ii );
553 }
554 else if( fieldName == wxT( "Netclass" ) && fieldText.IsEmpty() )
555 {
556 // delete empty Netclass fields if there are other Netclass fields present
557 int netclassFieldCount = 0;
558
559 for( int jj = 0; jj < m_fields->GetNumberRows(); ++jj )
560 {
561 if( m_fields->at( jj ).GetCanonicalName() == wxT( "Netclass" ) )
562 netclassFieldCount++;
563 }
564
565 if( netclassFieldCount > 1 )
566 m_fields->erase( m_fields->begin() + ii );
567 }
568 else if( fieldName.IsEmpty() )
569 {
570 // give non-empty, unnamed fields a name
571 field.SetName( _( "untitled" ) );
572 }
573 }
574
575 int ordinal = 42; // Arbitrarily larger than any mandatory FIELD_T ids.
576
577 for( SCH_FIELD& field : *m_fields )
578 {
579 if( !field.IsMandatory() )
580 field.SetOrdinal( ordinal++ );
581 }
582
583 m_currentLabel->SetFields( *m_fields );
584
585 if( m_shapeSizer->AreAnyItemsShown() )
586 {
587 if( m_bidirectional->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::L_BIDI );
588 else if( m_input->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::L_INPUT );
589 else if( m_output->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::L_OUTPUT );
590 else if( m_triState->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::L_TRISTATE );
591 else if( m_passive->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::L_UNSPECIFIED );
592 else if( m_dot->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::F_DOT );
593 else if( m_circle->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::F_ROUND );
594 else if( m_diamond->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::F_DIAMOND );
595 else if( m_rectangle->GetValue() ) m_currentLabel->SetShape( LABEL_FLAG_SHAPE::F_RECTANGLE );
596 }
597
598 if( m_fontCtrl->HaveFontSelection() )
599 m_currentLabel->SetFont( m_fontCtrl->GetFontSelection( m_bold->IsChecked(), m_italic->IsChecked() ) );
600
602 static_cast<SCH_DIRECTIVE_LABEL*>( m_currentLabel )->SetPinLength( m_textSize.GetIntValue() );
603 else if( m_currentLabel->GetTextWidth() != m_textSize.GetIntValue() )
604 m_currentLabel->SetTextSize( VECTOR2I( m_textSize.GetIntValue(), m_textSize.GetIntValue() ) );
605
606 // Must come after SetTextSize()
607 m_currentLabel->SetBold( m_bold->IsChecked() );
608 m_currentLabel->SetItalic( m_italic->IsChecked() );
609
610 m_currentLabel->SetTextColor( m_textColorSwatch->GetSwatchColor() );
611
612 SPIN_STYLE selectedSpinStyle= SPIN_STYLE::LEFT;
613
614 if( m_spin0->IsChecked() ) selectedSpinStyle = SPIN_STYLE::RIGHT;
615 else if( m_spin1->IsChecked() ) selectedSpinStyle = SPIN_STYLE::LEFT;
616 else if( m_spin2->IsChecked() ) selectedSpinStyle = SPIN_STYLE::UP;
617 else if( m_spin3->IsChecked() ) selectedSpinStyle = SPIN_STYLE::BOTTOM;
618
619 if( m_currentLabel->AutoRotateOnPlacementSupported() )
620 {
621 SCH_EDIT_FRAME* frame = static_cast<SCH_EDIT_FRAME*>( m_parentFrame );
622 m_currentLabel->SetAutoRotateOnPlacement( m_autoRotate->IsChecked() );
623 frame->AutoRotateItem( frame->GetScreen(), m_currentLabel );
624 }
625 else
626 {
627 m_currentLabel->SetAutoRotateOnPlacement( false );
628 }
629
630 if( !m_currentLabel->AutoRotateOnPlacement() && m_currentLabel->GetSpinStyle() != selectedSpinStyle )
631 m_currentLabel->SetSpinStyle( selectedSpinStyle );
632
633 AUTOPLACE_ALGO fieldsAutoplaced = m_currentLabel->GetFieldsAutoplaced();
634
635 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
636 m_currentLabel->AutoplaceFields( m_Parent->GetScreen(), fieldsAutoplaced );
637
638 if( !commit.Empty() )
639 {
640 commit.Push( _( "Edit Label Properties" ) );
641 }
642 else if( m_activeTextEntry && m_labelList )
643 {
644 text = m_activeTextEntry->GetValue();
645 // On macOS CTRL+Enter produces '\r' instead of '\n' regardless of EOL setting
646 text.Replace( wxS( "\r" ), wxS( "\n" ) );
647 wxArrayString lines = wxSplit( text, '\n' );
648
649 for( const wxString& line : lines )
650 {
651 text = EscapeString( line, CTX_NETNAME );
652 text.Trim( false ).Trim( true );
653
654 if( text.empty() )
655 continue;
656
657 // convert any text variable cross-references to their UUIDs
658 text = m_currentLabel->Schematic()->ConvertRefsToKIIDs( text );
659
660 switch ( m_currentLabel->Type() )
661 {
663 {
664 SCH_GLOBALLABEL* label = new SCH_GLOBALLABEL( *static_cast<SCH_GLOBALLABEL*>( m_currentLabel ) );
665 const_cast<KIID&>( label->m_Uuid ) = KIID(); // Gives a new UUID to the copy
666 label->SetText( text );
667 m_labelList->push_back( std::unique_ptr<SCH_LABEL_BASE>( label ) );
668 break;
669 }
670 case SCH_HIER_LABEL_T:
671 {
672 SCH_HIERLABEL* label = new SCH_HIERLABEL( *static_cast<SCH_HIERLABEL*>( m_currentLabel ) );
673 const_cast<KIID&>( label->m_Uuid ) = KIID();
674 label->SetText( text );
675 m_labelList->push_back( std::unique_ptr<SCH_LABEL_BASE>( label ) );
676 break;
677 }
678 case SCH_LABEL_T:
679 {
680 SCH_LABEL* label = new SCH_LABEL( *static_cast<SCH_LABEL*>( m_currentLabel ) );
681 const_cast<KIID&>( label->m_Uuid ) = KIID();
682 label->SetText( text );
683 m_labelList->push_back( std::unique_ptr<SCH_LABEL_BASE>( label ) );
684 break;
685 }
686 default:
687 break;
688 }
689 }
690 }
691 else if( m_labelList && m_currentLabel->Type() == SCH_DIRECTIVE_LABEL_T )
692 {
694 m_labelList->push_back( std::unique_ptr<SCH_LABEL_BASE>( label ) );
695 }
696
697 return true;
698}
699
700
701void DIALOG_LABEL_PROPERTIES::onSpinButton( wxCommandEvent& aEvent )
702{
703 for( BITMAP_BUTTON* btn : { m_spin0, m_spin1, m_spin2, m_spin3 } )
704 {
705 if( btn->IsChecked() && btn != aEvent.GetEventObject() )
706 btn->Check( false );
707 }
708}
709
710
711void DIALOG_LABEL_PROPERTIES::OnFormattingHelp( wxHyperlinkEvent& aEvent )
712{
714}
715
716
718{
719 wxGridCellEditor* editor = m_grid->GetCellEditor( event.GetRow(), event.GetCol() );
720 wxControl* control = editor->GetControl();
721
722 if( control && control->GetValidator() && !control->GetValidator()->Validate( control ) )
723 {
724 event.Veto();
725 m_delayedFocusRow = event.GetRow();
726 m_delayedFocusColumn = event.GetCol();
727 }
728 else if( event.GetCol() == FDC_NAME )
729 {
730 wxString newName = event.GetString();
731 bool isGlobalLabel = m_currentLabel->Type() == SCH_GLOBAL_LABEL_T;
732
733 for( int i = 0; i < m_grid->GetNumberRows(); ++i )
734 {
735 if( i == event.GetRow() )
736 continue;
737
738 wxString existing = m_grid->GetCellValue( i, FDC_NAME );
739
740 // Only global labels have a mandatory field (Intersheet References). Hierarchical,
741 // regular, and directive labels carry only user fields, which compare case-sensitively.
742 bool duplicate = isGlobalLabel
744 : FieldNamesAreDuplicates( newName, existing, {} );
745
746 if( duplicate )
747 {
748 DisplayError( this, wxString::Format( _( "Field name '%s' already in use." ),
749 newName ) );
750 event.Veto();
751 m_delayedFocusRow = event.GetRow();
752 m_delayedFocusColumn = event.GetCol();
753 break;
754 }
755 }
756 }
757
758 editor->DecRef();
759}
760
761
762void DIALOG_LABEL_PROPERTIES::OnAddField( wxCommandEvent& event )
763{
764 m_grid->OnAddRow(
765 [&]() -> std::pair<int, int>
766 {
767 wxString fieldName = wxT( "Netclass" );
768
769 for( SCH_FIELD& field : *m_fields )
770 {
771 if( field.GetId() != FIELD_T::INTERSHEET_REFS && field.GetName() != wxT( "Netclass" ) )
772 {
773 fieldName = wxEmptyString;
774 break;
775 }
776 }
777
778 fieldName = SCH_LABEL_BASE::GetDefaultFieldName( fieldName, true );
779
780 SCH_FIELD newField( m_currentLabel, FIELD_T::USER, fieldName );
781
782 if( m_fields->size() > 0 )
783 {
784 // SetAttributes() also covers text angle, size, italic and bold
785 newField.SetAttributes( m_fields->at( m_fields->size() - 1 ) );
786 newField.SetVisible( m_fields->at( m_fields->size() - 1 ).IsVisible() );
787 }
788 else
789 {
790 newField.SetVisible( true );
791 newField.SetItalic( true );
792 }
793
794 m_fields->push_back( newField );
795
796 // notify the grid
797 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
798 m_grid->ProcessTableMessage( msg );
799 return { m_fields->size() - 1, FDC_NAME };
800 } );
801}
802
803
804void DIALOG_LABEL_PROPERTIES::OnDeleteField( wxCommandEvent& event )
805{
806 m_grid->OnDeleteRows(
807 [&]( int row )
808 {
809 if( row < m_currentLabel->GetMandatoryFieldCount() )
810 {
811 DisplayError( this, _( "The first field is mandatory." ) );
812 return false;
813 }
814
815 return true;
816 },
817 [&]( int row )
818 {
819 m_fields->erase( m_fields->begin() + row );
820
821 // notify the grid
822 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
823 m_grid->ProcessTableMessage( msg );
824 } );
825}
826
827
828void DIALOG_LABEL_PROPERTIES::OnMoveUp( wxCommandEvent& event )
829{
830 m_grid->OnMoveRowUp(
831 [&]( int row )
832 {
833 return row > m_currentLabel->GetMandatoryFieldCount();
834 },
835 [&]( int row )
836 {
837 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row - 1 ) );
838 m_grid->ForceRefresh();
839 } );
840}
841
842
843void DIALOG_LABEL_PROPERTIES::OnMoveDown( wxCommandEvent& event )
844{
845 m_grid->OnMoveRowUp(
846 [&]( int row )
847 {
848 return row >= m_currentLabel->GetMandatoryFieldCount();
849 },
850 [&]( int row )
851 {
852 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row + 1 ) );
853 m_grid->ForceRefresh();
854 } );
855}
856
857
858void DIALOG_LABEL_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
859{
860 std::bitset<64> shownColumns = m_grid->GetShownColumns();
861
862 if( shownColumns != m_shownColumns )
863 {
864 m_shownColumns = shownColumns;
865
866 if( !m_grid->IsCellEditControlShown() )
867 m_grid->SetGridWidthsDirty();
868 }
869
870 // Handle a delayed focus
871 if( m_delayedFocusRow >= 0 && m_delayedFocusRow < m_grid->GetNumberRows() )
872 {
873 m_grid->SetFocus();
874 m_grid->MakeCellVisible( m_delayedFocusRow, m_delayedFocusColumn );
876 }
877
880}
881
882
884{
885 bool multiLine = m_multilineAllowed && m_cbMultiLine->IsChecked();
886
887 if( m_currentLabel->Type() == SCH_GLOBAL_LABEL_T || m_currentLabel->Type() == SCH_LABEL_T )
888 {
889 m_labelCombo->Show( !multiLine );
890 m_valueCombo->Show( !multiLine );
891 m_labelMultiLine->Show( multiLine );
892 m_valueMultiLine->Show( multiLine );
893
894 if( multiLine )
895 {
896 m_valueMultiLine->SetValue( m_valueCombo->GetValue() );
899 }
900 else
901 {
902 wxString multiText = m_valueMultiLine->GetValue();
903 m_valueCombo->SetValue( multiText.BeforeFirst( '\n' ) );
905 SetInitialFocus( m_valueCombo->GetTextCtrl() );
906 }
907 }
908 else if( m_currentLabel->Type() == SCH_HIER_LABEL_T )
909 {
910 m_labelSingleLine->Show( !multiLine );
911 m_valueSingleLine->Show( !multiLine );
912 m_labelMultiLine->Show( multiLine );
913 m_valueMultiLine->Show( multiLine );
914
915 if( multiLine )
916 {
917 m_valueMultiLine->SetValue( m_valueSingleLine->GetValue() );
920 }
921 else
922 {
923 wxString multiText = m_valueMultiLine->GetValue();
924 m_valueSingleLine->SetValue( multiText.BeforeFirst( '\n' ) );
927 }
928 }
929
930 Layout();
931}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
@ text_align_right
@ label_align_bottom
@ text_align_left
@ label_align_right
@ label_align_top
@ pinorient_right
@ label_align_left
@ text_align_bottom
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
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...
COLOR4D GetColor(int aLayer) const
bool Empty() const
Definition commit.h:134
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
DIALOG_LABEL_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Label Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
std::list< std::unique_ptr< SCH_LABEL_BASE > > * m_labelList
void OnMoveUp(wxCommandEvent &event) override
void OnMoveDown(wxCommandEvent &event) override
DIALOG_LABEL_PROPERTIES(SCH_EDIT_FRAME *parent, SCH_LABEL_BASE *aLabel, bool aNew)
void OnEnterKey(wxCommandEvent &aEvent) override
wxEVT_COMMAND_ENTER event handler for single-line control.
void OnAddField(wxCommandEvent &event) override
SCH_NETNAME_VALIDATOR m_netNameValidator
void OnFormattingHelp(wxHyperlinkEvent &aEvent) override
void OnCBValueCharHook(wxKeyEvent &aEvent) override
void OnUpdateUI(wxUpdateUIEvent &event) override
void onSpinButton(wxCommandEvent &aEvent)
void onMultiLabelCheck(wxCommandEvent &aEvent) override
void OnDeleteField(wxCommandEvent &event) override
void OnGridCellChanging(wxGridEvent &event)
void OnValueCharHook(wxKeyEvent &aEvent) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:94
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
EDA_BASE_FRAME * m_parentFrame
const KIID m_Uuid
Definition eda_item.h:531
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:110
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:428
virtual void Offset(const VECTOR2I &aOffset)
Definition eda_text.cpp:595
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:221
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:381
virtual EDA_ANGLE GetTextAngle() const
Definition eda_text.h:168
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:224
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:265
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition eda_text.cpp:302
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:46
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void AutoRotateItem(SCH_SCREEN *aScreen, SCH_ITEM *aItem)
Automatically set the rotation of an item (if the item supports it).
VECTOR2I GetPosition() const override
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:128
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
void SetName(const wxString &aName)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
static const wxString GetDefaultFieldName(const wxString &aName, bool aUseDefaultName)
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:746
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
Schematic symbol object.
Definition sch_symbol.h:69
bool IsSymbolLikePowerGlobalLabel() const
bool IsSymbolLikePowerLocalLabel() const
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
static HTML_MESSAGE_BOX * ShowSyntaxHelp(wxWindow *aParentWindow)
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:245
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
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
@ LAYER_FIELDS
Definition layer_ids.h:468
@ LAYER_NETCLASS_REFS
Definition layer_ids.h:470
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:494
@ LAYER_INTERSHEET_REFS
Definition layer_ids.h:469
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
AUTOPLACE_ALGO
Definition sch_item.h:65
@ AUTOPLACE_MANUAL
Definition sch_item.h:68
@ AUTOPLACE_NONE
Definition sch_item.h:66
@ AUTOPLACE_AUTO
Definition sch_item.h:67
@ L_BIDI
Definition sch_label.h:100
@ L_TRISTATE
Definition sch_label.h:101
@ L_UNSPECIFIED
Definition sch_label.h:102
@ F_DOT
Definition sch_label.h:105
@ F_ROUND
Definition sch_label.h:106
@ L_OUTPUT
Definition sch_label.h:99
@ F_DIAMOND
Definition sch_label.h:107
@ L_INPUT
Definition sch_label.h:98
@ F_RECTANGLE
Definition sch_label.h:108
std::vector< FAB_LAYER_COLOR > dummy
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
@ CTX_NETNAME
bool FieldNamesAreDuplicates(const wxString &aLhs, const wxString &aRhs, std::initializer_list< FIELD_T > aMandatoryFields)
Test whether two field names should be treated as duplicates for the purposes of field name uniquenes...
@ USER
The field ID hasn't been set yet; field is invalid.
@ INTERSHEET_REFS
Global label cross-reference page numbers.
@ VALUE
Field Value of part, i.e. "3.3K".
#define GLOBALLABEL_MANDATORY_FIELDS
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166
@ SCH_SHEET_PIN_T
Definition typeinfo.h:171
@ SCH_SYMBOL_LOCATE_POWER_T
Definition typeinfo.h:193
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683