KiCad PCB EDA Suite
dialog_edit_one_field.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com
10  * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
11  * Copyright (C) 2004-2018 KiCad Developers, see change_log.txt for contributors.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <fctsys.h>
32 #include <common.h>
33 #include <kiway.h>
34 #include <confirm.h>
35 #include <kicad_string.h>
36 #include <sch_base_frame.h>
37 #include <sch_component.h>
38 #include <class_libentry.h>
39 #include <lib_field.h>
40 #include <sch_component.h>
41 #include <template_fieldnames.h>
42 #include <class_library.h>
43 #include <sch_validators.h>
44 
45 #include <dialog_edit_one_field.h>
46 
47 
48 // These should probably moved into some other file as helpers.
50 {
51  wxASSERT( aHorizJustify >= GR_TEXT_HJUSTIFY_LEFT && aHorizJustify <= GR_TEXT_HJUSTIFY_RIGHT );
52 
53  if( aHorizJustify > GR_TEXT_HJUSTIFY_RIGHT )
55 
56  if( aHorizJustify < GR_TEXT_HJUSTIFY_LEFT )
57  return GR_TEXT_HJUSTIFY_LEFT;
58 
59  return (EDA_TEXT_HJUSTIFY_T) aHorizJustify;
60 }
61 
62 
64 {
65  wxASSERT( aVertJustify >= GR_TEXT_VJUSTIFY_TOP && aVertJustify <= GR_TEXT_VJUSTIFY_BOTTOM );
66 
67  if( aVertJustify > GR_TEXT_VJUSTIFY_BOTTOM )
69 
70  if( aVertJustify < GR_TEXT_VJUSTIFY_TOP )
71  return GR_TEXT_VJUSTIFY_TOP;
72 
73  return (EDA_TEXT_VJUSTIFY_T) aVertJustify;
74 }
75 
76 
78  const EDA_TEXT* aTextItem ) :
79  DIALOG_LIB_EDIT_TEXT_BASE( aParent ),
80  m_posX( aParent, m_xPosLabel, m_xPosCtrl, m_xPosUnits, true ),
81  m_posY( aParent, m_yPosLabel, m_yPosCtrl, m_yPosUnits, true ),
82  m_textSize( aParent, m_textSizeLabel, m_textSizeCtrl, m_textSizeUnits, true, 0 )
83 {
84  SetTitle( aTitle );
85 
86  // The field ID and power status are Initialized in the derived object's ctor.
87  m_fieldId = VALUE;
88  m_isPower = false;
89 
90  m_text = aTextItem->GetText();
91  m_isItalic = aTextItem->IsItalic();
92  m_isBold = aTextItem->IsBold();
93  m_position = aTextItem->GetTextPos();
94  m_size = aTextItem->GetTextWidth();
95  m_isVertical = ( aTextItem->GetTextAngle() == TEXT_ANGLE_VERT );
96  m_verticalJustification = aTextItem->GetVertJustify() + 1;
97  m_horizontalJustification = aTextItem->GetHorizJustify() + 1;
98  m_isVisible = aTextItem->IsVisible();
99 }
100 
101 
103 {
105  SCH_BASE_FRAME* parent = GetParent();
106  bool libedit = parent->IsType( FRAME_SCH_LIB_EDITOR );
107  m_TextValue->SetValidator( SCH_FIELD_VALIDATOR( libedit, m_fieldId, &m_text ) );
108 
109  // Disable options for graphic text editing which are not needed for fields.
110  m_CommonConvert->Show( false );
111  m_CommonUnit->Show( false );
112 
113  // Show the footprint selection dialog if this is the footprint field.
115 
116  // Value fields of power components cannot be modified. This will grey out
117  // the text box and display an explanation.
118  if( m_fieldId == VALUE && m_isPower )
119  {
120  m_PowerComponentValues->Show( true );
121  m_TextValue->Enable( false );
122  }
123  else
124  {
125  m_PowerComponentValues->Show( false );
126  m_TextValue->Enable( true );
127  }
128 
129  m_sdbSizerButtonsOK->SetDefault();
130 
131  // Now all widgets have the size fixed, call FinishDialogSettings
133 }
134 
135 
137 {
138  // pick a footprint using the footprint picker.
139  wxString fpid = m_TextValue->GetValue();
140 
142 
143  if( frame->ShowModal( &fpid, this ) )
144  {
145  m_TextValue->SetValue( fpid );
146  }
147 
148  frame->Destroy();
149 }
150 
151 
152 void DIALOG_EDIT_ONE_FIELD::OnSetFocusText( wxFocusEvent& event )
153 {
154  if( m_fieldId == REFERENCE )
155  SelectReferenceNumber( static_cast<wxTextEntry*>( m_TextValue ) );
156  else
157  m_TextValue->SetSelection( -1, -1 );
158 
159  event.Skip();
160 }
161 
162 
164 {
165  m_TextValue->SetValue( UnescapeString( m_text ) );
166 
170  m_orientChoice->SetSelection( m_isVertical ? 1 : 0 );
171  m_hAlignChoice->SetSelection( m_horizontalJustification );
172  m_vAlignChoice->SetSelection( m_verticalJustification );
173  m_visible->SetValue( m_isVisible );
174  m_italic->SetValue( m_isItalic );
175  m_bold->SetValue( m_isBold );
176 
177  return true;
178 }
179 
180 
182 {
183  m_text = EscapeString( m_TextValue->GetValue() );
184 
185  if( m_fieldId == REFERENCE )
186  {
187  // Test if the reference string is valid:
189  {
190  DisplayError( this, _( "Illegal reference field value!" ) );
191  return false;
192  }
193  }
194  else if( m_fieldId == VALUE )
195  {
196  if( m_text.IsEmpty() )
197  {
198  DisplayError( this, _( "Value may not be empty." ) );
199  return false;
200  }
201  }
202 
203  m_isVertical = m_orientChoice->GetSelection() == 1;
206  m_horizontalJustification = m_hAlignChoice->GetSelection();
207  m_verticalJustification = m_vAlignChoice->GetSelection();
208  m_isVisible = m_visible->GetValue();
209  m_isItalic = m_italic->GetValue();
210  m_isBold = m_bold->GetValue();
211 
212  return true;
213 }
214 
215 
217 {
218  aText->SetTextPos( m_position );
219  aText->SetTextSize( wxSize( m_size, m_size ) );
220  aText->SetVisible( m_isVisible );
222  aText->SetItalic( m_isItalic );
223  aText->SetBold( m_isBold );
226 }
227 
228 
230  const wxString& aTitle,
231  const LIB_FIELD* aField ) :
232  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
233 {
234  m_fieldId = aField->GetId();
235 
236  // When in the library editor, power components can be renamed.
237  m_isPower = false;
238  init();
239 }
240 
241 
243  const wxString& aTitle,
244  const SCH_FIELD* aField ) :
245  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
246 {
247  m_fieldId = aField->GetId();
248 
249  const SCH_COMPONENT* component = (SCH_COMPONENT*) aField->GetParent();
250 
251  wxASSERT_MSG( component && component->Type() == SCH_COMPONENT_T,
252  wxT( "Invalid schematic field parent item." ) );
253 
254  // The library symbol may have been removed so using SCH_COMPONENT::GetPartRef() here
255  // could result in a segfault. If the library symbol is no longer available, the
256  // schematic fields can still edit so set the power symbol flag to false. This may not
257  // be entirely accurate if the power library is missing but it's better then a segfault.
258  const LIB_PART* part = GetParent()->GetLibPart( component->GetLibId(), true );
259 
260  m_isPower = ( part ) ? part->IsPower() : false;
261 
262  init();
263 }
264 
265 
267 {
268  if( aField->GetId() == REFERENCE )
269  {
270  wxASSERT( aSheetPath );
271 
272  SCH_COMPONENT* component = dynamic_cast< SCH_COMPONENT* >( aField->GetParent() );
273 
274  wxASSERT( component );
275 
276  if( component )
277  component->SetRef( aSheetPath, m_text );
278  }
279 
280  bool positioningModified = false;
281 
282  if( aField->GetTextPos() != m_position )
283  positioningModified = true;
284 
285  if( ( aField->GetTextAngle() == TEXT_ANGLE_VERT ) != m_isVertical )
286  positioningModified = true;
287 
289  positioningModified = true;
290 
292  positioningModified = true;
293 
294  aField->SetText( m_text );
295  updateText( aField );
296 
297  if( positioningModified )
298  {
299  auto component = static_cast< SCH_COMPONENT* >( aField->GetParent() );
300  component->ClearFieldsAutoplaced();
301  }
302 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:169
#define TEXT_ANGLE_HORIZ
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
Class SCH_FIELD instances are attached to a component and provide a place for the component&#39;s value...
Definition: sch_field.h:56
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
Definition: kiway_player.h:120
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:62
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
const wxPoint & GetTextPos() const
Definition: eda_text.h:237
bool IsItalic() const
Definition: eda_text.h:183
#define TEXT_ANGLE_VERT
This file is part of the common library.
int GetId() const
Definition: sch_field.h:87
void SetItalic(bool isItalic)
Definition: eda_text.h:182
Field object used in symbol libraries.
Definition: lib_field.h:59
int GetTextWidth() const
Definition: eda_text.h:231
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:236
void SetVisible(bool aVisible)
Definition: eda_text.h:188
EDA_ITEM * GetParent() const
Definition: base_struct.h:211
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:227
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:203
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 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)
double GetTextAngle() const
Definition: eda_text.h:177
bool IsBold() const
Definition: eda_text.h:186
void ClearFieldsAutoplaced()
Set fields automatically placed flag false.
DIALOG_SCH_EDIT_ONE_FIELD(SCH_BASE_FRAME *aParent, const wxString &aTitle, const SCH_FIELD *aField)
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:127
wxString EscapeString(const wxString &aSource)
These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which ar...
Definition: string.cpp:49
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:155
Class DIALOG_EDIT_ONE_FIELD is a base class to edit schematic and component library fields...
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:300
int GetId() const
Definition: lib_field.h:138
Class LIB_ITEM definition.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:207
Defintions of control validators for schematic dialogs.
Define a library symbol object.
Class SCH_SHEET_PATH.
bool TransferDataToWindow() override
void updateText(EDA_TEXT *aText)
Class DIALOG_LIB_EDIT_TEXT_BASE.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:69
SCH_BASE_FRAME * GetParent()
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:204
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
Definition the SCH_COMPONENT class for Eeschema.
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:206
bool IsVisible() const
Definition: eda_text.h:189
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:93
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:69
const LIB_ID & GetLibId() const
The common library.
bool TransferDataFromWindow() override
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
bool IsType(FRAME_T aType) const
class SCH_FILED_VALIDATOR
Definition for part library class.
static bool IsReferenceStringValid(const wxString &aReferenceString)
Tests for an acceptable reference string.
EDA_TEXT_HJUSTIFY_T IntToEdaTextHorizJustify(int aHorizJustify)
#define VALUE
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME, and SCH_EDIT_FRAME, and it brings in a common way of handling the provided virtual functions for the derived classes.
void SetBold(bool aBold)
Definition: eda_text.h:185
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
VTBL_ENTRY 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 void SetText(const wxString &aText)
Definition: eda_text.h:154
EDA_TEXT_VJUSTIFY_T IntToEdaTextVertJustify(int aVertJustify)
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...