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-2017 KiCad Developers, see AUTHORS.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;
154 
156 
157  if( frame->ShowModal( &fpid, this ) )
158  {
159  m_TextValue->SetValue( fpid );
160  }
161 
162  frame->Destroy();
163 }
164 
165 
167 {
168  wxLogDebug( "In DIALOG_EDIT_ONE_FIELD::TransferDataToWindow()" );
169 
170  m_TextValue->SetValue( m_text );
171 
172  if( m_fieldId == REFERENCE )
173  {
174  if( m_text.find_first_of( '?' ) != m_text.npos )
175  {
176  m_TextValue->SetSelection( m_text.find_first_of( '?' ),
177  m_text.find_last_of( '?' ) + 1 );
178  }
179  else
180  {
181  wxString num = m_text;
182 
183  while( !num.IsEmpty() && ( !isdigit( num.Last() ) ||
184  !isdigit( num.GetChar( 0 ) ) ) )
185  {
186  if( !isdigit( num.Last() ) )
187  num.RemoveLast();
188  if( !isdigit( num.GetChar ( 0 ) ) )
189  num = num.Right( num.Length() - 1);
190  }
191 
192  m_TextValue->SetSelection( m_text.Find( num ),
193  m_text.Find( num ) + num.Length() );
194 
195  if( num.IsEmpty() )
196  m_TextValue->SetSelection( -1, -1 );
197  }
198  }
199  else
200  {
201  m_TextValue->SetSelection( -1, -1 );
202  }
203 
204  m_Orient->SetValue( m_orientation );
205  m_TextSize->SetValue( StringFromValue( g_UserUnit, m_size ) );
208  m_Invisible->SetValue( !m_isVisible );
209  m_TextShapeOpt->SetSelection( m_style );
210 
211  return true;
212 }
213 
214 
216 {
217  wxLogDebug( "In DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow()" );
218 
219  m_text = m_TextValue->GetValue();
220 
221  // There are lots of specific tests required to validate field text.
222  if( m_fieldId == REFERENCE )
223  {
224  // Test if the reference string is valid:
226  {
227  DisplayError( this, _( "Illegal reference field value!" ) );
228  return false;
229  }
230  }
231 
232  m_orientation = m_Orient->GetValue();
233  m_size = ValueFromString( g_UserUnit, m_TextSize->GetValue() );
236  m_isVisible = !m_Invisible->GetValue();
237  m_style = m_TextShapeOpt->GetSelection();
238 
239  return true;
240 }
241 
242 
244 {
245  aText->SetTextSize( wxSize( m_size, m_size ) );
246  aText->SetVisible( m_isVisible );
248  aText->SetItalic( (m_style & 1) != 0 );
249  aText->SetBold( (m_style & 2) != 0 );
252 }
253 
254 
256  const wxString& aTitle,
257  const LIB_FIELD* aField ) :
258  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
259 {
260  m_fieldId = aField->GetId();
261 
262  // When in the library editor, power components can be renamed.
263  m_isPower = false;
264  init();
265 }
266 
267 
269  const wxString& aTitle,
270  const SCH_FIELD* aField ) :
271  DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
272 {
273  m_fieldId = aField->GetId();
274 
275  const SCH_COMPONENT* component = (SCH_COMPONENT*) aField->GetParent();
276 
277  wxASSERT_MSG( component != NULL && component->Type() == SCH_COMPONENT_T,
278  wxT( "Invalid schematic field parent item." ) );
279 
280  const LIB_PART* part = GetParent()->Prj().SchLibs()->FindLibPart( component->GetLibId() );
281 
282  wxASSERT_MSG( part, wxT( "Library part for component <" ) +
283  FROM_UTF8( component->GetLibId().Format() ) +
284  wxT( "> could not be found." ) );
285 
286  m_isPower = part->IsPower();
287 
288  init();
289 }
290 
291 
293 {
294  wxASSERT( aField != NULL || aField->Type() != SCH_FIELD_T );
295 
296  if( aField->GetId() == REFERENCE )
297  {
298  wxASSERT( aSheetPath != NULL );
299 
300  SCH_COMPONENT* component = dynamic_cast< SCH_COMPONENT* >( aField->GetParent() );
301 
302  wxASSERT( component != NULL );
303 
304  if( component != NULL )
305  component->SetRef( aSheetPath, m_text );
306  }
307 
308  aField->SetText( m_text );
309  updateText( aField );
310 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:156
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
#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'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
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
bool IsItalic() const
Definition: eda_text.h:170
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:420
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:203
void SetItalic(bool isItalic)
Definition: eda_text.h:169
Class LIB_FIELD is used in symbol libraries.
Definition: lib_field.h:60
int GetTextWidth() const
Definition: eda_text.h:218
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:175
EDA_ITEM * GetParent() const
Definition: base_struct.h:208
void UpdateField(SCH_FIELD *aField, SCH_SHEET_PATH *aSheetPath)
void SetRef(const SCH_SHEET_PATH *sheet, const wxString &ref)
Set the reference, for the given sheet path.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:190
#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:164
bool IsBold() const
Definition: eda_text.h:173
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:114
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
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, KIWAY_PLAYER *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:302
int GetId() const
Definition: lib_field.h:136
Class LIB_ITEM definition.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:194
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:368
Class LIB_PART defines a library part 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:56
SCH_BASE_FRAME * GetParent()
Field Value of part, i.e. "3.3K".
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:191
Definition the SCH_COMPONENT class for Eeschema.
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:193
bool IsVisible() const
Definition: eda_text.h:176
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.
UTF8 Format() const
Function Format.
Definition: lib_id.cpp:263
virtual bool TransferDataFromWindow() override
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
bool IsType(FRAME_T aType) const
Definition: wxstruct.h:217
class SCH_FILED_VALIDATOR
Definition for part library class.
static bool IsReferenceStringValid(const wxString &aReferenceString)
Function IsReferenceStringValid (static) Tests for an acceptable reference string An acceptable refer...
EDA_TEXT_HJUSTIFY_T IntToEdaTextHorizJustify(int aHorizJustify)
Class SCH_BASE_FRAME is a shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRA...
void SetBold(bool aBold)
Definition: eda_text.h:172
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
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:141
EDA_TEXT_VJUSTIFY_T IntToEdaTextVertJustify(int aVertJustify)