KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 ) const
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 static const std::vector<KICAD_T> textTypes = { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T };
96 bool hasOnlyText = true;
97
98 // If the selection contains only texts calculate the center as the mean of all positions
99 // instead of using the center of the total bounding box. Otherwise rotating the selection will
100 // also translate it.
101
102 for( EDA_ITEM* item : m_items )
103 {
104 if( !item->IsType( textTypes ) )
105 {
106 hasOnlyText = false;
107 break;
108 }
109 }
110
111 BOX2I bbox;
112
113 if( hasOnlyText )
114 {
115 VECTOR2I center( 0, 0 );
116
117 for( EDA_ITEM* item : m_items )
118 center += item->GetPosition();
119
120 center = center / static_cast<int>( m_items.size() );
121 return static_cast<VECTOR2I>( center );
122 }
123
124 for( EDA_ITEM* item : m_items )
125 {
126 if( !item->IsType( { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T } ) )
127 bbox.Merge( item->GetBoundingBox() );
128 }
129
130 return static_cast<VECTOR2I>( bbox.GetCenter() );
131}
132
133
134BOX2I SELECTION::GetBoundingBox( bool aOnlyVisible ) const
135{
136 BOX2I bbox;
137
138 for( EDA_ITEM* item : m_items )
139 bbox.Merge( item->GetBoundingBox() );
140
141 return bbox;
142}
143
144
145bool SELECTION::HasType( KICAD_T aType ) const
146{
147 for( const EDA_ITEM* item : m_items )
148 {
149 if( item->IsType( { aType } ) )
150 return true;
151 }
152
153 return false;
154}
155
156
157size_t SELECTION::CountType( KICAD_T aType ) const
158{
159 size_t count = 0;
160
161 for( const EDA_ITEM* item : m_items )
162 {
163 if( item->IsType( { aType } ) )
164 count++;
165 }
166
167 return count;
168}
169
170
172{
173 if( m_referencePoint )
174 return *m_referencePoint;
175 else
176 return GetBoundingBox().Centre();
177}
178
179
181{
182 m_referencePoint = aP;
183}
184
185
187{
188 m_referencePoint = std::nullopt;
189}
190
191
192const std::vector<KIGFX::VIEW_ITEM*> SELECTION::updateDrawList() const
193{
194 std::vector<VIEW_ITEM*> items;
195
196 for( EDA_ITEM* item : m_items )
197 items.push_back( item );
198
199 return items;
200}
201
202
204{
205 return std::all_of( m_items.begin() + 1, m_items.end(),
206 [&]( const EDA_ITEM* r )
207 {
208 return r->Type() == m_items.front()->Type();
209 } );
210}
211
212
213bool SELECTION::OnlyContains( std::vector<KICAD_T> aList ) const
214{
215 return std::all_of( m_items.begin(), m_items.end(),
216 [&]( const EDA_ITEM* r )
217 {
218 return r->IsType( aList );
219 } );
220}
221
222
223std::vector<EDA_ITEM*> SELECTION::GetItemsSortedByTypeAndXY( bool leftBeforeRight,
224 bool topBeforeBottom ) const
225{
226 std::vector<EDA_ITEM*> sorted_items = std::vector<EDA_ITEM*>( m_items.begin(), m_items.end() );
227
228 std::sort( sorted_items.begin(), sorted_items.end(),
229 [&]( EDA_ITEM* a, EDA_ITEM* b )
230 {
231 if( a->Type() == b->Type() )
232 {
233 const VECTOR2I aPos = a->GetSortPosition();
234 const VECTOR2I bPos = b->GetSortPosition();
235
236 if( aPos.x == bPos.x )
237 {
238 // Ensure deterministic sort
239 if( aPos.y == bPos.y )
240 return a->m_Uuid < b->m_Uuid;
241
242 if( topBeforeBottom )
243 return aPos.y < bPos.y;
244 else
245 return aPos.y > bPos.y;
246 }
247 else if( leftBeforeRight )
248 {
249 return aPos.x < bPos.x;
250 }
251 else
252 {
253 return aPos.x > bPos.x;
254 }
255 }
256 else
257 {
258 return a->Type() < b->Type();
259 }
260 } );
261 return sorted_items;
262}
263
264
265std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const
266{
267 using pairedIterators = std::pair<decltype( m_items.begin() ),
268 decltype( m_itemsOrders.begin() )>;
269
270 // Create a vector of all {selection item, selection order} iterator pairs
271 std::vector<pairedIterators> pairs;
272 auto item = m_items.begin();
273 auto order = m_itemsOrders.begin();
274
275 for( ; item != m_items.end(); ++item, ++order )
276 pairs.emplace_back( make_pair( item, order ) );
277
278 // Sort the pairs by the selection order
279 std::sort( pairs.begin(), pairs.end(),
280 []( pairedIterators const& a, pairedIterators const& b )
281 {
282 return *a.second < *b.second;
283 } );
284
285 // Make a vector of just the sortedItems
286 std::vector<EDA_ITEM*> sortedItems;
287
288 for( pairedIterators sortedItem : pairs )
289 sortedItems.emplace_back( *sortedItem.first );
290
291 return sortedItems;
292}
Vec Centre() const
Definition: box2.h:87
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:623
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:84
std::deque< EDA_ITEM * > m_items
Definition: selection.h:235
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
int m_orderCounter
Definition: selection.h:237
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:75
bool operator==(const SELECTION &aOther) const
Definition: selection.cpp:32
bool m_isHover
Definition: selection.h:239
VECTOR2I GetReferencePoint() const
Definition: selection.cpp:171
std::deque< int > m_itemsOrders
Definition: selection.h:236
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:72
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
virtual BOX2I GetBoundingBox(bool aOnlyVisible=false) const
Definition: selection.cpp:134
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:203
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:145
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:71
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
Definition: selection.cpp:265
void ClearReferencePoint()
Definition: selection.cpp:186
bool OnlyContains(std::vector< KICAD_T > aList) const
Checks if all items in the selection have a type in aList.
Definition: selection.cpp:213
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.cpp:180
EDA_ITEM * m_lastAddedItem
Definition: selection.h:238
std::vector< EDA_ITEM * > GetItemsSortedByTypeAndXY(bool leftBeforeRight=true, bool topBeforeBottom=true) const
Returns a copy of this selection of items sorted by their X then Y position.
Definition: selection.cpp:223
size_t CountType(KICAD_T aType) const
Definition: selection.cpp:157
std::optional< VECTOR2I > m_referencePoint
Definition: selection.h:234
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:84
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
Definition: selection.cpp:192
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:190
@ SCH_TEXT_T
Definition: typeinfo.h:151