KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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#include <tool/actions.h>
20#include <frame_type.h>
21#include <eda_draw_frame.h>
22#include <symbol_edit_frame.h>
23#include <sch_edit_frame.h>
24#include <sch_screen.h>
25#include <kiplatform/ui.h>
26#include <tool/tool_manager.h>
27#include <tool/picker_tool.h>
30#include <status_popup.h>
31#include <sch_commit.h>
33#include <sch_group.h>
34#include <symbol.h>
35#include <wx/string.h>
36
37
38bool SCH_GROUP_TOOL::canGroupItem( EDA_ITEM* aItem, wxString& aErrorMsg ) const
39{
40 if( !aItem || !aItem->IsSCH_ITEM() )
41 {
42 aErrorMsg = _( "Some selected items cannot be grouped." );
43 return false;
44 }
45
46 bool isSymbolEditor = m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR;
47 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( aItem );
48
49 if( isSymbolEditor )
50 {
51 if( schItem->GetParentSymbol() )
52 {
53 aErrorMsg = _( "Child items cannot be grouped separately from their parent item." );
54 return false;
55 }
56 }
57 else
58 {
59 if( schItem->GetParent() && schItem->GetParent()->Type() != SCH_SCREEN_T )
60 {
61 aErrorMsg = _( "Child items cannot be grouped separately from their parent item." );
62 return false;
63 }
64 }
65
66 if( !schItem->IsGroupableType() )
67 {
68 aErrorMsg = _( "Some selected items cannot be grouped." );
69 return false;
70 }
71
72 return true;
73}
74
75
77{
78 bool isSymbolEditor = m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR;
79 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
80 PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
81
82 STATUS_TEXT_POPUP statusPopup( m_frame );
83 bool done = false;
84
86 m_propertiesDialog->Show( false );
87
88 Activate();
89
90 statusPopup.SetText( _( "Click on new member..." ) );
91
92 picker->SetClickHandler(
93 [&]( const VECTOR2D& aPoint ) -> bool
94 {
96
97 const SCH_SELECTION& sel = selTool->RequestSelection();
98
99 if( sel.Empty() )
100 return true; // still looking for an item
101
102 statusPopup.Hide();
103
105 {
106 EDA_ITEM* elem = sel.Front();
107
108 if( !isSymbolEditor )
109 {
110 while( elem->GetParent() && elem->GetParent()->Type() != SCH_SCREEN_T )
111 elem = elem->GetParent();
112 }
113
114 wxString errorMsg;
115
116 if( canGroupItem( elem, errorMsg ) )
117 m_propertiesDialog->DoAddMember( elem );
118 else
119 m_frame->ShowInfoBarWarning( errorMsg );
120
121 m_propertiesDialog->Show( true );
122 }
123
124 return false; // got our item; don't need any more
125 } );
126
127 picker->SetMotionHandler(
128 [&]( const VECTOR2D& aPos )
129 {
130 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
131 } );
132
133 picker->SetCancelHandler(
134 [&]()
135 {
137 m_propertiesDialog->Show( true );
138
139 statusPopup.Hide();
140 } );
141
142 picker->SetFinalizeHandler(
143 [&]( const int& aFinalState )
144 {
145 done = true;
146 } );
147
148 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
149 statusPopup.Popup();
150 m_frame->GetCanvas()->SetStatusPopup( statusPopup.GetPanel() );
151
152 m_toolMgr->RunAction( ACTIONS::pickerTool, &aEvent );
153
154 while( !done )
155 {
156 // Pass events unless we receive a null event, then we must shut down
157 if( TOOL_EVENT* evt = Wait() )
158 evt->SetPassEvent();
159 else
160 break;
161 }
162
163 picker->ClearHandlers();
164 m_frame->GetCanvas()->SetStatusPopup( nullptr );
165
166 return 0;
167}
168
169
171{
172 bool isSymbolEditor = m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR;
173 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
174 SCH_SELECTION selection = selTool->RequestSelection();
175 wxString errorMsg;
176
177 // Iterate from the back so we don't have to worry about removals.
178 for( int ii = selection.GetSize() - 1; ii >= 0; --ii )
179 {
180 EDA_ITEM* item = selection[ii];
181
182 if( !canGroupItem( item, errorMsg ) )
183 selection.Remove( item );
184 }
185
186 if( selection.GetSize() < 2 )
187 {
188 if( !errorMsg.IsEmpty() )
189 m_frame->ShowInfoBarWarning( errorMsg );
190
191 return 0;
192 }
193
195 SCH_SCREEN* screen = static_cast<SCH_BASE_FRAME*>( m_frame )->GetScreen();
196
197 if( isSymbolEditor )
198 group->SetParent( static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurSymbol() );
199 else
200 group->SetParent( screen );
201
202 for( EDA_ITEM* eda_item : selection )
203 {
204 if( EDA_GROUP* existingGroup = eda_item->GetParentGroup() )
205 m_commit->Modify( existingGroup->AsEdaItem(), screen, RECURSE_MODE::NO_RECURSE );
206
207 m_commit->Modify( eda_item, screen, RECURSE_MODE::NO_RECURSE );
208 group->AddItem( eda_item );
209 }
210
211 m_commit->Add( group, screen );
212 m_commit->Push( _( "Group Items" ) );
213
215 m_toolMgr->RunAction( ACTIONS::selectItem, group->AsEdaItem() );
216
218 m_frame->OnModify();
219
220 if( !errorMsg.IsEmpty() )
221 m_frame->ShowInfoBarWarning( errorMsg );
222
223 return 0;
224}
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 set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:43
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 item had a property changed (except movement)
Definition actions.h:352
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 IsSCH_ITEM() const
Definition view_item.h:97
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
bool canGroupItem(EDA_ITEM *aItem, wxString &aErrorMsg) const override
Check if an item can be a direct member of a group.
int Group(const TOOL_EVENT &aEvent) override
Group selected items.
int PickNewMember(const TOOL_EVENT &aEvent) override
Invoke the picker tool to select a new member of the group.
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:48
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:350
bool IsGroupableType() const
Definition sch_item.cpp:144
SCH_SELECTION & RequestSelection(const std::vector< KICAD_T > &aScanTypes={ SCH_LOCATE_ANY_T }, bool aPromoteCellSelections=false, bool aPromoteGroups=false)
Return either an existing selection (filtered), or the selection at the current cursor position if th...
EDA_ITEM * Front() const
Definition selection.h:176
bool Empty() const
Checks if there is anything selected.
Definition selection.h:114
Extension of STATUS_POPUP for displaying a single line text.
The symbol library editor main window.
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.
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Definition wxgtk/ui.cpp:851
Class to handle a set of SCH_ITEMs.
@ SCH_SCREEN_T
Definition typeinfo.h:198
VECTOR2< double > VECTOR2D
Definition vector2d.h:707