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 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
171{
172 if( m_referencePoint )
173 return *m_referencePoint;
174 else
175 return GetBoundingBox().Centre();
176}
177
178
180{
181 m_referencePoint = aP;
182}
183
184
186{
187 m_referencePoint = std::nullopt;
188}
189
190
191const std::vector<KIGFX::VIEW_ITEM*> SELECTION::updateDrawList() const
192{
193 std::vector<VIEW_ITEM*> items;
194
195 for( EDA_ITEM* item : m_items )
196 items.push_back( item );
197
198 return items;
199}
200
201
203{
204 return std::all_of( m_items.begin() + 1, m_items.end(),
205 [&]( const EDA_ITEM* r )
206 {
207 return r->Type() == m_items.front()->Type();
208 } );
209}
210
211
212bool SELECTION::OnlyContains( std::vector<KICAD_T> aList ) const
213{
214 return std::all_of( m_items.begin(), m_items.end(),
215 [&]( const EDA_ITEM* r )
216 {
217 return r->IsType( aList );
218 } );
219}
220
221
222const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const
223{
224 using pairedIterators = std::pair<decltype( m_items.begin() ),
225 decltype( m_itemsOrders.begin() )>;
226
227 // Create a vector of all {selection item, selection order} iterator pairs
228 std::vector<pairedIterators> pairs;
229 auto item = m_items.begin();
230 auto order = m_itemsOrders.begin();
231
232 for( ; item != m_items.end(); ++item, ++order )
233 pairs.emplace_back( make_pair( item, order ) );
234
235 // Sort the pairs by the selection order
236 std::sort( pairs.begin(), pairs.end(),
237 []( pairedIterators const& a, pairedIterators const& b )
238 {
239 return *a.second < *b.second;
240 } );
241
242 // Make a vector of just the sortedItems
243 std::vector<EDA_ITEM*> sortedItems;
244
245 for( pairedIterators sortedItem : pairs )
246 sortedItems.emplace_back( *sortedItem.first );
247
248 return sortedItems;
249}
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:271
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
int m_orderCounter
Definition: selection.h:273
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:75
bool operator==(const SELECTION &aOther) const
Definition: selection.cpp:32
const std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
Definition: selection.cpp:222
bool m_isHover
Definition: selection.h:275
VECTOR2I GetReferencePoint() const
Definition: selection.cpp:170
std::deque< int > m_itemsOrders
Definition: selection.h:272
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:202
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
void ClearReferencePoint()
Definition: selection.cpp:185
bool OnlyContains(std::vector< KICAD_T > aList) const
Checks if all items in the selection have a type in aList.
Definition: selection.cpp:212
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.cpp:179
EDA_ITEM * m_lastAddedItem
Definition: selection.h:274
size_t CountType(KICAD_T aType) const
Definition: selection.cpp:156
std::optional< VECTOR2I > m_referencePoint
Definition: selection.h:270
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:191
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:189
@ SCH_TEXT_T
Definition: typeinfo.h:151