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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2018 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 <fctsys.h>
26 #include <gr_basic.h>
27 #include <base_struct.h>
28 #include <draw_graphic_text.h>
29 #include <sch_draw_panel.h>
30 #include <confirm.h>
31 #include <sch_edit_frame.h>
32 #include <kicad_device_context.h>
33 #include <tool/tool_manager.h>
34 #include <tools/ee_actions.h>
35 #include <general.h>
36 #include <sch_text.h>
37 #include <eeschema_id.h>
38 #include <sch_view.h>
39 
40 
42 static int lastTextOrientation = 0;
43 static bool lastTextBold = false;
44 static bool lastTextItalic = false;
45 
46 
48 {
49  SCH_TEXT* textItem = NULL;
50 
51  switch( aType )
52  {
53  case LAYER_NOTES:
54  textItem = new SCH_TEXT( GetCrossHairPosition() );
55  break;
56 
57  case LAYER_LOCLABEL:
58  textItem = new SCH_LABEL( GetCrossHairPosition() );
59  break;
60 
61  case LAYER_HIERLABEL:
62  textItem = new SCH_HIERLABEL( GetCrossHairPosition() );
63  textItem->SetShape( lastGlobalLabelShape );
64  break;
65 
66  case LAYER_GLOBLABEL:
67  textItem = new SCH_GLOBALLABEL( GetCrossHairPosition() );
68  textItem->SetShape( lastGlobalLabelShape );
69  break;
70 
71  default:
72  DisplayError( this, wxT( "SCH_EDIT_FRAME::CreateNewText() Internal error" ) );
73  return NULL;
74  }
75 
76  textItem->SetBold( lastTextBold );
77  textItem->SetItalic( lastTextItalic );
79  textItem->SetTextSize( wxSize( GetDefaultTextSize(), GetDefaultTextSize() ) );
80  textItem->SetFlags( IS_NEW | IS_MOVED );
81 
82  EditSchematicText( textItem );
83 
84  if( textItem->GetText().IsEmpty() )
85  {
86  delete textItem;
87  return NULL;
88  }
89 
90  lastTextBold = textItem->IsBold();
91  lastTextItalic = textItem->IsItalic();
93 
94  if( textItem->Type() == SCH_GLOBAL_LABEL_T || textItem->Type() == SCH_HIER_LABEL_T )
95  lastGlobalLabelShape = textItem->GetShape();
96 
97  return textItem;
98 }
99 
100 
101 /*
102  * ConvertTextType changes a text from one type to another. It creates a new text of the
103  * appropriate type and deletes the old one.
104  */
106 {
107  bool selected = aText->IsSelected();
108 
109  wxCHECK_RET( aText->CanIncrementLabel(), "Cannot convert text type." );
110 
111  if( aText->Type() == aType )
112  return;
113 
114  SCH_TEXT* newtext = nullptr;
115  const wxPoint& position = aText->GetPosition();
116  const wxString txt = aText->GetText();
117 
118  switch( aType )
119  {
120  case SCH_LABEL_T: newtext = new SCH_LABEL( position, txt ); break;
121  case SCH_GLOBAL_LABEL_T: newtext = new SCH_GLOBALLABEL( position, txt ); break;
122  case SCH_HIER_LABEL_T: newtext = new SCH_HIERLABEL( position, txt ); break;
123  case SCH_TEXT_T: newtext = new SCH_TEXT( position, txt ); break;
124 
125  default:
126  wxASSERT_MSG( false, wxString::Format( "Invalid text type: %d.", aType ) );
127  return;
128  }
129 
130  // Copy the old text item settings to the new one. Justifications are not copied
131  // because they are not used in labels. Justifications will be set to default value
132  // in the new text item type.
133  //
134  newtext->SetFlags( aText->GetEditFlags() );
135  newtext->SetShape( aText->GetShape() );
136  newtext->SetLabelSpinStyle( aText->GetLabelSpinStyle() );
137  newtext->SetTextSize( aText->GetTextSize() );
138  newtext->SetThickness( aText->GetThickness() );
139  newtext->SetItalic( aText->IsItalic() );
140  newtext->SetBold( aText->IsBold() );
141  newtext->SetIsDangling( aText->IsDangling() );
142 
143  if( selected )
145 
146  if( !aText->IsNew() )
147  {
148  SaveCopyInUndoList( aText, UR_DELETED );
149  SaveCopyInUndoList( newtext, UR_NEW, true );
150 
151  RemoveFromScreen( aText );
152  AddToScreen( newtext );
153  }
154 
155  if( selected )
157 
158  delete aText;
159 
160  OnModify();
161 }
162 
163 
164 /*
165  * Function to increment bus label numbers. Adds aIncrement to labels which end in numbers.
166  */
167 void IncrementLabelMember( wxString& name, int aIncrement )
168 {
169  int ii, nn;
170  long number = 0;
171 
172  ii = name.Len() - 1; nn = 0;
173 
174  if( !wxIsdigit( name.GetChar( ii ) ) )
175  return;
176 
177  while( (ii >= 0) && wxIsdigit( name.GetChar( ii ) ) )
178  {
179  ii--; nn++;
180  }
181 
182  ii++; /* digits are starting at ii position */
183  wxString litt_number = name.Right( nn );
184 
185  if( litt_number.ToLong( &number ) )
186  {
187  number += aIncrement;
188  name.Remove( ii ); name << number;
189  }
190 }
bool IsDangling() const override
Definition: sch_text.h:177
TOOL_MANAGER * m_toolManager
Definition: draw_frame.h:130
bool IsBold() const
Definition: eda_text.h:190
void SetShape(PINSHEETLABEL_SHAPE aShape)
Definition: sch_text.h:123
SCH_TEXT * CreateNewText(int aType)
Definition: edit_label.cpp:47
bool IsSelected() const
Definition: base_struct.h:227
void AddToScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen=nullptr)
Add an item to the screen (and view) aScreen is the screen the item is located on,...
This file is part of the common library.
static int lastTextOrientation
Definition: edit_label.cpp:42
static PINSHEETLABEL_SHAPE lastGlobalLabelShape
Definition: edit_label.cpp:41
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:248
void SetItalic(bool isItalic)
Definition: eda_text.h:186
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:171
virtual const wxString GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:147
void RemoveFromScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen=nullptr)
Remove an item from the screen (and view) aScreen is the screen the item is located on,...
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:231
bool CanIncrementLabel() const override
Definition: sch_text.h:182
static bool lastTextItalic
Definition: edit_label.cpp:44
void SetIsDangling(bool aIsDangling)
Definition: sch_text.h:178
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
bool IsNew() const
Definition: base_struct.h:222
static TOOL_ACTION removeItemFromSel
Definition: ee_actions.h:61
bool IsItalic() const
Definition: eda_text.h:187
#define IS_NEW
New item, just created.
Definition: base_struct.h:114
int GetLabelSpinStyle() const
Definition: sch_text.h:119
a helper to handle the real device context used in KiCad
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:259
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:60
STATUS_FLAGS GetEditFlags() const
Definition: base_struct.h:263
const wxSize & GetTextSize() const
Definition: eda_text.h:232
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.
void ConvertTextType(SCH_TEXT *aText, KICAD_T aType)
Change a text type to another one.
Definition: edit_label.cpp:105
wxPoint GetPosition() const override
Function GetPosition.
Definition: sch_text.h:190
void EditSchematicText(SCH_TEXT *aText)
Launches the "Edit Text/Label" dialog.
PINSHEETLABEL_SHAPE
Definition: sch_text.h:47
const char * name
Definition: DXF_plotter.cpp:61
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 OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
Basic classes for most KiCad items.
static bool lastTextBold
Definition: edit_label.cpp:43
void IncrementLabelMember(wxString &name, int aIncrement)
Definition: edit_label.cpp:167
void SetBold(bool aBold)
Definition: eda_text.h:189
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:244
Implementation of the label properties dialog.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:165
int GetDefaultTextSize()
Default size for text in general.
PINSHEETLABEL_SHAPE GetShape() const
Definition: sch_text.h:121
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Return the current cross hair position in logical (drawing) coordinates.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204
#define IS_MOVED
Item being moved.
Definition: base_struct.h:113