KiCad PCB EDA Suite
symbdraw.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) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2009-2017 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2017 KiCad Developers, see change_log.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 <gr_basic.h>
33 #include <class_drawpanel.h>
34 #include <confirm.h>
35 #include <class_sch_screen.h>
36 #include <base_units.h>
37 #include <msgpanel.h>
38 
39 #include <eeschema_id.h>
40 #include <general.h>
41 #include <libeditframe.h>
42 #include <class_libentry.h>
43 #include <lib_arc.h>
44 #include <lib_circle.h>
45 #include <lib_polyline.h>
46 #include <lib_rectangle.h>
47 #include <lib_text.h>
48 
50 
51 
52 static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
53  bool aErase );
54 static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
55  bool aErase );
56 
57 
58 void LIB_EDIT_FRAME::EditGraphicSymbol( wxDC* DC, LIB_ITEM* DrawItem )
59 {
60  if( DrawItem == NULL )
61  return;
62 
63  LIB_PART* component = DrawItem->GetParent();
64 
65  DIALOG_LIB_EDIT_DRAW_ITEM dialog( this, DrawItem->GetTypeName() );
66 
68 
69  wxString val = StringFromValue( g_UserUnit, DrawItem->GetWidth() );
70  dialog.SetWidth( val );
71  dialog.SetApplyToAllUnits( DrawItem->GetUnit() == 0 );
72  dialog.EnableApplyToAllUnits( component && component->GetUnitCount() > 1 );
73  dialog.SetApplyToAllConversions( DrawItem->GetConvert() == 0 );
74  dialog.EnableApplyToAllConversions( component && component->HasConversion() );
75  dialog.SetFillStyle( DrawItem->GetFillMode() );
76  dialog.EnableFillStyle( DrawItem->IsFillable() );
77 
78  if( dialog.ShowModal() == wxID_CANCEL )
79  return;
80 
81  // Init default values (used to create a new draw item)
82  val = dialog.GetWidth();
84  m_drawSpecificConvert = !dialog.GetApplyToAllConversions();
85  m_drawSpecificUnit = !dialog.GetApplyToAllUnits();
86 
87 #if 0
88  /* TODO: see if m_drawFillStyle must retain the last fill option or not.
89  * if the last is Filled, having next new graphic items created
90  * with filled body is often bad.
91  * currently m_drawFillStyle is left with the default value (not filled)
92  */
93  if( DrawItem->IsFillable() )
94  m_drawFillStyle = (FILL_T) dialog.GetFillStyle();
95 #endif
96 
97  // Save copy for undo if not in edit (edit command already handle the save copy)
98  if( !DrawItem->InEditMode() )
99  SaveCopyInUndoList( DrawItem->GetParent() );
100 
101  if( m_drawSpecificUnit )
102  DrawItem->SetUnit( GetUnit() );
103  else
104  DrawItem->SetUnit( 0 );
105 
107  DrawItem->SetConvert( GetConvert() );
108  else
109  DrawItem->SetConvert( 0 );
110 
111  if( DrawItem->IsFillable() )
112  DrawItem->SetFillMode( (FILL_T) dialog.GetFillStyle() );
113 
114  DrawItem->SetWidth( m_drawLineWidth );
115 
116  OnModify( );
117 
118  MSG_PANEL_ITEMS items;
119  DrawItem->GetMsgPanelInfo( items );
120  SetMsgPanel( items );
121  m_canvas->Refresh();
122 }
123 
124 
125 static void AbortSymbolTraceOn( EDA_DRAW_PANEL* Panel, wxDC* DC )
126 {
127  LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) Panel->GetParent();
128  LIB_ITEM* item = parent->GetDrawItem();
129 
130  if( item == NULL )
131  return;
132 
133  bool newItem = item->IsNew();
134  item->EndEdit( parent->GetCrossHairPosition( true ), true );
135 
136  if( newItem )
137  {
138  delete item;
139  }
140  else
141  parent->RestoreComponent();
142 
143  parent->SetDrawItem( NULL );
144  Panel->Refresh();
145 }
146 
147 
149 {
151  wxPoint drawPos = GetCrossHairPosition( true );
152 
153  // no temp copy -> the current version of component will be used for Undo
154  // This is normal when adding new items to the current component
156 
157  switch( GetToolId() )
158  {
160  m_drawItem = new LIB_ARC( LibEntry );
161  break;
162 
164  m_drawItem = new LIB_CIRCLE( LibEntry );
165  break;
166 
168  m_drawItem = new LIB_RECTANGLE( LibEntry );
169  break;
170 
172  m_drawItem = new LIB_POLYLINE( LibEntry );
173  break;
174 
176  {
177  LIB_TEXT* text = new LIB_TEXT( LibEntry );
178  text->SetTextSize( wxSize( m_textSize, m_textSize ) );
180 
181  // Enter the graphic text info
183  EditSymbolText( NULL, text );
184 
185  m_canvas->SetIgnoreMouseEvents( false );
187 
188  if( text->GetText().IsEmpty() )
189  {
190  delete text;
191  m_drawItem = NULL;
192  }
193  else
194  {
195  m_drawItem = text;
196  }
197  }
198  break;
199 
200  default:
201  DisplayError( this, wxT( "LIB_EDIT_FRAME::CreateGraphicItem() error" ) );
202  return NULL;
203  }
204 
205  if( m_drawItem )
206  {
207  m_drawItem->BeginEdit( IS_NEW, drawPos );
208 
209  // Don't set line parameters for text objects.
210  if( m_drawItem->Type() != LIB_TEXT_T )
211  {
214  }
215 
216  if( m_drawSpecificUnit )
218 
221 
222  // Draw initial symbol:
223  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
224  }
225  else
226  {
228  return NULL;
229  }
230 
232  m_canvas->SetIgnoreMouseEvents( false );
233 
234  return m_drawItem;
235 }
236 
237 
239 {
240  if( m_drawItem == NULL )
241  return;
242 
243  wxPoint pos = GetCrossHairPosition( true );
244 
245  if( m_drawItem->ContinueEdit( pos ) )
246  {
247  m_drawItem->Draw( m_canvas, DC, pos, COLOR4D::UNSPECIFIED, g_XorMode, NULL,
249  return;
250  }
251 
252  EndDrawGraphicItem( DC );
253 }
254 
255 
256 /*
257  * Redraw the graphic shape while moving
258  */
259 static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
260  bool aErase )
261 {
262  LIB_ITEM* item;
263 
264  item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
265 
266  if( item == NULL )
267  return;
268 
269  item->SetEraseLastDrawItem( aErase );
270 
271  // if item is the reference field, we must add the current unit id
272  if( item->Type() == LIB_FIELD_T )
273  {
274  int unit = ((LIB_EDIT_FRAME*)aPanel->GetParent())->GetUnit();
275  wxString text = ((LIB_FIELD*)item)->GetFullText( unit );
276 
277  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
278  COLOR4D::UNSPECIFIED, g_XorMode, &text,
280  }
281  else
282  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
283  COLOR4D::UNSPECIFIED, g_XorMode, NULL,
285 }
286 
287 
289 {
290  if( m_drawItem == NULL )
291  return;
292 
293  SetCursor( wxCURSOR_HAND );
294 
296 
297  // For fields only, move the anchor point of the field
298  // to the cursor position to allow user to see the text justification
299  if( m_drawItem->Type() == LIB_FIELD_T )
301  else
303 
305  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
306 }
307 
308 
310 {
311  if( m_drawItem == NULL )
312  return;
313 
317  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
318 }
319 
320 
322 static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
323  bool aErase )
324 {
325  LIB_ITEM* item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
326 
327  if( item == NULL )
328  return;
329 
330  item->SetEraseLastDrawItem( aErase );
331  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
332  COLOR4D::UNSPECIFIED, g_XorMode, NULL, DefaultTransform );
333 }
334 
335 
337 {
338  if( LIB_PART* part = GetCurPart() )
339  {
340  if( !m_drawItem )
341  return;
342 
343  if( GetToolId() != ID_NO_TOOL_SELECTED )
344  SetCursor( wxCURSOR_PENCIL );
345  else
346  SetCursor( (wxStockCursor) m_canvas->GetDefaultCursor() );
347 
348  if( GetTempCopyComponent() ) // used when editing an existing item
350  else
351  {
352  // When creating a new item, there is still no change for the
353  // current component. So save it.
354  SaveCopyInUndoList( part );
355  }
356 
357  if( m_drawItem->IsNew() )
358  part->AddDrawItem( m_drawItem );
359 
361 
362  m_drawItem = NULL;
363 
364  OnModify();
365 
366  m_canvas->SetMouseCapture( NULL, NULL );
367  m_canvas->Refresh();
368  }
369 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:156
virtual wxPoint GetPosition() const =0
Function GetPosition returns the current draw object position.
GR_DRAWMODE g_XorMode
Definition: gr_basic.cpp:51
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
static double m_current_text_angle
Current text angle setting.
Definition: libeditframe.h:121
static void RedrawWhileMovingCursor(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: symbdraw.cpp:259
FILL_T GetFillMode() const
LIB_ITEM * GetDrawItem()
Definition: libeditframe.h:429
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
static int m_convert
Definition: libeditframe.h:108
void RestoreComponent()
Restore the current edited component from its temporary copy.
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
void SetEraseLastDrawItem(bool aErase=true)
int GetUnit() const
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
static LIB_ITEM * m_drawItem
Definition: libeditframe.h:100
Class LIB_TEXT defines a component library graphical text item.
Definition: lib_text.h:45
void ClearTempCopyComponent()
Delete temporary copy of the current component and clear pointer.
virtual void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aPosition=wxPoint(0, 0))
Begin an editing a component library draw item in aEditMode at aPosition.
wxString ReturnUnitSymbol(EDA_UNITS_T aUnit, const wxString &formatString)
Returns the units symbol.
Definition: base_units.cpp:420
virtual void SetWidth(int aWidth)=0
Function SetWidth sets the width of the draw item to aWidth.
Subclass of DIALOG_LIB_EDIT_DRAW_ITEM_BASE, which is generated by wxFormBuilder.
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
Class LIB_FIELD is used in symbol libraries.
Definition: lib_field.h:60
virtual void EndEdit(const wxPoint &aPosition, bool aAbort=false)
End an object editing action.
void EndMouseCapture(int aId=-1, int aCursorId=-1, const wxString &aTitle=wxEmptyString, bool aCallEndFunc=true)
Function EndMouseCapture ends mouse a capture.
bool InEditMode() const
Return the draw item editing mode status.
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen...
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 LIB_ITEM is the base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:67
#define IS_NEW
New item, just created.
Definition: base_struct.h:127
LIB_PART * GetParent() const
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
Definition: libeditframe.h:158
void SetDrawItem(LIB_ITEM *drawItem)
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Function SetMsgPanel clears the message panel and populates it with the contents of aList...
Definition: draw_frame.cpp:784
virtual bool ContinueEdit(const wxPoint aPosition)
Continue an edit in progress at aPosition.
wxString GetTypeName()
Definitions for the Eeschema program SCH_SCREEN class.
Class LIB_ITEM definition.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
void CallMouseCapture(wxDC *aDC, const wxPoint &aPosition, bool aErase)
Function CallMouseCapture calls the mouse capture callback.
void TempCopyComponent()
Create a temporary copy of the current edited component.
void GraphicItemBeginDraw(wxDC *DC)
Definition: symbdraw.cpp:238
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
Class LIB_PART defines a library part object.
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:174
static int m_textSize
The current text size setting.
Definition: libeditframe.h:118
static void AbortSymbolTraceOn(EDA_DRAW_PANEL *Panel, wxDC *DC)
Definition: symbdraw.cpp:125
void StartModifyDrawSymbol(wxDC *DC)
Definition: symbdraw.cpp:309
void SetFillMode(FILL_T aFillMode)
void SetWidthUnits(const wxString &units)
LIB_PART * GetTempCopyComponent()
Definition: libeditframe.h:459
void SetConvert(int aConvert)
int GetUnitCount() const
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
void EditSymbolText(wxDC *DC, LIB_ITEM *DrawItem)
Dialog to edit library component graphic items.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
virtual int GetWidth() const =0
Function GetWidth return the width of the draw item.
void SetUnit(int aUnit)
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
The symbol library editor main window.
Definition: libeditframe.h:58
LIB_ITEM * CreateGraphicItem(LIB_PART *LibEntry, wxDC *DC)
Definition: symbdraw.cpp:148
TRANSFORM DefaultTransform
Definition: eeschema.cpp:58
virtual void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform)
Draw an item.
static int m_unit
Definition: libeditframe.h:104
int GetToolId() const
Definition: draw_frame.h:471
static FILL_T m_drawFillStyle
The current draw or edit graphic item fill style.
Definition: libeditframe.h:94
void EndDrawGraphicItem(wxDC *DC)
Definition: symbdraw.cpp:336
void SetIgnoreMouseEvents(bool aIgnore)
static void SymbolDisplayDraw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Manage mouse events when creating new graphic object or modifying an graphic object.
Definition: symbdraw.cpp:322
void EditGraphicSymbol(wxDC *DC, LIB_ITEM *DrawItem)
Definition: symbdraw.cpp:58
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:128
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
bool IsFillable() const
Check if draw object can be filled.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:70
static int m_drawLineWidth
Default line width for drawing or editing graphic items.
Definition: libeditframe.h:97
bool m_drawSpecificConvert
Convert of the item currently being drawn.
Definition: libeditframe.h:69
void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
bool m_drawSpecificUnit
Specify which component parts the current draw item applies to.
Definition: libeditframe.h:77
Message panel definition file.
int GetDefaultCursor() const
Function GetDefaultCursor.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
void StartMoveDrawSymbol(wxDC *DC)
Definition: symbdraw.cpp:288
Definition of class LIB_EDIT_FRAME.
int GetConvert() const
#define IS_MOVED
Item being moved.
Definition: base_struct.h:126
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy)
Create a copy of the current component, and save it in the undo list.