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 m_propertiesDialog->DoAddMember( elem );
115 m_propertiesDialog->Show( true );
116 }
117
118 return false; // got our item; don't need any more
119 } );
120
121 picker->SetMotionHandler(
122 [&]( const VECTOR2D& aPos )
123 {
124 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
125 } );
126
127 picker->SetCancelHandler(
128 [&]()
129 {
131 m_propertiesDialog->Show( true );
132
133 statusPopup.Hide();
134 } );
135
136 picker->SetFinalizeHandler(
137 [&]( const int& aFinalState )
138 {
139 done = true;
140 } );
141
142 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
143 statusPopup.Popup();
144 m_frame->GetCanvas()->SetStatusPopup( statusPopup.GetPanel() );
145
146 m_toolMgr->RunAction( ACTIONS::pickerTool, &aEvent );
147
148 while( !done )
149 {
150 // Pass events unless we receive a null event, then we must shut down
151 if( TOOL_EVENT* evt = Wait() )
152 evt->SetPassEvent();
153 else
154 break;
155 }
156
157 picker->ClearHandlers();
158 m_frame->GetCanvas()->SetStatusPopup( nullptr );
159
160 return 0;
161}
162
163
165{
166 bool isSymbolEditor = m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR;
167 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
168 SCH_SELECTION selection = selTool->RequestSelection();
169 wxString errorMsg;
170
171 // Iterate from the back so we don't have to worry about removals.
172 for( int ii = selection.GetSize() - 1; ii >= 0; --ii )
173 {
174 EDA_ITEM* item = selection[ii];
175
176 if( !canGroupItem( item, errorMsg ) )
177 selection.Remove( item );
178 }
179
180 if( selection.GetSize() < 2 )
181 {
182 if( !errorMsg.IsEmpty() )
183 m_frame->ShowInfoBarWarning( errorMsg );
184
185 return 0;
186 }
187
189 SCH_SCREEN* screen = static_cast<SCH_BASE_FRAME*>( m_frame )->GetScreen();
190
191 if( isSymbolEditor )
192 group->SetParent( static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurSymbol() );
193 else
194 group->SetParent( screen );
195
196 for( EDA_ITEM* eda_item : selection )
197 {
198 if( EDA_GROUP* existingGroup = eda_item->GetParentGroup() )
199 m_commit->Modify( existingGroup->AsEdaItem(), screen, RECURSE_MODE::NO_RECURSE );
200
201 m_commit->Modify( eda_item, screen, RECURSE_MODE::NO_RECURSE );
202 group->AddItem( eda_item );
203 }
204
205 m_commit->Add( group, screen );
206 m_commit->Push( _( "Group Items" ) );
207
209 m_toolMgr->RunAction( ACTIONS::selectItem, group->AsEdaItem() );
210
212 m_frame->OnModify();
213
214 if( !errorMsg.IsEmpty() )
215 m_frame->ShowInfoBarWarning( errorMsg );
216
217 return 0;
218}
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: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
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
Get the correctly casted group type from the item.
int Group(const TOOL_EVENT &aEvent) override
Ungroup 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:162
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:274
bool IsGroupableType() const
Definition sch_item.cpp:113
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:173
bool Empty() const
Checks if there is anything selected.
Definition selection.h:111
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:50
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Definition wxgtk/ui.cpp:766
Class to handle a set of SCH_ITEMs.
@ SCH_SCREEN_T
Definition typeinfo.h:199
VECTOR2< double > VECTOR2D
Definition vector2d.h:682