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 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef SELECTION_H
24#define SELECTION_H
25
26#include <optional>
27#include <core/typeinfo.h>
28#include <deque>
29#include <view/view_group.h>
30
31class EDA_ITEM;
32
33
35{
36public:
39 {
40 m_isHover = false;
41 m_lastAddedItem = nullptr;
43 }
44
45 SELECTION( const SELECTION& aOther ) :
47 {
48 m_items = aOther.m_items;
50 m_isHover = aOther.m_isHover;
53 }
54
55 SELECTION& operator= ( const SELECTION& aOther )
56 {
57 m_items = aOther.m_items;
59 m_isHover = aOther.m_isHover;
62 return *this;
63 }
64
65 wxString GetClass() const override
66 {
67 return wxT( "SELECTION" );
68 }
69
70 bool operator==( const SELECTION& aOther ) const;
71
72 using ITER = std::deque<EDA_ITEM*>::iterator;
73 using CITER = std::deque<EDA_ITEM*>::const_iterator;
74
75 ITER begin() { return m_items.begin(); }
76 ITER end() { return m_items.end(); }
77 CITER begin() const { return m_items.cbegin(); }
78 CITER end() const { return m_items.cend(); }
79
80 void SetIsHover( bool aIsHover )
81 {
82 m_isHover = aIsHover;
83 }
84
85 bool IsHover() const
86 {
87 return m_isHover;
88 }
89
90 virtual void Add( EDA_ITEM* aItem );
91
92 virtual void Remove( EDA_ITEM *aItem );
93
94 virtual void Clear() override
95 {
96 m_items.clear();
97 m_itemsOrders.clear();
99 }
100
101 virtual unsigned int GetSize() const override
102 {
103 return m_items.size();
104 }
105
106 virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
107
108 bool Contains( EDA_ITEM* aItem ) const;
109
111 bool Empty() const
112 {
113 return m_items.empty();
114 }
115
117 int Size() const
118 {
119 return m_items.size();
120 }
121
122 const std::deque<EDA_ITEM*> GetItems() const
123 {
124 return m_items;
125 }
126
128 {
129 return m_lastAddedItem;
130 }
131
137 std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
138 bool topBeforeBottom = true ) const;
139
140 std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
141
143 virtual VECTOR2I GetCenter() const;
144
145 virtual const BOX2I ViewBBox() const override
146 {
147 BOX2I r;
148 r.SetMaximum();
149 return r;
150 }
151
154 {
155 return GetBoundingBox().GetPosition();
156 }
157
158 virtual BOX2I GetBoundingBox() const;
159
160 virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
161 {
162 return nullptr;
163 }
164
165 EDA_ITEM* operator[]( const size_t aIdx ) const
166 {
167 if( aIdx < m_items.size() )
168 return m_items[ aIdx ];
169
170 return nullptr;
171 }
172
174 {
175 return m_items.size() ? m_items.front() : nullptr;
176 }
177
178 std::deque<EDA_ITEM*>& Items()
179 {
180 return m_items;
181 }
182
183 const std::deque<EDA_ITEM*>& Items() const
184 {
185 return m_items;
186 }
187
188 template<class T>
189 T* FirstOfKind() const
190 {
191 for( auto item : m_items )
192 {
193 if( IsA<T, EDA_ITEM>( item ) )
194 return static_cast<T*> ( item );
195 }
196
197 return nullptr;
198 }
199
206 bool HasType( KICAD_T aType ) const;
207
208 size_t CountType( KICAD_T aType ) const;
209
210 virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
211
212 bool HasReferencePoint() const
213 {
214 return m_referencePoint != std::nullopt;
215 }
216
218 void SetReferencePoint( const VECTOR2I& aP );
219 void ClearReferencePoint();
220
226 bool AreAllItemsIdentical() const;
227
232 bool OnlyContains( std::vector<KICAD_T> aList ) const;
233
234protected:
235 std::optional<VECTOR2I> m_referencePoint;
236 std::deque<EDA_ITEM*> m_items;
237 std::deque<int> m_itemsOrders;
241
242 // mute hidden overloaded virtual function warnings
243 using VIEW_GROUP::Add;
244 using VIEW_GROUP::Remove;
245};
246
247
248#endif
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr const Vec & GetPosition() const
Definition box2.h:207
constexpr void SetMaximum()
Definition box2.h:76
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition view_group.h:39
VIEW_GROUP(VIEW *aView=nullptr)
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
std::deque< EDA_ITEM * > m_items
Definition selection.h:236
virtual void Add(EDA_ITEM *aItem)
Definition selection.cpp:38
SELECTION & operator=(const SELECTION &aOther)
Definition selection.h:55
CITER end() const
Definition selection.h:78
int m_orderCounter
Definition selection.h:238
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition selection.cpp:71
T * FirstOfKind() const
Definition selection.h:189
ITER end()
Definition selection.h:76
bool operator==(const SELECTION &aOther) const
Definition selection.cpp:28
const std::deque< EDA_ITEM * > GetItems() const
Definition selection.h:122
ITER begin()
Definition selection.h:75
bool m_isHover
Definition selection.h:240
VECTOR2I GetReferencePoint() const
std::deque< int > m_itemsOrders
Definition selection.h:237
const std::deque< EDA_ITEM * > & Items() const
Definition selection.h:183
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition selection.h:153
void SetIsHover(bool aIsHover)
Definition selection.h:80
std::deque< EDA_ITEM * >::const_iterator CITER
Definition selection.h:73
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition selection.cpp:88
EDA_ITEM * GetLastAddedItem() const
Definition selection.h:127
virtual void Remove(EDA_ITEM *aItem)
Definition selection.cpp:56
CITER begin() const
Definition selection.h:77
bool IsHover() const
Definition selection.h:85
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition selection.h:101
EDA_ITEM * Front() const
Definition selection.h:173
virtual void Clear() override
Remove all the stored items from the group.
Definition selection.h:94
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
int Size() const
Returns the number of selected parts.
Definition selection.h:117
wxString GetClass() const override
Return the class name.
Definition selection.h:65
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition selection.h:160
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition selection.h:145
std::deque< EDA_ITEM * >::iterator ITER
Definition selection.h:72
std::deque< EDA_ITEM * > & Items()
Definition selection.h:178
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
EDA_ITEM * operator[](const size_t aIdx) const
Definition selection.h:165
void ClearReferencePoint()
bool OnlyContains(std::vector< KICAD_T > aList) const
Checks if all items in the selection have a type in aList.
void SetReferencePoint(const VECTOR2I &aP)
bool Empty() const
Checks if there is anything selected.
Definition selection.h:111
EDA_ITEM * m_lastAddedItem
Definition selection.h:239
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.
bool HasReferencePoint() const
Definition selection.h:212
size_t CountType(KICAD_T aType) const
std::optional< VECTOR2I > m_referencePoint
Definition selection.h:235
bool Contains(EDA_ITEM *aItem) const
Definition selection.cpp:80
virtual BOX2I GetBoundingBox() const
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
SELECTION(const SELECTION &aOther)
Definition selection.h:45
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
bool IsA(const I *aObject)
Check if the type of aObject is T.
Definition typeinfo.h:35
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683