KiCad PCB EDA Suite
dialog_text_properties.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) 2004-2018 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 2010-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <dialog_text_properties.h>
26 #include <confirm.h>
27 #include <gr_text.h>
28 #include <fctsys.h>
29 #include <widgets/tab_traversal.h>
30 #include <widgets/unit_binder.h>
31 #include <board_commit.h>
32 #include <class_board.h>
33 #include <class_dimension.h>
34 #include <class_module.h>
35 #include <class_pcb_text.h>
36 #include <class_text_mod.h>
37 #include <pcb_edit_frame.h>
38 #include <pcb_layer_box_selector.h>
39 #include <pcbnew.h>
40 #include <wx/valnum.h>
41 
42 
44  DIALOG_TEXT_PROPERTIES_BASE( aParent ),
45  m_Parent( aParent ),
46  m_item( aItem ),
47  m_edaText( nullptr ),
48  m_modText( nullptr ),
49  m_pcbText( nullptr ),
50  m_textWidth( aParent, m_SizeXLabel, m_SizeXCtrl, m_SizeXUnits, true ),
51  m_textHeight( aParent, m_SizeYLabel, m_SizeYCtrl, m_SizeYUnits, true ),
52  m_thickness( aParent, m_ThicknessLabel, m_ThicknessCtrl, m_ThicknessUnits, true ),
53  m_posX( aParent, m_PositionXLabel, m_PositionXCtrl, m_PositionXUnits ),
54  m_posY( aParent, m_PositionYLabel, m_PositionYCtrl, m_PositionYUnits ),
55  m_OrientValidator( 1, &m_OrientValue )
56 {
57  wxString title;
58 
59  if( m_item->Type() == PCB_DIMENSION_T )
60  {
61  title = _( "Dimension Text Properties" );
62 
63  DIMENSION* dimension = (DIMENSION*) m_item;
64  m_edaText = &dimension->Text();
65  m_pcbText = &dimension->Text();
66 
68  m_SingleLineSizer->Show( false );
69  m_MultiLineSizer->Show( false );
70 
71  m_KeepUpright->Show( false );
72  m_statusLine->Show( false );
73  }
74  else if( m_item->Type() == PCB_MODULE_TEXT_T )
75  {
76  title = _( "Footprint Text Properties" );
77 
79  m_edaText = static_cast<EDA_TEXT*>( m_modText );
80 
81  switch( m_modText->GetType() )
82  {
83  case TEXTE_MODULE::TEXT_is_REFERENCE: m_TextLabel->SetLabel( _( "Reference:" ) ); break;
84  case TEXTE_MODULE::TEXT_is_VALUE: m_TextLabel->SetLabel( _( "Value:" ) ); break;
85  case TEXTE_MODULE::TEXT_is_DIVERS: m_TextLabel->SetLabel( _( "Text:" ) ); break;
86  }
87 
89  m_MultiLineSizer->Show( false );
90  m_DimensionTextSizer->Show( false );
91  }
92  else
93  {
94  title = _( "Text Properties" );
95 
96  m_pcbText = (TEXTE_PCB*) aItem;
97  m_edaText = static_cast<EDA_TEXT*>( m_pcbText );
98 
100  m_SingleLineSizer->Show( false );
101  m_DimensionTextSizer->Show( false );
102 
103  // This option make sense only for footprint texts,
104  // Texts on board are always visible:
105  m_Visible->SetValue( true );
106  m_Visible->Show( false );
107 
108  m_KeepUpright->Show( false );
109  m_statusLine->Show( false );
110  }
111 
112  SetTitle( title );
113  m_hash_key = title;
114 
115  // Configure the layers list selector. Note that footprints are built outside the current
116  // board and so we may need to show all layers if the text is on an unactivated layer.
119 
124 
125  m_OrientValue = 0.0;
126  m_OrientValidator.SetRange( -360.0, 360.0 );
127  m_OrientCtrl->SetValidator( m_OrientValidator );
128  m_OrientValidator.SetWindow( m_OrientCtrl );
129 
130  // Handle decimal separators in combo dropdown
131  for( size_t i = 0; i < m_OrientCtrl->GetCount(); ++i )
132  {
133  wxString item = m_OrientCtrl->GetString( i );
134  item.Replace( '.', localeconv()->decimal_point[0] );
135  m_OrientCtrl->SetString( i, item );
136  }
137 
138  // Set font sizes
139  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
140  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
141  m_statusLine->SetFont( infoFont );
142 
143  m_sdbSizerOK->SetDefault();
144 
145  // Tab down the left side first
147  m_LayerLabel,
149  m_SizeXCtrl,
150  m_SizeYCtrl,
154  m_Visible,
155  m_Italic,
157  m_OrientCtrl,
158  m_Mirrored,
160  } );
161 
162  // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
163  // so we have to listen to wxEVT_CHAR_HOOK events instead.
164  Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
165 
167 }
168 
169 
171 {
172  Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
173 }
174 
175 
180 {
181  DIALOG_TEXT_PROPERTIES dlg( this, aText );
182  dlg.ShowModal();
183 }
184 
185 
186 void DIALOG_TEXT_PROPERTIES::OnCharHook( wxKeyEvent& aEvent )
187 {
188  if( aEvent.GetKeyCode() == WXK_TAB )
189  {
190  int flags = 0;
191 
192  if( !aEvent.ShiftDown() )
193  flags |= wxNavigationKeyEvent::IsForward;
194 
195  if( aEvent.ControlDown() )
196  flags |= wxNavigationKeyEvent::WinChange;
197 
198  NavigateIn( flags );
199  }
200  else if( aEvent.GetKeyCode() == WXK_RETURN )
201  {
202  // Allow typing returns into a multi-line text
203  if( m_MultiLineText->IsShown() && m_MultiLineText->HasFocus() && !aEvent.ShiftDown() )
204  aEvent.Skip();
205  else
206  {
207  if( TransferDataFromWindow() )
208  EndModal( wxID_OK );
209  }
210  }
211  else
212  {
213  aEvent.Skip();
214  }
215 }
216 
217 
219 {
220  EDA_UNITS_T units = UNSCALED_UNITS;
221  bool useMils;
222 
223  FetchUnitsFromString( m_DimensionText->GetValue(), units, useMils );
224 
225  if( units != UNSCALED_UNITS )
226  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
227 }
228 
229 
231 {
232  DIMENSION* dimension = (DIMENSION*) m_item;
233  EDA_UNITS_T units;
234  bool useMils;
235 
236  // Get default units in case dimension text doesn't contain units.
237  dimension->GetUnits( units, useMils );
238 
239  double value = ValueFromString( units, m_DimensionText->GetValue(), useMils );
240 
241  switch( event.GetSelection() )
242  {
243  case 0: units = INCHES; useMils = false; break;
244  case 1: units = INCHES; useMils = true; break;
245  case 2: units = MILLIMETRES; useMils = false; break;
246  default: break;
247  }
248 
249  m_DimensionText->SetValue( StringFromValue( units, value, true, useMils ) );
250 }
251 
252 
254 {
255  if( m_SingleLineText->IsShown() )
256  {
257  m_SingleLineText->SetValue( m_edaText->GetText() );
258 
260  SelectReferenceNumber( static_cast<wxTextEntry*>( m_SingleLineText ) );
261  else
262  m_SingleLineText->SetSelection( -1, -1 );
263  }
264  else if( m_MultiLineText->IsShown() )
265  {
266  m_MultiLineText->SetValue( m_edaText->GetText() );
267  m_MultiLineText->SetSelection( -1, -1 );
268  }
269  else if (m_DimensionText->IsShown() )
270  {
271  m_DimensionText->SetValue( m_edaText->GetText() );
272  m_DimensionText->SetSelection( -1, -1 );
273 
274  DIMENSION* dimension = (DIMENSION*) m_item;
275  EDA_UNITS_T units;
276  bool useMils;
277  dimension->GetUnits( units, useMils );
278 
279  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
280  }
281 
282  if( m_item->Type() == PCB_MODULE_TEXT_T && m_modText )
283  {
284  MODULE* module = dynamic_cast<MODULE*>( m_modText->GetParent() );
285  wxString msg;
286 
287  if( module )
288  {
289  msg.Printf( _("Footprint %s (%s), %s, rotated %.1f deg"),
290  module->GetReference(),
291  module->GetValue(),
292  module->IsFlipped() ? _( "back side (mirrored)" ) : _( "front side" ),
293  module->GetOrientation() / 10.0 );
294  }
295 
296  m_statusLine->SetLabel( msg );
297  }
298  else
299  {
300  m_statusLine->Show( false );
301  }
302 
304  {
305  wxMessageBox( _( "This item was on a non-existing or forbidden layer.\n"
306  "It has been moved to the first allowed layer." ) );
307  m_LayerSelectionCtrl->SetSelection( 0 );
308  }
309 
315 
316  m_Visible->SetValue( m_edaText->IsVisible() );
317  m_Italic->SetValue( m_edaText->IsItalic() );
319  m_JustifyChoice->SetSelection( (int) hJustify + 1 );
321  m_Mirrored->SetValue( m_edaText->IsMirrored() );
322 
323  if( m_modText )
324  m_KeepUpright->SetValue( m_modText->IsKeepUpright() );
325 
326  return DIALOG_TEXT_PROPERTIES_BASE::TransferDataToWindow();
327 }
328 
329 
331 {
332  if( !DIALOG_TEXT_PROPERTIES_BASE::TransferDataFromWindow() )
333  return false;
334 
337  return false;
338 
339  BOARD_COMMIT commit( m_Parent );
340  commit.Modify( m_item );
341 
342  // If no other command in progress, prepare undo command
343  // (for a command in progress, will be made later, at the completion of command)
344  bool pushCommit = ( m_item->GetEditFlags() == 0 );
345 
346  /* set flag in edit to force undo/redo/abort proper operation,
347  * and avoid new calls to SaveCopyInUndoList for the same text
348  * this can occurs when a text is moved, and then rotated, edited ..
349  */
350  if( !pushCommit )
351  m_item->SetFlags( IN_EDIT );
352 
353  // Set the new text content
354  if( m_SingleLineText->IsShown() )
355  {
356  if( !m_SingleLineText->GetValue().IsEmpty() )
357  m_edaText->SetText( m_SingleLineText->GetValue() );
358  }
359  else if( m_MultiLineText->IsShown() )
360  {
361  if( !m_MultiLineText->GetValue().IsEmpty() )
362  m_edaText->SetText( m_MultiLineText->GetValue() );
363  }
364  else if( m_DimensionText->IsShown() )
365  {
366  if( !m_DimensionText->GetValue().IsEmpty() )
367  m_edaText->SetText( m_DimensionText->GetValue() );
368 
369  DIMENSION* dimension = (DIMENSION*) m_item;
370 
371  switch( m_DimensionUnitsOpt->GetSelection() )
372  {
373  case 0: dimension->SetUnits( INCHES, false ); break;
374  case 1: dimension->SetUnits( INCHES, true ); break;
375  case 2: dimension->SetUnits( MILLIMETRES, false ); break;
376  default: break;
377  }
378  }
379 
381 
384  m_edaText->SetTextPos( wxPoint( m_posX.GetValue(), m_posY.GetValue() ) );
385 
386  if( m_modText )
388 
389  // Test for acceptable values for thickness and size and clamp if fails
390  int maxthickness = Clamp_Text_PenSize( m_edaText->GetThickness(), m_edaText->GetTextSize() );
391 
392  if( m_edaText->GetThickness() > maxthickness )
393  {
394  DisplayError( this, _( "The text thickness is too large for the text size.\n"
395  "It will be clamped." ) );
396  m_edaText->SetThickness( maxthickness );
397  }
398 
399  m_edaText->SetVisible( m_Visible->GetValue() );
400  m_edaText->SetItalic( m_Italic->GetValue() );
402  m_edaText->SetMirrored( m_Mirrored->GetValue() );
403 
404  if( m_modText )
405  m_modText->SetKeepUpright( m_KeepUpright->GetValue() );
406 
407  switch( m_JustifyChoice->GetSelection() )
408  {
412  default: break;
413  }
414 
415  m_Parent->Refresh();
416 
417  if( pushCommit )
418  commit.Push( _( "Change text properties" ) );
419 
420  return true;
421 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:172
void SetTextAngle(double aAngle)
Definition: eda_text.h:150
Functions for manipulating tab traversal in forms and dialogs.
double GetOrientation() const
Definition: class_module.h:193
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
void SetKeepUpright(bool aKeepUpright)
#define IN_EDIT
Item currently edited.
Definition: base_struct.h:118
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:44
bool IsMirrored() const
Definition: eda_text.h:173
DIALOG_TEXT_PROPERTIES(PCB_BASE_EDIT_FRAME *aParent, BOARD_ITEM *aItem)
TEXTE_PCB class definition.
Class DIALOG_TEXT_PROPERTIES_BASE.
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
bool IsFlipped() const
function IsFlipped
Definition: class_module.h:263
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
This file is part of the common library.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
std::string m_hash_key
Definition: dialog_shim.h:176
void SetNotAllowedLayerSet(LSET aMask)
void OnCharHook(wxKeyEvent &aEvent)
bool IsVisible() const
Definition: eda_text.h:170
virtual bool Validate(int aMin, int aMax, bool setFocusOnError=true)
Function Validate Validates the control against the given range, informing the user of any errors fou...
void SetItalic(bool isItalic)
Definition: eda_text.h:163
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:229
void SetVisible(bool aVisible)
Definition: eda_text.h:169
const wxString GetValue() const
Function GetValue.
Definition: class_module.h:432
void SetControlsTabOrder(const std::vector< wxWindow * > &aControlsInTabOrder)
Set a list of controls to have a defined sequential tab order.
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:148
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:220
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:414
void FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS_T &aUnits, bool &aUseMils)
Function FetchUnitsFromString writes any unit info found in the string to aUnits and aUseMils.
Definition: base_units.cpp:375
void InstallTextOptionsFrame(BOARD_ITEM *aText)
Routine for main window class to launch text properties dialog.
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
Function IsLayerEnabled is a proxy function that calls the correspondent function in m_BoardSettings ...
Definition: class_board.h:447
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:118
void OnDimensionUnitsChange(wxCommandEvent &event) override
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:407
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
bool IsItalic() const
Definition: eda_text.h:164
bool IsKeepUpright()
DIMENSION class definition.
LAYER_NUM GetLayerSelection() const
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:159
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aBold)
Function Clamp_Text_PenSize Don't allow text to become cluttered up in its own fatness.
Definition: gr_text.cpp:64
Footprint text class description.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:265
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:184
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:112
STATUS_FLAGS GetEditFlags() const
Definition: base_struct.h:269
void ShowNonActivatedLayers(bool aShow)
const wxSize & GetTextSize() const
Definition: eda_text.h:221
Definition: common.h:155
TEXT_TYPE GetType() const
int SetLayerSelection(LAYER_NUM layer)
#define TEXTS_MAX_SIZE
Maximum text size in internal units (10 inches)
Definition: pcbnew.h:39
void SetUnits(EDA_UNITS_T aUnits, bool aUseMils)
bool SetLayersHotkeys(bool value)
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:98
#define _(s)
PCB_BASE_EDIT_FRAME * m_Parent
#define TEXTS_MIN_SIZE
Minimum text size in internal units (1 mil)
Definition: pcbnew.h:38
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void SetLocalCoord()
Set relative coordinates.
double GetTextAngleDegrees() const
Definition: eda_text.h:160
PCB_LAYER_BOX_SELECTOR * m_LayerSelectionCtrl
Common, abstract interface for edit frames.
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:187
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
TEXTE_PCB & Text()
size_t i
Definition: json11.cpp:597
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
void GetUnits(EDA_UNITS_T &aUnits, bool &aUseMils) const
wxString StringFromValue(EDA_UNITS_T aUnits, int aValue, bool aAddUnitSymbol, bool aUseMils)
Function StringFromValue returns the string from aValue according to units (inch, mm ....
Definition: base_units.cpp:210
const wxPoint & GetTextPos() const
Definition: eda_text.h:230
static LSET ForbiddenTextLayers()
Function ForbiddenTextLayers Layers which are now allowed to have text on them.
Definition: lset.cpp:796
Module description (excepted pads)
BOARD * GetBoard() const
BOARD_ITEM_CONTAINER * GetParent() const
void OnDimensionTextChange(wxCommandEvent &event) override
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:243
virtual const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:124
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:813
wxFloatingPointValidator< double > m_OrientValidator
EDA_UNITS_T
Definition: common.h:154
Class DIMENSION.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:142
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210