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-2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2011 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  if( component )
117  component->GetDrawItemList().sort();
118  OnModify( );
119 
120  MSG_PANEL_ITEMS items;
121  DrawItem->GetMsgPanelInfo( items );
122  SetMsgPanel( items );
123  m_canvas->Refresh();
124 }
125 
126 
127 static void AbortSymbolTraceOn( EDA_DRAW_PANEL* Panel, wxDC* DC )
128 {
129  LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) Panel->GetParent();
130  LIB_ITEM* item = parent->GetDrawItem();
131 
132  if( item == NULL )
133  return;
134 
135  bool newItem = item->IsNew();
136  item->EndEdit( parent->GetCrossHairPosition( true ), true );
137 
138  if( newItem )
139  {
140  delete item;
141  }
142  else
143  parent->RestoreComponent();
144 
145  parent->SetDrawItem( NULL );
146  Panel->Refresh();
147 }
148 
149 
151 {
153  wxPoint drawPos = GetCrossHairPosition( true );
154 
155  // no temp copy -> the current version of component will be used for Undo
156  // This is normal when adding new items to the current component
158 
159  switch( GetToolId() )
160  {
162  m_drawItem = new LIB_ARC( LibEntry );
163  break;
164 
166  m_drawItem = new LIB_CIRCLE( LibEntry );
167  break;
168 
170  m_drawItem = new LIB_RECTANGLE( LibEntry );
171  break;
172 
174  m_drawItem = new LIB_POLYLINE( LibEntry );
175  break;
176 
178  {
179  LIB_TEXT* text = new LIB_TEXT( LibEntry );
180  text->SetTextSize( wxSize( m_textSize, m_textSize ) );
182 
183  // Enter the graphic text info
185  EditSymbolText( NULL, text );
186 
187  m_canvas->SetIgnoreMouseEvents( false );
189 
190  if( text->GetText().IsEmpty() )
191  {
192  delete text;
193  m_drawItem = NULL;
194  }
195  else
196  {
197  m_drawItem = text;
198  }
199  }
200  break;
201 
202  default:
203  DisplayError( this, wxT( "LIB_EDIT_FRAME::CreateGraphicItem() error" ) );
204  return NULL;
205  }
206 
207  if( m_drawItem )
208  {
209  m_drawItem->BeginEdit( IS_NEW, drawPos );
212 
213  if( m_drawSpecificUnit )
217 
218  // Draw initial symbol:
219  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
220  }
221  else
222  {
224  return NULL;
225  }
226 
228  m_canvas->SetIgnoreMouseEvents( false );
229 
230  return m_drawItem;
231 }
232 
233 
235 {
236  if( m_drawItem == NULL )
237  return;
238 
239  wxPoint pos = GetCrossHairPosition( true );
240 
241  if( m_drawItem->ContinueEdit( pos ) )
242  {
243  m_drawItem->Draw( m_canvas, DC, pos, COLOR4D::UNSPECIFIED, g_XorMode, NULL, DefaultTransform );
244  return;
245  }
246 
247  EndDrawGraphicItem( DC );
248 }
249 
250 
251 /*
252  * Redraw the graphic shape while moving
253  */
254 static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
255  bool aErase )
256 {
257  LIB_ITEM* item;
258 
259  item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
260 
261  if( item == NULL )
262  return;
263 
264  item->SetEraseLastDrawItem( aErase );
265 
266  // if item is the reference field, we must add the current unit id
267  if( item->Type() == LIB_FIELD_T )
268  {
269  int unit = ((LIB_EDIT_FRAME*)aPanel->GetParent())->GetUnit();
270  wxString text = ((LIB_FIELD*)item)->GetFullText( unit );
271 
272  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
273  COLOR4D::UNSPECIFIED, g_XorMode, &text,
275  }
276  else
277  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
278  COLOR4D::UNSPECIFIED, g_XorMode, NULL,
280 }
281 
282 
284 {
285  if( m_drawItem == NULL )
286  return;
287 
288  SetCursor( wxCURSOR_HAND );
289 
291 
292  // For fields only, move the anchor point of the field
293  // to the cursor position to allow user to see the text justification
294  if( m_drawItem->Type() == LIB_FIELD_T )
296  else
298 
300  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
301 }
302 
303 
305 {
306  if( m_drawItem == NULL )
307  return;
308 
312  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
313 }
314 
315 
317 static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
318  bool aErase )
319 {
320  LIB_ITEM* item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
321 
322  if( item == NULL )
323  return;
324 
325  item->SetEraseLastDrawItem( aErase );
326  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ), COLOR4D::UNSPECIFIED, g_XorMode, NULL,
328 }
329 
330 
332 {
333  if( LIB_PART* part = GetCurPart() )
334  {
335  if( !m_drawItem )
336  return;
337 
338  if( GetToolId() != ID_NO_TOOL_SELECTED )
339  SetCursor( wxCURSOR_PENCIL );
340  else
341  SetCursor( (wxStockCursor) m_canvas->GetDefaultCursor() );
342 
343  if( GetTempCopyComponent() ) // used when editing an existing item
345  else
346  {
347  // When creating a new item, there is still no change for the
348  // current component. So save it.
349  SaveCopyInUndoList( part );
350  }
351 
352  if( m_drawItem->IsNew() )
353  part->AddDrawItem( m_drawItem );
354 
356 
357  m_drawItem = NULL;
358 
359  OnModify();
360 
361  m_canvas->SetMouseCapture( NULL, NULL );
362  m_canvas->Refresh();
363  }
364 }
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:198
static double m_current_text_angle
Current text angle setting.
Definition: libeditframe.h:112
static void RedrawWhileMovingCursor(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: symbdraw.cpp:254
FILL_T GetFillMode() const
LIB_ITEMS & GetDrawItemList()
Return a reference to the draw item list.
LIB_ITEM * GetDrawItem()
Definition: libeditframe.h:399
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
static int m_convert
Definition: libeditframe.h:99
void RestoreComponent()
Function RestoreComponent Restore the current edited component from its temporary copy...
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
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:216
static LIB_ITEM * m_drawItem
Definition: libeditframe.h:91
Class LIB_TEXT defines a component library graphical text item.
Definition: lib_text.h:45
void ClearTempCopyComponent()
Function 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()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: libeditframe.h:369
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:348
Class LIB_ITEM is the base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:77
#define IS_NEW
New item, just created.
Definition: base_struct.h:113
LIB_PART * GetParent() const
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:751
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()
Function TempCopyComponent create a temporary copy of the current edited component Used to prepare an...
void GraphicItemBeginDraw(wxDC *DC)
Definition: symbdraw.cpp:234
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:175
static int m_textSize
The current text size setting.
Definition: libeditframe.h:109
static void AbortSymbolTraceOn(EDA_DRAW_PANEL *Panel, wxDC *DC)
Definition: symbdraw.cpp:127
void StartModifyDrawSymbol(wxDC *DC)
Definition: symbdraw.cpp:304
void SetFillMode(FILL_T aFillMode)
void SetWidthUnits(const wxString &units)
LIB_PART * GetTempCopyComponent()
Function GetTempCopyComponent.
Definition: libeditframe.h:431
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:92
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 component library editor main window.
Definition: libeditframe.h:51
LIB_ITEM * CreateGraphicItem(LIB_PART *LibEntry, wxDC *DC)
Definition: symbdraw.cpp:150
TRANSFORM DefaultTransform
Definition: eeschema.cpp:57
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:95
int GetToolId() const
Definition: draw_frame.h:406
static FILL_T m_drawFillStyle
The current draw or edit graphic item fill style.
Definition: libeditframe.h:85
void EndDrawGraphicItem(wxDC *DC)
Definition: symbdraw.cpp:331
void SetIgnoreMouseEvents(bool aIgnore)
LIB_PART * GetCurPart()
Function GetCurPart returns the current part being edited, or NULL if none selected.
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:317
void EditGraphicSymbol(wxDC *DC, LIB_ITEM *DrawItem)
Definition: symbdraw.cpp:58
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:114
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:56
static int m_drawLineWidth
Default line width for drawing or editing graphic items.
Definition: libeditframe.h:88
bool m_drawSpecificConvert
Convert of the item currently being drawn.
Definition: libeditframe.h:60
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:68
Message panel definition file.
int GetDefaultCursor() const
Function GetDefaultCursor return the default cursor shape.
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...
void StartMoveDrawSymbol(wxDC *DC)
Definition: symbdraw.cpp:283
Definition of class LIB_EDIT_FRAME.
int GetConvert() const
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy)
Function SaveCopyInUndoList.