KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <eda_draw_frame.h>
25#include <kiplatform/ui.h>
26#include <tool/actions.h>
27#include <tool/tool_manager.h>
28#include <tool/picker_tool.h>
29#include <tool/group_tool.h>
30#include <tool/selection.h>
31#include <status_popup.h>
32#include <commit.h>
33#include <bitmaps.h>
35#include <eda_group.h>
36
38{
39public:
41 {
42 SetIcon( BITMAPS::group ); // fixme
43 SetTitle( _( "Grouping" ) );
44
48 }
49
50 ACTION_MENU* create() const override
51 {
54 return menu;
55 }
56
58
59private:
60 void update() override
61 {
62 bool canGroup = false;
63 bool hasGroup = false;
64 bool hasMember = false;
65
66 if( m_selectionTool != nullptr )
67 {
68 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
69 {
70 canGroup = true;
71
72 if( item->Type() == PCB_GROUP_T || item->Type() == SCH_GROUP_T )
73 hasGroup = true;
74
75 if( item->GetParentGroup() )
76 hasMember = true;
77 }
78 }
79
80 Enable( ACTIONS::group.GetUIId(), canGroup );
81 Enable( ACTIONS::ungroup.GetUIId(), hasGroup );
82 Enable( ACTIONS::removeFromGroup.GetUIId(), hasMember );
83 }
84
85private:
87};
88
89
91{
92}
93
94
96{
97 m_frame = getEditFrame<EDA_DRAW_FRAME>();
99}
100
101
103{
104 m_frame = getEditFrame<EDA_DRAW_FRAME>();
106
107 // Find the selection tool, so they can cooperate
108 m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "common.InteractiveSelection" ) );
109 wxCHECK( m_selectionTool, false );
110
111 TOOL_MENU& selToolMenu = m_selectionTool->GetToolMenu();
112
113 std::shared_ptr<GROUP_CONTEXT_MENU> groupMenu = std::make_shared<GROUP_CONTEXT_MENU>();
114 groupMenu->SetTool( this );
115 groupMenu->SetSelectionTool( m_selectionTool );
116 selToolMenu.RegisterSubMenu( groupMenu );
117
118 selToolMenu.GetMenu().AddMenu( groupMenu.get(), SELECTION_CONDITIONS::NotEmpty, 100 );
119
120 return true;
121}
122
123
125{
126 EDA_GROUP* group = aEvent.Parameter<EDA_GROUP*>();
127
129 m_propertiesDialog->Destroy();
130
132
133 m_propertiesDialog->Show( true );
134
135 return 0;
136}
137
138
139int GROUP_TOOL::Ungroup( const TOOL_EVENT& aEvent )
140{
141 const SELECTION& selection = m_selectionTool->GetSelection();
142 EDA_ITEMS toSelect;
143
144 if( selection.Empty() )
146
147 SELECTION selCopy = selection;
149
150 for( EDA_ITEM* item : selCopy )
151 {
152 EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( item );
153
154 if( group )
155 {
156 for( EDA_ITEM* member : group->GetItems() )
157 {
158 m_commit->Stage( member, CHT_UNGROUP, m_frame->GetScreen() );
159 toSelect.push_back( member );
160 }
161
162 group->AsEdaItem()->SetSelected();
163 m_commit->Remove( group->AsEdaItem(), m_frame->GetScreen() );
164 }
165 }
166
167 m_commit->Push( _( "Ungroup Items" ) );
168
170
172 m_frame->OnModify();
173
174 return 0;
175}
176
177
179{
180 const SELECTION& selection = m_selectionTool->GetSelection();
181
182 if( selection.Empty() )
184
185 for( EDA_ITEM* item : selection )
186 {
187 if( item->GetParentGroup() )
188 m_commit->Stage( item, CHT_UNGROUP, m_frame->GetScreen() );
189 }
190
191 m_commit->Push( _( "Remove Group Items" ) );
192
194 m_frame->OnModify();
195
196 return 0;
197}
198
199
201{
202 const SELECTION& selection = m_selectionTool->GetSelection();
203
204 if( selection.GetSize() == 1 &&
205 (selection[0]->Type() == SCH_GROUP_T || selection[0]->Type() == PCB_GROUP_T) )
206 {
208 }
209
210 return 0;
211}
212
213
215{
216 m_selectionTool->ExitGroup( true /* Select the group */ );
217 return 0;
218}
219
220
222{
225
226 Go( &GROUP_TOOL::Group, ACTIONS::group.MakeEvent() );
227 Go( &GROUP_TOOL::Ungroup, ACTIONS::ungroup.MakeEvent() );
231}
static TOOL_ACTION pickNewGroupMember
Definition: actions.h:243
static TOOL_ACTION selectionCursor
Select a single item under the cursor position.
Definition: actions.h:217
static TOOL_ACTION group
Definition: actions.h:235
static TOOL_ACTION groupEnter
Definition: actions.h:238
static TOOL_ACTION groupProperties
Definition: actions.h:242
static TOOL_ACTION ungroup
Definition: actions.h:236
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: actions.h:220
static TOOL_ACTION removeFromGroup
Definition: actions.h:237
static TOOL_ACTION groupLeave
Definition: actions.h:239
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: actions.h:228
Define the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
void SetTitle(const wxString &aTitle) override
Set title for the menu.
Definition: action_menu.cpp:92
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
Definition: action_menu.cpp:78
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Add a submenu to the menu.
bool Show(bool show) override
virtual void OnModify()
Must be called after a model change in order to set the "modify" flag and do other frame-specific pro...
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
A set of EDA_ITEMs (i.e., without duplicates).
Definition: eda_group.h:45
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:96
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:344
SELECTION_TOOL * m_selectionTool
Definition: group_tool.cpp:86
void SetSelectionTool(SELECTION_TOOL *aTool)
Definition: group_tool.cpp:57
ACTION_MENU * create() const override
Return an instance of this class. It has to be overridden in inheriting classes.
Definition: group_tool.cpp:50
void update() override
Update menu state stub.
Definition: group_tool.cpp:60
virtual std::unique_ptr< COMMIT > createCommit()=0
virtual int GroupProperties(const TOOL_EVENT &aEvent)
Definition: group_tool.cpp:124
SELECTION_TOOL * m_selectionTool
Definition: group_tool.h:73
virtual int LeaveGroup(const TOOL_EVENT &aEvent)
Definition: group_tool.cpp:214
bool Init() override
Init() is called once upon a registration of the tool.
Definition: group_tool.cpp:102
virtual int RemoveFromGroup(const TOOL_EVENT &aEvent)
Restrict selection to only member of the group.
Definition: group_tool.cpp:178
DIALOG_GROUP_PROPERTIES * m_propertiesDialog
Definition: group_tool.h:72
std::unique_ptr< COMMIT > m_commit
Definition: group_tool.h:74
EDA_DRAW_FRAME * m_frame
Definition: group_tool.h:71
virtual int Ungroup(const TOOL_EVENT &aEvent)
Remove selection from group.
Definition: group_tool.cpp:139
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: group_tool.cpp:95
virtual int Group(const TOOL_EVENT &aEvent)=0
Ungroup selected items.
virtual int PickNewMember(const TOOL_EVENT &aEvent)=0
Invoke the picker tool to select a new member of the group.
void setTransitions() override
< Set up handlers for various events.
Definition: group_tool.cpp:221
virtual int EnterGroup(const TOOL_EVENT &aEvent)
Leave the current group (deselect its members and select the group as a whole).
Definition: group_tool.cpp:200
static bool NotEmpty(const SELECTION &aSelection)
Test if there are any items selected.
virtual void EnterGroup()
SELECTION & GetSelection()
virtual void ExitGroup(bool aSelectGroup=false)
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:100
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:110
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:465
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
TOOL_MENU & GetToolMenu()
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
TOOL_BASE * FindTool(int aId) const
Search for a tool with given ID.
Manage a CONDITIONAL_MENU and some number of CONTEXT_MENUs as sub-menus.
Definition: tool_menu.h:43
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
void RegisterSubMenu(std::shared_ptr< ACTION_MENU > aSubMenu)
Store a submenu of this menu model.
Definition: tool_menu.cpp:50
@ CHT_UNGROUP
Definition: commit.h:46
#define _(s)
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition: eda_item.h:551
@ SCH_GROUP_T
Definition: typeinfo.h:173
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110