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-2013 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 <schframe.h>
35 #include <base_units.h>
36 
37 #include <class_drawpanel.h>
38 #include <general.h>
39 #include <drawtxt.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 );
54 
55  void SetTitle( const wxString& aTitle ) override
56  {
57  // This class is shared for numerous tasks: a couple of
58  // single line labels and multi-line text fields.
59  // Often the desired size of the multi-line text field editor
60  // is larger than is needed for the single line label.
61  // Therefore the session retained sizes of these dialogs needs
62  // to be class independent, make them title dependent.
63  switch( m_CurrentText->Type() )
64  {
65  case SCH_GLOBAL_LABEL_T:
67  case SCH_LABEL_T:
68  // labels can share retained settings probably.
69  break;
70 
71  default:
72  m_hash_key = TO_UTF8( aTitle );
73  m_hash_key += typeid(*this).name();
74  }
75 
76  DIALOG_LABEL_EDITOR_BASE::SetTitle( aTitle );
77  }
78 
79 private:
80  void InitDialog( ) override;
81  virtual void OnEnterKey( wxCommandEvent& aEvent ) override;
82  virtual void OnOkClick( wxCommandEvent& aEvent ) override;
83  virtual void OnCancelClick( wxCommandEvent& aEvent ) override;
84  void TextPropertiesAccept( wxCommandEvent& aEvent );
85 
88  wxTextCtrl* m_textLabel;
89 };
90 
91 
92 
93 /* Edit the properties of the text (Label, Global label, graphic text).. )
94  * pointed by "aTextStruct"
95  */
97 {
98  if( aTextItem == NULL )
99  return;
100 
101  DIALOG_LABEL_EDITOR dialog( this, aTextItem );
102 
103  dialog.ShowModal();
104 }
105 
106 
108  DIALOG_LABEL_EDITOR_BASE( aParent )
109 {
110  m_Parent = aParent;
111  m_CurrentText = aTextItem;
112  InitDialog();
113 
114  // Conservative limits 0.0 to 10.0 inches
115  const int minSize = 0; // a value like 0.01 is better, but if > 0, creates
116  // annoying issues when trying to enter a value starting by 0 or .0
117  const int maxSize = 10 * 1000 * IU_PER_MILS;
118 
119  wxFloatingPointValidator<double> textSizeValidator( NULL, wxNUM_VAL_NO_TRAILING_ZEROES );
120  textSizeValidator.SetPrecision( 4 );
121  textSizeValidator.SetRange( To_User_Unit( g_UserUnit, minSize ),
122  To_User_Unit( g_UserUnit, maxSize ) );
123 
124  m_TextSize->SetValidator( textSizeValidator );
125 
126  // Now all widgets have the size fixed, call FinishDialogSettings
128 }
129 
130 
132 {
133  wxString msg;
134  bool multiLine = false;
135 
137  {
139  m_textLabelSingleLine->Show( false );
140  multiLine = true;
141  }
142  else
143  {
145  m_textLabelMultiLine->Show( false );
146  wxTextValidator* validator = (wxTextValidator*) m_textLabel->GetValidator();
147  wxArrayString excludes;
148 
149  // Add invalid label characters to this list.
150  excludes.Add( wxT( " " ) );
151  validator->SetExcludes( excludes );
152  }
153 
154  m_textLabel->SetValue( m_CurrentText->GetText() );
155  m_textLabel->SetFocus();
156 
157  switch( m_CurrentText->Type() )
158  {
159  case SCH_GLOBAL_LABEL_T:
160  SetTitle( _( "Global Label Properties" ) );
161  break;
162 
164  SetTitle( _( "Hierarchical Label Properties" ) );
165  break;
166 
167  case SCH_LABEL_T:
168  SetTitle( _( "Label Properties" ) );
169  break;
170 
171  case SCH_SHEET_PIN_T:
172  SetTitle( _( "Hierarchical Sheet Pin Properties." ) );
173  break;
174 
175  default:
176  SetTitle( _( "Text Properties" ) );
177  break;
178  }
179 
180  const int MINTEXTWIDTH = 40; // M's are big characters, a few establish a lot of width
181 
182  int max_len = 0;
183 
184  if ( !multiLine )
185  {
186  max_len = m_CurrentText->GetText().Length();
187  }
188  else
189  {
190  // calculate the length of the biggest line
191  // we cannot use the length of the entire text that has no meaning
192  int curr_len = MINTEXTWIDTH;
193  int imax = m_CurrentText->GetText().Length();
194 
195  for( int count = 0; count < imax; count++ )
196  {
197  if( m_CurrentText->GetText()[count] == '\n' ||
198  m_CurrentText->GetText()[count] == '\r' ) // new line
199  {
200  curr_len = 0;
201  }
202  else
203  {
204  curr_len++;
205 
206  if ( max_len < curr_len )
207  max_len = curr_len;
208  }
209  }
210  }
211 
212  if( max_len < MINTEXTWIDTH )
213  max_len = MINTEXTWIDTH;
214 
215  wxString textWidth;
216  textWidth.Append( 'M', MINTEXTWIDTH );
217  EnsureTextCtrlWidth( m_textLabel, &textWidth );
218 
219  // Set text options:
220  m_TextOrient->SetSelection( m_CurrentText->GetLabelSpinStyle() );
221  m_TextShape->SetSelection( m_CurrentText->GetShape() );
222 
223  int style = 0;
224 
225  if( m_CurrentText->IsItalic() )
226  style = 1;
227 
228  if( m_CurrentText->IsBold() )
229  style += 2;
230 
231  m_TextStyle->SetSelection( style );
232 
233  wxString units = ReturnUnitSymbol( g_UserUnit, wxT( "(%s)" ) );
234  msg.Printf( _( "H%s x W%s" ), GetChars( units ), GetChars( units ) );
235  m_staticSizeUnits->SetLabel( msg );
236 
238  m_TextSize->SetValue( msg );
239 
242  {
243  m_TextShape->Show( false );
244  }
245 
246  m_sdbSizer1OK->SetDefault();
247 }
248 
249 
254 void DIALOG_LABEL_EDITOR::OnEnterKey( wxCommandEvent& aEvent )
255 {
256  TextPropertiesAccept( aEvent );
257 }
258 
259 
264 void DIALOG_LABEL_EDITOR::OnOkClick( wxCommandEvent& aEvent )
265 {
266  TextPropertiesAccept( aEvent );
267 }
268 
269 
274 void DIALOG_LABEL_EDITOR::OnCancelClick( wxCommandEvent& aEvent )
275 {
277  EndModal( wxID_CANCEL );
278 }
279 
280 
281 void DIALOG_LABEL_EDITOR::TextPropertiesAccept( wxCommandEvent& aEvent )
282 {
283  wxString text;
284  int value;
285 
286  /* save old text in undo list if not already in edit */
287  /* or the label to be edited is part of a block */
288  if( m_CurrentText->GetFlags() == 0 ||
291 
293 
294  text = m_textLabel->GetValue();
295 
296  if( !text.IsEmpty() )
297  m_CurrentText->SetText( text );
298  else if( !m_CurrentText->IsNew() )
299  {
300  DisplayError( this, _( "Empty Text!" ) );
301  return;
302  }
303 
304  m_CurrentText->SetLabelSpinStyle( m_TextOrient->GetSelection() );
305  text = m_TextSize->GetValue();
306  value = ValueFromString( g_UserUnit, text );
307  m_CurrentText->SetTextSize( wxSize( value, value ) );
308 
309  if( m_TextShape )
311  m_CurrentText->SetShape( static_cast<PINSHEETLABEL_SHAPE>( m_TextShape->GetSelection() ) );
312 
313  int style = m_TextStyle->GetSelection();
314 
315  m_CurrentText->SetItalic( ( style & 1 ) );
316 
317  if( ( style & 2 ) )
318  {
319  m_CurrentText->SetBold( true );
321  }
322  else
323  {
324  m_CurrentText->SetBold( false );
326  }
327 
328  m_Parent->OnModify();
329 
330  // Make the text size the new default size ( if it is a new text ):
331  if( m_CurrentText->IsNew() )
333 
336  EndModal( wxID_OK );
337 }
PINSHEETLABEL_SHAPE GetShape() const
Definition: sch_text.h:121
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
void SetTitle(const wxString &aTitle) override
void SetShape(PINSHEETLABEL_SHAPE aShape)
Definition: sch_text.h:123
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:269
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
virtual void OnOkClick(wxCommandEvent &aEvent) override
bool IsMultilineAllowed() const
Definition: eda_text.h:188
bool IsItalic() const
Definition: eda_text.h:170
void SaveCopyInUndoList(SCH_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0))
Function SaveCopyInUndoList.
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:230
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:337
void OnModify()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: schframe.cpp:771
std::string m_hash_key
Definition: dialog_shim.h:128
wxString ReturnUnitSymbol(EDA_UNITS_T aUnit, const wxString &formatString)
Returns the units symbol.
Definition: base_units.cpp:420
virtual void SetLabelSpinStyle(int aSpinStyle)
Function SetLabelSpinStyle sets a spin or rotation angle, along with specific horizontal and vertical...
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:203
void SetItalic(bool isItalic)
Definition: eda_text.h:169
int GetTextWidth() const
Definition: eda_text.h:218
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:119
void RefreshDrawingRect(const EDA_RECT &aRect, bool aEraseBackground=true)
Function RefreshDrawingRect redraws the contents of aRect in drawing units.
Definition: draw_panel.cpp:305
Schematic editor (Eeschema) main window.
Definition: schframe.h:117
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:347
Class DIALOG_LABEL_EDITOR_BASE.
SCH_EDIT_FRAME * m_Parent
DIALOG_LABEL_EDITOR(SCH_EDIT_FRAME *parent, SCH_TEXT *aTextItem)
bool IsBold() const
Definition: eda_text.h:173
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:530
#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:130
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
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:587
void EditSchematicText(SCH_TEXT *TextStruct)
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: drawtxt.cpp:49
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
BLOCK_STATE_T GetState() const
void InitDialog() override
This file is part of the common libary.
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:90
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:71
Implementation of the label properties dialog.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:148
virtual void SetText(const wxString &aText)
Definition: eda_text.h:141