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  wxASSERT( aTextItem );
50 
51  SetTitle( aTitle );
52 
53  // The field ID and power status are Initialized in the derived object's ctor.
54  m_fieldId = VALUE;
55  m_isPower = false;
56 
57  m_text = aTextItem->GetText();
58  m_isItalic = aTextItem->IsItalic();
59  m_isBold = aTextItem->IsBold();
60  m_position = aTextItem->GetTextPos();
61  m_size = aTextItem->GetTextWidth();
62  m_isVertical = ( aTextItem->GetTextAngle() == TEXT_ANGLE_VERT );
63  m_verticalJustification = aTextItem->GetVertJustify() + 1;
64  m_horizontalJustification = aTextItem->GetHorizJustify() + 1;
65  m_isVisible = aTextItem->IsVisible();
66 }
67 
68 
70 {
72  SCH_BASE_FRAME* parent = GetParent();
73  bool libedit = parent->IsType( FRAME_SCH_LIB_EDITOR );
74  m_TextValue->SetValidator( SCH_FIELD_VALIDATOR( libedit, m_fieldId, &m_text ) );
75 
76  // Disable options for graphic text editing which are not needed for fields.
77  m_CommonConvert->Show( false );
78  m_CommonUnit->Show( false );
79 
80  // Show the footprint selection dialog if this is the footprint field.
82 
83  // Value fields of power components cannot be modified. This will grey out
84  // the text box and display an explanation.
85  if( m_fieldId == VALUE && m_isPower )
86  {
87  m_PowerComponentValues->Show( true );
88  m_TextValue->Enable( false );
89  }
90  else
91  {
92  m_PowerComponentValues->Show( false );
93  m_TextValue->Enable( true );
94  }
95 
96  m_sdbSizerButtonsOK->SetDefault();
97 
98  // Now all widgets have the size fixed, call FinishDialogSettings
100 }
101 
102 
104 {
105  // pick a footprint using the footprint picker.
106  wxString fpid = m_TextValue->GetValue();
107 
109 
110  if( frame->ShowModal( &fpid, this ) )
111  {
112  m_TextValue->SetValue( fpid );
113  }
114 
115  frame->Destroy();
116 }
117 
118 
119 void DIALOG_EDIT_ONE_FIELD::OnSetFocusText( wxFocusEvent& event )
120 {
121 #ifdef __WXGTK__
122  // Force an update of the text control before setting the text selection
123  // This is needed because GTK seems to ignore the selection on first update
124  //
125  // Note that we can't do this on OSX as it tends to provoke Apple's
126  // "[NSAlert runModal] may not be invoked inside of transaction begin/commit pair"
127  // bug. See: https://bugs.launchpad.net/kicad/+bug/1837225
128  m_TextValue->Update();
129 #endif
130 
131  if( m_fieldId == REFERENCE )
132  SelectReferenceNumber( static_cast<wxTextEntry*>( m_TextValue ) );
133  else
134  m_TextValue->SetSelection( -1, -1 );
135 
136  event.Skip();
137 }
138 
139 
141 {
142  m_TextValue->SetValue( m_text );
143 
147  m_orientChoice->SetSelection( m_isVertical ? 1 : 0 );
148  m_hAlignChoice->SetSelection( m_horizontalJustification );
149  m_vAlignChoice->SetSelection( m_verticalJustification );
150  m_visible->SetValue( m_isVisible );
151  m_italic->SetValue( m_isItalic );
152  m_bold->SetValue( m_isBold );
153 
154  return true;
155 }
156 
157 
159 {
160  m_text = m_TextValue->GetValue();
161 
162  if( m_fieldId == REFERENCE )
163  {
164  // Test if the reference string is valid:
166  {
167  DisplayError( this, _( "Illegal reference designator value!" ) );
168  return false;
169  }
170  }
171  else if( m_fieldId == VALUE )
172  {
173  if( m_text.IsEmpty() )
174  {
175  DisplayError( this, _( "Value may not be empty." ) );
176  return false;
177  }
178  }
179 
180  m_isVertical = m_orientChoice->GetSelection() == 1;
183  m_horizontalJustification = m_hAlignChoice->GetSelection();
184  m_verticalJustification = m_vAlignChoice->GetSelection();
185  m_isVisible = m_visible->GetValue();
186  m_isItalic = m_italic->GetValue();
187  m_isBold = m_bold->GetValue();
188 
189  return true;
190 }
191 
192 
194 {
195  aText->SetTextPos( m_position );
196  aText->SetTextSize( wxSize( m_size, m_size ) );
197  aText->SetVisible( m_isVisible );
199  aText->SetItalic( m_isItalic );
200  aText->SetBold( m_isBold );
203 }
204 
205 
207  SCH_BASE_FRAME* aParent, const wxString& aTitle, const LIB_FIELD* aField )
208  : DIALOG_EDIT_ONE_FIELD( aParent, aTitle, aField )
209 {
210  m_fieldId = aField->GetId();
211 
212  // When in the library editor, power components can be renamed.
213  m_isPower = false;
214  init();
215 }
216 
217 
219  const wxString& aTitle,
220  const SCH_FIELD* aField ) :
221  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, aField )
222 {
223  m_fieldId = aField->GetId();
224  m_isPower = false;
225 
226  // The library symbol may have been removed so using SCH_COMPONENT::GetPartRef() here
227  // could result in a segfault. If the library symbol is no longer available, the
228  // schematic fields can still edit so set the power symbol flag to false. This may not
229  // be entirely accurate if the power library is missing but it's better then a segfault.
230  if( aField->GetParent() && aField->GetParent()->Type() == SCH_COMPONENT_T )
231  {
232  const SCH_COMPONENT* component = (SCH_COMPONENT*) aField->GetParent();
233  const LIB_PART* part = GetParent()->GetLibPart( component->GetLibId(), true );
234 
235  if( part && part->IsPower() )
236  m_isPower = true;
237  }
238 
239  init();
240 }
241 
242 
244 {
245  EDA_ITEM* parent = aField->GetParent();
246 
247  if( parent && parent->Type() == SCH_COMPONENT_T && aField->GetId() == REFERENCE )
248  {
249  wxASSERT( aSheetPath );
250 
251  static_cast<SCH_COMPONENT*>( parent )->SetRef( aSheetPath, m_text );
252  }
253 
254  bool positioningModified = false;
255 
256  if( aField->GetTextPos() != m_position )
257  positioningModified = true;
258 
259  if( ( aField->GetTextAngle() == TEXT_ANGLE_VERT ) != m_isVertical )
260  positioningModified = true;
261 
263  positioningModified = true;
264 
266  positioningModified = true;
267 
268  aField->SetText( m_text );
269  updateText( aField );
270 
271  if( positioningModified )
272  static_cast<SCH_ITEM*>( parent )->ClearFieldsAutoplaced();
273 }
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:239
bool IsBold() const
Definition: eda_text.h:167
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of ...
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:184
#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)
Load symbol from symbol library table.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:222
int GetId() const
Definition: lib_field.h:139
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
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
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
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:307
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:183
static EDA_TEXT_HJUSTIFY_T MapHorizJustify(int aHorizJustify)
Definition: eda_text.cpp:58
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:111
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:341
Class LIB_PIN definition.
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:187
Definitions of control validators for schematic dialogs.
Define a library symbol object.
EDA_ITEM * GetParent() const
Definition: base_struct.h:217
SCH_SHEET_PATH.
static EDA_TEXT_VJUSTIFY_T MapVertJustify(int aVertJustify)
Definition: eda_text.cpp:72
bool TransferDataToWindow() override
void updateText(EDA_TEXT *aText)
Class DIALOG_LIB_EDIT_TEXT_BASE.
SCH_BASE_FRAME * GetParent()
int GetTextWidth() const
Definition: eda_text.h:226
bool IsPower() const
bool IsType(FRAME_T aType) const
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:186
#define _(s)
Definition: 3d_actions.cpp:33
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...
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:99
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
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
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
Return the string associated with the text object.
Definition: eda_text.h:123
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
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...