KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_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 The 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 <kiplatform/ui.h>
25#include <tool/tool_manager.h>
29#include <status_popup.h>
30#include <board_commit.h>
32#include <pcb_group.h>
33#include <collectors.h>
34#include <footprint.h>
35
36
37std::shared_ptr<COMMIT> PCB_GROUP_TOOL::createCommit()
38{
39 return std::make_shared<BOARD_COMMIT>( m_toolMgr, m_frame->IsType( FRAME_PCB_EDITOR ),
41}
42
43
45{
46 bool isFootprintEditor = m_frame->GetFrameType() == FRAME_FOOTPRINT_EDITOR;
47 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
48 PCB_PICKER_TOOL* picker = m_toolMgr->GetTool<PCB_PICKER_TOOL>();
49
50 STATUS_TEXT_POPUP statusPopup( m_frame );
51 bool done = false;
52
54 m_propertiesDialog->Show( false );
55
56 Activate();
57
58 statusPopup.SetText( _( "Click on new member..." ) );
59
60 picker->SetCursor( KICURSOR::BULLSEYE );
61 picker->SetSnapping( false );
62 picker->ClearHandlers();
63
64 picker->SetClickHandler(
65 [&]( const VECTOR2D& aPoint ) -> bool
66 {
68
69 const PCB_SELECTION& sel = selTool->RequestSelection(
70 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
71 {
72 } );
73
74 if( sel.Empty() )
75 return true; // still looking for an item
76
77 statusPopup.Hide();
78
80 {
81 EDA_ITEM* elem = sel.Front();
82
83 if( !isFootprintEditor )
84 {
85 while( elem->GetParent() && elem->GetParent()->Type() != PCB_T )
86 elem = elem->GetParent();
87 }
88
89 m_propertiesDialog->DoAddMember( elem );
90 m_propertiesDialog->Show( true );
91 }
92
93 return false; // got our item; don't need any more
94 } );
95
96 picker->SetMotionHandler(
97 [&]( const VECTOR2D& aPos )
98 {
99 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
100 } );
101
102 picker->SetCancelHandler(
103 [&]()
104 {
106 m_propertiesDialog->Show( true );
107
108 statusPopup.Hide();
109 } );
110
111 picker->SetFinalizeHandler(
112 [&]( const int& aFinalState )
113 {
114 done = true;
115 } );
116
117 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
118 statusPopup.Popup();
119 m_frame->GetCanvas()->SetStatusPopup( statusPopup.GetPanel() );
120
121 m_toolMgr->RunAction( ACTIONS::pickerTool, &aEvent );
122
123 while( !done )
124 {
125 // Pass events unless we receive a null event, then we must shut down
126 if( TOOL_EVENT* evt = Wait() )
127 evt->SetPassEvent();
128 else
129 break;
130 }
131
132 picker->ClearHandlers();
133 m_frame->GetCanvas()->SetStatusPopup( nullptr );
134
135 return 0;
136}
137
138
140{
141 bool isFootprintEditor = m_frame->GetFrameType() == FRAME_FOOTPRINT_EDITOR;
142 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
143 PCB_SELECTION selection;
144
145 if( isFootprintEditor )
146 {
147 selection = selTool->RequestSelection(
148 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
149 {
150 // Iterate from the back so we don't have to worry about removals.
151 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
152 {
153 BOARD_ITEM* item = aCollector[i];
154
155 if( !item->IsGroupableType() )
156 aCollector.Remove( item );
157 }
158 } );
159 }
160 else
161 {
162 selection = selTool->RequestSelection(
163 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
164 {
165 // Iterate from the back so we don't have to worry about removals.
166 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
167 {
168 BOARD_ITEM* item = aCollector[i];
169
170 if( item->GetParentFootprint() )
171 aCollector.Remove( item );
172
173 if( !item->IsGroupableType() )
174 aCollector.Remove( item );
175 }
176 } );
177 }
178
179 if( selection.Empty() )
180 return 0;
181
182 BOARD* board = getModel<BOARD>();
183 PCB_GROUP* group = nullptr;
184
185 if( isFootprintEditor )
186 group = new PCB_GROUP( board->GetFirstFootprint() );
187 else
188 group = new PCB_GROUP( board );
189
190 for( EDA_ITEM* eda_item : selection )
191 {
192 if( eda_item->IsBOARD_ITEM() )
193 {
194 if( static_cast<BOARD_ITEM*>( eda_item )->IsLocked() )
195 group->SetLocked( true );
196 }
197 }
198
199 for( EDA_ITEM* eda_item : selection )
200 {
201 if( eda_item->IsBOARD_ITEM() )
202 {
203 if( EDA_GROUP* existingGroup = eda_item->GetParentGroup() )
204 m_commit->Modify( existingGroup->AsEdaItem(), nullptr, RECURSE_MODE::NO_RECURSE );
205
206 m_commit->Modify( eda_item );
207 group->AddItem( eda_item );
208 }
209 }
210
211 m_commit->Add( group );
212 m_commit->Push( _( "Group Items" ) );
213
215 m_toolMgr->RunAction( ACTIONS::selectItem, group->AsEdaItem() );
216
218 m_frame->OnModify();
219
220 return 0;
221}
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:226
static TOOL_ACTION pickerTool
Definition actions.h:252
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:223
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
bool IsGroupableType() const
FOOTPRINT * GetParentFootprint() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:317
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition board.h:488
int GetCount() const
Return the number of objects in the list.
Definition collector.h:83
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:111
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM * GetParent() const
Definition eda_item.h:112
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition actions.h:353
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:207
DIALOG_GROUP_PROPERTIES * m_propertiesDialog
Definition group_tool.h:82
EDA_DRAW_FRAME * m_frame
Definition group_tool.h:81
std::shared_ptr< COMMIT > m_commit
Definition group_tool.h:84
int PickNewMember(const TOOL_EVENT &aEvent) override
Invoke the picker tool to select a new member of the group.
int Group(const TOOL_EVENT &aEvent) override
Ungroup selected items.
std::shared_ptr< COMMIT > createCommit() override
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:53
Generic tool for picking an item.
The selection tool: currently supports:
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter)
Return the current selection, filtered according to aClientFilter.
EDA_ITEM * Front() const
Definition selection.h:177
bool Empty() const
Checks if there is anything selected.
Definition selection.h:115
Extension of STATUS_POPUP for displaying a single line text.
T * getModel() const
Return the model object if it matches the requested type.
Definition tool_base.h:198
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
Generic, UI-independent tool event.
Definition tool_event.h:171
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.
void Activate()
Run the tool.
@ BULLSEYE
Definition cursors.h:56
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
@ FRAME_PCB_EDITOR
Definition frame_type.h:42
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:43
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Definition wxgtk/ui.cpp:689
Class to handle a set of BOARD_ITEMs.
@ PCB_T
Definition typeinfo.h:82
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694