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 <base_units.h>
34 #include <kiway.h>
35 #include <confirm.h>
36 
37 #include <general.h>
38 #include <sch_base_frame.h>
39 #include <sch_component.h>
40 #include <class_libentry.h>
41 #include <lib_field.h>
42 #include <sch_component.h>
43 #include <template_fieldnames.h>
44 #include <class_library.h>
45 #include <sch_validators.h>
46 
47 #include <dialog_edit_one_field.h>
48 
49 
50 // These should probably moved into some other file as helpers.
52 {
53  wxASSERT( aHorizJustify >= GR_TEXT_HJUSTIFY_LEFT && aHorizJustify <= GR_TEXT_HJUSTIFY_RIGHT );
54 
55  if( aHorizJustify > GR_TEXT_HJUSTIFY_RIGHT )
57 
58  if( aHorizJustify < GR_TEXT_HJUSTIFY_LEFT )
59  return GR_TEXT_HJUSTIFY_LEFT;
60 
61  return (EDA_TEXT_HJUSTIFY_T) aHorizJustify;
62 }
63 
64 
66 {
67  wxASSERT( aVertJustify >= GR_TEXT_VJUSTIFY_TOP && aVertJustify <= GR_TEXT_VJUSTIFY_BOTTOM );
68 
69  if( aVertJustify > GR_TEXT_VJUSTIFY_BOTTOM )
71 
72  if( aVertJustify < GR_TEXT_VJUSTIFY_TOP )
73  return GR_TEXT_VJUSTIFY_TOP;
74 
75  return (EDA_TEXT_VJUSTIFY_T) aVertJustify;
76 }
77 
78 
80  const EDA_TEXT* aTextItem ) :
81  DIALOG_LIB_EDIT_TEXT_BASE( aParent )
82 {
83  SetTitle( aTitle );
84 
85  // The field ID and power status are Initialized in the derived object's ctor.
86  m_fieldId = VALUE;
87  m_isPower = false;
88 
89  m_text = aTextItem->GetText();
90  m_style = aTextItem->IsItalic() ? 1 : 0;
91  m_style += aTextItem->IsBold() ? 2 : 0;
92  m_size = aTextItem->GetTextWidth();
93  m_orientation = ( aTextItem->GetTextAngle() == TEXT_ANGLE_VERT );
94  m_verticalJustification = aTextItem->GetVertJustify() + 1;
95  m_horizontalJustification = aTextItem->GetHorizJustify() + 1;
96  m_isVisible = aTextItem->IsVisible();
97 }
98 
99 
101 {
102  wxString msg;
103 
104  m_TextValue->SetFocus();
105  SCH_BASE_FRAME* parent = static_cast<SCH_BASE_FRAME*>( GetParent() );
106  m_TextValue->SetValidator( SCH_FIELD_VALIDATOR(
107  parent->IsType( FRAME_SCH_LIB_EDITOR ),
108  m_fieldId, &m_text ) );
109 
110  // Disable options for graphic text editing which are not needed for fields.
111  m_CommonConvert->Show( false );
112  m_CommonUnit->Show( false );
113 
114  // Show the footprint selection dialog if this is the footprint field.
115  if( m_fieldId == FOOTPRINT )
116  {
117  m_TextValueSelectButton->Show();
118  m_TextValueSelectButton->Enable();
119  }
120  else
121  {
122  m_TextValueSelectButton->Hide();
123  m_TextValueSelectButton->Disable();
124  }
125 
126  msg = m_TextSizeText->GetLabel() + ReturnUnitSymbol();
127  m_TextSizeText->SetLabel( msg );
128 
129 
130  // Value fields of power components cannot be modified. This will grey out
131  // the text box and display an explanation.
132  if( m_fieldId == VALUE && m_isPower )
133  {
134  m_PowerComponentValues->Show( true );
135  m_TextValue->Enable( false );
136  }
137  else
138  {
139  m_PowerComponentValues->Show( false );
140  m_TextValue->Enable( true );
141  }
142 
143  m_sdbSizerButtonsOK->SetDefault();
144 
145  // Now all widgets have the size fixed, call FinishDialogSettings
147 }
148 
149 
151 {
152  // pick a footprint using the footprint picker.
153  wxString fpid = m_TextValue->GetValue();
154 
156 
157  if( frame->ShowModal( &fpid, this ) )
158  {
159  m_TextValue->SetValue( fpid );
160  }
161 
162  frame->Destroy();
163 }
164 
165 
167 {
168  m_TextValue->SetValue( m_text );
169 
170  if( m_fieldId == REFERENCE )
171  {
172  if( m_text.find_first_of( '?' ) != m_text.npos )
173  {
174  m_TextValue->SetSelection( m_text.find_first_of( '?' ),
175  m_text.find_last_of( '?' ) + 1 );
176  }
177  else
178  {
179  wxString num = m_text;
180 
181  while( !num.IsEmpty() && ( !isdigit( num.Last() ) ||
182  !isdigit( num.GetChar( 0 ) ) ) )
183  {
184  if( !isdigit( num.Last() ) )
185  num.RemoveLast();
186  if( !isdigit( num.GetChar ( 0 ) ) )
187  num = num.Right( num.Length() - 1);
188  }
189 
190  m_TextValue->SetSelection( m_text.Find( num ),
191  m_text.Find( num ) + num.Length() );
192 
193  if( num.IsEmpty() )
194  m_TextValue->SetSelection( -1, -1 );
195  }
196  }
197  else
198  {
199  m_TextValue->SetSelection( -1, -1 );
200  }
201 
202  m_Orient->SetValue( m_orientation );
203  m_TextSize->SetValue( StringFromValue( g_UserUnit, m_size ) );
206  m_Invisible->SetValue( !m_isVisible );
207  m_TextShapeOpt->SetSelection( m_style );
208 
209  return true;
210 }
211 
212 
214 {
215  m_text = m_TextValue->GetValue();
216 
217  // There are lots of specific tests required to validate field text.
218  if( m_fieldId == REFERENCE )
219  {
220  // Test if the reference string is valid:
222  {
223  DisplayError( this, _( "Illegal reference field value!" ) );
224  return false;
225  }
226  }
227 
228  m_orientation = m_Orient->GetValue();
229  m_size = ValueFromString( g_UserUnit, m_TextSize->GetValue() );
232  m_isVisible = !m_Invisible->GetValue();
233  m_style = m_TextShapeOpt->GetSelection();
234 
235  return true;
236 }
237 
238 
240 {
241  aText->SetTextSize( wxSize( m_size, m_size ) );
242  aText->SetVisible( m_isVisible );
244  aText->SetItalic( (m_style & 1) != 0 );
245  aText->SetBold( (m_style & 2) != 0 );
248 }
249 
250 
252  const wxString& aTitle,
253  const LIB_FIELD* aField ) :
254  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
255 {
256  m_fieldId = aField->GetId();
257 
258  // When in the library editor, power components can be renamed.
259  m_isPower = false;
260  init();
261 }
262 
263 
265  const wxString& aTitle,
266  const SCH_FIELD* aField ) :
267  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
268 {
269  m_fieldId = aField->GetId();
270 
271  const SCH_COMPONENT* component = (SCH_COMPONENT*) aField->GetParent();
272 
273  wxASSERT_MSG( component != NULL && component->Type() == SCH_COMPONENT_T,
274  wxT( "Invalid schematic field parent item." ) );
275 
276  // The library symbol may have been removed so using SCH_COMPONENT::GetPartRef() here
277  // could result in a segfault. If the library symbol is no longer available, the
278  // schematic fields can still edit so set the power symbol flag to false. This may not
279  // be entirely accurate if the power library is missing but it's better then a segfault.
280  const LIB_PART* part = GetParent()->GetLibPart( component->GetLibId(), true );
281 
282  m_isPower = ( part ) ? part->IsPower() : false;
283 
284  init();
285 }
286 
287 
289 {
290  wxASSERT( aField != NULL || aField->Type() != SCH_FIELD_T );
291 
292  if( aField->GetId() == REFERENCE )
293  {
294  wxASSERT( aSheetPath != NULL );
295 
296  SCH_COMPONENT* component = dynamic_cast< SCH_COMPONENT* >( aField->GetParent() );
297 
298  wxASSERT( component != NULL );
299 
300  if( component != NULL )
301  component->SetRef( aSheetPath, m_text );
302  }
303 
304  bool modified = false;
305 
306  modified = ( modified ||
307  ( ( aField->GetTextAngle() == TEXT_ANGLE_VERT ) != m_orientation ) );
308 
309  modified = ( modified ||
310  ( ( aField->GetHorizJustify() !=
312 
313  modified = ( modified ||
314  ( ( aField->GetVertJustify() !=
316 
317  aField->SetText( m_text );
318  updateText( aField );
319 
320  if( modified )
321  {
322  auto component = static_cast< SCH_COMPONENT* >( aField->GetParent() );
323  component->ClearFieldsAutoplaced();
324  }
325 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:154
KICAD_T Type() const
Function Type()
Definition: base_struct.h:209
#define TEXT_ANGLE_HORIZ
Frequent text rotations, used with {Set,Get}TextAngle(), in 0.1 degrees for now, hoping to migrate to...
Definition: common.h:91
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:111
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
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
bool IsItalic() const
Definition: eda_text.h:168
Implementation of conversion functions that require both schematic and board internal units...
This file is part of the common library.
wxString ReturnUnitSymbol(EDA_UNITS_T aUnit, const wxString &formatString)
Returns the units symbol.
Definition: base_units.cpp:426
int GetId() const
Definition: sch_field.h:87
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:205
void SetItalic(bool isItalic)
Definition: eda_text.h:167
Field object used in symbol libraries.
Definition: lib_field.h:59
int GetTextWidth() const
Definition: eda_text.h:216
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetVisible(bool aVisible)
Definition: eda_text.h:173
EDA_ITEM * GetParent() const
Definition: base_struct.h:219
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:212
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:188
#define TEXT_ANGLE_VERT
Definition: common.h:92
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:162
bool IsBold() const
Definition: eda_text.h:171
void ClearFieldsAutoplaced()
Set fields automatically placed flag false.
DIALOG_SCH_EDIT_ONE_FIELD(SCH_BASE_FRAME *aParent, const wxString &aTitle, const SCH_FIELD *aField)
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:112
DIALOG_LIB_EDIT_ONE_FIELD(SCH_BASE_FRAME *aParent, const wxString &aTitle, const LIB_FIELD *aField)
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:128
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:192
Defintions of control validators for schematic dialogs.
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:370
Define a library symbol object.
Class SCH_SHEET_PATH.
virtual bool TransferDataToWindow() override
void updateText(EDA_TEXT *aText)
Class DIALOG_LIB_EDIT_TEXT_BASE.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
SCH_BASE_FRAME * GetParent()
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:189
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:191
bool IsVisible() const
Definition: eda_text.h:174
void OnTextValueSelectButtonClick(wxCommandEvent &aEvent) override
Function OnTextValueSelectButtonClick Handles the select button next to the text value field...
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
const LIB_ID & GetLibId() const
The common library.
virtual 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:170
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
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:139
EDA_TEXT_VJUSTIFY_T IntToEdaTextVertJustify(int aVertJustify)