KiCad PCB EDA Suite
group_tool.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) 2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <tool/tool_manager.h>
25 #include <tools/pcb_actions.h>
27 #include <tools/group_tool.h>
28 #include <tools/pcb_picker_tool.h>
29 #include <status_popup.h>
30 #include <board_commit.h>
31 #include <bitmaps.h>
33 
34 
36 {
37 public:
39  {
40  SetIcon( group_xpm ); // fixme
41  SetTitle( _( "Grouping" ) );
42 
47  }
48 
49  ACTION_MENU* create() const override
50  {
51  return new GROUP_CONTEXT_MENU();
52  }
53 
54 private:
55  void update() override
56  {
58  BOARD* board = selTool->GetBoard();
59 
60  const auto& selection = selTool->GetSelection();
61 
62  wxString check = board->GroupsSanityCheck();
63  wxCHECK_RET( check == wxEmptyString, _( "Group is in inconsistent state:" ) + wxS( " " )+ check );
64 
65  BOARD::GroupLegalOpsField legalOps = board->GroupLegalOps( selection );
66 
67  Enable( PCB_ACTIONS::group.GetUIId(), legalOps.create );
68  Enable( PCB_ACTIONS::ungroup.GetUIId(), legalOps.ungroup );
69  Enable( PCB_ACTIONS::removeFromGroup.GetUIId(), legalOps.removeItems );
70  Enable( PCB_ACTIONS::groupEnter.GetUIId(), legalOps.enter );
71  }
72 };
73 
74 
76  PCB_TOOL_BASE( "pcbnew.Groups" ),
77  m_frame( nullptr ),
78  m_propertiesDialog( nullptr ),
79  m_selectionTool( nullptr )
80 {
81 }
82 
83 
85 {
86  m_frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
87 
88  if( aReason != RUN )
89  m_commit = std::make_unique<BOARD_COMMIT>( this );
90 }
91 
92 
94 {
95  m_frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
96 
97  // Find the selection tool, so they can cooperate
99 
100  auto groupMenu = std::make_shared<GROUP_CONTEXT_MENU>();
101  groupMenu->SetTool( this );
102 
103  // Add the group control menus to relevant other tools
104  if( m_selectionTool )
105  {
106  auto& toolMenu = m_selectionTool->GetToolMenu();
107  auto& menu = toolMenu.GetMenu();
108 
109  toolMenu.AddSubMenu( groupMenu );
110 
111  menu.AddMenu( groupMenu.get(), SELECTION_CONDITIONS::NotEmpty, 100 );
112  }
113 
114  return true;
115 }
116 
117 
119 {
120  PCB_BASE_EDIT_FRAME* editFrame = getEditFrame<PCB_BASE_EDIT_FRAME>();
121  PCB_GROUP* group = aEvent.Parameter<PCB_GROUP*>();
122 
123  if( m_propertiesDialog )
124  m_propertiesDialog->Destroy();
125 
126  m_propertiesDialog = new DIALOG_GROUP_PROPERTIES( editFrame, group );
127 
128  m_propertiesDialog->Show( true );
129 
130  return 0;
131 }
132 
133 
135 {
136  std::string tool = "pcbnew.EditGroups.selectNewMember";
138  STATUS_TEXT_POPUP statusPopup( frame() );
139  bool done = false;
140 
141  if( m_propertiesDialog )
142  m_propertiesDialog->Show( false );
143 
144  Activate();
145 
146  statusPopup.SetText( _( "Click on new member..." ) );
147 
148  picker->SetClickHandler(
149  [&]( const VECTOR2D& aPoint ) -> bool
150  {
152 
154  []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector,
155  PCB_SELECTION_TOOL* sTool )
156  {
157  } );
158 
159  if( sel.Empty() )
160  return true; // still looking for an item
161 
162  statusPopup.Hide();
163 
164  if( m_propertiesDialog )
165  {
167  m_propertiesDialog->Show( true );
168  }
169 
170  return false; // got our item; don't need any more
171  } );
172 
173  picker->SetMotionHandler(
174  [&] ( const VECTOR2D& aPos )
175  {
176  statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
177  } );
178 
179  picker->SetCancelHandler(
180  [&]()
181  {
182  if( m_propertiesDialog )
183  m_propertiesDialog->Show( true );
184 
185  statusPopup.Hide();
186  } );
187 
188  picker->SetFinalizeHandler(
189  [&]( const int& aFinalState )
190  {
191  done = true;
192  } );
193 
194  statusPopup.Move( wxGetMousePosition() + wxPoint( 20, -50 ) );
195  statusPopup.Popup();
196 
197  m_toolMgr->RunAction( ACTIONS::pickerTool, true, &tool );
198 
199  while( !done )
200  {
201  // Pass events unless we receive a null event, then we must shut down
202  if( TOOL_EVENT* evt = Wait() )
203  evt->SetPassEvent();
204  else
205  break;
206  }
207 
208  return 0;
209 }
210 
211 
212 int GROUP_TOOL::Group( const TOOL_EVENT& aEvent )
213 {
216 
217  if( m_isFootprintEditor )
218  {
219  selection = selTool->RequestSelection(
220  []( const VECTOR2I& , GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
221  {
222  } );
223  }
224  else
225  {
226  selection = selTool->RequestSelection(
227  []( const VECTOR2I& , GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
228  {
229  // Iterate from the back so we don't have to worry about removals.
230  for( int i = aCollector.GetCount() - 1; i >= 0; --i )
231  {
232  BOARD_ITEM* item = aCollector[ i ];
233 
234  switch( item->Type() )
235  {
236  case PCB_FP_TEXT_T:
237  case PCB_FP_SHAPE_T:
238  case PCB_FP_ZONE_T:
239  aCollector.Remove( item );
240  break;
241 
242  default:
243  break;
244  }
245  }
246  } );
247  }
248 
249  if( selection.Empty() )
250  return 0;
251 
252  BOARD* board = getModel<BOARD>();
253  PCB_GROUP* group = nullptr;
254 
255  if( m_isFootprintEditor )
256  {
257  FOOTPRINT* parentFootprint = board->GetFirstFootprint();
258 
259  m_frame->SaveCopyInUndoList( parentFootprint, UNDO_REDO::CHANGED );
260 
261  group = new PCB_GROUP( parentFootprint );
262  parentFootprint->Add( group );
263 
264  for( EDA_ITEM* item : selection )
265  group->AddItem( static_cast<BOARD_ITEM*>( item ) );
266  }
267  else
268  {
269  PICKED_ITEMS_LIST undoList;
270 
271  group = new PCB_GROUP( board );
272  board->Add( group );
273 
274  undoList.PushItem( ITEM_PICKER( nullptr, group, UNDO_REDO::NEWITEM ) );
275 
276  for( EDA_ITEM* item : selection )
277  {
278  group->AddItem( static_cast<BOARD_ITEM*>( item ) );
279  undoList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::GROUP ) );
280  }
281 
283  }
284 
285  selTool->ClearSelection();
286  selTool->select( group );
287 
289  m_frame->OnModify();
290 
291  return 0;
292 }
293 
294 
295 int GROUP_TOOL::Ungroup( const TOOL_EVENT& aEvent )
296 {
297  const PCB_SELECTION& selection = m_toolMgr->GetTool<PCB_SELECTION_TOOL>()->GetSelection();
298  BOARD* board = getModel<BOARD>();
299  std::vector<BOARD_ITEM*> members;
300 
301  if( selection.Empty() )
303 
304  PCB_SELECTION selCopy = selection;
306 
307  for( EDA_ITEM* item : selCopy )
308  {
309  PCB_GROUP* group = dynamic_cast<PCB_GROUP*>( item );
310 
311  if( group )
312  {
313  if( m_isFootprintEditor )
314  {
315  FOOTPRINT* parentFootprint = board->GetFirstFootprint();
316 
317  m_frame->SaveCopyInUndoList( parentFootprint, UNDO_REDO::CHANGED );
318 
319  group->RemoveAll();
320  parentFootprint->Remove( group );
321  }
322  else
323  {
324  PICKED_ITEMS_LIST undoList;
325 
326  for( BOARD_ITEM* member : group->GetItems() )
327  {
328  undoList.PushItem( ITEM_PICKER( nullptr, member, UNDO_REDO::UNGROUP ) );
329  members.push_back( member );
330  }
331 
332  group->RemoveAll();
333  board->Remove( group );
334 
335  undoList.PushItem( ITEM_PICKER( nullptr, group, UNDO_REDO::DELETED ) );
336 
338  }
339 
340  group->SetSelected();
341  }
342  }
343 
344  m_toolMgr->RunAction( PCB_ACTIONS::selectItems, true, &members );
345 
347  m_frame->OnModify();
348 
349  return 0;
350 }
351 
352 
354 {
356  const PCB_SELECTION& selection = selTool->GetSelection();
357  BOARD_COMMIT commit( m_frame );
358 
359  if( selection.Empty() )
361 
362  std::map<PCB_GROUP*, std::vector<BOARD_ITEM*>> groupMap;
363 
364  for( EDA_ITEM* item : selection )
365  {
366  BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
367  PCB_GROUP* group = boardItem->GetParentGroup();
368 
369  if( group )
370  groupMap[ group ].push_back( boardItem );
371  }
372 
373  for( std::pair<PCB_GROUP*, std::vector<BOARD_ITEM*>> pair : groupMap )
374  {
375  commit.Modify( pair.first );
376 
377  for( BOARD_ITEM* item : pair.second )
378  pair.first->RemoveItem( item );
379  }
380 
381  commit.Push( "Remove Group Items" );
382 
384  m_frame->OnModify();
385 
386  return 0;
387 }
388 
389 
390 int GROUP_TOOL::EnterGroup( const TOOL_EVENT& aEvent )
391 {
393  const PCB_SELECTION& selection = selTool->GetSelection();
394 
395  if( selection.GetSize() == 1 && selection[0]->Type() == PCB_GROUP_T )
396  selTool->EnterGroup();
397 
398  return 0;
399 }
400 
401 
402 int GROUP_TOOL::LeaveGroup( const TOOL_EVENT& aEvent )
403 {
404  m_toolMgr->GetTool<PCB_SELECTION_TOOL>()->ExitGroup( true /* Select the group */ );
405  return 0;
406 }
407 
408 
410 {
413 
414  Go( &GROUP_TOOL::Group, PCB_ACTIONS::group.MakeEvent() );
419 }
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: pcb_actions.h:70
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:63
int LeaveGroup(const TOOL_EVENT &aEvent)
Definition: group_tool.cpp:402
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:93
void RemoveAll()
Definition: pcb_group.cpp:63
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Add a submenu to the menu.
int GroupProperties(const TOOL_EVENT &aEvent)
Definition: group_tool.cpp:118
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:38
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Add a wxWidgets-style entry to the menu.
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
void select(BOARD_ITEM *aItem)
Take necessary action mark an item as selected.
static TOOL_ACTION groupLeave
Definition: pcb_actions.h:427
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
void SaveCopyInUndoList(EDA_ITEM *aItemToCopy, UNDO_REDO aTypeCommand) override
Create a new entry in undo list of commands.
Definition: undo_redo.cpp:177
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.
BOARD * board() const
void update() override
Update menu state stub.
Definition: group_tool.cpp:55
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:45
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
void setTransitions() override
< Set up handlers for various events.
Definition: group_tool.cpp:409
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:50
Extension of STATUS_POPUP for displaying a single line text.
Definition: status_popup.h:79
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:46
Tool is invoked after being inactive.
Definition: tool_base.h:80
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
void Remove(BOARD_ITEM *aItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
Definition: footprint.cpp:520
TOOL_MENU & GetToolMenu()
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:141
PCB_BASE_EDIT_FRAME * m_frame
Definition: group_tool.h:73
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
static TOOL_ACTION group
Definition: pcb_actions.h:423
void SetFinalizeHandler(FINALIZE_HANDLER aHandler)
Set a handler for the finalize event.
Definition: picker_tool.h:100
static TOOL_ACTION ungroup
Definition: pcb_actions.h:424
std::unordered_set< BOARD_ITEM * > & GetItems()
Definition: pcb_group.h:68
static bool NotEmpty(const SELECTION &aSelection)
Test if there are any items selected.
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).
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition: collector.h:115
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:214
int RemoveFromGroup(const TOOL_EVENT &aEvent)
Restrict selection to only member of the group.
Definition: group_tool.cpp:353
static TOOL_ACTION pickerTool
Definition: actions.h:158
PCB_BASE_EDIT_FRAME * frame() const
ACTION_MENU * create() const override
< Return an instance of this class. It has to be overridden in inheriting classes.
Definition: group_tool.cpp:49
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:87
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
static TOOL_ACTION removeFromGroup
Definition: pcb_actions.h:425
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Adds an item to the container.
Definition: board.cpp:563
const PCB_SELECTION & selection() const
FOOTPRINT * GetFirstFootprint() const
Gets the first footprint on the board or nullptr.
Definition: board.h:380
std::unique_ptr< BOARD_COMMIT > m_commit
Definition: group_tool.h:76
void SetSelected()
Definition: eda_item.h:178
PCB_SELECTION_TOOL * m_selectionTool
Definition: group_tool.h:75
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: group_tool.cpp:84
DIALOG_GROUP_PROPERTIES * m_propertiesDialog
Definition: group_tool.h:74
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:443
void SetIcon(const BITMAP_OPAQUE *aIcon)
Assign an icon for the entry.
Definition: action_menu.cpp:72
Generic, UI-independent tool event.
Definition: tool_event.h:173
void SetMotionHandler(MOTION_HANDLER aHandler)
Set a handler for mouse motion.
Definition: picker_tool.h:80
static TOOL_ACTION pickNewGroupMember
Definition: pcb_actions.h:221
void DoAddMember(EDA_ITEM *aItem)
bool Init() override
Init() is called once upon a registration of the tool.
Definition: group_tool.cpp:93
A holder to handle information on schematic or board items.
bool m_isFootprintEditor
bool Show(bool show) override
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:122
Generic tool for picking an item.
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:101
void SetClickHandler(CLICK_HANDLER aHandler)
Set a handler for mouse click event.
Definition: picker_tool.h:69
int Ungroup(const TOOL_EVENT &aEvent)
Remove selection from group.
Definition: group_tool.cpp:295
virtual void OnModify()
Must be called after a change in order to set the "modify" flag of the current screen and update the ...
void SetTitle(const wxString &aTitle) override
Set title for the menu.
Definition: action_menu.cpp:90
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter, bool aConfirmLockedItems=false)
Return the current selection set, filtered according to aFlags and aClientFilter.
Common, abstract interface for edit frames.
const BITMAP_OPAQUE group_xpm[1]
Definition: group.cpp:21
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define _(s)
Definition: 3d_actions.cpp:33
TOOL_MANAGER * getToolManager() const
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:241
BOARD * GetBoard() const
class ZONE, managed by a footprint
Definition: typeinfo.h:94
The selection tool: currently supports:
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
void Remove(BOARD_ITEM *aBoardItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
Definition: board.cpp:665
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
int Group(const TOOL_EVENT &aEvent)
Ungroup selected items.
Definition: group_tool.cpp:212
void Activate()
Run the tool.
int PickNewMember(const TOOL_EVENT &aEvent)
Invoke the picker tool to select a new member of the group.
Definition: group_tool.cpp:134
int EnterGroup(const TOOL_EVENT &aEvent)
Leave the current group (deselect its members and select the group as a whole).
Definition: group_tool.cpp:390
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:467
int ClearSelection(const TOOL_EVENT &aEvent)
static TOOL_ACTION groupEnter
Definition: pcb_actions.h:426
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:267
static TOOL_ACTION selectionCursor
Select a single item under the cursor position.
Definition: pcb_actions.h:60
static TOOL_ACTION groupProperties
Definition: pcb_actions.h:220
void SetCancelHandler(CANCEL_HANDLER aHandler)
Set a handler for cancel events (ESC or context-menu Cancel).
Definition: picker_tool.h:89
EDA_ITEM * Front() const
Definition: selection.h:203
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162