KiCad PCB EDA Suite
selection_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 (C) 2021-2022 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 3
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/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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 <bitmaps.h>
25#include <widgets/ui_common.h>
26#include <collector.h>
27#include <tool/tool_manager.h>
28#include <tool/actions.h>
29#include <tool/selection_tool.h>
30#include <view/view.h>
31#include <eda_draw_frame.h>
32
33
34SELECTION_TOOL::SELECTION_TOOL( const std::string& aName ) :
35 TOOL_INTERACTIVE( aName ),
36 m_additive( false ),
37 m_subtractive( false ),
38 m_exclusive_or( false ),
39 m_multiple( false ),
40 m_skip_heuristics( false ),
41 m_highlight_modifier( false ),
42 m_drag_additive( false ),
43 m_drag_subtractive( false ),
44 m_canceledMenu( false )
45{
46}
47
48void SELECTION_TOOL::setModifiersState( bool aShiftState, bool aCtrlState, bool aAltState )
49{
50 // Set the configuration of m_additive, m_subtractive, m_exclusive_or from the state of
51 // modifier keys SHIFT and CTRL
52
53 // ALT key cannot be used on MSW because of a conflict with the system menu
54
55 m_subtractive = aCtrlState && aShiftState;
56 m_additive = !aCtrlState && aShiftState;
57
59 {
60 m_exclusive_or = false;
61 m_highlight_modifier = aCtrlState && !aShiftState;
62 }
63 else
64 {
65 m_exclusive_or = aCtrlState && !aShiftState;
67 }
68
69 // Drag is more forgiving and allows either Ctrl+Drag or Shift+Drag to add to the selection
70 // Note, however that we cannot provide disambiguation at the same time as the box selection
71 m_drag_additive = ( aCtrlState || aShiftState ) && !aAltState;
72 m_drag_subtractive = aCtrlState && aShiftState && !aAltState;
73
74 // While the ALT key has some conflicts under MSW (and some flavors of Linux WMs), it remains
75 // useful for users who only use tap-click rather than holding the button. We disable it for
76 // windows because it flashes the disambiguation menu without showing data
77#ifndef __WINDOWS__
78 m_skip_heuristics = aAltState;
79#else
80 m_skip_heuristics = false;
81#endif
82
83}
84
85
87{
88 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
89 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
90
91 if( conditionalMenu )
92 conditionalMenu->Evaluate( selection() );
93
94 if( actionMenu )
95 actionMenu->UpdateAll();
96
97 return 0;
98}
99
100
102{
103 AddItemToSel( aEvent.Parameter<EDA_ITEM*>() );
104 selection().SetIsHover( false );
105 return 0;
106}
107
108
109void SELECTION_TOOL::AddItemToSel( EDA_ITEM* aItem, bool aQuietMode )
110{
111 if( aItem )
112 {
113 select( aItem );
114
115 // Inform other potentially interested tools
116 if( !aQuietMode )
118 }
119}
120
121
123{
124 AddItemsToSel( aEvent.Parameter<EDA_ITEMS*>(), false );
125 selection().SetIsHover( false );
126 return 0;
127}
128
129
130void SELECTION_TOOL::AddItemsToSel( EDA_ITEMS* aList, bool aQuietMode )
131{
132 if( aList )
133 {
134 for( EDA_ITEM* item : *aList )
135 select( item );
136
137 // Inform other potentially interested tools
138 if( !aQuietMode )
140 }
141}
142
143
145{
147 selection().SetIsHover( false );
148 return 0;
149}
150
151
152void SELECTION_TOOL::RemoveItemFromSel( EDA_ITEM* aItem, bool aQuietMode )
153{
154 if( aItem )
155 {
156 unselect( aItem );
157
158 // Inform other potentially interested tools
159 if( !aQuietMode )
161 }
162}
163
164
166{
167 RemoveItemsFromSel( aEvent.Parameter<EDA_ITEMS*>(), false );
168 selection().SetIsHover( false );
169 return 0;
170}
171
172
173void SELECTION_TOOL::RemoveItemsFromSel( EDA_ITEMS* aList, bool aQuietMode )
174{
175 if( aList )
176 {
177 for( EDA_ITEM* item : *aList )
178 unselect( item );
179
180 // Inform other potentially interested tools
181 if( !aQuietMode )
183 }
184}
185
186
187void SELECTION_TOOL::RemoveItemsFromSel( std::vector<KIID>* aList, bool aQuietMode )
188{
189 EDA_ITEMS removeItems;
190
191 for( EDA_ITEM* item : selection() )
192 {
193 if( alg::contains( *aList, item->m_Uuid ) )
194 removeItems.push_back( item );
195 }
196
197 RemoveItemsFromSel( &removeItems, aQuietMode );
198}
199
200
202{
203 highlight( aItem, BRIGHTENED );
204}
205
206
208{
209 unhighlight( aItem, BRIGHTENED );
210}
211
212
213void SELECTION_TOOL::onDisambiguationExpire( wxTimerEvent& aEvent )
214{
215 // If there is a multiple selection then it's more likely that we're seeing a paused drag
216 // than a long-click.
217 if( selection().GetSize() >= 2 )
218 return;
219
221}
222
223
225{
226 COLLECTOR* collector = aEvent.Parameter<COLLECTOR*>();
227
228 if( !doSelectionMenu( collector ) )
229 collector->m_MenuCancelled = true;
230
231 return 0;
232}
233
234
236{
237 UNITS_PROVIDER* unitsProvider = getEditFrame<EDA_DRAW_FRAME>();
238 EDA_ITEM* current = nullptr;
239 SELECTION highlightGroup;
240 bool selectAll = false;
241 bool expandSelection = false;
242
243 highlightGroup.SetLayer( LAYER_SELECT_OVERLAY );
244 getView()->Add( &highlightGroup );
245
246 do
247 {
249 if( expandSelection )
250 aCollector->Combine();
251
252 expandSelection = false;
253
254 int limit = std::min( 100, aCollector->GetCount() );
255 ACTION_MENU menu( true );
256
257 for( int i = 0; i < limit; ++i )
258 {
259 EDA_ITEM* item = ( *aCollector )[i];
260 wxString menuText;
261
262 if( i < 9 )
263 {
264#ifdef __WXMAC__
265 menuText = wxString::Format( "%s\t%d",
266 item->GetSelectMenuText( unitsProvider ),
267 i + 1 );
268#else
269 menuText = wxString::Format( "&%d %s\t%d",
270 i + 1,
271 item->GetSelectMenuText( unitsProvider ),
272 i + 1 );
273#endif
274 }
275 else
276 {
277 menuText = item->GetSelectMenuText( unitsProvider );
278 }
279
280 menu.Add( menuText, i + 1, item->GetMenuImage() );
281 }
282
283 menu.AppendSeparator();
284 menu.Add( _( "Select &All\tA" ), limit + 1, BITMAPS::INVALID_BITMAP );
285
286 if( !expandSelection && aCollector->HasAdditionalItems() )
287 menu.Add( _( "&Expand Selection\tE" ), limit + 2, BITMAPS::INVALID_BITMAP );
288
289 if( aCollector->m_MenuTitle.Length() )
290 {
291 menu.SetTitle( aCollector->m_MenuTitle );
292 menu.SetIcon( BITMAPS::info );
293 menu.DisplayTitle( true );
294 }
295 else
296 {
297 menu.DisplayTitle( false );
298 }
299
300 SetContextMenu( &menu, CMENU_NOW );
301
302 while( TOOL_EVENT* evt = Wait() )
303 {
304 if( evt->Action() == TA_CHOICE_MENU_UPDATE )
305 {
306 if( selectAll )
307 {
308 for( int i = 0; i < aCollector->GetCount(); ++i )
309 unhighlight( ( *aCollector )[i], BRIGHTENED, &highlightGroup );
310 }
311 else if( current )
312 {
313 unhighlight( current, BRIGHTENED, &highlightGroup );
314 }
315
316 int id = *evt->GetCommandId();
317
318 // User has pointed an item, so show it in a different way
319 if( id > 0 && id <= limit )
320 {
321 current = ( *aCollector )[id - 1];
322 highlight( current, BRIGHTENED, &highlightGroup );
323 }
324 else
325 {
326 current = nullptr;
327 }
328
329 // User has pointed on the "Select All" option
330 if( id == limit + 1 )
331 {
332 for( int i = 0; i < aCollector->GetCount(); ++i )
333 highlight( ( *aCollector )[i], BRIGHTENED, &highlightGroup );
334
335 selectAll = true;
336 }
337 else
338 {
339 selectAll = false;
340 }
341 }
342 else if( evt->Action() == TA_CHOICE_MENU_CHOICE )
343 {
344 if( selectAll )
345 {
346 for( int i = 0; i < aCollector->GetCount(); ++i )
347 unhighlight( ( *aCollector )[i], BRIGHTENED, &highlightGroup );
348 }
349 else if( current )
350 {
351 unhighlight( current, BRIGHTENED, &highlightGroup );
352 }
353
354 std::optional<int> id = evt->GetCommandId();
355
356 // User has selected the "Select All" option
357 if( id == limit + 1 )
358 {
359 selectAll = true;
360 current = nullptr;
361 }
362 // User has selected the "Expand Selection" option
363 else if( id == limit + 2 )
364 {
365 selectAll = false;
366 current = nullptr;
367 expandSelection = true;
368 }
369 // User has selected an item, so this one will be returned
370 else if( id && ( *id > 0 ) && ( *id <= limit ) )
371 {
372 selectAll = false;
373 current = ( *aCollector )[*id - 1];
374 }
375 // User has cancelled the menu (either by <esc> or clicking out of it)
376 else
377 {
378 selectAll = false;
379 current = nullptr;
380 }
381 }
382 else if( evt->Action() == TA_CHOICE_MENU_CLOSED )
383 {
384 break;
385 }
386 }
387 } while( expandSelection );
388
389 getView()->Remove( &highlightGroup );
390
391 if( selectAll )
392 {
393 return true;
394 }
395 else if( current )
396 {
397 aCollector->Empty();
398 aCollector->Append( current );
399 return true;
400 }
401
402 return false;
403}
404
405
@ INVALID_BITMAP
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
void DisplayTitle(bool aDisplay=true)
Decide whether a title for a pop up menu should be displayed.
Definition: action_menu.cpp:98
void UpdateAll()
Run update handlers for the menu and its submenus.
void SetTitle(const wxString &aTitle) override
Set title for the menu.
Definition: action_menu.cpp:87
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
Definition: action_menu.cpp:73
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
An abstract class that will find and hold all the objects according to an inspection done by the Insp...
Definition: collector.h:48
bool m_MenuCancelled
Definition: collector.h:237
void Empty()
Clear the list.
Definition: collector.h:89
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:81
wxString m_MenuTitle
Definition: collector.h:236
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:132
void Combine()
Re-combine the backup list into the main list of the collector.
Definition: collector.h:140
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition: collector.h:99
void Evaluate(SELECTION &aSelection)
Update the contents of the menu based on the supplied conditions.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
virtual BITMAPS GetMenuImage() const
Return a pointer to an image to be used in menus.
Definition: eda_item.cpp:269
virtual wxString GetSelectMenuText(UNITS_PROVIDER *aUnitsProvider) const
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: eda_item.cpp:108
static const TOOL_EVENT DisambiguatePoint
Definition: actions.h:221
static const TOOL_EVENT SelectedEvent
Definition: actions.h:206
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:207
virtual void SetLayer(int aLayer)
Set layer used to draw the group.
Definition: view_group.h:98
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:316
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:346
int RemoveItemFromSel(const TOOL_EVENT &aEvent)
bool doSelectionMenu(COLLECTOR *aCollector)
int AddItemsToSel(const TOOL_EVENT &aEvent)
void BrightenItem(EDA_ITEM *aItem)
virtual void unselect(EDA_ITEM *aItem)=0
Take necessary action mark an item as unselected.
int AddItemToSel(const TOOL_EVENT &aEvent)
int UpdateMenu(const TOOL_EVENT &aEvent)
Update a menu's state based on the current selection.
void setModifiersState(bool aShiftState, bool aCtrlState, bool aAltState)
Set the configuration of m_additive, m_subtractive, m_exclusive_or, m_skip_heuristics from the state ...
void UnbrightenItem(EDA_ITEM *aItem)
virtual void select(EDA_ITEM *aItem)=0
Take necessary action mark an item as selected.
int SelectionMenu(const TOOL_EVENT &aEvent)
Show a popup menu to trim the COLLECTOR passed as aEvent's parameter down to a single item.
int RemoveItemsFromSel(const TOOL_EVENT &aEvent)
SELECTION_TOOL(const std::string &aName)
virtual void unhighlight(EDA_ITEM *aItem, int aHighlightMode, SELECTION *aGroup=nullptr)=0
Unhighlight the item visually.
virtual void highlight(EDA_ITEM *aItem, int aHighlightMode, SELECTION *aGroup=nullptr)=0
Highlight the item visually.
virtual SELECTION & selection()=0
Return a reference to the selection.
virtual bool ctrlClickHighlights()
Determines if ctrl-click is highlight net or XOR selection.
void onDisambiguationExpire(wxTimerEvent &aEvent)
Start the process to show our disambiguation menu once the user has kept the mouse down for the minim...
void SetIsHover(bool aIsHover)
Definition: selection.h:76
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
Generic, UI-independent tool event.
Definition: tool_event.h:156
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:442
void SetContextMenu(ACTION_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger=CMENU_BUTTON)
Assign a context menu and tells when it should be activated.
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.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
#define _(s)
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition: eda_item.h:524
#define BRIGHTENED
item is drawn with a bright contour
@ LAYER_SELECT_OVERLAY
currently selected items overlay
Definition: layer_ids.h:219
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
@ CMENU_NOW
Definition: tool_event.h:148
@ TA_CHOICE_MENU_CHOICE
Definition: tool_event.h:93
@ TA_CHOICE_MENU_UPDATE
Definition: tool_event.h:89
@ TA_CHOICE_MENU_CLOSED
Definition: tool_event.h:96
Functions to provide common constants and other functions to assist in making a consistent UI.