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 <gr_basic.h>
30 #include <widgets/tab_traversal.h>
31 #include <widgets/unit_binder.h>
32 #include <board_commit.h>
33 #include <class_board.h>
34 #include <class_dimension.h>
35 #include <class_module.h>
36 #include <class_pcb_text.h>
37 #include <class_text_mod.h>
38 #include <pcb_edit_frame.h>
39 #include <pcb_layer_box_selector.h>
40 #include <pcbnew.h>
41 #include <wx/valnum.h>
42 
43 
52  DIALOG_TEXT_PROPERTIES_BASE( aParent ),
53  m_Parent( aParent ), m_item( aItem ),
54  m_edaText( nullptr ), m_modText( nullptr ), m_pcbText( nullptr ),
55  m_textWidth( aParent, m_SizeXLabel, m_SizeXCtrl, m_SizeXUnits, true ),
56  m_textHeight( aParent, m_SizeYLabel, m_SizeYCtrl, m_SizeYUnits, true ),
57  m_thickness( aParent, m_ThicknessLabel, m_ThicknessCtrl, m_ThicknessUnits, true ),
58  m_posX( aParent, m_PositionXLabel, m_PositionXCtrl, m_PositionXUnits ),
59  m_posY( aParent, m_PositionYLabel, m_PositionYCtrl, m_PositionYUnits ),
60  m_OrientValidator( 1, &m_OrientValue )
61 {
62  wxString title;
63 
64  if( m_item->Type() == PCB_DIMENSION_T )
65  {
66  title = _( "Dimension Text Properties" );
67 
68  DIMENSION* dimension = (DIMENSION*) m_item;
69  m_edaText = &dimension->Text();
70  m_pcbText = &dimension->Text();
71 
73  m_SingleLineSizer->Show( false );
74  m_MultiLineSizer->Show( false );
75 
76  m_KeepUpright->Show( false );
77  m_statusLine->Show( false );
78  }
79  else if( m_item->Type() == PCB_MODULE_TEXT_T )
80  {
81  title = _( "Footprint Text Properties" );
82 
84  m_edaText = static_cast<EDA_TEXT*>( m_modText );
85 
86  switch( m_modText->GetType() )
87  {
88  case TEXTE_MODULE::TEXT_is_REFERENCE: m_TextLabel->SetLabel( _( "Reference:" ) ); break;
89  case TEXTE_MODULE::TEXT_is_VALUE: m_TextLabel->SetLabel( _( "Value:" ) ); break;
90  case TEXTE_MODULE::TEXT_is_DIVERS: m_TextLabel->SetLabel( _( "Text:" ) ); break;
91  }
92 
94  m_MultiLineSizer->Show( false );
95  m_DimensionTextSizer->Show( false );
96  }
97  else
98  {
99  title = _( "Text Properties" );
100 
101  m_pcbText = (TEXTE_PCB*) aItem;
102  m_edaText = static_cast<EDA_TEXT*>( m_pcbText );
103 
105  m_SingleLineSizer->Show( false );
106  m_DimensionTextSizer->Show( false );
107 
108  // This option make sense only for footprint texts,
109  // Texts on board are always visible:
110  m_Visible->SetValue( true );
111  m_Visible->Show( false );
112 
113  m_KeepUpright->Show( false );
114  m_statusLine->Show( false );
115  }
116 
117  SetTitle( title );
118  m_hash_key = title;
119 
120  // Configure the layers list selector. Note that footprints are built outside the current
121  // board and so we may need to show all layers if the text is on an unactivated layer.
124 
129 
130  m_OrientValue = 0.0;
131  m_OrientValidator.SetRange( -360.0, 360.0 );
132  m_OrientCtrl->SetValidator( m_OrientValidator );
133  m_OrientValidator.SetWindow( m_OrientCtrl );
134 
135  // Handle decimal separators in combo dropdown
136  for( size_t i = 0; i < m_OrientCtrl->GetCount(); ++i )
137  {
138  wxString item = m_OrientCtrl->GetString( i );
139  item.Replace( '.', localeconv()->decimal_point[0] );
140  m_OrientCtrl->SetString( i, item );
141  }
142 
143  // Set font sizes
144  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
145  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
146  m_statusLine->SetFont( infoFont );
147 
148  m_sdbSizerOK->SetDefault();
149 
150  // Tab down the left side first
152  m_LayerLabel,
154  m_SizeXCtrl,
155  m_SizeYCtrl,
159  m_Visible,
160  m_Italic,
162  m_OrientCtrl,
163  m_Mirrored,
165  } );
166 
167  // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
168  // so we have to listen to wxEVT_CHAR_HOOK events instead.
169  Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
170 
172 }
173 
174 
176 {
177  Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( DIALOG_TEXT_PROPERTIES::OnCharHook ), NULL, this );
178 }
179 
180 
185 {
186  DIALOG_TEXT_PROPERTIES dlg( this, aText );
187  dlg.ShowModal();
188 }
189 
190 
191 void DIALOG_TEXT_PROPERTIES::OnCharHook( wxKeyEvent& aEvent )
192 {
193  if( aEvent.GetKeyCode() == WXK_TAB )
194  {
195  int flags = 0;
196 
197  if( !aEvent.ShiftDown() )
198  flags |= wxNavigationKeyEvent::IsForward;
199 
200  if( aEvent.ControlDown() )
201  flags |= wxNavigationKeyEvent::WinChange;
202 
203  NavigateIn( flags );
204  }
205  else if( aEvent.GetKeyCode() == WXK_RETURN )
206  {
207  if( FindFocus() == m_MultiLineText && !aEvent.ShiftDown() )
208  aEvent.Skip();
209  else
210  {
212  EndModal( wxID_OK );
213  }
214  }
215  else
216  {
217  aEvent.Skip();
218  }
219 }
220 
221 
223 {
224  EDA_UNITS_T units = UNSCALED_UNITS;
225  bool useMils;
226 
227  FetchUnitsFromString( m_DimensionText->GetValue(), units, useMils );
228 
229  if( units != UNSCALED_UNITS )
230  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
231 }
232 
233 
235 {
236  DIMENSION* dimension = (DIMENSION*) m_item;
237  EDA_UNITS_T units;
238  bool useMils;
239 
240  // Get default units in case dimension text doesn't contain units.
241  dimension->GetUnits( units, useMils );
242 
243  double value = ValueFromString( units, m_DimensionText->GetValue(), useMils );
244 
245  switch( event.GetSelection() )
246  {
247  case 0: units = INCHES; useMils = false; break;
248  case 1: units = INCHES; useMils = true; break;
249  case 2: units = MILLIMETRES; useMils = false; break;
250  default: break;
251  }
252 
253  m_DimensionText->SetValue( StringFromValue( units, value, true, useMils ) );
254 }
255 
256 
258 {
259  if( m_SingleLineText->IsShown() )
260  {
261  m_SingleLineText->SetValue( m_edaText->GetText() );
262 
264  SelectReferenceNumber( static_cast<wxTextEntry*>( m_SingleLineText ) );
265  else
266  m_SingleLineText->SetSelection( -1, -1 );
267  }
268  else if( m_MultiLineText->IsShown() )
269  {
270  m_MultiLineText->SetValue( m_edaText->GetText() );
271  m_MultiLineText->SetSelection( -1, -1 );
272  }
273  else if (m_DimensionText->IsShown() )
274  {
275  m_DimensionText->SetValue( m_edaText->GetText() );
276  m_DimensionText->SetSelection( -1, -1 );
277 
278  DIMENSION* dimension = (DIMENSION*) m_item;
279  EDA_UNITS_T units;
280  bool useMils;
281  dimension->GetUnits( units, useMils );
282 
283  m_DimensionUnitsOpt->SetSelection( units == MILLIMETRES ? 2 : useMils ? 1 : 0 );
284  }
285 
286  if( m_item->Type() == PCB_MODULE_TEXT_T && m_modText )
287  {
288  MODULE* module = dynamic_cast<MODULE*>( m_modText->GetParent() );
289  wxString msg;
290 
291  if( module )
292  {
293  msg.Printf( _("Footprint %s (%s), %s, rotated %.1f deg"),
294  module->GetReference(),
295  module->GetValue(),
296  module->IsFlipped() ? _( "back side (mirrored)" ) : _( "front side" ),
297  module->GetOrientation() / 10.0 );
298  }
299 
300  m_statusLine->SetLabel( msg );
301  }
302  else
303  {
304  m_statusLine->Show( false );
305  }
306 
308  {
309  wxMessageBox( _( "This item was on a non-existing or forbidden layer.\n"
310  "It has been moved to the first allowed layer." ) );
311  m_LayerSelectionCtrl->SetSelection( 0 );
312  }
313 
319 
320  m_Visible->SetValue( m_edaText->IsVisible() );
321  m_Italic->SetValue( m_edaText->IsItalic() );
323  m_JustifyChoice->SetSelection( (int) hJustify + 1 );
325  m_Mirrored->SetValue( m_edaText->IsMirrored() );
326 
327  if( m_modText )
328  m_KeepUpright->SetValue( m_modText->IsKeepUpright() );
329 
330  return DIALOG_TEXT_PROPERTIES_BASE::TransferDataToWindow();
331 }
332 
333 
335 {
336  if( !DIALOG_TEXT_PROPERTIES_BASE::TransferDataFromWindow() )
337  return false;
338 
341  return false;
342 
343  BOARD_COMMIT commit( m_Parent );
344  commit.Modify( m_item );
345 
346  // If no other command in progress, prepare undo command
347  // (for a command in progress, will be made later, at the completion of command)
348  bool pushCommit = ( m_item->GetEditFlags() == 0 );
349 
350  /* set flag in edit to force undo/redo/abort proper operation,
351  * and avoid new calls to SaveCopyInUndoList for the same text
352  * this can occurs when a text is moved, and then rotated, edited ..
353  */
354  if( !pushCommit )
355  m_item->SetFlags( IN_EDIT );
356 
357  // Set the new text content
358  if( m_SingleLineText->IsShown() )
359  {
360  if( !m_SingleLineText->GetValue().IsEmpty() )
361  m_edaText->SetText( m_SingleLineText->GetValue() );
362  }
363  else if( m_MultiLineText->IsShown() )
364  {
365  if( !m_MultiLineText->GetValue().IsEmpty() )
366  m_edaText->SetText( m_MultiLineText->GetValue() );
367  }
368  else if( m_DimensionText->IsShown() )
369  {
370  if( !m_DimensionText->GetValue().IsEmpty() )
371  m_edaText->SetText( m_DimensionText->GetValue() );
372 
373  DIMENSION* dimension = (DIMENSION*) m_item;
374 
375  switch( m_DimensionUnitsOpt->GetSelection() )
376  {
377  case 0: dimension->SetUnits( INCHES, false ); break;
378  case 1: dimension->SetUnits( INCHES, true ); break;
379  case 2: dimension->SetUnits( MILLIMETRES, false ); break;
380  default: break;
381  }
382  }
383 
385 
388  m_edaText->SetTextPos( wxPoint( m_posX.GetValue(), m_posY.GetValue() ) );
389 
390  if( m_modText )
392 
393  // Test for acceptable values for thickness and size and clamp if fails
394  int maxthickness = Clamp_Text_PenSize( m_edaText->GetThickness(), m_edaText->GetTextSize() );
395 
396  if( m_edaText->GetThickness() > maxthickness )
397  {
398  DisplayError( this, _( "The text thickness is too large for the text size.\n"
399  "It will be clamped." ) );
400  m_edaText->SetThickness( maxthickness );
401  }
402 
403  m_edaText->SetVisible( m_Visible->GetValue() );
404  m_edaText->SetItalic( m_Italic->GetValue() );
406  m_edaText->SetMirrored( m_Mirrored->GetValue() );
407 
408  if( m_modText )
409  m_modText->SetKeepUpright( m_KeepUpright->GetValue() );
410 
411  switch( m_JustifyChoice->GetSelection() )
412  {
416  default: break;
417  }
418 
419  m_Parent->Refresh();
420 
421  if( pushCommit )
422  commit.Push( _( "Change text properties" ) );
423 
424  return true;
425 }
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:105
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)
DIALOG_PCB_TEXT_PROPERTIES, derived from DIALOG_PCB_TEXT_PROPERTIES_BASE.
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:118
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:174
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:217
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:208
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:116
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 As a rule, pen width should not be >1/4em, otherwise the character will b...
Definition: gr_text.cpp:67
Footprint text class description.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:252
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:256
void ShowNonActivatedLayers(bool aShow)
const wxSize & GetTextSize() const
Definition: eda_text.h:209
Definition: common.h:158
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:218
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:157
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:197