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 <sch_draw_panel.h>
33 #include <confirm.h>
34 #include <base_units.h>
35 #include <msgpanel.h>
36 
37 #include <eeschema_id.h>
38 #include <lib_edit_frame.h>
39 #include <class_libentry.h>
40 #include <lib_arc.h>
41 #include <lib_circle.h>
42 #include <lib_polyline.h>
43 #include <lib_rectangle.h>
44 #include <lib_text.h>
45 
46 #include <sch_view.h>
48 
49 
50 static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
51  bool aErase );
52 
53 
54 void LIB_EDIT_FRAME::EditGraphicSymbol( wxDC* DC, LIB_ITEM* DrawItem )
55 {
56  if( DrawItem == NULL )
57  return;
58 
59  DIALOG_LIB_EDIT_DRAW_ITEM dialog( this, DrawItem );
60 
61  if( dialog.ShowModal() == wxID_CANCEL )
62  return;
63 
64  // Init default values (used to create a new draw item)
65  m_drawLineWidth = dialog.GetWidth();
68 
69 #if 0
70  /* TODO: see if m_drawFillStyle must retain the last fill option or not.
71  * if the last is Filled, having next new graphic items created
72  * with filled body is often bad.
73  * currently m_drawFillStyle is left with the default value (not filled)
74  */
75  if( DrawItem->IsFillable() )
76  m_drawFillStyle = (FILL_T) dialog.GetFillStyle();
77 #endif
78 
79  // Save copy for undo if not in edit (edit command already handle the save copy)
80  if( !DrawItem->InEditMode() )
81  SaveCopyInUndoList( DrawItem->GetParent() );
82 
83  if( m_drawSpecificUnit )
84  DrawItem->SetUnit( GetUnit() );
85  else
86  DrawItem->SetUnit( 0 );
87 
89  DrawItem->SetConvert( GetConvert() );
90  else
91  DrawItem->SetConvert( 0 );
92 
93  if( DrawItem->IsFillable() )
94  DrawItem->SetFillMode( (FILL_T) dialog.GetFillStyle() );
95 
96  DrawItem->SetWidth( m_drawLineWidth );
97 
98  GetCanvas()->GetView()->Update( DrawItem );
99  GetCanvas()->Refresh();
100  OnModify( );
101 
102  MSG_PANEL_ITEMS items;
103  DrawItem->GetMsgPanelInfo( m_UserUnits, items );
104  SetMsgPanel( items );
105 }
106 
107 
108 static void AbortSymbolTraceOn( EDA_DRAW_PANEL* aPanel, wxDC* DC )
109 {
110  LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) aPanel->GetParent();
111  LIB_ITEM* item = parent->GetDrawItem();
112 
113  if( item == NULL )
114  return;
115 
116  bool newItem = item->IsNew();
117  item->EndEdit( parent->GetCrossHairPosition( true ), true );
118 
119  if( newItem )
120  delete item;
121  else
122  parent->RestoreComponent();
123 
124  parent->SetDrawItem( NULL );
125 
126  auto view = static_cast<SCH_DRAW_PANEL*>(aPanel)->GetView();
127  view->ClearPreview();
128  view->ShowPreview( false );
129  view->ClearHiddenFlags();
130  parent->RebuildView();
131 }
132 
133 
135 {
136  LIB_ITEM* item = GetDrawItem();
138  wxPoint drawPos = GetCrossHairPosition( true );
139 
140  // no temp copy -> the current version of symbol will be used for Undo
141  // This is normal when adding new items to the current symbol
143 
144  auto view = static_cast<SCH_DRAW_PANEL*>(m_canvas)->GetView();
145  view->ShowPreview( true );
146 
147  switch( GetToolId() )
148  {
150  item = new LIB_ARC( LibEntry );
151  break;
152 
154  item = new LIB_CIRCLE( LibEntry );
155  break;
156 
158  item = new LIB_RECTANGLE( LibEntry );
159  break;
160 
162  item = new LIB_POLYLINE( LibEntry );
163  break;
164 
166  {
167  LIB_TEXT* text = new LIB_TEXT( LibEntry );
168  text->SetTextSize( wxSize( m_textSize, m_textSize ) );
170 
171  // Enter the graphic text info
173  EditSymbolText( NULL, text );
174 
175  m_canvas->SetIgnoreMouseEvents( false );
177 
178  if( text->GetText().IsEmpty() )
179  {
180  delete text;
181  item = NULL;
182  }
183  else
184  {
185  item = text;
186  }
187  }
188  break;
189 
190  default:
191  DisplayError( this, wxT( "LIB_EDIT_FRAME::CreateGraphicItem() error" ) );
192  return NULL;
193  }
194 
195  if( item )
196  {
197  item->BeginEdit( IS_NEW, drawPos );
198 
199  // Don't set line parameters for text objects.
200  if( item->Type() != LIB_TEXT_T )
201  {
202  item->SetWidth( m_drawLineWidth );
203  item->SetFillMode( m_drawFillStyle );
204  }
205 
206  if( m_drawSpecificUnit )
207  item->SetUnit( m_unit );
208 
210  item->SetConvert( m_convert );
211 
212  // Draw initial symbol:
213  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
214  }
215  else
216  {
218  return NULL;
219  }
220 
222  m_canvas->SetIgnoreMouseEvents( false );
223  SetDrawItem( item );
224 
225  return item;
226 }
227 
228 
230 {
231  if( GetDrawItem() == NULL )
232  return;
233 
234  wxPoint pos = GetCrossHairPosition( true );
235 
236  auto view = static_cast<SCH_DRAW_PANEL*>(m_canvas)->GetView();
237  view->ShowPreview( true );
238 
239 
240  if( GetDrawItem()->ContinueEdit( pos ) )
241  return;
242 
243  EndDrawGraphicItem( DC );
244 }
245 
246 
247 /*
248  * Redraw the graphic shape while moving
249  */
250 static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
251  bool aErase )
252 {
253  LIB_ITEM* item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
254 
255  if( item == NULL )
256  return;
257 
258  auto view = static_cast<SCH_DRAW_PANEL*>(aPanel)->GetView();
259  auto p = aPanel->GetParent()->GetCrossHairPosition( true );
260 
261  item->CalcEdit( p );
262 
263  view->Hide( item );
264  view->ClearPreview();
265  view->AddToPreview( item->Clone() );
266 }
267 
268 
270 {
271  if( aItem == NULL )
272  return;
273 
274  SetCursor( wxCURSOR_HAND );
275 
276  GetGalCanvas()->GetView()->Hide ( aItem );
277 
279 
280  // For fields only, move the anchor point of the field
281  // to the cursor position to allow user to see the text justification
282  if( aItem->Type() == LIB_FIELD_T )
283  aItem->BeginEdit( IS_MOVED, aItem->GetPosition() );
284  else
285  aItem->BeginEdit( IS_MOVED, GetCrossHairPosition( true ) );
286 
288  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
289 }
290 
291 
293 {
294  if( aItem == NULL )
295  return;
296 
297  DBG(printf("startmdifyraw\n");)
298 
300  aItem->BeginEdit( IS_RESIZED, GetCrossHairPosition( true ) );
302  m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
303 }
304 
305 
307 {
308  LIB_ITEM* item = GetDrawItem();
309 
310  if( item == NULL )
311  return;
312 
313  if( LIB_PART* part = GetCurPart() )
314  {
315  if( GetToolId() != ID_NO_TOOL_SELECTED )
316  SetCursor( wxCURSOR_PENCIL );
317  else
318  SetCursor( (wxStockCursor) GetGalCanvas()->GetDefaultCursor() );
319 
320  if( GetTempCopyComponent() ) // used when editing an existing item
322  else
323  {
324  // When creating a new item, there is still no change for the
325  // current symbol. So save it.
326  SaveCopyInUndoList( part );
327  }
328 
329  if( item->IsNew() )
330  part->AddDrawItem( item );
331 
332  item->EndEdit( GetCrossHairPosition( true ) );
333 
334  SetDrawItem( NULL );
335 
336  m_canvas->SetMouseCapture( NULL, NULL );
337 
338  auto view = static_cast<SCH_DRAW_PANEL*>(m_canvas)->GetView();
339 
340  DBG(printf("end: pos %d %d\n", item->GetPosition().x, item->GetPosition().y );)
341 
342  view->ClearHiddenFlags();
343  view->ClearPreview();
344 
345  OnModify();
346  }
347 
348  RebuildView();
349  GetCanvas()->Refresh();
350 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:169
virtual wxPoint GetPosition() const =0
Return the current draw object position.
void Hide(VIEW_ITEM *aItem, bool aHide=true)
Temporarily hides the item in the view (e.g.
Definition: view.cpp:1497
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
virtual void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
static double m_current_text_angle
Current text angle setting.
static void RedrawWhileMovingCursor(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: symbdraw.cpp:250
static int m_convert
void RestoreComponent()
Restore the current edited component from its temporary copy.
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:219
Define a symbol library graphical text item.
Definition: lib_text.h:44
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.
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
virtual void SetWidth(int aWidth)=0
Set the width of the draw item to aWidth.
virtual void EndEdit(const wxPoint &aPosition, bool aAbort=false)
End an object editing action.
virtual 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:227
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
virtual void CalcEdit(const wxPoint &aPosition)
Calculates the attributes of an item at aPosition when it is being edited.
virtual void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
void StartMoveDrawSymbol(wxDC *DC, LIB_ITEM *aItem)
Definition: symbdraw.cpp:269
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:114
void StartModifyDrawSymbol(wxDC *DC, LIB_ITEM *aItem)
Definition: symbdraw.cpp:292
virtual EDA_DRAW_FRAME * GetParent() const =0
LIB_PART * GetParent() const
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
void SetDrawItem(LIB_ITEM *drawItem)
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
Class LIB_ITEM definition.
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...
SCH_DRAW_PANEL * GetCanvas() const override
virtual 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:229
Define a library symbol object.
static int m_textSize
The current text size setting.
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1530
KIGFX::SCH_VIEW * GetView() const
void SetFillMode(FILL_T aFillMode)
LIB_PART * GetTempCopyComponent()
void SetConvert(int aConvert)
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:123
void SetUnit(int aUnit)
The symbol library editor main window.
LIB_ITEM * CreateGraphicItem(LIB_PART *LibEntry, wxDC *DC)
Definition: symbdraw.cpp:134
static int m_unit
int GetToolId() const
Definition: draw_frame.h:519
static FILL_T m_drawFillStyle
The current draw or edit graphic item fill style.
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT)
Create a copy of the current component, and save it in the undo list.
void EndDrawGraphicItem(wxDC *DC)
Definition: symbdraw.cpp:306
LIB_ITEM * GetDrawItem() const
void SetIgnoreMouseEvents(bool aIgnore)
void EditGraphicSymbol(wxDC *DC, LIB_ITEM *DrawItem)
Definition: symbdraw.cpp:54
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:115
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:54
Definition of class LIB_EDIT_FRAME.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:921
#define DBG(x)
Definition: fctsys.h:33
static void AbortSymbolTraceOn(EDA_DRAW_PANEL *aPanel, wxDC *DC)
Definition: symbdraw.cpp:108
static int m_drawLineWidth
Default line width for drawing or editing graphic items.
bool m_drawSpecificConvert
Convert of the item currently being drawn.
bool m_drawSpecificUnit
Specify which component parts the current draw item applies to.
Message panel definition file.
EDA_UNITS_T m_UserUnits
Definition: draw_frame.h:120
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Return the current cross hair position in logical (drawing) coordinates.
#define IS_MOVED
Item being moved.
Definition: base_struct.h:113