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, 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#include <tool/actions.h>
24#include <frame_type.h>
25#include <eda_draw_frame.h>
26#include <symbol_edit_frame.h>
27#include <sch_edit_frame.h>
28#include <sch_screen.h>
29#include <kiplatform/ui.h>
30#include <tool/tool_manager.h>
31#include <tool/picker_tool.h>
34#include <status_popup.h>
35#include <sch_commit.h>
37#include <sch_group.h>
38#include <symbol.h>
39
40
42{
43 bool isSymbolEditor = m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR;
44 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
45 PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
46
47 STATUS_TEXT_POPUP statusPopup( m_frame );
48 bool done = false;
49
51 m_propertiesDialog->Show( false );
52
53 Activate();
54
55 statusPopup.SetText( _( "Click on new member..." ) );
56
57 picker->SetClickHandler(
58 [&]( const VECTOR2D& aPoint ) -> bool
59 {
61
62 const SCH_SELECTION& sel = selTool->RequestSelection();
63
64 if( sel.Empty() )
65 return true; // still looking for an item
66
67 statusPopup.Hide();
68
70 {
71 EDA_ITEM* elem = sel.Front();
72
73 if( !isSymbolEditor )
74 {
75 while( elem->GetParent() && elem->GetParent()->Type() != SCH_SCREEN_T )
76 elem = elem->GetParent();
77 }
78
79 m_propertiesDialog->DoAddMember( elem );
80 m_propertiesDialog->Show( true );
81 }
82
83 return false; // got our item; don't need any more
84 } );
85
86 picker->SetMotionHandler(
87 [&]( const VECTOR2D& aPos )
88 {
89 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
90 } );
91
92 picker->SetCancelHandler(
93 [&]()
94 {
96 m_propertiesDialog->Show( true );
97
98 statusPopup.Hide();
99 } );
100
101 picker->SetFinalizeHandler(
102 [&]( const int& aFinalState )
103 {
104 done = true;
105 } );
106
107 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
108 statusPopup.Popup();
109 m_frame->GetCanvas()->SetStatusPopup( statusPopup.GetPanel() );
110
111 m_toolMgr->RunAction( ACTIONS::pickerTool, &aEvent );
112
113 while( !done )
114 {
115 // Pass events unless we receive a null event, then we must shut down
116 if( TOOL_EVENT* evt = Wait() )
117 evt->SetPassEvent();
118 else
119 break;
120 }
121
122 picker->ClearHandlers();
123 m_frame->GetCanvas()->SetStatusPopup( nullptr );
124
125 return 0;
126}
127
128
130{
131 bool isSymbolEditor = m_frame->GetFrameType() == FRAME_SCH_SYMBOL_EDITOR;
132 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
133 SCH_SELECTION selection = selTool->RequestSelection();
134
135 // Iterate from the back so we don't have to worry about removals.
136 for( int ii = selection.GetSize() - 1; ii >= 0; --ii )
137 {
138 if( !selection[ii]->IsSCH_ITEM() )
139 {
140 selection.Remove( selection[ii] );
141 continue;
142 }
143
144 SCH_ITEM* schItem = static_cast<SCH_ITEM*>( selection[ii] );
145
146 if( !schItem->IsGroupableType() )
147 selection.Remove( schItem );
148 }
149
150 if( selection.Empty() )
151 return 0;
152
154 SCH_SCREEN* screen = static_cast<SCH_BASE_FRAME*>( m_frame )->GetScreen();
155
156 if( isSymbolEditor )
157 group->SetParent( static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurSymbol() );
158 else
159 group->SetParent( screen );
160
161 for( EDA_ITEM* eda_item : selection )
162 {
163 if( EDA_GROUP* existingGroup = eda_item->GetParentGroup() )
164 m_commit->Modify( existingGroup->AsEdaItem(), screen, RECURSE_MODE::NO_RECURSE );
165
166 m_commit->Modify( eda_item, screen, RECURSE_MODE::NO_RECURSE );
167 group->AddItem( eda_item );
168 }
169
170 m_commit->Add( group, screen );
171 m_commit->Push( _( "Group Items" ) );
172
174 m_toolMgr->RunAction( ACTIONS::selectItem, group->AsEdaItem() );
175
177 m_frame->OnModify();
178
179 return 0;
180}
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 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
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
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
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:52
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
bool IsGroupableType() const
Definition sch_item.cpp:105
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:177
bool Empty() const
Checks if there is anything selected.
Definition selection.h:115
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: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.
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:35
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Definition wxgtk/ui.cpp:689
Class to handle a set of SCH_ITEMs.
@ SCH_SCREEN_T
Definition typeinfo.h:204
VECTOR2< double > VECTOR2D
Definition vector2d.h:694