KiCad PCB EDA Suite
selection.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) 2013-2017 CERN
5 * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <algorithm>
28#include <eda_item.h>
29#include <tool/selection.h>
30
31
32bool SELECTION::operator==( const SELECTION& aOther )
33{
34 return ( m_items == aOther.m_items
35 && m_itemsOrders == aOther.m_itemsOrders
36 && m_isHover == aOther.m_isHover
38 && m_orderCounter == aOther.m_orderCounter );
39}
40
41
43{
44 // We're not sorting here; this is just a time-optimized way to do an
45 // inclusion check. std::lower_bound will return the first i >= aItem
46 // and the second i > aItem check rules out i == aItem.
47 ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
48
49 if( i == m_items.end() || *i > aItem )
50 {
51 m_itemsOrders.insert( m_itemsOrders.begin() + std::distance( m_items.begin(), i ),
53 m_items.insert( i, aItem );
55 m_lastAddedItem = aItem;
56 }
57}
58
59
61{
62 ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
63
64 if( !( i == m_items.end() || *i > aItem ) )
65 {
66 m_itemsOrders.erase( m_itemsOrders.begin() + std::distance( m_items.begin(), i ) );
67 m_items.erase( i );
68
69 if( aItem == m_lastAddedItem )
70 m_lastAddedItem = nullptr;
71 }
72}
73
74
75KIGFX::VIEW_ITEM* SELECTION::GetItem( unsigned int aIdx ) const
76{
77 if( aIdx < m_items.size() )
78 return m_items[aIdx];
79
80 return nullptr;
81}
82
83
84bool SELECTION::Contains( EDA_ITEM* aItem ) const
85{
86 CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
87
88 return !( i == m_items.end() || *i > aItem );
89}
90
91
94{
95 bool hasOnlyText = true;
96
97 // If the selection contains only texts calculate the center as the mean of all positions
98 // instead of using the center of the total bounding box. Otherwise rotating the selection will
99 // also translate it.
100
101 for( EDA_ITEM* item : m_items )
102 {
103 if( !item->IsType( { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T } ) )
104 {
105 hasOnlyText = false;
106 break;
107 }
108 }
109
110 BOX2I bbox;
111
112 if( hasOnlyText )
113 {
114 VECTOR2I center( 0, 0 );
115
116 for( EDA_ITEM* item : m_items )
117 center += item->GetPosition();
118
119 center = center / static_cast<int>( m_items.size() );
120 return static_cast<VECTOR2I>( center );
121 }
122
123 for( EDA_ITEM* item : m_items )
124 {
125 if( !item->IsType( { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T } ) )
126 bbox.Merge( item->GetBoundingBox() );
127 }
128
129 return static_cast<VECTOR2I>( bbox.GetCenter() );
130}
131
132
134{
135 BOX2I bbox;
136
137 for( EDA_ITEM* item : m_items )
138 bbox.Merge( item->GetBoundingBox() );
139
140 return bbox;
141}
142
143
144bool SELECTION::HasType( KICAD_T aType ) const
145{
146 for( const EDA_ITEM* item : m_items )
147 {
148 if( item->IsType( { aType } ) )
149 return true;
150 }
151
152 return false;
153}
154
155
156size_t SELECTION::CountType( KICAD_T aType ) const
157{
158 size_t count = 0;
159
160 for( const EDA_ITEM* item : m_items )
161 {
162 if( item->IsType( { aType } ) )
163 count++;
164 }
165
166 return count;
167}
168
169
170const std::vector<KIGFX::VIEW_ITEM*> SELECTION::updateDrawList() const
171{
172 std::vector<VIEW_ITEM*> items;
173
174 for( EDA_ITEM* item : m_items )
175 items.push_back( item );
176
177 return items;
178}
179
180
182{
183 return ( std::all_of( m_items.begin() + 1, m_items.end(),
184 [&]( const EDA_ITEM* r )
185 {
186 return r->Type() == m_items.front()->Type();
187 } ) );
188}
189
190
191bool SELECTION::OnlyContains( std::vector<KICAD_T> aList ) const
192{
193 return ( std::all_of( m_items.begin(), m_items.end(),
194 [&]( const EDA_ITEM* r )
195 {
196 return r->IsType( aList );
197 } ) );
198}
199
200
201const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const
202{
203 using pairedIterators =
204 std::pair<decltype( m_items.begin() ), decltype( m_itemsOrders.begin() )>;
205
206 // Create a vector of all {selection item, selection order} iterator pairs
207 std::vector<pairedIterators> pairs;
208 auto item = m_items.begin();
209 auto order = m_itemsOrders.begin();
210
211 for( ; item != m_items.end(); ++item, ++order )
212 pairs.emplace_back( make_pair( item, order ) );
213
214 // Sort the pairs by the selection order
215 std::sort( pairs.begin(), pairs.end(),
216 []( pairedIterators const& a, pairedIterators const& b )
217 {
218 return *a.second < *b.second;
219 } );
220
221 // Make a vector of just the sortedItems
222 std::vector<EDA_ITEM*> sortedItems;
223
224 for( pairedIterators sortedItem : pairs )
225 sortedItems.emplace_back( *sortedItem.first );
226
227 return sortedItems;
228}
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:588
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:77
std::deque< EDA_ITEM * > m_items
Definition: selection.h:285
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
int m_orderCounter
Definition: selection.h:287
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:75
const std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
Definition: selection.cpp:201
bool m_isHover
Definition: selection.h:289
std::deque< int > m_itemsOrders
Definition: selection.h:286
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:71
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:93
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:60
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:181
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:144
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:70
bool OnlyContains(std::vector< KICAD_T > aList) const
Checks if all items in the selection have a type in aList.
Definition: selection.cpp:191
EDA_ITEM * m_lastAddedItem
Definition: selection.h:288
bool operator==(const SELECTION &aOther)
Definition: selection.cpp:32
size_t CountType(KICAD_T aType) const
Definition: selection.cpp:156
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:84
virtual BOX2I GetBoundingBox() const
Definition: selection.cpp:133
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
Definition: selection.cpp:170
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_LABEL_LOCATE_ANY_T
Definition: typeinfo.h:175
@ SCH_TEXT_T
Definition: typeinfo.h:150