KiCad PCB EDA Suite
Loading...
Searching...
No Matches
hover_picker.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
20#include <tools/hover_picker.h>
21
22#include <board.h>
23#include <board_item.h>
24#include <collectors.h>
25#include <tool/tool_manager.h>
27
28#include <limits>
29#include <set>
30
31
33 m_toolMgr( aToolMgr ),
34 m_selectionTool( aToolMgr->GetTool<PCB_SELECTION_TOOL>() )
35{
36}
37
38
43
44
45BOARD_ITEM* HOVER_PICKER::Pick( const VECTOR2I& aPointer, const ACCEPTS& aAccepts,
46 const PROXIMITY& aProximity ) const
47{
48 std::vector<BOARD_ITEM*> candidates = m_selectionTool->CollectPoint(
49 aPointer,
50 [&aAccepts]( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* )
51 {
52 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
53 {
54 if( !aAccepts( *aCollector[i] ) )
55 aCollector.Remove( i );
56 }
57 } );
58
59 if( candidates.empty() )
60 return nullptr;
61
62 if( !aProximity )
63 return candidates.front();
64
65 BOARD_ITEM* best = nullptr;
66 double bestProximity = std::numeric_limits<double>::infinity();
67
68 for( BOARD_ITEM* item : candidates )
69 {
70 double proximity = aProximity( *item, aPointer );
71
72 if( proximity < bestProximity )
73 {
74 bestProximity = proximity;
75 best = item;
76 }
77 }
78
79 return best;
80}
81
82
84{
85 m_brightened.push_back( aItem->m_Uuid );
86 m_selectionTool->BrightenItem( aItem );
87}
88
89
90void HOVER_PICKER::BrightenOnly( const std::vector<BOARD_ITEM*>& aItems )
91{
92 std::set<KIID> wanted;
93
94 for( BOARD_ITEM* item : aItems )
95 wanted.insert( item->m_Uuid );
96
97 BOARD* board = static_cast<BOARD*>( m_toolMgr->GetModel() );
98
99 for( const KIID& id : m_brightened )
100 {
101 if( !wanted.contains( id ) )
102 {
103 if( BOARD_ITEM* item = board->ResolveItem( id, true ) )
104 m_selectionTool->UnbrightenItem( item );
105 }
106 }
107
108 const std::set<KIID> lit( m_brightened.begin(), m_brightened.end() );
109
110 for( BOARD_ITEM* item : aItems )
111 {
112 if( !lit.contains( item->m_Uuid ) )
113 m_selectionTool->BrightenItem( item );
114 }
115
116 m_brightened.assign( wanted.begin(), wanted.end() );
117}
118
119
121{
122 BOARD* board = static_cast<BOARD*>( m_toolMgr->GetModel() );
123
124 for( const KIID& id : m_brightened )
125 {
126 if( BOARD_ITEM* item = board->ResolveItem( id, true ) )
127 m_selectionTool->UnbrightenItem( item );
128 }
129
130 m_brightened.clear();
131}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:2116
int GetCount() const
Return the number of objects in the list.
Definition collector.h:79
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:107
const KIID m_Uuid
Definition eda_item.h:597
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:203
PCB_SELECTION_TOOL * m_selectionTool
void Brighten(BOARD_ITEM *aItem)
HOVER_PICKER(TOOL_MANAGER *aToolMgr)
std::function< double(const BOARD_ITEM &aItem, const VECTOR2I &aPointer)> PROXIMITY
Optional tie-break among what the selection heuristics left. Nearest wins.
~HOVER_PICKER()
Puts back whatever is still lit, so an early exit cannot strand a highlight.
TOOL_MANAGER * m_toolMgr
std::function< bool(BOARD_ITEM &aItem)> ACCEPTS
Everything it sees is already selectable.
void BrightenOnly(const std::vector< BOARD_ITEM * > &aItems)
Light exactly these, leaving what is already lit alone.
void ClearBrightening()
Items go back by id, so a commit that replaced one still puts the original back.
BOARD_ITEM * Pick(const VECTOR2I &aPointer, const ACCEPTS &aAccepts, const PROXIMITY &aProximity=nullptr) const
The item a click would take, or null.
std::vector< KIID > m_brightened
Definition kiid.h:46
The selection tool: currently supports:
Master controller class:
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683