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