KiCad PCB EDA Suite
edtxtmod.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-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 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <common.h>
33 #include <class_drawpanel.h>
34 #include <draw_graphic_text.h>
35 #include <trigo.h>
36 #include <pcb_base_frame.h>
37 #include <macros.h>
38 
39 #include <pcbnew.h>
40 #include <pcb_edit_frame.h>
41 #include <footprint_edit_frame.h>
42 
43 #include <class_board.h>
44 #include <class_module.h>
45 #include <class_text_mod.h>
46 #include <class_pcb_text.h>
47 
48 
49 static void Show_MoveTexte_Module( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
50  bool aErase );
51 static void AbortMoveTextModule( EDA_DRAW_PANEL* Panel, wxDC* DC );
52 
53 
54 wxPoint MoveVector; // Move vector for move edge, exported
55  // to dialog_edit mod_text.cpp
56 static wxPoint TextInitialPosition; // Mouse cursor initial position for
57  // undo/abort move command
58 static double TextInitialOrientation; // module text initial orientation for
59  // undo/abort move+rot command+rot
60 
61 
62 /* Add a new graphical text to the active module (footprint)
63  * Note there always are 2 mandatory texts: reference and value.
64  * New texts have the member TEXTE_MODULE.GetType() set to TEXT_is_DIVERS
65  */
67 {
68  TEXTE_MODULE* text = new TEXTE_MODULE( aModule );
69 
70  text->SetFlags( IS_NEW );
71 
72  if( LSET::AllTechMask().test( GetActiveLayer() ) ) // i.e. a possible layer for a text
73  text->SetLayer( GetActiveLayer() );
74 
75  InstallTextOptionsFrame( text, NULL );
76 
77  if( text->GetText().IsEmpty() )
78  {
79  delete text;
80  return NULL;
81  }
82 
83  // Add the new text object to the beginning of the footprint draw list.
84  if( aModule )
85  aModule->GraphicalItemsList().PushFront( text );
86 
87  text->ClearFlags();
88 
89  if( aDC )
90  text->Draw( m_canvas, aDC, GR_OR );
91 
92  SetMsgPanel( text );
93 
94  return text;
95 }
96 
97 
99 {
100  if( Text == NULL )
101  return;
102 
103  MODULE* module = (MODULE*) Text->GetParent();
104 
105  if( module && module->GetFlags() == 0 && Text->GetFlags() == 0 ) // prepare undo command
106  {
107  if( IsType( FRAME_PCB ) )
108  SaveCopyInUndoList( module, UR_CHANGED );
109  }
110 
111  // we expect MoveVector to be (0,0) if there is no move in progress
112  Text->Draw( m_canvas, DC, GR_XOR, MoveVector );
113 
114  Text->SetTextAngle( Text->GetTextAngle() + 900 );
115 
116  Text->Draw( m_canvas, DC, GR_XOR, MoveVector );
117  SetMsgPanel( Text );
118 
119  if( module )
120  module->SetLastEditTime();
121 
122  OnModify();
123 }
124 
125 
127 {
128  MODULE* module;
129 
130  if( aText == NULL )
131  return;
132 
133  module = static_cast<MODULE*>( aText->GetParent() );
134 
135  if( aText->GetType() == TEXTE_MODULE::TEXT_is_DIVERS )
136  {
137  if( module && module->GetFlags() == 0 && aText->GetFlags() == 0 && IsType( FRAME_PCB ) )
138  SaveCopyInUndoList( module, UR_CHANGED );
139 
141  aText->DeleteStructure();
142  OnModify();
143  module->SetLastEditTime();
144  }
145 }
146 
147 
153 static void AbortMoveTextModule( EDA_DRAW_PANEL* Panel, wxDC* DC )
154 {
155  BASE_SCREEN* screen = Panel->GetScreen();
156  TEXTE_MODULE* Text = static_cast<TEXTE_MODULE*>( screen->GetCurItem() );
157  MODULE* Module;
158 
159  Panel->SetMouseCapture( NULL, NULL );
160 
161  if( Text == NULL )
162  return;
163 
164  Module = static_cast<MODULE*>( Text->GetParent() );
165 
166  Text->DrawUmbilical( Panel, DC, GR_XOR, -MoveVector );
167  Text->Draw( Panel, DC, GR_XOR, MoveVector );
168 
169  // If the text was moved (the move does not change internal data)
170  // it could be rotated while moving. So set old value for orientation
171  if( Text->IsMoving() )
173 
174  // Redraw the text
175  Panel->RefreshDrawingRect( Text->GetBoundingBox() );
176 
177  // leave it at (0,0) so we can use it Rotate when not moving.
178  MoveVector.x = MoveVector.y = 0;
179 
180  Text->ClearFlags();
181  Module->ClearFlags();
182 
183  screen->SetCurItem( NULL );
184 }
185 
186 
188 {
189  if( Text == NULL )
190  return;
191 
192  MODULE *Module = static_cast<MODULE*>( Text->GetParent() );
193 
194  Text->SetFlags( IS_MOVED );
195  Module->SetFlags( IN_EDIT );
196 
197  MoveVector.x = MoveVector.y = 0;
198 
199  TextInitialPosition = Text->GetTextPos();
201 
202  // Center cursor on initial position of text
203  SetCrossHairPosition( TextInitialPosition );
205 
206  SetMsgPanel( Text );
207  SetCurItem( Text );
209  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
210 }
211 
212 
214 {
215  if( Text != NULL )
216  {
218  Text->DrawUmbilical( m_canvas, DC, GR_XOR, -MoveVector );
219 
220  // Update the coordinates for anchor.
221  MODULE* Module = static_cast<MODULE*>( Text->GetParent() );
222 
223  if( Module )
224  {
225  // Prepare undo command (a rotation can be made while moving)
226  double tmp = Text->GetTextAngle();
228 
229  if( IsType( FRAME_PCB ) )
230  SaveCopyInUndoList( Module, UR_CHANGED );
231  else
232  SaveCopyInUndoList( Module, UR_CHANGED );
233 
234  Text->SetTextAngle( tmp );
235 
236  // Set the new position for text.
237  Text->SetTextPos( GetCrossHairPosition() );
238  wxPoint textRelPos = Text->GetTextPos() - Module->GetPosition();
239  RotatePoint( &textRelPos, -Module->GetOrientation() );
240  Text->SetPos0( textRelPos );
241  Text->ClearFlags();
242  Module->ClearFlags();
243  Module->SetLastEditTime();
244  OnModify();
245 
246  // Redraw text.
248  }
249  else
250  {
251  Text->SetTextPos( GetCrossHairPosition() );
252  }
253  }
254 
255  // leave it at (0,0) so we can use it Rotate when not moving.
256  MoveVector.x = MoveVector.y = 0;
257 
258  m_canvas->SetMouseCapture( NULL, NULL );
259 }
260 
261 
262 static void Show_MoveTexte_Module( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
263  bool aErase )
264 {
265  BASE_SCREEN* screen = aPanel->GetScreen();
266  TEXTE_MODULE* Text = static_cast<TEXTE_MODULE*>( screen->GetCurItem() );
267 
268  if( Text == NULL )
269  return;
270 
271  // Erase umbilical and text if necessary
272  if( aErase )
273  {
274  Text->DrawUmbilical( aPanel, aDC, GR_XOR, -MoveVector );
275  Text->Draw( aPanel, aDC, GR_XOR, MoveVector );
276  }
277 
278  MoveVector = TextInitialPosition - aPanel->GetParent()->GetCrossHairPosition();
279 
280  // Draw umbilical if text moved
281  if( MoveVector.x || MoveVector.y )
282  Text->DrawUmbilical( aPanel, aDC, GR_XOR, -MoveVector );
283 
284  // Redraw text
285  Text->Draw( aPanel, aDC, GR_XOR, MoveVector );
286 }
287 
288 
289 void PCB_BASE_FRAME::ResetTextSize( BOARD_ITEM* aItem, wxDC* aDC )
290 {
291  wxSize newSize = GetDesignSettings().GetTextSize( aItem->GetLayer() );
292  int newThickness = GetDesignSettings().GetTextThickness( aItem->GetLayer() );
293  bool newItalic = GetDesignSettings().GetTextItalic( aItem->GetLayer() );
294 
295  if( aItem->Type() == PCB_TEXT_T )
296  {
297  TEXTE_PCB* text = static_cast<TEXTE_PCB*>( aItem );
298 
299  // Exit if there's nothing to do
300  if( text->GetTextSize() == newSize && text->GetThickness() == newThickness )
301  return;
302 
304  text->SetTextSize( newSize );
305  text->SetThickness( newThickness );
306  text->SetItalic( newItalic );
307  }
308 
309  else if( aItem->Type() == PCB_MODULE_TEXT_T )
310  {
311  TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( aItem );
312 
313  // Exit if there's nothing to do
314  if( text->GetTextSize() == newSize && text->GetThickness() == newThickness )
315  return;
316 
318  text->SetTextSize( newSize );
319  text->SetThickness( newThickness );
320  text->SetItalic( newItalic );
321  }
322  else
323  return;
324 
325  if( aDC )
326  m_canvas->Refresh();
327 
328  OnModify();
329 }
virtual BASE_SCREEN * GetScreen()=0
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
BOARD_ITEM_CONTAINER * GetParent() const
void SetCurItem(EDA_ITEM *aItem)
Function SetCurItem sets the currently selected object, m_CurrentItem.
Definition: base_screen.h:231
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:258
bool IsMoving() const
Definition: base_struct.h:221
#define IN_EDIT
Item currently edited.
Definition: base_struct.h:112
TEXTE_PCB class definition.
bool GetTextItalic(PCB_LAYER_ID aLayer) const
const wxPoint & GetTextPos() const
Definition: eda_text.h:237
BOARD_DESIGN_SETTINGS & GetDesignSettings() const override
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
EDA_ITEM * GetCurItem() const
Definition: base_screen.h:233
void PushFront(T *aNewElement)
Function PushFront puts aNewElement at front of list sequence.
Definition: dlist.h:240
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
TEXT_TYPE GetType() const
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
Class BOARD to handle a board.
void SetItalic(bool isItalic)
Definition: eda_text.h:182
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:236
void SetCurItem(BOARD_ITEM *aItem, bool aDisplayInfo=true)
Function SetCurItem sets the currently selected item and displays it in the MsgPanel.
void DeleteStructure()
Function DeleteStructure deletes this object after UnLink()ing it from its owner if it has one...
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
virtual void RefreshDrawingRect(const EDA_RECT &aRect, bool aEraseBackground=true)
Function RefreshDrawingRect redraws the contents of aRect in drawing units.
int GetTextThickness(PCB_LAYER_ID aLayer) const
Function GetTextThickness Returns the default text thickness from the layer class for the given layer...
wxPoint MoveVector
Definition: edtxtmod.cpp:54
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:227
TEXTE_MODULE * CreateTextModule(MODULE *aModule, wxDC *aDC)
Creates a new text for the footprint.
Definition: edtxtmod.cpp:66
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
void RotateTextModule(TEXTE_MODULE *Text, wxDC *DC)
Definition: edtxtmod.cpp:98
virtual void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
static LSET AllTechMask()
Function AllTechMask returns a mask holding all technical layers (no CU layer) on both side...
Definition: lset.cpp:743
void InstallTextOptionsFrame(BOARD_ITEM *aText, wxDC *aDC)
Routine for main window class to launch text properties dialog.
Definition of class FOOTPRINT_EDIT_FRAME.
wxSize GetTextSize(PCB_LAYER_ID aLayer) const
Function GetTextSize Returns the default text size from the layer class for the given layer...
double GetTextAngle() const
Definition: eda_text.h:177
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:167
This file contains miscellaneous commonly used macros and functions.
Classes used in Pcbnew, CvPcb and GerbView.
#define IS_NEW
New item, just created.
Definition: base_struct.h:114
virtual EDA_DRAW_FRAME * GetParent() const =0
static void AbortMoveTextModule(EDA_DRAW_PANEL *Panel, wxDC *DC)
Abort text move in progress.
Definition: edtxtmod.cpp:153
void ResetTextSize(BOARD_ITEM *aItem, wxDC *aDC)
Function ResetTextSize resets given field text size and width to current settings in Preferences->Dim...
Definition: edtxtmod.cpp:289
static wxPoint TextInitialPosition
Definition: edtxtmod.cpp:56
Footprint text class description.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:256
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
virtual void OnModify() override
Must be called after a footprint change in order to set the "modify" flag of the current screen and p...
double GetOrientation() const
Definition: class_module.h:189
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:76
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
virtual void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
void SetLastEditTime(timestamp_t aTime)
Definition: class_module.h:314
virtual void CallMouseCapture(wxDC *aDC, const wxPoint &aPosition, bool aErase)
Function CallMouseCapture calls the mouse capture callback.
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0)) override
Function SaveCopyInUndoList Creates a new entry in undo list of commands.
Definition: undo_redo.cpp:202
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, GR_DRAWMODE aDrawMode, const wxPoint &aOffset=ZeroOffset) override
Function Draw Draw the text according to the footprint pos and orient.
void SetPos0(const wxPoint &aPos)
void PlaceTexteModule(TEXTE_MODULE *Text, wxDC *DC)
Definition: edtxtmod.cpp:213
Definition: gr_basic.h:38
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void DeleteTextModule(TEXTE_MODULE *Text)
Definition: edtxtmod.cpp:126
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:123
static void Show_MoveTexte_Module(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: edtxtmod.cpp:262
void DrawUmbilical(EDA_DRAW_PANEL *aPanel, wxDC *aDC, GR_DRAWMODE aDrawMode, const wxPoint &aOffset=ZeroOffset)
Function DrawUmbilical draws a line from the TEXTE_MODULE origin to parent MODULE origin...
The common library.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:257
DLIST< BOARD_ITEM > & GraphicalItemsList()
Definition: class_module.h:166
bool IsType(FRAME_T aType) const
void StartMoveTexteModule(TEXTE_MODULE *Text, wxDC *DC)
Definition: edtxtmod.cpp:187
void SetTextAngle(double aAngle)
Module description (excepted pads)
const wxSize & GetTextSize() const
Definition: eda_text.h:228
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL)
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Set the screen cross hair position to aPosition in logical (drawing) units.
static double TextInitialOrientation
Definition: edtxtmod.cpp:58
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Return the current cross hair position in logical (drawing) coordinates.
const wxPoint GetPosition() const override
Definition: class_module.h:184
virtual PCB_LAYER_ID GetActiveLayer() const
Function GetActiveLayer returns the active layer.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:161
#define IS_MOVED
Item being moved.
Definition: base_struct.h:113