KiCad PCB EDA Suite
pl_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 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 <class_draw_panel_gal.h>
26 #include <confirm.h>
27 #include <view/view_group.h>
28 #include <view/view_controls.h>
29 #include <view/view.h>
30 #include <tool/tool_manager.h>
31 #include <bitmaps.h>
34 
36 #include "pl_editor_frame.h"
37 #include "pl_editor_id.h"
38 #include "pl_point_editor.h"
39 #include "tools/pl_actions.h"
41 #include "tools/pl_drawing_tools.h"
42 
44  TOOL_INTERACTIVE( "plEditor.InteractiveDrawing" ),
45  m_frame( nullptr ),
46  m_selectionTool( nullptr )
47 {
48 }
49 
50 
52 {
53  m_frame = getEditFrame<PL_EDITOR_FRAME>();
55 
56  auto& ctxMenu = m_menu.GetMenu();
57 
58  // cancel current tool goes in main context menu at the top if present
60  ctxMenu.AddSeparator( 1 );
61 
62  // Finally, add the standard zoom/grid items
64 
65  return true;
66 }
67 
68 
70 {
71  if( aReason == MODEL_RELOAD )
72  m_frame = getEditFrame<PL_EDITOR_FRAME>();
73 }
74 
75 
77 {
79  VECTOR2I cursorPos;
80  DS_DRAW_ITEM_BASE* item = nullptr;
81  bool isText = aEvent.IsAction( &PL_ACTIONS::placeText );
82 
84  getViewControls()->ShowCursor( true );
85 
86  std::string tool = aEvent.GetCommandStr().get();
87  m_frame->PushTool( tool );
88  Activate();
89 
90  // Prime the pump
91  if( aEvent.HasPosition() || ( !aEvent.IsReactivate() && isText ) )
93 
94  auto setCursor =
95  [&]()
96  {
97  if( item )
99  else
101  };
102 
103  setCursor();
104 
105  // Main loop: keep receiving events
106  while( TOOL_EVENT* evt = Wait() )
107  {
108  setCursor();
109 
110  cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
111 
112  auto cleanup =
113  [&] ()
114  {
116  item = nullptr;
117 
118  // There's nothing to roll-back, but we still need to pop the undo stack
119  // This also deletes the item being placed.
121  };
122 
123  if( evt->IsCancelInteractive() )
124  {
125  if( item )
126  cleanup();
127  else
128  {
129  m_frame->PopTool( tool );
130  break;
131  }
132  }
133  else if( evt->IsActivate() )
134  {
135  if( item )
136  cleanup();
137 
138  if( evt->IsMoveTool() )
139  {
140  // leave ourselves on the stack so we come back after the move
141  break;
142  }
143  else
144  {
145  m_frame->PopTool( tool );
146  break;
147  }
148  }
149  else if( evt->IsClick( BUT_LEFT ) )
150  {
151  // First click creates...
152  if( !item )
153  {
154  DS_DATA_ITEM* dataItem = m_frame->AddPageLayoutItem( type );
155 
156  if( dataItem ) // dataItem = nullptr can happens if the command was cancelled
157  {
159 
161 
162  item = dataItem->GetDrawItems()[0];
163  item->SetFlags( IS_NEW | IS_MOVED );
164 
165  // Select the item but don't inform other tools (to prevent the Properties
166  // panel from updating the item before it has been placed)
167  m_selectionTool->AddItemToSel( item, true );
168 
169  // update the cursor so it looks correct before another event
170  setCursor();
171  }
172  }
173  // ... and second click places:
174  else
175  {
176  item->GetPeer()->MoveStartPointToUi( (wxPoint) cursorPos );
177  item->SetPosition( item->GetPeer()->GetStartPosUi( 0 ) );
178  item->ClearEditFlags();
179  getView()->Update( item );
180 
181  // Now we re-select and inform other tools, so that the Properties panel
182  // is updated.
184  m_selectionTool->AddItemToSel( item, false );
185 
186  item = nullptr;
187 
188  m_frame->OnModify();
189  }
190  }
191  else if( evt->IsClick( BUT_RIGHT ) )
192  {
193  // Warp after context menu only if dragging...
194  if( !item )
196 
198  }
199  else if( item && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
200  {
201  item->GetPeer()->MoveStartPointToUi( (wxPoint) cursorPos );
202  item->SetPosition( item->GetPeer()->GetStartPosUi( 0 ) );
203  getView()->Update( item );
204  }
205  else
206  {
207  evt->SetPassEvent();
208  }
209 
210  // Enable autopanning and cursor capture only when there is an item to be placed
211  getViewControls()->SetAutoPan( item != nullptr );
212  getViewControls()->CaptureCursor( item != nullptr );
213  }
214 
216  return 0;
217 }
218 
219 
221 {
223  DS_DRAW_ITEM_BASE* item = nullptr;
224 
225  // We might be running as the same shape in another co-routine. Make sure that one
226  // gets whacked.
228 
230  getViewControls()->ShowCursor( true );
231 
232  std::string tool = aEvent.GetCommandStr().get();
233  m_frame->PushTool( tool );
234  Activate();
235 
236  // Prime the pump
237  if( aEvent.HasPosition() )
239 
240  auto setCursor =
241  [&]()
242  {
244  };
245 
246  // Set initial cursor
247  setCursor();
248 
249  // Main loop: keep receiving events
250  while( TOOL_EVENT* evt = Wait() )
251  {
252  setCursor();
253 
254  VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
255 
256  if( evt->IsCancelInteractive() || evt->IsActivate() )
257  {
259 
260  if( item )
261  {
262  item = nullptr;
263 
264  // Pop the undo stack and delete the item being placed
266  }
267  else if( evt->IsCancelInteractive() )
268  {
269  break;
270  }
271 
272  if( evt->IsActivate() && !evt->IsPointEditor() && !evt->IsMoveTool() )
273  break;
274  }
275  else if( evt->IsClick( BUT_LEFT ) )
276  {
277  if( !item ) // start drawing
278  {
281 
282  DS_DATA_ITEM* dataItem = m_frame->AddPageLayoutItem( type );
283  dataItem->MoveToUi( (wxPoint) cursorPos );
284 
285  item = dataItem->GetDrawItems()[0];
286  item->SetFlags( IS_NEW );
287 
288  // Select the item but don't inform other tools (to prevent the Properties
289  // panel from updating the item before it has been placed)
290  m_selectionTool->AddItemToSel( item, true );
291  }
292  else // finish drawing
293  {
294  // Now we re-select and inform other tools, so that the Properties panel
295  // is updated.
297  m_selectionTool->AddItemToSel( item, false );
298 
299  item->ClearEditFlags();
300  item = nullptr;
301 
302  // Activate point editor immediately to allow resizing of the item just created
304 
305  m_frame->OnModify();
306  }
307  }
308  else if( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() )
309  {
310  if( item )
311  {
312  item->GetPeer()->MoveEndPointToUi( (wxPoint) cursorPos );
313  item->SetEnd( item->GetPeer()->GetEndPosUi( 0 ) );
314  getView()->Update( item );
315  }
316  }
317  else if( evt->IsClick( BUT_RIGHT ) )
318  {
319  // Warp after context menu only if dragging...
320  if( !item )
322 
324  }
325  else
326  {
327  evt->SetPassEvent();
328  }
329 
330  // Enable autopanning and cursor capture only when there is a shape being drawn
331  getViewControls()->SetAutoPan( item != nullptr );
332  getViewControls()->CaptureCursor( item != nullptr );
333  }
334 
336  m_frame->PopTool( tool );
337  return 0;
338 }
339 
340 
342 {
347 }
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
void AddStandardSubMenus(TOOL_MENU &aMenu)
Construct a "basic" menu for a tool, containing only items that apply to all tools (e....
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
void setTransitions() override
< Set up handlers for various events.
PL_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
static TOOL_ACTION activatePointEditor
Definition: actions.h:171
Model changes (required full reload)
Definition: tool_base.h:81
This file is part of the common library.
virtual void SetPosition(const wxPoint &aPos)
Definition: eda_item.h:302
static TOOL_ACTION drawLine
Definition: pl_actions.h:65
static TOOL_ACTION placeImage
Definition: pl_actions.h:63
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:141
static TOOL_ACTION placeText
Definition: pl_actions.h:62
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
int AddItemToSel(const TOOL_EVENT &aEvent)
void DeactivateTool()
Deactivate the currently active tool.
void SetCurrentCursor(KICURSOR cursor)
Set the current cursor shape for this panel.
PL_SELECTION & GetSelection()
Return the set of currently selected items.
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:70
void OnModify()
Must be called after a change in order to set the "modify" flag.
Base class to handle basic graphic items.
Definition: ds_draw_item.h:58
int PlaceItem(const TOOL_EVENT &aEvent)
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:202
int DrawShape(const TOOL_EVENT &aEvent)
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:443
bool Init() override
Init() is called once upon a registration of the tool.
Generic, UI-independent tool event.
Definition: tool_event.h:173
const std::vector< DS_DRAW_ITEM_BASE * > & GetDrawItems() const
Definition: ds_data_item.h:111
static TOOL_ACTION drawRectangle
Definition: pl_actions.h:64
#define IS_MOVED
Item being moved.
Definition: eda_item.h:105
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
PL_SELECTION_TOOL * m_selectionTool
static TOOL_ACTION clearSelection
Clear the current selection.
Definition: pl_actions.h:47
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:471
DS_DATA_ITEM * GetPeer() const
Definition: ds_draw_item.h:63
Drawing sheet structure type definitions.
Definition: ds_data_item.h:95
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:428
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
bool IsReactivate() const
Definition: tool_event.h:273
void SaveCopyInUndoList()
Save a copy of the description (in a S expr string) for Undo/redo commands.
void Activate()
Run the tool.
void MoveToUi(wxPoint aPosition)
Move item to a new position.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
bool HasPosition() const
Definition: tool_event.h:261
void MoveStartPointToUi(wxPoint aPosition)
Move the starting point of the item to a new position.
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
PL_EDITOR_FRAME * m_frame
void ClearEditFlags()
Definition: eda_item.h:221
DS_DATA_ITEM * AddPageLayoutItem(int aType)
Function AddPageLayoutItem Add a new item to the page layout item list.
void RollbackFromUndo()
Apply the last command in Undo List without stacking a Redo.
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
#define IS_NEW
New item, just created.
Definition: eda_item.h:106
static TOOL_ACTION refreshPreview
Definition: actions.h:109
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
static TOOL_ACTION cursorClick
Definition: actions.h:126
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1508
const wxPoint GetStartPosUi(int ii=0) const