KiCad PCB EDA Suite
dialog_edit_one_field.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) 2012 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
6  * Copyright (C) 2004-2018 KiCad Developers, see change_log.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, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <common.h>
28 #include <kiway.h>
29 #include <confirm.h>
30 #include <kicad_string.h>
31 #include <sch_base_frame.h>
32 #include <sch_component.h>
33 #include <class_libentry.h>
34 #include <lib_field.h>
35 #include <template_fieldnames.h>
36 #include <class_library.h>
37 #include <sch_validators.h>
38 
39 #include <dialog_edit_one_field.h>
40 #include <sch_text.h>
41 
43  const EDA_TEXT* aTextItem ) :
44  DIALOG_LIB_EDIT_TEXT_BASE( aParent ),
45  m_posX( aParent, m_xPosLabel, m_xPosCtrl, m_xPosUnits, true ),
46  m_posY( aParent, m_yPosLabel, m_yPosCtrl, m_yPosUnits, true ),
47  m_textSize( aParent, m_textSizeLabel, m_textSizeCtrl, m_textSizeUnits, true )
48 {
49  SetTitle( aTitle );
50 
51  // The field ID and power status are Initialized in the derived object's ctor.
52  m_fieldId = VALUE;
53  m_isPower = false;
54 
55  m_text = aTextItem->GetText();
56  m_isItalic = aTextItem->IsItalic();
57  m_isBold = aTextItem->IsBold();
58  m_position = aTextItem->GetTextPos();
59  m_size = aTextItem->GetTextWidth();
60  m_isVertical = ( aTextItem->GetTextAngle() == TEXT_ANGLE_VERT );
61  m_verticalJustification = aTextItem->GetVertJustify() + 1;
62  m_horizontalJustification = aTextItem->GetHorizJustify() + 1;
63  m_isVisible = aTextItem->IsVisible();
64 }
65 
66 
68 {
70  SCH_BASE_FRAME* parent = GetParent();
71  bool libedit = parent->IsType( FRAME_SCH_LIB_EDITOR );
72  m_TextValue->SetValidator( SCH_FIELD_VALIDATOR( libedit, m_fieldId, &m_text ) );
73 
74  // Disable options for graphic text editing which are not needed for fields.
75  m_CommonConvert->Show( false );
76  m_CommonUnit->Show( false );
77 
78  // Show the footprint selection dialog if this is the footprint field.
80 
81  // Value fields of power components cannot be modified. This will grey out
82  // the text box and display an explanation.
83  if( m_fieldId == VALUE && m_isPower )
84  {
85  m_PowerComponentValues->Show( true );
86  m_TextValue->Enable( false );
87  }
88  else
89  {
90  m_PowerComponentValues->Show( false );
91  m_TextValue->Enable( true );
92  }
93 
94  m_sdbSizerButtonsOK->SetDefault();
95 
96  // Now all widgets have the size fixed, call FinishDialogSettings
98 }
99 
100 
102 {
103  // pick a footprint using the footprint picker.
104  wxString fpid = m_TextValue->GetValue();
105 
107 
108  if( frame->ShowModal( &fpid, this ) )
109  {
110  m_TextValue->SetValue( fpid );
111  }
112 
113  frame->Destroy();
114 }
115 
116 
117 void DIALOG_EDIT_ONE_FIELD::OnSetFocusText( wxFocusEvent& event )
118 {
119 #ifdef __WXGTK__
120  // Force an update of the text control before setting the text selection
121  // This is needed because GTK seems to ignore the selection on first update
122  //
123  // Note that we can't do this on OSX as it tends to provoke Apple's
124  // "[NSAlert runModal] may not be invoked inside of transaction begin/commit pair"
125  // bug. See: https://bugs.launchpad.net/kicad/+bug/1837225
126  m_TextValue->Update();
127 #endif
128 
129  if( m_fieldId == REFERENCE )
130  SelectReferenceNumber( static_cast<wxTextEntry*>( m_TextValue ) );
131  else
132  m_TextValue->SetSelection( -1, -1 );
133 
134  event.Skip();
135 }
136 
137 
139 {
140  m_TextValue->SetValue( m_text );
141 
145  m_orientChoice->SetSelection( m_isVertical ? 1 : 0 );
146  m_hAlignChoice->SetSelection( m_horizontalJustification );
147  m_vAlignChoice->SetSelection( m_verticalJustification );
148  m_visible->SetValue( m_isVisible );
149  m_italic->SetValue( m_isItalic );
150  m_bold->SetValue( m_isBold );
151 
152  return true;
153 }
154 
155 
157 {
158  m_text = m_TextValue->GetValue();
159 
160  if( m_fieldId == REFERENCE )
161  {
162  // Test if the reference string is valid:
164  {
165  DisplayError( this, _( "Illegal reference field value!" ) );
166  return false;
167  }
168  }
169  else if( m_fieldId == VALUE )
170  {
171  if( m_text.IsEmpty() )
172  {
173  DisplayError( this, _( "Value may not be empty." ) );
174  return false;
175  }
176  }
177 
178  m_isVertical = m_orientChoice->GetSelection() == 1;
179  m_position = wxPoint( m_posX.GetValue(), m_posY.GetValue() );
181  m_horizontalJustification = m_hAlignChoice->GetSelection();
182  m_verticalJustification = m_vAlignChoice->GetSelection();
183  m_isVisible = m_visible->GetValue();
184  m_isItalic = m_italic->GetValue();
185  m_isBold = m_bold->GetValue();
186 
187  return true;
188 }
189 
190 
192 {
193  aText->SetTextPos( m_position );
194  aText->SetTextSize( wxSize( m_size, m_size ) );
195  aText->SetVisible( m_isVisible );
197  aText->SetItalic( m_isItalic );
198  aText->SetBold( m_isBold );
201 }
202 
203 
205  const wxString& aTitle,
206  const LIB_FIELD* aField ) :
207  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
208 {
209  m_fieldId = aField->GetId();
210 
211  // When in the library editor, power components can be renamed.
212  m_isPower = false;
213  init();
214 }
215 
216 
218  const wxString& aTitle,
219  const SCH_FIELD* aField ) :
220  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
221 {
222  m_fieldId = aField->GetId();
223 
224  const SCH_COMPONENT* component = (SCH_COMPONENT*) aField->GetParent();
225 
226  wxASSERT_MSG( component && component->Type() == SCH_COMPONENT_T,
227  wxT( "Invalid schematic field parent item." ) );
228 
229  // The library symbol may have been removed so using SCH_COMPONENT::GetPartRef() here
230  // could result in a segfault. If the library symbol is no longer available, the
231  // schematic fields can still edit so set the power symbol flag to false. This may not
232  // be entirely accurate if the power library is missing but it's better then a segfault.
233  const LIB_PART* part = GetParent()->GetLibPart( component->GetLibId(), true );
234 
235  m_isPower = ( part ) ? part->IsPower() : false;
236 
237  init();
238 }
239 
240 
242 {
243  if( aField->GetId() == REFERENCE )
244  {
245  wxASSERT( aSheetPath );
246 
247  SCH_COMPONENT* component = dynamic_cast< SCH_COMPONENT* >( aField->GetParent() );
248 
249  wxASSERT( component );
250 
251  if( component )
252  component->SetRef( aSheetPath, m_text );
253  }
254 
255  bool positioningModified = false;
256 
257  if( aField->GetTextPos() != m_position )
258  positioningModified = true;
259 
260  if( ( aField->GetTextAngle() == TEXT_ANGLE_VERT ) != m_isVertical )
261  positioningModified = true;
262 
264  positioningModified = true;
265 
267  positioningModified = true;
268 
269  aField->SetText( m_text );
270  updateText( aField );
271 
272  if( positioningModified )
273  {
274  auto component = static_cast< SCH_COMPONENT* >( aField->GetParent() );
275  component->ClearFieldsAutoplaced();
276  }
277 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:150
#define TEXT_ANGLE_HORIZ
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
bool IsBold() const
Definition: eda_text.h:167
Class SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
Definition: kiway_player.h:59
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:185
#define TEXT_ANGLE_VERT
This file is part of the common library.
bool IsVisible() const
Definition: eda_text.h:170
void SetItalic(bool isItalic)
Definition: eda_text.h:163
Field object used in symbol libraries.
Definition: lib_field.h:59
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:231
void SetVisible(bool aVisible)
Definition: eda_text.h:169
double GetTextAngle() const
Definition: eda_text.h:158
int GetId() const
Definition: sch_field.h:108
void UpdateField(SCH_FIELD *aField, SCH_SHEET_PATH *aSheetPath)
LIB_PART * GetLibPart(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:222
int GetId() const
Definition: lib_field.h:135
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:118
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
DIALOG_EDIT_ONE_FIELD(SCH_BASE_FRAME *aParent, const wxString &aTitle, const EDA_TEXT *aTextItem)
DIALOG_SCH_EDIT_ONE_FIELD(SCH_BASE_FRAME *aParent, const wxString &aTitle, const SCH_FIELD *aField)
bool IsItalic() const
Definition: eda_text.h:164
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels,...
Definition: eda_text.h:109
DIALOG_LIB_EDIT_ONE_FIELD(SCH_BASE_FRAME *aParent, const wxString &aTitle, const LIB_FIELD *aField)
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:159
Class DIALOG_EDIT_ONE_FIELD is a base class to edit schematic and component library fields.
#define VALUE
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:184
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:73
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:126
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:321
Class LIB_PIN definition.
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:188
Definitions of control validators for schematic dialogs.
Define a library symbol object.
EDA_ITEM * GetParent() const
Definition: base_struct.h:220
Class SCH_SHEET_PATH.
#define _(s)
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:87
bool TransferDataToWindow() override
void updateText(EDA_TEXT *aText)
Class DIALOG_LIB_EDIT_TEXT_BASE.
SCH_BASE_FRAME * GetParent()
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
int GetTextWidth() const
Definition: eda_text.h:226
bool IsType(FRAME_T aType) const
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:187
void OnTextValueSelectButtonClick(wxCommandEvent &aEvent) override
Function OnTextValueSelectButtonClick Handles the select button next to the text value field.
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:73
The common library.
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
bool TransferDataFromWindow() override
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
const wxPoint & GetTextPos() const
Definition: eda_text.h:232
A text control validator used for validating the text allowed in library and schematic component fiel...
Definition for part library class.
static bool IsReferenceStringValid(const wxString &aReferenceString)
Tests for an acceptable reference string.
const LIB_ID & GetLibId() const
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME,...
void SetBold(bool aBold)
Definition: eda_text.h:166
Definitions of the SCH_TEXT class and derivatives for Eeschema.
virtual bool ShowModal(wxString *aResult=NULL, wxWindow *aResultantFocusWindow=NULL)
Function ShowModal puts up this wxFrame as if it were a modal dialog, with all other instantiated wxF...
virtual const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:124
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210
virtual void OnSetFocusText(wxFocusEvent &event) override
Used to select the variant part of some text fields (for instance, the question mark or number in a r...