KiCad PCB EDA Suite
selection.h
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#ifndef SELECTION_H
28#define SELECTION_H
29
30#include <optional>
31#include <core/typeinfo.h>
32#include <deque>
33#include <eda_item.h>
34#include <view/view_group.h>
35
36
38{
39public:
42 {
43 m_isHover = false;
44 m_lastAddedItem = nullptr;
46 }
47
48 SELECTION( const SELECTION& aOther ) :
50 {
51 m_items = aOther.m_items;
53 m_isHover = aOther.m_isHover;
56 }
57
58 SELECTION& operator= ( const SELECTION& aOther )
59 {
60 m_items = aOther.m_items;
62 m_isHover = aOther.m_isHover;
65 return *this;
66 }
67
68 using ITER = std::deque<EDA_ITEM*>::iterator;
69 using CITER = std::deque<EDA_ITEM*>::const_iterator;
70
71 ITER begin() { return m_items.begin(); }
72 ITER end() { return m_items.end(); }
73 CITER begin() const { return m_items.cbegin(); }
74 CITER end() const { return m_items.cend(); }
75
76 void SetIsHover( bool aIsHover )
77 {
78 m_isHover = aIsHover;
79 }
80
81 bool IsHover() const
82 {
83 return m_isHover;
84 }
85
86 virtual void Add( EDA_ITEM* aItem );
87
88 virtual void Remove( EDA_ITEM *aItem );
89
90 virtual void Clear() override
91 {
92 m_items.clear();
93 m_itemsOrders.clear();
95 }
96
97 virtual unsigned int GetSize() const override
98 {
99 return m_items.size();
100 }
101
102 virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
103
104 bool Contains( EDA_ITEM* aItem ) const;
105
107 bool Empty() const
108 {
109 return m_items.empty();
110 }
111
113 int Size() const
114 {
115 return m_items.size();
116 }
117
118 const std::deque<EDA_ITEM*> GetItems() const
119 {
120 return m_items;
121 }
122
124 {
125 return m_lastAddedItem;
126 }
127
133 const std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
134 bool topBeforeBottom = true ) const
135 {
136 std::vector<EDA_ITEM*> sorted_items = std::vector<EDA_ITEM*>( m_items.begin(),
137 m_items.end() );
138
139 std::sort( sorted_items.begin(), sorted_items.end(),
140 [&]( EDA_ITEM* a, EDA_ITEM* b )
141 {
142 if( a->Type() == b->Type() )
143 {
144 if( a->GetSortPosition().x == b->GetSortPosition().x )
145 {
146 // Ensure deterministic sort
147 if( a->GetSortPosition().y == b->GetSortPosition().y )
148 return a->m_Uuid < b->m_Uuid;
149
150 if( topBeforeBottom )
151 return a->GetSortPosition().y < b->GetSortPosition().y;
152 else
153 return a->GetSortPosition().y > b->GetSortPosition().y;
154 }
155 else if( leftBeforeRight )
156 return a->GetSortPosition().x < b->GetSortPosition().x;
157 else
158 return a->GetSortPosition().x > b->GetSortPosition().x;
159 }
160 else
161 return a->Type() < b->Type();
162 } );
163
164 return sorted_items;
165 }
166
167 const std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
168
170 virtual VECTOR2I GetCenter() const;
171
172 virtual const BOX2I ViewBBox() const override
173 {
174 BOX2I r;
175 r.SetMaximum();
176 return r;
177 }
178
181 {
182 return GetBoundingBox().GetPosition();
183 }
184
185 virtual BOX2I GetBoundingBox() const;
186
187 virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
188 {
189 return nullptr;
190 }
191
192 EDA_ITEM* operator[]( const size_t aIdx ) const
193 {
194 if( aIdx < m_items.size() )
195 return m_items[ aIdx ];
196
197 return nullptr;
198 }
199
201 {
202 return m_items.size() ? m_items.front() : nullptr;
203 }
204
205 std::deque<EDA_ITEM*>& Items()
206 {
207 return m_items;
208 }
209
210 const std::deque<EDA_ITEM*>& Items() const
211 {
212 return m_items;
213 }
214
215 template<class T>
216 T* FirstOfKind() const
217 {
218 for( auto item : m_items )
219 {
220 if( IsA<T, EDA_ITEM>( item ) )
221 return static_cast<T*> ( item );
222 }
223
224 return nullptr;
225 }
226
233 bool HasType( KICAD_T aType ) const;
234
235 size_t CountType( KICAD_T aType ) const;
236
237 virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
238
239 bool HasReferencePoint() const
240 {
241 return m_referencePoint != std::nullopt;
242 }
243
245 {
246 if( m_referencePoint )
247 return *m_referencePoint;
248 else
249 return GetBoundingBox().Centre();
250 }
251
252 void SetReferencePoint( const VECTOR2I& aP )
253 {
254 m_referencePoint = aP;
255 }
256
258 {
259 m_referencePoint = std::nullopt;
260 }
261
267 bool AreAllItemsIdentical() const;
268
273 bool OnlyContains( std::vector<KICAD_T> aList ) const;
274
275protected:
276 std::optional<VECTOR2I> m_referencePoint;
277 std::deque<EDA_ITEM*> m_items;
278 std::deque<int> m_itemsOrders;
282
283 // mute hidden overloaded virtual function warnings
284 using VIEW_GROUP::Add;
285 using VIEW_GROUP::Remove;
286};
287
288
289#endif
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition: view_group.h:47
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:44
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:277
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:32
SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:58
CITER end() const
Definition: selection.h:74
int m_orderCounter
Definition: selection.h:279
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:65
T * FirstOfKind() const
Definition: selection.h:216
const 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.h:133
ITER end()
Definition: selection.h:72
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:118
ITER begin()
Definition: selection.h:71
bool m_isHover
Definition: selection.h:281
VECTOR2I GetReferencePoint() const
Definition: selection.h:244
std::deque< int > m_itemsOrders
Definition: selection.h:278
const std::deque< EDA_ITEM * > & Items() const
Definition: selection.h:210
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.h:180
void SetIsHover(bool aIsHover)
Definition: selection.h:76
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:69
EDA_ITEM * GetLastAddedItem() const
Definition: selection.h:123
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:50
CITER begin() const
Definition: selection.h:73
bool IsHover() const
Definition: selection.h:81
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:97
EDA_ITEM * Front() const
Definition: selection.h:200
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:90
int Size() const
Returns the number of selected parts.
Definition: selection.h:113
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.h:187
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: selection.h:172
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:68
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:205
SELECTION()
Definition: selection.h:40
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:192
void ClearReferencePoint()
Definition: selection.h:257
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:252
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:107
EDA_ITEM * m_lastAddedItem
Definition: selection.h:280
bool HasReferencePoint() const
Definition: selection.h:239
std::optional< VECTOR2I > m_referencePoint
Definition: selection.h:276
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:74
SELECTION(const SELECTION &aOther)
Definition: selection.h:48
E_SERIE r
Definition: eserie.cpp:41
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:243
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78