KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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#include <eda_item.h>
33
34
35SELECTION_TOOL::SELECTION_TOOL( const std::string& aName ) :
36 TOOL_INTERACTIVE( aName ),
37 m_additive( false ),
38 m_subtractive( false ),
39 m_exclusive_or( false ),
40 m_multiple( false ),
41 m_skip_heuristics( false ),
42 m_highlight_modifier( false ),
43 m_drag_additive( false ),
44 m_drag_subtractive( false ),
45 m_canceledMenu( false )
46{
47}
48
49void SELECTION_TOOL::setModifiersState( bool aShiftState, bool aCtrlState, bool aAltState )
50{
51 // Set the configuration of m_additive, m_subtractive, m_exclusive_or from the state of
52 // modifier keys SHIFT and CTRL
53
54 // ALT key cannot be used on MSW because of a conflict with the system menu
55
56 m_subtractive = aCtrlState && aShiftState;
57 m_additive = !aCtrlState && aShiftState;
58
60 {
61 m_exclusive_or = false;
62 m_highlight_modifier = aCtrlState && !aShiftState;
63 }
64 else
65 {
66 m_exclusive_or = aCtrlState && !aShiftState;
68 }
69
70 // Drag is more forgiving and allows either Ctrl+Drag or Shift+Drag to add to the selection
71 // Note, however that we cannot provide disambiguation at the same time as the box selection
72 m_drag_additive = ( aCtrlState || aShiftState ) && !aAltState;
73 m_drag_subtractive = aCtrlState && aShiftState && !aAltState;
74
75 // While the ALT key has some conflicts under MSW (and some flavors of Linux WMs), it remains
76 // useful for users who only use tap-click rather than holding the button. We disable it for
77 // windows because it flashes the disambiguation menu without showing data
78#ifndef __WINDOWS__
79 m_skip_heuristics = aAltState;
80#else
81 m_skip_heuristics = false;
82#endif
83
84}
85
86
88{
90}
91
92
94{
95 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
96 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
97
98 if( conditionalMenu )
99 conditionalMenu->Evaluate( selection() );
100
101 if( actionMenu )
102 actionMenu->UpdateAll();
103
104 return 0;
105}
106
107
109{
110 AddItemToSel( aEvent.Parameter<EDA_ITEM*>() );
111 selection().SetIsHover( false );
112 return 0;
113}
114
115
116void SELECTION_TOOL::AddItemToSel( EDA_ITEM* aItem, bool aQuietMode )
117{
118 if( aItem )
119 {
120 select( aItem );
121
122 // Inform other potentially interested tools
123 if( !aQuietMode )
125 }
126}
127
128
130{
132 selection().SetIsHover( false );
133
134 AddItemToSel( aEvent.Parameter<EDA_ITEM*>() );
135 selection().SetIsHover( false );
136
137 return 0;
138}
139
140
142{
143 AddItemsToSel( aEvent.Parameter<EDA_ITEMS*>(), false );
144 selection().SetIsHover( false );
145 return 0;
146}
147
148
149void SELECTION_TOOL::AddItemsToSel( EDA_ITEMS* aList, bool aQuietMode )
150{
151 if( aList )
152 {
153 for( EDA_ITEM* item : *aList )
154 select( item );
155
156 // Inform other potentially interested tools
157 if( !aQuietMode )
159 }
160}
161
162
164{
166 selection().SetIsHover( false );
167 return 0;
168}
169
170
171void SELECTION_TOOL::RemoveItemFromSel( EDA_ITEM* aItem, bool aQuietMode )
172{
173 if( aItem )
174 {
175 unselect( aItem );
176
177 // Inform other potentially interested tools
178 if( !aQuietMode )
180 }
181}
182
183
185{
186 RemoveItemsFromSel( aEvent.Parameter<EDA_ITEMS*>(), false );
187 selection().SetIsHover( false );
188 return 0;
189}
190
191
192void SELECTION_TOOL::RemoveItemsFromSel( EDA_ITEMS* aList, bool aQuietMode )
193{
194 if( aList )
195 {
196 for( EDA_ITEM* item : *aList )
197 unselect( item );
198
199 // Inform other potentially interested tools
200 if( !aQuietMode )
202 }
203}
204
205
206void SELECTION_TOOL::RemoveItemsFromSel( std::vector<KIID>* aList, bool aQuietMode )
207{
208 EDA_ITEMS removeItems;
209
210 for( EDA_ITEM* item : selection() )
211 {
212 if( alg::contains( *aList, item->m_Uuid ) )
213 removeItems.push_back( item );
214 }
215
216 RemoveItemsFromSel( &removeItems, aQuietMode );
217}
218
219
221{
222 highlight( aItem, BRIGHTENED );
223}
224
225
227{
228 unhighlight( aItem, BRIGHTENED );
229}
230
231
232void SELECTION_TOOL::onDisambiguationExpire( wxTimerEvent& aEvent )
233{
234 // If there is a multiple selection then it's more likely that we're seeing a paused drag
235 // than a long-click.
236 if( selection().GetSize() >= 2 && !hasModifier() )
237 return;
238
239 // If another tool has since started running then we don't want to interrupt
240 if( !getEditFrame<EDA_DRAW_FRAME>()->ToolStackIsEmpty() )
241 return;
242
244}
245
246
248{
249 COLLECTOR* collector = aEvent.Parameter<COLLECTOR*>();
250
251 if( !doSelectionMenu( collector ) )
252 collector->m_MenuCancelled = true;
253
254 return 0;
255}
256
257
259{
260 UNITS_PROVIDER* unitsProvider = getEditFrame<EDA_DRAW_FRAME>();
261 EDA_ITEM* current = nullptr;
262 SELECTION highlightGroup;
263 bool selectAll = false;
264 bool expandSelection = false;
265
266 highlightGroup.SetLayer( LAYER_SELECT_OVERLAY );
267 getView()->Add( &highlightGroup );
268
269 do
270 {
272 if( expandSelection )
273 aCollector->Combine();
274
275 expandSelection = false;
276
277 int limit = std::min( 100, aCollector->GetCount() );
278 ACTION_MENU menu( true );
279
280 for( int i = 0; i < limit; ++i )
281 {
282 EDA_ITEM* item = ( *aCollector )[i];
283 wxString menuText;
284
285 if( i < 9 )
286 {
287#ifdef __WXMAC__
288 menuText = wxString::Format( "%s\t%d",
289 item->GetItemDescription( unitsProvider, false ),
290 i + 1 );
291#else
292 menuText = wxString::Format( "&%d %s\t%d",
293 i + 1,
294 item->GetItemDescription( unitsProvider, false ),
295 i + 1 );
296#endif
297 }
298 else
299 {
300 menuText = item->GetItemDescription( unitsProvider, false );
301 }
302
303 menu.Add( menuText, i + 1, item->GetMenuImage() );
304 }
305
306 menu.AppendSeparator();
307 menu.Add( _( "Select &All\tA" ), limit + 1, BITMAPS::INVALID_BITMAP );
308
309 if( !expandSelection && aCollector->HasAdditionalItems() )
310 menu.Add( _( "&Expand Selection\tE" ), limit + 2, BITMAPS::INVALID_BITMAP );
311
312 if( aCollector->m_MenuTitle.Length() )
313 {
314 menu.SetTitle( aCollector->m_MenuTitle );
315 menu.SetIcon( BITMAPS::info );
316 menu.DisplayTitle( true );
317 }
318 else
319 {
320 menu.DisplayTitle( false );
321 }
322
323 SetContextMenu( &menu, CMENU_NOW );
324
325 while( TOOL_EVENT* evt = Wait() )
326 {
327 if( evt->Action() == TA_CHOICE_MENU_UPDATE )
328 {
329 if( selectAll )
330 {
331 for( int i = 0; i < aCollector->GetCount(); ++i )
332 unhighlight( ( *aCollector )[i], BRIGHTENED, &highlightGroup );
333 }
334 else if( current )
335 {
336 unhighlight( current, BRIGHTENED, &highlightGroup );
337 }
338
339 int id = *evt->GetCommandId();
340
341 // User has pointed an item, so show it in a different way
342 if( id > 0 && id <= limit )
343 {
344 current = ( *aCollector )[id - 1];
345 highlight( current, BRIGHTENED, &highlightGroup );
346 }
347 else
348 {
349 current = nullptr;
350 }
351
352 // User has pointed on the "Select All" option
353 if( id == limit + 1 )
354 {
355 for( int i = 0; i < aCollector->GetCount(); ++i )
356 highlight( ( *aCollector )[i], BRIGHTENED, &highlightGroup );
357
358 selectAll = true;
359 }
360 else
361 {
362 selectAll = false;
363 }
364 }
365 else if( evt->Action() == TA_CHOICE_MENU_CHOICE )
366 {
367 if( selectAll )
368 {
369 for( int i = 0; i < aCollector->GetCount(); ++i )
370 unhighlight( ( *aCollector )[i], BRIGHTENED, &highlightGroup );
371 }
372 else if( current )
373 {
374 unhighlight( current, BRIGHTENED, &highlightGroup );
375 }
376
377 std::optional<int> id = evt->GetCommandId();
378
379 // User has selected the "Select All" option
380 if( id == limit + 1 )
381 {
382 selectAll = true;
383 current = nullptr;
384 }
385 // User has selected the "Expand Selection" option
386 else if( id == limit + 2 )
387 {
388 selectAll = false;
389 current = nullptr;
390 expandSelection = true;
391 }
392 // User has selected an item, so this one will be returned
393 else if( id && ( *id > 0 ) && ( *id <= limit ) )
394 {
395 selectAll = false;
396 current = ( *aCollector )[*id - 1];
397 }
398 // User has cancelled the menu (either by <esc> or clicking out of it)
399 else
400 {
401 selectAll = false;
402 current = nullptr;
403 }
404 }
405 else if( evt->Action() == TA_CHOICE_MENU_CLOSED )
406 {
407 break;
408 }
409
410 getView()->UpdateItems();
411 getEditFrame<EDA_DRAW_FRAME>()->GetCanvas()->Refresh();
412 }
413 } while( expandSelection );
414
415 getView()->Remove( &highlightGroup );
416
417 if( selectAll )
418 {
419 return true;
420 }
421 else if( current )
422 {
423 aCollector->Empty();
424 aCollector->Append( current );
425 return true;
426 }
427
428 return false;
429}
430
431
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.
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: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.
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(const 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:89
virtual wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const
Return a user-visible description string of this item.
Definition: eda_item.cpp:111
virtual BITMAPS GetMenuImage() const
Return a pointer to an image to be used in menus.
Definition: eda_item.cpp:291
static const TOOL_EVENT DisambiguatePoint
Used for hotkey feedback.
Definition: actions.h:309
static const TOOL_EVENT SelectedEvent
Definition: actions.h:292
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:293
virtual void SetLayer(int aLayer)
Set layer used to draw the group.
Definition: view_group.h:99
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:317
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:357
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1471
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.
int ReselectItem(const TOOL_EVENT &aEvent)
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.
bool hasModifier()
True if a selection modifier is enabled, false otherwise.
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:79
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:218
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:167
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:460
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:536
#define BRIGHTENED
item is drawn with a bright contour
@ LAYER_SELECT_OVERLAY
currently selected items overlay
Definition: layer_ids.h:220
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
@ CMENU_NOW
Definition: tool_event.h:152
@ TA_CHOICE_MENU_CHOICE
Definition: tool_event.h:97
@ TA_CHOICE_MENU_UPDATE
Definition: tool_event.h:93
@ TA_CHOICE_MENU_CLOSED
Definition: tool_event.h:100
Functions to provide common constants and other functions to assist in making a consistent UI.