KiCad PCB EDA Suite
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) 2004-2013 KiCad Developers, see change_log.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 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <base_struct.h>
33 #include <drawtxt.h>
34 #include <class_drawpanel.h>
35 #include <confirm.h>
36 #include <schframe.h>
37 #include <kicad_device_context.h>
38 
39 #include <general.h>
40 #include <sch_text.h>
41 #include <eeschema_id.h>
42 
43 
45 static int lastTextOrientation = 0;
46 static bool lastTextBold = false;
47 static bool lastTextItalic = false;
48 
49 
51 {
52  wxCHECK_RET( (aTextItem != NULL) && aTextItem->CanIncrementLabel(),
53  wxT( "Invalid schematic text item." ) );
54 
55  int orient = ( aTextItem->GetLabelSpinStyle() + 1 ) & 3;
56 
57  // Save current text orientation in undo list if is not already in edit.
58  if( aTextItem->GetFlags() == 0 )
59  SaveCopyInUndoList( aTextItem, UR_CHANGED );
60 
61  aTextItem->SetLabelSpinStyle( orient );
62  OnModify();
63 }
64 
65 
66 SCH_TEXT* SCH_EDIT_FRAME::CreateNewText( wxDC* aDC, int aType )
67 {
68  SCH_TEXT* textItem = NULL;
69 
70  SetRepeatItem( NULL );
71 
72  switch( aType )
73  {
74  case LAYER_NOTES:
75  textItem = new SCH_TEXT( GetCrossHairPosition() );
76  break;
77 
78  case LAYER_LOCLABEL:
79  textItem = new SCH_LABEL( GetCrossHairPosition() );
80  break;
81 
82  case LAYER_HIERLABEL:
83  textItem = new SCH_HIERLABEL( GetCrossHairPosition() );
84  textItem->SetShape( lastGlobalLabelShape );
85  break;
86 
87  case LAYER_GLOBLABEL:
88  textItem = new SCH_GLOBALLABEL( GetCrossHairPosition() );
89  textItem->SetShape( lastGlobalLabelShape );
90  break;
91 
92  default:
93  DisplayError( this, wxT( "SCH_EDIT_FRAME::CreateNewText() Internal error" ) );
94  return NULL;
95  }
96 
97  textItem->SetBold( lastTextBold );
98  textItem->SetItalic( lastTextItalic );
100  textItem->SetTextSize( wxSize( GetDefaultTextSize(), GetDefaultTextSize() ) );
101  textItem->SetFlags( IS_NEW | IS_MOVED );
102 
103  EditSchematicText( textItem );
104 
105  if( textItem->GetText().IsEmpty() )
106  {
107  delete textItem;
108  return NULL;
109  }
110 
111  lastTextBold = textItem->IsBold();
112  lastTextItalic = textItem->IsItalic();
114 
115  if( ( textItem->Type() == SCH_GLOBAL_LABEL_T ) ||
116  ( textItem->Type() == SCH_HIERARCHICAL_LABEL_T ) )
117  {
118  lastGlobalLabelShape = textItem->GetShape();
119  }
120 
121  // Prepare display to move the new item
122  textItem->Draw( m_canvas, aDC, wxPoint( 0, 0 ), g_XorMode );
123  PrepareMoveItem( (SCH_ITEM*) textItem, aDC );
124 
125  return textItem;
126 }
127 
128 
129 /*
130  * OnConvertTextType is a command event handler to change a text type to an other one.
131  * The new text, label, hierarchical label, or global label is created from the old text
132  * The old text is deleted.
133  * A tricky case is when the 'old" text is being edited (i.e. moving)
134  * because we must create a new text, and prepare the undo/redo command data for this
135  * change and the current move/edit command
136  */
137 void SCH_EDIT_FRAME::OnConvertTextType( wxCommandEvent& aEvent )
138 {
139  SCH_SCREEN* screen = GetScreen();
140  SCH_TEXT* text = (SCH_TEXT*) screen->GetCurItem();
141 
142  wxCHECK_RET( (text != NULL) && text->CanIncrementLabel(),
143  wxT( "Cannot convert text type." ) );
144 
145  KICAD_T type;
146 
147  switch( aEvent.GetId() )
148  {
150  type = SCH_LABEL_T;
151  break;
152 
154  type = SCH_GLOBAL_LABEL_T;
155  break;
156 
159  break;
160 
162  type = SCH_TEXT_T;
163  break;
164 
165  default:
166  wxFAIL_MSG( wxString::Format( wxT( "Invalid text type command ID %d." ),
167  aEvent.GetId() ) );
168  return;
169  }
170 
171  if( text->Type() == type )
172  return;
173 
174  SCH_TEXT* newtext;
175  const wxPoint &position = text->GetPosition();
176  const wxString &txt = text->GetText();
177 
178  switch( type )
179  {
180  case SCH_LABEL_T:
181  newtext = new SCH_LABEL( position, txt );
182  break;
183 
184  case SCH_GLOBAL_LABEL_T:
185  newtext = new SCH_GLOBALLABEL( position, txt );
186  break;
187 
189  newtext = new SCH_HIERLABEL( position, txt );
190  break;
191 
192  case SCH_TEXT_T:
193  newtext = new SCH_TEXT( position, txt );
194  break;
195 
196  default:
197  newtext = NULL;
198  wxFAIL_MSG( wxString::Format( wxT( "Cannot convert text type to %d" ), type ) );
199  return;
200  }
201 
202  /* Copy the old text item settings to the new one. Justifications are not copied because
203  * they are not used in labels. Justifications will be set to default value in the new
204  * text item type.
205  */
206  newtext->SetFlags( text->GetFlags() );
207  newtext->SetShape( text->GetShape() );
208  newtext->SetLabelSpinStyle( text->GetLabelSpinStyle() );
209  newtext->SetTextSize( text->GetTextSize() );
210  newtext->SetThickness( text->GetThickness() );
211  newtext->SetItalic( text->IsItalic() );
212  newtext->SetBold( text->IsBold() );
213 
214  /* Save the new text in undo list if the old text was not itself a "new created text"
215  * In this case, the old text is already in undo list as a deleted item.
216  * Of course if the old text was a "new created text" the new text will be
217  * put in undo list later, at the end of the current command (if not aborted)
218  */
219 
221  m_canvas->CrossHairOff( &dc ); // Erase schematic cursor
222  text->Draw( m_canvas, &dc, wxPoint( 0, 0 ), g_XorMode );
223 
224  // For an exiting item (i.e. already in list):
225  // replace the existing item by the new text in list
226  for( SCH_ITEM* item = screen->GetDrawItems(); item != NULL; item = item->Next() )
227  {
228  if( item == text )
229  {
230  screen->Remove( text );
231  screen->Append( newtext );
232  break;
233  }
234  }
235 
236  SetRepeatItem( NULL );
237  OnModify();
238  newtext->Draw( m_canvas, &dc, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
239  m_canvas->CrossHairOn( &dc ); // redraw schematic cursor
240 
241  // if the old item is the current schematic item, replace it by the new text:
242  if( screen->GetCurItem() == text )
243  screen->SetCurItem( newtext );
244 
245  if( text->IsNew() )
246  {
247  // if the previous text is new, no undo command to prepare here
248  // just delete this previous text.
249  delete text;
250  return;
251  }
252 
253  // previous text is not new and we replace text by new text.
254  // So this is equivalent to delete text and add newtext
255  // If text if being currently edited (i.e. moved)
256  // we also save the initial copy of text, and prepare undo command for new text modifications.
257  // we must save it as modified text,if it is currently edited, then save as deleted text,
258  // and replace text with newtext
259  PICKED_ITEMS_LIST pickList;
260  ITEM_PICKER picker( text, UR_CHANGED );
261 
262  if( text->GetFlags() )
263  {
264  // text is being edited, save initial text for undo command
265  picker.SetLink( GetUndoItem() );
266  pickList.PushItem( picker );
267 
268  // the owner of undoItem is no more "this", it is now "picker":
269  SetUndoItem( NULL );
270 
271  // save current newtext copy for undo/abort current command
272  SetUndoItem( newtext );
273  }
274 
275  // Prepare undo command for delete old text
276  picker.SetStatus( UR_DELETED );
277  picker.SetLink( NULL );
278  pickList.PushItem( picker );
279 
280  // Prepare undo command for new text
281  picker.SetStatus( UR_NEW );
282  picker.SetItem(newtext);
283  pickList.PushItem( picker );
284 
285  SaveCopyInUndoList( pickList, UR_UNSPECIFIED );
286 }
287 
288 
289 /* Function to increment bus label members numbers,
290  * i.e. when a text is ending with a number, adds
291  * aIncrement to this number
292  */
293 void IncrementLabelMember( wxString& name, int aIncrement )
294 {
295  int ii, nn;
296  long number = 0;
297 
298  ii = name.Len() - 1; nn = 0;
299 
300  if( !isdigit( name.GetChar( ii ) ) )
301  return;
302 
303  while( (ii >= 0) && isdigit( name.GetChar( ii ) ) )
304  {
305  ii--; nn++;
306  }
307 
308  ii++; /* digits are starting at ii position */
309  wxString litt_number = name.Right( nn );
310 
311  if( litt_number.ToLong( &number ) )
312  {
313  number += aIncrement;
314  name.Remove( ii ); name << number;
315  }
316 }
GR_DRAWMODE g_XorMode
Definition: gr_basic.cpp:51
PINSHEETLABEL_SHAPE GetShape() const
Definition: sch_text.h:121
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
void SetShape(PINSHEETLABEL_SHAPE aShape)
Definition: sch_text.h:123
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:255
virtual bool CanIncrementLabel() const override
Definition: sch_text.h:191
virtual void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, const wxPoint &offset, GR_DRAWMODE draw_mode, COLOR4D Color=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
Definition: sch_text.cpp:344
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.
This file is part of the common library.
bool IsNew() const
Definition: base_struct.h:216
static int lastTextOrientation
Definition: edit_label.cpp:45
void OnModify()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: schframe.cpp:761
void SetStatus(UNDO_REDO_T aStatus)
static PINSHEETLABEL_SHAPE lastGlobalLabelShape
Definition: edit_label.cpp:44
virtual void SetLabelSpinStyle(int aSpinStyle)
Function SetLabelSpinStyle sets a spin or rotation angle, along with specific horizontal and vertical...
Definition: sch_text.cpp:277
void OnConvertTextType(wxCommandEvent &aEvent)
Function OnCovertTextType is a command event handler to change a text type to an other one...
Definition: edit_label.cpp:137
void ChangeTextOrient(SCH_TEXT *aTextItem)
Definition: edit_label.cpp:50
void SetItalic(bool isItalic)
Definition: eda_text.h:169
int GetLabelSpinStyle() const
Definition: sch_text.h:119
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
SCH_ITEM * GetUndoItem() const
Definition: schframe.h:1309
void Remove(SCH_ITEM *aItem)
Function Remove removes aItem from the schematic associated with this screen.
Definition: sch_screen.cpp:158
SCH_TEXT * CreateNewText(wxDC *aDC, int aType)
Definition: edit_label.cpp:66
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
static bool lastTextItalic
Definition: edit_label.cpp:47
void SetLink(EDA_ITEM *aItem)
SCH_ITEM * Next() const
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:90
bool IsBold() const
Definition: eda_text.h:173
void PrepareMoveItem(SCH_ITEM *aItem, wxDC *aDC)
Function PrepareMoveItem start moving aItem using the mouse.
Definition: schedit.cpp:784
void SetRepeatItem(SCH_ITEM *aItem)
Function SetRepeatItem clones aItem and owns that clone in this container.
Definition: schframe.cpp:459
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:154
#define IS_NEW
New item, just created.
Definition: base_struct.h:113
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:520
a helper to handle the real device context used in KiCad
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:253
SCH_ITEM * GetCurItem() const
Function GetCurItem returns the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
void EditSchematicText(SCH_TEXT *TextStruct)
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
void SetItem(EDA_ITEM *aItem)
void CrossHairOff(wxDC *DC)
Definition: draw_panel.cpp:253
virtual wxPoint GetPosition() const override
Function GetPosition.
Definition: sch_text.h:200
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
SCH_ITEM * GetDrawItems() const
Function GetDrawItems().
#define GR_DEFAULT_DRAWMODE
Definition: general.h:69
void CrossHairOn(wxDC *DC)
Definition: draw_panel.cpp:260
PINSHEETLABEL_SHAPE
Definition: sch_text.h:46
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void Append(SCH_ITEM *aItem)
#define INSTALL_UNBUFFERED_DC(name, parent)
void SetCurItem(SCH_ITEM *aItem)
Function SetCurItem sets the currently selected object, m_CurrentItem.
This file is part of the common libary.
const wxSize & GetTextSize() const
Definition: eda_text.h:215
void SetUndoItem(const SCH_ITEM *aItem)
Function SetUndoItem clones aItem which can be used to restore the state of the item being edited whe...
Definition: schframe.cpp:579
Basic classes for most KiCad items.
static bool lastTextBold
Definition: edit_label.cpp:46
void IncrementLabelMember(wxString &name, int aIncrement)
Definition: edit_label.cpp:293
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
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Implementation of the label properties dialog.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:148
int GetDefaultTextSize()
Default size for text in general.
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112