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 <view/view_group.h>
34
35class EDA_ITEM;
36
37
39{
40public:
43 {
44 m_isHover = false;
45 m_lastAddedItem = nullptr;
47 }
48
49 SELECTION( const SELECTION& aOther ) :
51 {
52 m_items = aOther.m_items;
54 m_isHover = aOther.m_isHover;
57 }
58
59 SELECTION& operator= ( const SELECTION& aOther )
60 {
61 m_items = aOther.m_items;
63 m_isHover = aOther.m_isHover;
66 return *this;
67 }
68
69 bool operator==( const SELECTION& aOther ) const;
70
71 using ITER = std::deque<EDA_ITEM*>::iterator;
72 using CITER = std::deque<EDA_ITEM*>::const_iterator;
73
74 ITER begin() { return m_items.begin(); }
75 ITER end() { return m_items.end(); }
76 CITER begin() const { return m_items.cbegin(); }
77 CITER end() const { return m_items.cend(); }
78
79 void SetIsHover( bool aIsHover )
80 {
81 m_isHover = aIsHover;
82 }
83
84 bool IsHover() const
85 {
86 return m_isHover;
87 }
88
89 virtual void Add( EDA_ITEM* aItem );
90
91 virtual void Remove( EDA_ITEM *aItem );
92
93 virtual void Clear() override
94 {
95 m_items.clear();
96 m_itemsOrders.clear();
98 }
99
100 virtual unsigned int GetSize() const override
101 {
102 return m_items.size();
103 }
104
105 virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
106
107 bool Contains( EDA_ITEM* aItem ) const;
108
110 bool Empty() const
111 {
112 return m_items.empty();
113 }
114
116 int Size() const
117 {
118 return m_items.size();
119 }
120
121 const std::deque<EDA_ITEM*> GetItems() const
122 {
123 return m_items;
124 }
125
127 {
128 return m_lastAddedItem;
129 }
130
136 std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
137 bool topBeforeBottom = true ) const;
138
139 std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
140
142 virtual VECTOR2I GetCenter() const;
143
144 virtual const BOX2I ViewBBox() const override
145 {
146 BOX2I r;
147 r.SetMaximum();
148 return r;
149 }
150
153 {
154 return GetBoundingBox().GetPosition();
155 }
156
157 virtual BOX2I GetBoundingBox() const;
158
159 virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
160 {
161 return nullptr;
162 }
163
164 EDA_ITEM* operator[]( const size_t aIdx ) const
165 {
166 if( aIdx < m_items.size() )
167 return m_items[ aIdx ];
168
169 return nullptr;
170 }
171
173 {
174 return m_items.size() ? m_items.front() : nullptr;
175 }
176
177 std::deque<EDA_ITEM*>& Items()
178 {
179 return m_items;
180 }
181
182 const std::deque<EDA_ITEM*>& Items() const
183 {
184 return m_items;
185 }
186
187 template<class T>
188 T* FirstOfKind() const
189 {
190 for( auto item : m_items )
191 {
192 if( IsA<T, EDA_ITEM>( item ) )
193 return static_cast<T*> ( item );
194 }
195
196 return nullptr;
197 }
198
205 bool HasType( KICAD_T aType ) const;
206
207 size_t CountType( KICAD_T aType ) const;
208
209 virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
210
211 bool HasReferencePoint() const
212 {
213 return m_referencePoint != std::nullopt;
214 }
215
217 void SetReferencePoint( const VECTOR2I& aP );
218 void ClearReferencePoint();
219
225 bool AreAllItemsIdentical() const;
226
231 bool OnlyContains( std::vector<KICAD_T> aList ) const;
232
233protected:
234 std::optional<VECTOR2I> m_referencePoint;
235 std::deque<EDA_ITEM*> m_items;
236 std::deque<int> m_itemsOrders;
240
241 // mute hidden overloaded virtual function warnings
242 using VIEW_GROUP::Add;
243 using VIEW_GROUP::Remove;
244};
245
246
247#endif
constexpr const Vec & GetPosition() const
Definition: box2.h:211
constexpr void SetMaximum()
Definition: box2.h:80
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
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:235
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:59
CITER end() const
Definition: selection.h:77
int m_orderCounter
Definition: selection.h:237
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:75
T * FirstOfKind() const
Definition: selection.h:188
ITER end()
Definition: selection.h:75
bool operator==(const SELECTION &aOther) const
Definition: selection.cpp:32
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:121
ITER begin()
Definition: selection.h:74
bool m_isHover
Definition: selection.h:239
VECTOR2I GetReferencePoint() const
Definition: selection.cpp:171
std::deque< int > m_itemsOrders
Definition: selection.h:236
const std::deque< EDA_ITEM * > & Items() const
Definition: selection.h:182
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.h:152
void SetIsHover(bool aIsHover)
Definition: selection.h:79
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
EDA_ITEM * GetLastAddedItem() const
Definition: selection.h:126
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:60
CITER begin() const
Definition: selection.h:76
bool IsHover() const
Definition: selection.h:84
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:203
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:100
EDA_ITEM * Front() const
Definition: selection.h:172
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:93
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:145
int Size() const
Returns the number of selected parts.
Definition: selection.h:116
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.h:159
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: selection.h:144
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:71
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:177
SELECTION()
Definition: selection.h:41
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
Definition: selection.cpp:265
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:164
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
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:110
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
bool HasReferencePoint() const
Definition: selection.h:211
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 BOX2I GetBoundingBox() const
Definition: selection.cpp:134
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
Definition: selection.cpp:192
SELECTION(const SELECTION &aOther)
Definition: selection.h:49
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