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