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
93 virtual void Add( EDA_ITEM* aItem );
94
95 virtual void Remove( EDA_ITEM *aItem );
96
97 virtual void Clear() override
98 {
99 m_items.clear();
100 m_itemsOrders.clear();
101 m_orderCounter = 0;
102 }
103
104 virtual unsigned int GetSize() const override
105 {
106 return m_items.size();
107 }
108
109 virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
110
111 bool Contains( EDA_ITEM* aItem ) const;
112
114 bool Empty() const
115 {
116 return m_items.empty();
117 }
118
120 int Size() const
121 {
122 return m_items.size();
123 }
124
125 const std::deque<EDA_ITEM*> GetItems() const
126 {
127 return m_items;
128 }
129
131 {
132 return m_lastAddedItem;
133 }
134
140 std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
141 bool topBeforeBottom = true ) const;
142
143 std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
144
146 virtual VECTOR2I GetCenter() const;
147
148 virtual const BOX2I ViewBBox() const override
149 {
150 BOX2I r;
151 r.SetMaximum();
152 return r;
153 }
154
157 {
158 return GetBoundingBox().GetPosition();
159 }
160
161 virtual BOX2I GetBoundingBox() const;
162
163 virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
164 {
165 return nullptr;
166 }
167
168 EDA_ITEM* operator[]( const size_t aIdx ) const
169 {
170 if( aIdx < m_items.size() )
171 return m_items[ aIdx ];
172
173 return nullptr;
174 }
175
177 {
178 return m_items.size() ? m_items.front() : nullptr;
179 }
180
181 std::deque<EDA_ITEM*>& Items()
182 {
183 return m_items;
184 }
185
186 const std::deque<EDA_ITEM*>& Items() const
187 {
188 return m_items;
189 }
190
191 template<class T>
192 T* FirstOfKind() const
193 {
194 for( auto item : m_items )
195 {
196 if( IsA<T, EDA_ITEM>( item ) )
197 return static_cast<T*> ( item );
198 }
199
200 return nullptr;
201 }
202
209 bool HasType( KICAD_T aType ) const;
210
211 size_t CountType( KICAD_T aType ) const;
212
213 virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
214
215 bool HasReferencePoint() const
216 {
217 return m_referencePoint != std::nullopt;
218 }
219
221 void SetReferencePoint( const VECTOR2I& aP );
222 void ClearReferencePoint();
223
229 bool AreAllItemsIdentical() const;
230
235 bool OnlyContains( std::vector<KICAD_T> aList ) const;
236
237protected:
238 std::optional<VECTOR2I> m_referencePoint;
239 std::deque<EDA_ITEM*> m_items;
240 std::deque<int> m_itemsOrders;
244
245 // mute hidden overloaded virtual function warnings
246 using VIEW_GROUP::Add;
247 using VIEW_GROUP::Remove;
248};
249
250
251#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:239
virtual void Add(EDA_ITEM *aItem)
A null aItem is ignored; the selection never holds null members.
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:241
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition selection.cpp:75
T * FirstOfKind() const
Definition selection.h:192
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:125
ITER begin()
Definition selection.h:75
bool m_isHover
Definition selection.h:243
VECTOR2I GetReferencePoint() const
std::deque< int > m_itemsOrders
Definition selection.h:240
const std::deque< EDA_ITEM * > & Items() const
Definition selection.h:186
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition selection.h:156
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:92
EDA_ITEM * GetLastAddedItem() const
Definition selection.h:130
virtual void Remove(EDA_ITEM *aItem)
Definition selection.cpp:60
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:104
EDA_ITEM * Front() const
Definition selection.h:176
virtual void Clear() override
Remove all the stored items from the group.
Definition selection.h:97
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:120
wxString GetClass() const override
Return the class name.
Definition selection.h:65
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition selection.h:163
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition selection.h:148
std::deque< EDA_ITEM * >::iterator ITER
Definition selection.h:72
std::deque< EDA_ITEM * > & Items()
Definition selection.h:181
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
EDA_ITEM * operator[](const size_t aIdx) const
Definition selection.h:168
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:114
EDA_ITEM * m_lastAddedItem
Definition selection.h:242
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:215
size_t CountType(KICAD_T aType) const
std::optional< VECTOR2I > m_referencePoint
Definition selection.h:238
bool Contains(EDA_ITEM *aItem) const
Definition selection.cpp:84
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