KiCad PCB EDA Suite
block_libedit.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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2017 KiCad Developers, see AUTHORS.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 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <class_drawpanel.h>
33 #include <confirm.h>
34 
35 #include <general.h>
36 #include <class_library.h>
37 #include <libeditframe.h>
38 
39 
40 static void DrawMovingBlockOutlines( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
41  bool aErase );
42 
43 
45 {
46  int cmd = BLOCK_IDLE;
47 
48  switch( key )
49  {
50  default:
51  cmd = key & 0xFF;
52  break;
53 
54  case EDA_KEY_C( 0xffffffff ): // -1
55  // Historically, -1 has been used as a key, which can cause bit flag
56  // clashes with unaware code. On debug builds, catch any old code that
57  // might still be doing this. TODO: remove if sure all this old code is gone.
58  wxFAIL_MSG( "negative EDA_KEY value should be converted to GR_KEY_INVALID" );
59  // fall through on release builds
60 
61  case GR_KEY_INVALID:
63  break;
64 
65  case GR_KEY_NONE:
66  cmd = BLOCK_MOVE;
67  break;
68 
69  case GR_KB_SHIFT:
70  cmd = BLOCK_COPY;
71  break;
72 
73  case GR_KB_ALT:
74  cmd = BLOCK_ROTATE;
75  break;
76 
77  case GR_KB_SHIFTCTRL:
78  cmd = BLOCK_DELETE;
79  break;
80 
81  case GR_KB_CTRL:
82  cmd = BLOCK_MIRROR_Y;
83  break;
84 
85  case MOUSE_MIDDLE:
86  cmd = BLOCK_ZOOM;
87  break;
88  }
89 
90  return cmd;
91 }
92 
93 
95 {
96  int ItemCount = 0;
97  bool nextCmd = false;
98  wxPoint pt;
99 
100  if( GetScreen()->m_BlockLocate.GetCount() )
101  {
105  GetScreen()->m_BlockLocate.SetState( state );
106  GetScreen()->m_BlockLocate.SetCommand( command );
108  SetCrossHairPosition( wxPoint( GetScreen()->m_BlockLocate.GetRight(),
111  }
112 
113  switch( GetScreen()->m_BlockLocate.GetCommand() )
114  {
115  case BLOCK_IDLE:
116  DisplayError( this, wxT( "Error in HandleBlockPLace" ) );
117  break;
118 
119  case BLOCK_DRAG: // Drag
120  case BLOCK_DRAG_ITEM:
121  case BLOCK_MOVE: // Move
122  case BLOCK_COPY: // Copy
123  if( GetCurPart() )
124  ItemCount = GetCurPart()->SelectItems( GetScreen()->m_BlockLocate,
125  m_unit, m_convert,
127  if( ItemCount )
128  {
129  nextCmd = true;
130 
131  if( m_canvas->IsMouseCaptured() )
132  {
133  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
135  m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
136  }
137 
139  m_canvas->Refresh( true );
140  }
141  break;
142 
143  case BLOCK_PRESELECT_MOVE: // Move with preselection list
144  nextCmd = true;
147  break;
148 
149  case BLOCK_DELETE: // Delete
150  if( GetCurPart() )
151  ItemCount = GetCurPart()->SelectItems( GetScreen()->m_BlockLocate,
152  m_unit, m_convert,
154  if( ItemCount )
156 
157  if( GetCurPart() )
158  {
160  OnModify();
161  }
162  break;
163 
164  case BLOCK_SAVE: // Save
165  case BLOCK_PASTE:
166  case BLOCK_FLIP:
167  break;
168 
169  case BLOCK_ROTATE:
170  case BLOCK_MIRROR_X:
171  case BLOCK_MIRROR_Y:
172  if( GetCurPart() )
173  ItemCount = GetCurPart()->SelectItems( GetScreen()->m_BlockLocate,
174  m_unit, m_convert,
176  if( ItemCount )
178 
179  pt = GetScreen()->m_BlockLocate.Centre();
180  pt = GetNearestGridPosition( pt );
181  pt.y = -pt.y;
182 
183  if( GetCurPart() )
184  {
185  OnModify();
186  int block_cmd = GetScreen()->m_BlockLocate.GetCommand();
187 
188  if( block_cmd == BLOCK_MIRROR_Y)
190  else if( block_cmd == BLOCK_MIRROR_X)
192  else if( block_cmd == BLOCK_ROTATE )
194  }
195 
196  break;
197 
198  case BLOCK_ZOOM: // Window Zoom
199  Window_Zoom( GetScreen()->m_BlockLocate );
200  break;
201 
202  case BLOCK_ABORT:
203  break;
204 
206  break;
207 
208  case BLOCK_COPY_AND_INCREMENT: // not used in Eeschema
209  case BLOCK_MOVE_EXACT: // not used in Eeschema
210  break;
211  }
212 
213  if( !nextCmd )
214  {
215  if( GetScreen()->m_BlockLocate.GetCommand() != BLOCK_SELECT_ITEMS_ONLY && GetCurPart() )
217 
220  GetScreen()->SetCurItem( NULL );
222  false );
223  m_canvas->Refresh( true );
224  }
225 
226  return nextCmd;
227 }
228 
229 
231 {
232  wxPoint pt;
233 
234  if( !m_canvas->IsMouseCaptured() )
235  {
236  DisplayError( this, wxT( "HandleBlockPLace : m_mouseCaptureCallback = NULL" ) );
237  }
238 
240 
241  switch( GetScreen()->m_BlockLocate.GetCommand() )
242  {
243  case BLOCK_IDLE:
244  break;
245 
246  case BLOCK_DRAG: // Drag
247  case BLOCK_DRAG_ITEM:
248  case BLOCK_MOVE: // Move
249  case BLOCK_PRESELECT_MOVE: // Move with preselection list
251 
252  if( GetCurPart() )
254 
256  pt.y *= -1;
257 
258  if( GetCurPart() )
259  GetCurPart()->MoveSelectedItems( pt );
260 
261  m_canvas->Refresh( true );
262  break;
263 
264  case BLOCK_COPY: // Copy
266 
267  if( GetCurPart() )
269 
271  pt.y = -pt.y;
272 
273  if( GetCurPart() )
274  GetCurPart()->CopySelectedItems( pt );
275 
276  break;
277 
278  case BLOCK_PASTE: // Paste (recopy the last block saved)
280  break;
281 
282  case BLOCK_ROTATE: // Invert by popup menu, from block move
283  case BLOCK_MIRROR_X: // Invert by popup menu, from block move
284  case BLOCK_MIRROR_Y: // Invert by popup menu, from block move
285  if( GetCurPart() )
287 
288  pt = GetScreen()->m_BlockLocate.Centre();
289  pt = GetNearestGridPosition( pt );
290  pt.y = -pt.y;
291 
292  if( GetCurPart() )
293  {
294  int block_cmd = GetScreen()->m_BlockLocate.GetCommand();
295 
296  if( block_cmd == BLOCK_MIRROR_Y)
298  else if( block_cmd == BLOCK_MIRROR_X)
300  else if( block_cmd == BLOCK_ROTATE )
302  }
303 
304  break;
305 
306  case BLOCK_ZOOM: // Handled by HandleBlockEnd
307  case BLOCK_DELETE:
308  case BLOCK_SAVE:
309  case BLOCK_ABORT:
310  default:
311  break;
312  }
313 
314  OnModify();
315 
318  GetScreen()->SetCurItem( NULL );
319  m_canvas->EndMouseCapture( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString, false );
320  m_canvas->Refresh( true );
321 }
322 
323 
324 /*
325  * Traces the outline of the search block structures
326  * The entire block follows the cursor
327  */
328 void DrawMovingBlockOutlines( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
329  bool aErase )
330 {
332  BASE_SCREEN* screen = aPanel->GetScreen();
333  block = &screen->m_BlockLocate;
334 
335  LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) aPanel->GetParent();
336  wxASSERT( parent != NULL );
337 
338  LIB_PART* component = parent->GetCurPart();
339 
340  if( component == NULL )
341  return;
342 
343  int unit = parent->GetUnit();
344  int convert = parent->GetConvert();
345 
346  auto opts = PART_DRAW_OPTIONS::Default();
347  opts.draw_mode = g_XorMode;
348  opts.only_selected = true;
349 
350  if( aErase )
351  {
352  block->Draw( aPanel, aDC, block->GetMoveVector(), g_XorMode, block->GetColor() );
353  component->Draw( aPanel, aDC, block->GetMoveVector(), unit, convert, opts );
354  }
355 
356  // Repaint new view
357  block->SetMoveVector( parent->GetCrossHairPosition() - block->GetLastCursorPosition() );
358 
359  GRSetDrawMode( aDC, g_XorMode );
360  block->Draw( aPanel, aDC, block->GetMoveVector(), g_XorMode, block->GetColor() );
361 
362  component->Draw( aPanel, aDC, block->GetMoveVector(), unit, convert, opts );
363 }
GR_DRAWMODE g_XorMode
Definition: gr_basic.cpp:51
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDc, const wxPoint &aOffset, int aMulti, int aConvert, const PART_DRAW_OPTIONS &aOpts)
Draw part.
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
virtual int BlockCommand(EDA_KEY aKey) override
Function BlockCommand returns the block command (BLOCK_MOVE, BLOCK_COPY...) corresponding to the aKey...
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:290
static int m_convert
Definition: libeditframe.h:99
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor)
This file is part of the common library.
int GetCurrentCursor() const
Function GetCurrentCursor return the current cursor shape, depending on the current selected tool...
void SetMoveVector(const wxPoint &aMoveVector)
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
void EndMouseCapture(int aId=-1, int aCursorId=-1, const wxString &aTitle=wxEmptyString, bool aCallEndFunc=true)
Function EndMouseCapture ends mouse a capture.
BLOCK_COMMAND_T
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
BLOCK_STATE_T
void ClearSelectedItems()
Clears all the draw items marked by a block select.
void DrawAndSizingBlockOutlines(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Function DrawAndSizingBlockOutlines redraws the outlines of the block which shows the search area for...
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:348
#define GR_KB_ALT
Definition: common.h:65
void CallEndMouseCapture(wxDC *aDC)
Function CallEndMouseCapture calls the end mouse capture callback.
virtual bool HandleBlockEnd(wxDC *DC) override
Function HandleBlockEnd performs a block end command.
#define GR_KEY_INVALID
Definition: common.h:69
void MirrorSelectedItemsH(const wxPoint &aCenter)
Horizontally (X axis) mirror selected draw items about a point.
void ClearItemsList()
Function ClearItemsList clear only the list of EDA_ITEM pointers, it does NOT delete the EDA_ITEM obj...
void MirrorSelectedItemsV(const wxPoint &aCenter)
Vertically (Y axis) mirror selected draw items about a point.
void SetState(BLOCK_STATE_T aState)
COLOR4D GetColor() const
#define GR_KB_SHIFT
Definition: common.h:66
bool IsMouseCaptured() const
void CopySelectedItems(const wxPoint &aOffset)
Make a copy of the selected draw items marked by a block select.
void RotateSelectedItems(const wxPoint &aCenter)
Rotate CCW selected draw items about a point.
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
void CallMouseCapture(wxDC *aDC, const wxPoint &aPosition, bool aErase)
Function CallMouseCapture calls the mouse capture callback.
Class LIB_PART defines a library part object.
uint32_t EDA_KEY
Definition: common.h:52
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:175
int GetBottom() const
wxPoint Centre() const
static PART_DRAW_OPTIONS Default()
#define GR_KB_SHIFTCTRL
Definition: common.h:67
void SetMouseCaptureCallback(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback)
Definition: solve.cpp:178
void AbortBlockCurrentCommand(EDA_DRAW_PANEL *aPanel, wxDC *aDC)
Function AbortBlockCurrentCommand cancels the current block operation.
#define GR_KEY_NONE
Definition: common.h:70
wxPoint GetLastCursorPosition() const
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
wxPoint GetNearestGridPosition(const wxPoint &aPosition, wxRealPoint *aGridSize=NULL) const
Function GetNearestGridPosition returns the nearest aGridSize location to aPosition.
The component library editor main window.
Definition: libeditframe.h:51
int SelectItems(EDA_RECT &aRect, int aUnit, int aConvert, bool aEditPinByPin)
Checks all draw objects of part to see if they are with block.
static int m_unit
Definition: libeditframe.h:95
BLOCK_STATE_T GetState() const
virtual void HandleBlockPlace(wxDC *DC) override
Function HandleBlockPlace handles the block place command.
int GetToolId() const
Definition: draw_frame.h:406
#define MOUSE_MIDDLE
Definition: common.h:68
void SetCurItem(SCH_ITEM *aItem)
Function SetCurItem sets the currently selected object, m_CurrentItem.
LIB_PART * GetCurPart()
Function GetCurPart returns the current part being edited, or NULL if none selected.
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:188
void MoveSelectedItems(const wxPoint &aOffset)
Move the selected draw items marked by a block select.
void Window_Zoom(EDA_RECT &Rect)
Compute the zoom factor and the new draw offset to draw the selected area (Rect) in full window scree...
Definition: zoom.cpp:103
static void DrawMovingBlockOutlines(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
BLOCK_COMMAND_T GetCommand() const
#define EDA_KEY_C
Definition: common.h:60
Definition for part library class.
void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
void DeleteSelectedItems()
Deletes the select draw items marked by a block select.
#define GR_KB_CTRL
Definition: common.h:64
bool m_editPinsPerPartOrConvert
Set to true to not synchronize pins at the same position when editing components with multiple parts ...
Definition: libeditframe.h:77
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...
Definition of class LIB_EDIT_FRAME.
void SetCommand(BLOCK_COMMAND_T aCommand)
wxPoint GetMoveVector() const
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy)
Function SaveCopyInUndoList.