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