KiCad PCB EDA Suite
dialog_edit_label.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) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #include <fctsys.h>
32 #include <wx/valgen.h>
33 #include <wx/valnum.h>
34 #include <sch_edit_frame.h>
35 #include <base_units.h>
36 
37 #include <class_drawpanel.h>
38 #include <general.h>
39 #include <draw_graphic_text.h>
40 #include <confirm.h>
41 #include <sch_text.h>
42 #include <typeinfo>
43 
44 #include <dialog_edit_label_base.h>
45 
46 class SCH_EDIT_FRAME;
47 class SCH_TEXT;
48 
49 
51 {
52 public:
53  DIALOG_LABEL_EDITOR( SCH_EDIT_FRAME* parent, SCH_TEXT* aTextItem );
55 
56  void SetTitle( const wxString& aTitle ) override
57  {
58  // This class is shared for numerous tasks: a couple of
59  // single line labels and multi-line text fields.
60  // Often the desired size of the multi-line text field editor
61  // is larger than is needed for the single line label.
62  // Therefore the session retained sizes of these dialogs needs
63  // to be class independent, make them title dependent.
64  switch( m_CurrentText->Type() )
65  {
66  case SCH_GLOBAL_LABEL_T:
68  case SCH_LABEL_T:
69  // labels can share retained settings probably.
70  break;
71 
72  default:
73  m_hash_key = TO_UTF8( aTitle );
74  m_hash_key += typeid(*this).name();
75  }
76 
77  DIALOG_LABEL_EDITOR_BASE::SetTitle( aTitle );
78  }
79 
80 private:
81  void InitDialog( ) override;
82  virtual void OnEnterKey( wxCommandEvent& aEvent ) override;
83  virtual void OnOkClick( wxCommandEvent& aEvent ) override;
84  virtual void OnCancelClick( wxCommandEvent& aEvent ) override;
85  void OnCharHook( wxKeyEvent& aEvent );
86  void TextPropertiesAccept( wxCommandEvent& aEvent );
87 
90  wxTextCtrl* m_textLabel;
91 };
92 
93 
94 
95 /* Edit the properties of the text (Label, Global label, graphic text).. )
96  * pointed by "aTextStruct"
97  */
99 {
100  if( aTextItem == NULL )
101  return;
102 
103  DIALOG_LABEL_EDITOR dialog( this, aTextItem );
104 
105  dialog.ShowModal();
106 }
107 
108 
110  DIALOG_LABEL_EDITOR_BASE( aParent )
111 {
112  m_Parent = aParent;
113  m_CurrentText = aTextItem;
114  InitDialog();
115 
116  // Conservative limits 0.0 to 10.0 inches
117  const int minSize = 0; // a value like 0.01 is better, but if > 0, creates
118  // annoying issues when trying to enter a value starting by 0 or .0
119  const int maxSize = 10 * 1000 * IU_PER_MILS;
120 
121  wxFloatingPointValidator<double> textSizeValidator( NULL, wxNUM_VAL_NO_TRAILING_ZEROES );
122  textSizeValidator.SetPrecision( 4 );
123  textSizeValidator.SetRange( To_User_Unit( g_UserUnit, minSize ),
124  To_User_Unit( g_UserUnit, maxSize ) );
125 
126  m_TextSize->SetValidator( textSizeValidator );
127 
128  // wxTextCtrls fail to generate wxEVT_CHAR events when the wxTE_MULTILINE flag is set,
129  // so we have to listen to wxEVT_CHAR_HOOK events instead.
130  m_textLabelMultiLine->Connect( wxEVT_CHAR_HOOK,
131  wxKeyEventHandler( DIALOG_LABEL_EDITOR::OnCharHook ),
132  NULL, this );
133 
134  // Now all widgets have the size fixed, call FinishDialogSettings
136 }
137 
138 
140 {
141  m_textLabelMultiLine->Disconnect( wxEVT_CHAR_HOOK,
142  wxKeyEventHandler( DIALOG_LABEL_EDITOR::OnCharHook ),
143  NULL, this );
144 }
145 
146 
147 // Sadly we store the orientation of hierarchical and global labels using a different
148 // int encoding than that for local labels:
149 // Global Local
150 // Left justified 0 2
151 // Up 1 3
152 // Right justified 2 0
153 // Down 3 1
154 static int mapOrientation( KICAD_T labelType, int aOrientation )
155 {
156  if( labelType == SCH_LABEL_T )
157  return aOrientation;
158 
159  switch( aOrientation )
160  {
161  case 0: return 2;
162  case 2: return 0;
163  default: return aOrientation;
164  }
165 }
166 
167 
169 {
170  wxString msg;
171  bool multiLine = false;
172 
174  {
176  m_textLabelSingleLine->Show( false );
177  multiLine = true;
178  }
179  else
180  {
182  m_textLabelMultiLine->Show( false );
183  wxTextValidator* validator = (wxTextValidator*) m_textLabel->GetValidator();
184 
185  // Add invalid label characters to this list.
186  // for any label type but SCH_TEXT_T (that has the multiline allowed)
187  validator->SetCharExcludes( wxT( " /" ) );
188  }
189 
190  m_textLabel->SetValue( m_CurrentText->GetText() );
191  m_textLabel->SetFocus();
192 
193  switch( m_CurrentText->Type() )
194  {
195  case SCH_GLOBAL_LABEL_T:
196  SetTitle( _( "Global Label Properties" ) );
197  break;
198 
200  SetTitle( _( "Hierarchical Label Properties" ) );
201  break;
202 
203  case SCH_LABEL_T:
204  SetTitle( _( "Label Properties" ) );
205  break;
206 
207  case SCH_SHEET_PIN_T:
208  SetTitle( _( "Hierarchical Sheet Pin Properties." ) );
209  break;
210 
211  default:
212  SetTitle( _( "Text Properties" ) );
213  break;
214  }
215 
216  const int MINTEXTWIDTH = 40; // M's are big characters, a few establish a lot of width
217 
218  int max_len = 0;
219 
220  if ( !multiLine )
221  {
222  max_len = m_CurrentText->GetText().Length();
223  }
224  else
225  {
226  // calculate the length of the biggest line
227  // we cannot use the length of the entire text that has no meaning
228  int curr_len = MINTEXTWIDTH;
229  int imax = m_CurrentText->GetText().Length();
230 
231  for( int count = 0; count < imax; count++ )
232  {
233  if( m_CurrentText->GetText()[count] == '\n' ||
234  m_CurrentText->GetText()[count] == '\r' ) // new line
235  {
236  curr_len = 0;
237  }
238  else
239  {
240  curr_len++;
241 
242  if ( max_len < curr_len )
243  max_len = curr_len;
244  }
245  }
246  }
247 
248  if( max_len < MINTEXTWIDTH )
249  max_len = MINTEXTWIDTH;
250 
251  wxString textWidth;
252  textWidth.Append( 'M', MINTEXTWIDTH );
253  EnsureTextCtrlWidth( m_textLabel, &textWidth );
254 
255  // Set text options:
257  m_TextOrient->SetSelection( orientation );
258 
259  m_TextShape->SetSelection( m_CurrentText->GetShape() );
260 
261  int style = 0;
262 
263  if( m_CurrentText->IsItalic() )
264  style = 1;
265 
266  if( m_CurrentText->IsBold() )
267  style += 2;
268 
269  m_TextStyle->SetSelection( style );
270 
271  wxString units = ReturnUnitSymbol( g_UserUnit, wxT( "(%s)" ) );
272  msg.Printf( _( "H%s x W%s" ), GetChars( units ), GetChars( units ) );
273  m_staticSizeUnits->SetLabel( msg );
274 
276  m_TextSize->SetValue( msg );
277 
280  {
281  m_TextShape->Show( false );
282  }
283 
284  m_sdbSizer1OK->SetDefault();
285 }
286 
287 
292 void DIALOG_LABEL_EDITOR::OnEnterKey( wxCommandEvent& aEvent )
293 {
294  TextPropertiesAccept( aEvent );
295 }
296 
297 
302 void DIALOG_LABEL_EDITOR::OnCharHook( wxKeyEvent& aEvent )
303 {
304  if( aEvent.GetKeyCode() == WXK_TAB )
305  {
306  int flags = 0;
307  if( !aEvent.ShiftDown() )
308  flags |= wxNavigationKeyEvent::IsForward;
309  if( aEvent.ControlDown() )
310  flags |= wxNavigationKeyEvent::WinChange;
311  NavigateIn( flags );
312  }
313  else if( aEvent.GetKeyCode() == WXK_RETURN && aEvent.ShiftDown() )
314  {
315  wxCommandEvent cmdEvent( wxEVT_COMMAND_ENTER );
316  TextPropertiesAccept( cmdEvent );
317  }
318  else
319  {
320  aEvent.Skip();
321  }
322 }
323 
324 
329 void DIALOG_LABEL_EDITOR::OnOkClick( wxCommandEvent& aEvent )
330 {
331  TextPropertiesAccept( aEvent );
332 }
333 
334 
339 void DIALOG_LABEL_EDITOR::OnCancelClick( wxCommandEvent& aEvent )
340 {
342  EndModal( wxID_CANCEL );
343 }
344 
345 
346 void DIALOG_LABEL_EDITOR::TextPropertiesAccept( wxCommandEvent& aEvent )
347 {
348  wxString text;
349  int value;
350 
351  /* save old text in undo list if not already in edit */
352  /* or the label to be edited is part of a block */
353  if( m_CurrentText->GetFlags() == 0 ||
356 
358 
359  text = m_textLabel->GetValue();
360 
361  if( !text.IsEmpty() )
362  m_CurrentText->SetText( text );
363  else if( !m_CurrentText->IsNew() )
364  {
365  DisplayError( this, _( "Empty Text!" ) );
366  return;
367  }
368 
369  int orientation = m_TextOrient->GetSelection();
371 
372  text = m_TextSize->GetValue();
373  value = ValueFromString( g_UserUnit, text );
374  m_CurrentText->SetTextSize( wxSize( value, value ) );
375 
376  if( m_TextShape )
378  m_CurrentText->SetShape( static_cast<PINSHEETLABEL_SHAPE>( m_TextShape->GetSelection() ) );
379 
380  int style = m_TextStyle->GetSelection();
381 
382  m_CurrentText->SetItalic( ( style & 1 ) );
383 
384  if( ( style & 2 ) )
385  {
386  m_CurrentText->SetBold( true );
388  }
389  else
390  {
391  m_CurrentText->SetBold( false );
393  }
394 
395  m_Parent->OnModify();
396 
397  // Make the text size the new default size ( if it is a new text ):
398  if( m_CurrentText->IsNew() )
400 
403  EndModal( wxID_OK );
404 }
void OnCharHook(wxKeyEvent &aEvent)
PINSHEETLABEL_SHAPE GetShape() const
Definition: sch_text.h:118
KICAD_T Type() const
Function Type()
Definition: base_struct.h:227
void SetTitle(const wxString &aTitle) override
void SetShape(PINSHEETLABEL_SHAPE aShape)
Definition: sch_text.h:120
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:284
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
Definition: base_screen.h:214
virtual void OnOkClick(wxCommandEvent &aEvent) override
bool IsMultilineAllowed() const
Definition: eda_text.h:186
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
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.
bool IsNew() const
Definition: base_struct.h:245
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:342
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen* and ...
std::string m_hash_key
Definition: dialog_shim.h:151
wxString ReturnUnitSymbol(EDA_UNITS_T aUnit, const wxString &formatString)
Returns the units symbol.
Definition: base_units.cpp:425
virtual void SetLabelSpinStyle(int aSpinStyle)
Set a spin or rotation angle, along with specific horizontal and vertical justification styles with e...
Definition: sch_text.cpp:277
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:204
void SetItalic(bool isItalic)
Definition: eda_text.h:167
int GetTextWidth() const
Definition: eda_text.h:216
virtual void OnEnterKey(wxCommandEvent &aEvent) override
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
int GetLabelSpinStyle() const
Definition: sch_text.h:116
void RefreshDrawingRect(const EDA_RECT &aRect, bool aEraseBackground=true)
Function RefreshDrawingRect redraws the contents of aRect in drawing units.
Definition: draw_panel.cpp:319
Schematic editor (Eeschema) main window.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:212
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:361
Class DIALOG_LABEL_EDITOR_BASE.
SCH_EDIT_FRAME * m_Parent
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
DIALOG_LABEL_EDITOR(SCH_EDIT_FRAME *parent, SCH_TEXT *aTextItem)
bool IsBold() const
Definition: eda_text.h:171
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
void TextPropertiesAccept(wxCommandEvent &aEvent)
void SetDefaultTextSize(int aTextSize)
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString)
Function EnsureTextCtrlWidth sets the minimum pixel width on a text control in order to make a text s...
Definition: common.cpp:108
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:128
void SaveCopyInUndoList(SCH_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, bool aAppend=false, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
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:369
virtual void OnCancelClick(wxCommandEvent &aEvent) override
virtual const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: sch_text.cpp:469
void EditSchematicText(SCH_TEXT *TextStruct)
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
static int mapOrientation(KICAD_T labelType, int aOrientation)
BLOCK_STATE_T GetState() const
void InitDialog() override
double To_User_Unit(EDA_UNITS_T aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:91
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:241
Implementation of the label properties dialog.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:146
virtual void SetText(const wxString &aText)
Definition: eda_text.h:139