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  if( component )
117  component->GetDrawItemList().sort();
118 
119  OnModify( );
120 
121  MSG_PANEL_ITEMS items;
122  DrawItem->GetMsgPanelInfo( items );
123  SetMsgPanel( items );
124  m_canvas->Refresh();
125 }
126 
127 
128 static void AbortSymbolTraceOn( EDA_DRAW_PANEL* Panel, wxDC* DC )
129 {
130  LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) Panel->GetParent();
131  LIB_ITEM* item = parent->GetDrawItem();
132 
133  if( item == NULL )
134  return;
135 
136  bool newItem = item->IsNew();
137  item->EndEdit( parent->GetCrossHairPosition( true ), true );
138 
139  if( newItem )
140  {
141  delete item;
142  }
143  else
144  parent->RestoreComponent();
145 
146  parent->SetDrawItem( NULL );
147  Panel->Refresh();
148 }
149 
150 
152 {
154  wxPoint drawPos = GetCrossHairPosition( true );
155 
156  // no temp copy -> the current version of component will be used for Undo
157  // This is normal when adding new items to the current component
159 
160  switch( GetToolId() )
161  {
163  m_drawItem = new LIB_ARC( LibEntry );
164  break;
165 
167  m_drawItem = new LIB_CIRCLE( LibEntry );
168  break;
169 
171  m_drawItem = new LIB_RECTANGLE( LibEntry );
172  break;
173 
175  m_drawItem = new LIB_POLYLINE( LibEntry );
176  break;
177 
179  {
180  LIB_TEXT* text = new LIB_TEXT( LibEntry );
181  text->SetTextSize( wxSize( m_textSize, m_textSize ) );
183 
184  // Enter the graphic text info
186  EditSymbolText( NULL, text );
187 
188  m_canvas->SetIgnoreMouseEvents( false );
190 
191  if( text->GetText().IsEmpty() )
192  {
193  delete text;
194  m_drawItem = NULL;
195  }
196  else
197  {
198  m_drawItem = text;
199  }
200  }
201  break;
202 
203  default:
204  DisplayError( this, wxT( "LIB_EDIT_FRAME::CreateGraphicItem() error" ) );
205  return NULL;
206  }
207 
208  if( m_drawItem )
209  {
210  m_drawItem->BeginEdit( IS_NEW, drawPos );
211 
212  // Don't set line parameters for text objects.
213  if( m_drawItem->Type() != LIB_TEXT_T )
214  {
217  }
218 
219  if( m_drawSpecificUnit )
221 
224 
225  // Draw initial symbol:
226  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
227  }
228  else
229  {
231  return NULL;
232  }
233 
235  m_canvas->SetIgnoreMouseEvents( false );
236 
237  return m_drawItem;
238 }
239 
240 
242 {
243  if( m_drawItem == NULL )
244  return;
245 
246  wxPoint pos = GetCrossHairPosition( true );
247 
248  if( m_drawItem->ContinueEdit( pos ) )
249  {
250  m_drawItem->Draw( m_canvas, DC, pos, COLOR4D::UNSPECIFIED, g_XorMode, NULL,
252  return;
253  }
254 
255  EndDrawGraphicItem( DC );
256 }
257 
258 
259 /*
260  * Redraw the graphic shape while moving
261  */
262 static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
263  bool aErase )
264 {
265  LIB_ITEM* item;
266 
267  item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
268 
269  if( item == NULL )
270  return;
271 
272  item->SetEraseLastDrawItem( aErase );
273 
274  // if item is the reference field, we must add the current unit id
275  if( item->Type() == LIB_FIELD_T )
276  {
277  int unit = ((LIB_EDIT_FRAME*)aPanel->GetParent())->GetUnit();
278  wxString text = ((LIB_FIELD*)item)->GetFullText( unit );
279 
280  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
281  COLOR4D::UNSPECIFIED, g_XorMode, &text,
283  }
284  else
285  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
286  COLOR4D::UNSPECIFIED, g_XorMode, NULL,
288 }
289 
290 
292 {
293  if( m_drawItem == NULL )
294  return;
295 
296  SetCursor( wxCURSOR_HAND );
297 
299 
300  // For fields only, move the anchor point of the field
301  // to the cursor position to allow user to see the text justification
302  if( m_drawItem->Type() == LIB_FIELD_T )
304  else
306 
308  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
309 }
310 
311 
313 {
314  if( m_drawItem == NULL )
315  return;
316 
320  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
321 }
322 
323 
325 static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
326  bool aErase )
327 {
328  LIB_ITEM* item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
329 
330  if( item == NULL )
331  return;
332 
333  item->SetEraseLastDrawItem( aErase );
334  item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
335  COLOR4D::UNSPECIFIED, g_XorMode, NULL, DefaultTransform );
336 }
337 
338 
340 {
341  if( LIB_PART* part = GetCurPart() )
342  {
343  if( !m_drawItem )
344  return;
345 
346  if( GetToolId() != ID_NO_TOOL_SELECTED )
347  SetCursor( wxCURSOR_PENCIL );
348  else
349  SetCursor( (wxStockCursor) m_canvas->GetDefaultCursor() );
350 
351  if( GetTempCopyComponent() ) // used when editing an existing item
353  else
354  {
355  // When creating a new item, there is still no change for the
356  // current component. So save it.
357  SaveCopyInUndoList( part );
358  }
359 
360  if( m_drawItem->IsNew() )
361  part->AddDrawItem( m_drawItem );
362 
364 
365  m_drawItem = NULL;
366 
367  OnModify();
368 
369  m_canvas->SetMouseCapture( NULL, NULL );
370  m_canvas->Refresh();
371  }
372 }
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:262
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:773
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:241
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:128
void StartModifyDrawSymbol(wxDC *DC)
Definition: symbdraw.cpp:312
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:151
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:419
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:339
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:325
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.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
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:291
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.