KiCad PCB EDA Suite
lib_drawing_tools.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) 2019 CERN
5  * Copyright (C) 2019-2020 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 
25 #include <ee_actions.h>
26 #include <lib_edit_frame.h>
27 #include <view/view_controls.h>
28 #include <tool/tool_manager.h>
31 #include <tools/lib_pin_tool.h>
32 #include <class_libentry.h>
33 #include <bitmaps.h>
34 #include <lib_text.h>
36 #include <lib_arc.h>
37 #include <lib_circle.h>
38 #include <lib_polyline.h>
39 #include <lib_rectangle.h>
40 #include <pgm_base.h>
43 #include "ee_point_editor.h"
44 
45 static void* g_lastPinWeakPtr;
46 
47 
49  EE_TOOL_BASE<LIB_EDIT_FRAME>( "eeschema.SymbolDrawing" ),
50  m_lastTextAngle( 0.0 ),
51  m_lastFillStyle( NO_FILL ),
52  m_drawSpecificConvert( true ),
53  m_drawSpecificUnit( false )
54 {
55 }
56 
57 
59 {
61 
62  auto isDrawingCondition = [] ( const SELECTION& aSel ) {
63  LIB_ITEM* item = (LIB_ITEM*) aSel.Front();
64  return item && item->IsNew();
65  };
66 
67  m_menu.GetMenu().AddItem( EE_ACTIONS::finishDrawing, isDrawingCondition, 2 );
68 
69  return true;
70 }
71 
72 
74 {
75  KICAD_T type = aEvent.Parameter<KICAD_T>();
76  LIBEDIT_SETTINGS* settings = Pgm().GetSettingsManager().GetAppSettings<LIBEDIT_SETTINGS>();
77  LIB_PIN_TOOL* pinTool = type == LIB_PIN_T ? m_toolMgr->GetTool<LIB_PIN_TOOL>() : nullptr;
78  VECTOR2I cursorPos;
79  EDA_ITEM* item = nullptr;
80 
82  getViewControls()->ShowCursor( true );
83 
84  std::string tool = aEvent.GetCommandStr().get();
85  m_frame->PushTool( tool );
86  Activate();
87 
88  // Prime the pump
89  if( aEvent.HasPosition() )
91 
92  // Main loop: keep receiving events
93  while( TOOL_EVENT* evt = Wait() )
94  {
95  m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
96  cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
97 
98  auto cleanup = [&] () {
100  m_view->ClearPreview();
101  delete item;
102  item = nullptr;
103  };
104 
105  if( evt->IsCancelInteractive() )
106  {
107  if( item )
108  cleanup();
109  else
110  {
111  m_frame->PopTool( tool );
112  break;
113  }
114  }
115  else if( evt->IsActivate() )
116  {
117  if( item )
118  cleanup();
119 
120  if( evt->IsMoveTool() )
121  {
122  // leave ourselves on the stack so we come back after the move
123  break;
124  }
125  else
126  {
127  m_frame->PopTool( tool );
128  break;
129  }
130  }
131 
132  else if( evt->IsClick( BUT_LEFT ) )
133  {
134  LIB_PART* part = m_frame->GetCurPart();
135 
136  if( !part )
137  continue;
138 
139  // First click creates...
140  if( !item )
141  {
143 
144  switch( type )
145  {
146  case LIB_PIN_T:
147  {
148  item = pinTool->CreatePin( wxPoint( cursorPos.x, -cursorPos.y ), part );
149  g_lastPinWeakPtr = item;
150  break;
151  }
152  case LIB_TEXT_T:
153  {
154  LIB_TEXT* text = new LIB_TEXT( part );
155 
156  text->SetPosition( wxPoint( cursorPos.x, -cursorPos.y ) );
157  text->SetTextSize( wxSize( Mils2iu( settings->m_Defaults.text_size ),
158  Mils2iu( settings->m_Defaults.text_size ) ) );
159  text->SetTextAngle( m_lastTextAngle );
160 
161  DIALOG_LIB_EDIT_TEXT dlg( m_frame, text );
162 
163  if( dlg.ShowModal() != wxID_OK )
164  delete text;
165  else
166  item = text;
167 
168  break;
169  }
170  default:
171  wxFAIL_MSG( "TwoClickPlace(): unknown type" );
172  }
173 
174  // Restore cursor after dialog
175  getViewControls()->WarpCursor( getViewControls()->GetCursorPosition(), true );
176 
177  if( item )
178  {
179  item->SetFlags( IS_NEW | IS_MOVED );
180  m_view->ClearPreview();
181  m_view->AddToPreview( item->Clone() );
182  m_selectionTool->AddItemToSel( item );
183  }
184 
185  getViewControls()->SetCursorPosition( cursorPos, false );
186  }
187 
188  // ... and second click places:
189  else
190  {
191  m_frame->SaveCopyInUndoList( part );
192 
193  switch( item->Type() )
194  {
195  case LIB_PIN_T:
196  pinTool->PlacePin( (LIB_PIN*) item );
197  break;
198  case LIB_TEXT_T:
199  part->AddDrawItem( (LIB_TEXT*) item );
200  break;
201  default:
202  wxFAIL_MSG( "TwoClickPlace(): unknown type" );
203  }
204 
205  item->ClearEditFlags();
206  item = nullptr;
207  m_view->ClearPreview();
208 
209  m_frame->RebuildView();
210  m_frame->OnModify();
211  }
212  }
213  else if( evt->IsClick( BUT_RIGHT ) )
214  {
215  // Warp after context menu only if dragging...
216  if( !item )
218 
220  }
221 
222  else if( item && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
223  {
224  static_cast<LIB_ITEM*>( item )->SetPosition( wxPoint( cursorPos.x, -cursorPos.y ) );
225  m_view->ClearPreview();
226  m_view->AddToPreview( item->Clone() );
227  }
228 
229  else
230  evt->SetPassEvent();
231 
232  // Enable autopanning and cursor capture only when there is an item to be placed
233  getViewControls()->SetAutoPan( item != nullptr );
234  getViewControls()->CaptureCursor( item != nullptr );
235  }
236 
237  return 0;
238 }
239 
240 
242 {
243  LIBEDIT_SETTINGS* settings = Pgm().GetSettingsManager().GetAppSettings<LIBEDIT_SETTINGS>();
244  KICAD_T type = aEvent.Parameter<KICAD_T>();
246 
247  // We might be running as the same shape in another co-routine. Make sure that one
248  // gets whacked.
250 
252  getViewControls()->ShowCursor( true );
253 
254  std::string tool = aEvent.GetCommandStr().get();
255  m_frame->PushTool( tool );
256  Activate();
257 
258  LIB_PART* part = m_frame->GetCurPart();
259  LIB_ITEM* item = nullptr;
260 
261  // Prime the pump
262  if( aEvent.HasPosition() )
264 
265  // Main loop: keep receiving events
266  while( TOOL_EVENT* evt = Wait() )
267  {
268  if( !pointEditor->HasPoint() )
269  m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
270 
271  VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
272 
273  auto cleanup = [&] () {
275  m_view->ClearPreview();
276  delete item;
277  item = nullptr;
278  };
279 
280  if( evt->IsCancelInteractive() )
281  {
282  if( item )
283  cleanup();
284  else
285  {
286  m_frame->PopTool( tool );
287  break;
288  }
289  }
290 
291  else if( evt->IsActivate() )
292  {
293  if( item )
294  cleanup();
295 
296  if( evt->IsPointEditor() )
297  {
298  // don't exit (the point editor runs in the background)
299  }
300  else if( evt->IsMoveTool() )
301  {
302  // leave ourselves on the stack so we come back after the move
303  break;
304  }
305  else
306  {
307  m_frame->PopTool( tool );
308  break;
309  }
310  }
311 
312  else if( evt->IsClick( BUT_LEFT ) && !item )
313  {
314  if( !part )
315  continue;
316 
318 
319  switch( type )
320  {
321  case LIB_ARC_T: item = new LIB_ARC( part ); break;
322  case LIB_CIRCLE_T: item = new LIB_CIRCLE( part ); break;
323  case LIB_POLYLINE_T: item = new LIB_POLYLINE( part ); break;
324  case LIB_RECTANGLE_T: item = new LIB_RECTANGLE( part ); break;
325  default: break; // keep compiler quiet
326  }
327 
328  wxASSERT( item );
329 
330  item->SetWidth( settings->m_Defaults.line_width );
331  item->SetFillMode( m_lastFillStyle );
332  item->SetFlags( IS_NEW );
333  item->BeginEdit( wxPoint( cursorPos.x, -cursorPos.y ) );
334 
335  if( m_drawSpecificUnit )
336  item->SetUnit( m_frame->GetUnit() );
337 
339  item->SetConvert( m_frame->GetConvert() );
340 
341  m_selectionTool->AddItemToSel( item );
342  }
343 
344  else if( item && ( evt->IsClick( BUT_LEFT ) || evt->IsDblClick( BUT_LEFT )
345  || evt->IsAction( &EE_ACTIONS::finishDrawing ) ) )
346  {
347  if( evt->IsDblClick( BUT_LEFT ) || evt->IsAction( &EE_ACTIONS::finishDrawing )
348  || !item->ContinueEdit( wxPoint( cursorPos.x, -cursorPos.y ) ) )
349  {
350  item->EndEdit();
351  item->ClearEditFlags();
352  m_view->ClearPreview();
353 
354  m_frame->SaveCopyInUndoList( part );
355  part->AddDrawItem( item );
356  item = nullptr;
357 
358  m_frame->RebuildView();
359  m_frame->OnModify();
361  }
362  }
363 
364  else if( item && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
365  {
366  item->CalcEdit( wxPoint( cursorPos.x, -cursorPos.y) );
367  m_view->ClearPreview();
368  m_view->AddToPreview( item->Clone() );
369  }
370 
371  else if( evt->IsDblClick( BUT_LEFT ) && !item )
372  {
374  }
375 
376  else if( evt->IsClick( BUT_RIGHT ) )
377  {
378  // Warp after context menu only if dragging...
379  if( !item )
381 
383  }
384 
385  else
386  evt->SetPassEvent();
387 
388  // Enable autopanning and cursor capture only when there is a shape being drawn
389  getViewControls()->SetAutoPan( item != nullptr );
390  getViewControls()->CaptureCursor( item != nullptr );
391  }
392 
393  return 0;
394 }
395 
396 
398 {
399  getViewControls()->ShowCursor( true );
400  getViewControls()->SetSnapping( true );
401 
402  std::string tool = aEvent.GetCommandStr().get();
403  m_frame->PushTool( tool );
404  Activate();
405 
406  // Main loop: keep receiving events
407  while( TOOL_EVENT* evt = Wait() )
408  {
409  m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_BULLSEYE );
410 
411  if( evt->IsCancelInteractive() )
412  {
413  m_frame->PopTool( tool );
414  break;
415  }
416  else if( evt->IsActivate() )
417  {
418  m_frame->PopTool( tool );
419  break;
420  }
421  else if( evt->IsClick( BUT_LEFT ) )
422  {
423  LIB_PART* part = m_frame->GetCurPart();
424 
425  if( !part )
426  continue;
427 
428  VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
429  wxPoint offset( -cursorPos.x, cursorPos.y );
430 
431  part->SetOffset( offset );
432 
433  // Refresh the view without changing the viewport
434  auto center = m_view->GetCenter();
435  center.x += offset.x;
436  center.y -= offset.y;
437  m_view->SetCenter( center );
439  m_frame->OnModify();
440  }
441  else if( evt->IsClick( BUT_RIGHT ) )
442  {
444  }
445  else
446  evt->SetPassEvent();
447  }
448 
449  return 0;
450 }
451 
452 
454 {
456  LIB_PART* part = m_frame->GetCurPart();
457  LIB_PIN* sourcePin = nullptr;
458 
459  if( !part )
460  return 0;
461 
462  // See if we have a pin matching our weak ptr
463  for( LIB_PIN* test = part->GetNextPin(); test; test = part->GetNextPin( test ) )
464  {
465  if( (void*) test == g_lastPinWeakPtr )
466  sourcePin = test;
467  }
468 
469  if( sourcePin )
470  {
471  LIB_PIN* pin = pinTool->RepeatPin( sourcePin );
472  g_lastPinWeakPtr = pin;
473 
475 
476  if( pin )
478  }
479 
480  return 0;
481 }
482 
483 
485 {
494 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
static TOOL_ACTION properties
Definition: ee_actions.h:120
static TOOL_ACTION placeSymbolText
Definition: ee_actions.h:102
static TOOL_ACTION drawSymbolLines
Definition: ee_actions.h:106
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
void AddToPreview(EDA_ITEM *aItem, bool aTakeOwnership=true)
Definition: sch_view.cpp:195
int PlaceAnchor(const TOOL_EVENT &aEvent)
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
static TOOL_ACTION activatePointEditor
Definition: actions.h:164
int GetConvert() const
void SetPosition(const wxPoint &aPosition)
Definition: lib_item.h:248
Define a symbol library graphical text item.
Definition: lib_text.h:40
const VECTOR2D & GetCenter() const
Function GetCenter() Returns the center point of this VIEW (in world space coordinates)
Definition: view.h:328
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
virtual void SetWidth(int aWidth)=0
VIEW_CONTROLS class definition.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
void RecacheAllItems()
Function RecacheAllItems() Rebuilds GAL display lists.
Definition: view.cpp:1402
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
int TwoClickPlace(const TOOL_EVENT &aEvent)
static void * g_lastPinWeakPtr
virtual void SetCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true, bool aTriggeredByArrows=false, long aArrowCommand=0)=0
Moves cursor to the requested position expressed in world coordinates.
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:237
virtual void CalcEdit(const wxPoint &aPosition)
Calculates the attributes of an item at aPosition when it is being edited.
Definition: lib_item.h:163
int DrawShape(const TOOL_EVENT &aEvent)
void setTransitions() override
Sets up handlers for various events.
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
void DeactivateTool()
Function DeactivateTool() Deactivates the currently active tool.
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
The base class for drawable items used by schematic library components.
Definition: lib_item.h:61
bool IsNew() const
Definition: base_struct.h:199
void SetOffset(const wxPoint &aOffset)
Move the part aOffset.
static TOOL_ACTION drawSymbolRectangle
Definition: ee_actions.h:103
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:69
void AddDrawItem(LIB_ITEM *aItem)
Add a new draw aItem to the draw object list.
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
EE_SELECTION & GetSelection()
Function GetSelection()
#define IS_NEW
New item, just created.
Definition: base_struct.h:117
virtual void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
Function WarpCursor() If enabled (.
static TOOL_ACTION placeSymbolPin
Definition: ee_actions.h:101
int GetUnit() const
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:604
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:232
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:59
virtual bool ContinueEdit(const wxPoint aPosition)
Continue an edit in progress at aPosition.
Definition: lib_item.h:145
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
Class LIB_PIN definition.
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:435
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
TOOL_EVENT.
Definition: tool_event.h:171
void ClearPreview()
Definition: sch_view.cpp:183
static TOOL_ACTION repeatDrawItem
Definition: ee_actions.h:115
static TOOL_ACTION drawSymbolCircle
Definition: ee_actions.h:104
Define a library symbol object.
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:153
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:56
static TOOL_ACTION finishDrawing
Definition: ee_actions.h:108
LIB_PIN * RepeatPin(const LIB_PIN *aSourcePin)
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
void SetFillMode(FILL_T aFillMode)
Definition: lib_item.h:300
void SetConvert(int aConvert)
Definition: lib_item.h:297
virtual void BeginEdit(const wxPoint aPosition)
Begin drawing a component library draw item at aPosition.
Definition: lib_item.h:133
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:463
void SetUnit(int aUnit)
Definition: lib_item.h:294
The symbol library editor main window.
see class PGM_BASE
int AddItemToSel(const TOOL_EVENT &aEvent)
EE_POINT_EDITOR.
int RepeatDrawItem(const TOOL_EVENT &aEvent)
void VetoContextMenuMouseWarp()
Disables mouse warping after the current context menu is closed.
Definition: tool_manager.h:408
static TOOL_ACTION drawSymbolArc
Definition: ee_actions.h:105
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
void Activate()
Function Activate() Runs the tool.
EE_TOOL_BASE.
Definition: ee_tool_base.h:50
bool HasPosition() const
Returns if it this event has a valid position (true for mouse events and context-menu or hotkey-based...
Definition: tool_event.h:260
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:166
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
void ClearEditFlags()
Definition: base_struct.h:251
virtual void EndEdit()
End an object editing action.
Definition: lib_item.h:152
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT, bool aAppend=false)
Create a copy of the current component, and save it in the undo list.
static TOOL_ACTION placeSymbolAnchor
Definition: ee_actions.h:107
static TOOL_ACTION refreshPreview
Definition: actions.h:102
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
static TOOL_ACTION cursorClick
Definition: actions.h:119
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
#define IS_MOVED
Item being moved.
Definition: base_struct.h:116