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, 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 wxString GetClass() const override
70 {
71 return wxT( "SELECTION" );
72 }
73
74 bool operator==( const SELECTION& aOther ) const;
75
76 using ITER = std::deque<EDA_ITEM*>::iterator;
77 using CITER = std::deque<EDA_ITEM*>::const_iterator;
78
79 ITER begin() { return m_items.begin(); }
80 ITER end() { return m_items.end(); }
81 CITER begin() const { return m_items.cbegin(); }
82 CITER end() const { return m_items.cend(); }
83
84 void SetIsHover( bool aIsHover )
85 {
86 m_isHover = aIsHover;
87 }
88
89 bool IsHover() const
90 {
91 return m_isHover;
92 }
93
94 virtual void Add( EDA_ITEM* aItem );
95
96 virtual void Remove( EDA_ITEM *aItem );
97
98 virtual void Clear() override
99 {
100 m_items.clear();
101 m_itemsOrders.clear();
102 m_orderCounter = 0;
103 }
104
105 virtual unsigned int GetSize() const override
106 {
107 return m_items.size();
108 }
109
110 virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
111
112 bool Contains( EDA_ITEM* aItem ) const;
113
115 bool Empty() const
116 {
117 return m_items.empty();
118 }
119
121 int Size() const
122 {
123 return m_items.size();
124 }
125
126 const std::deque<EDA_ITEM*> GetItems() const
127 {
128 return m_items;
129 }
130
132 {
133 return m_lastAddedItem;
134 }
135
141 std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
142 bool topBeforeBottom = true ) const;
143
144 std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
145
147 virtual VECTOR2I GetCenter() const;
148
149 virtual const BOX2I ViewBBox() const override
150 {
151 BOX2I r;
152 r.SetMaximum();
153 return r;
154 }
155
158 {
159 return GetBoundingBox().GetPosition();
160 }
161
162 virtual BOX2I GetBoundingBox() const;
163
164 virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
165 {
166 return nullptr;
167 }
168
169 EDA_ITEM* operator[]( const size_t aIdx ) const
170 {
171 if( aIdx < m_items.size() )
172 return m_items[ aIdx ];
173
174 return nullptr;
175 }
176
178 {
179 return m_items.size() ? m_items.front() : nullptr;
180 }
181
182 std::deque<EDA_ITEM*>& Items()
183 {
184 return m_items;
185 }
186
187 const std::deque<EDA_ITEM*>& Items() const
188 {
189 return m_items;
190 }
191
192 template<class T>
193 T* FirstOfKind() const
194 {
195 for( auto item : m_items )
196 {
197 if( IsA<T, EDA_ITEM>( item ) )
198 return static_cast<T*> ( item );
199 }
200
201 return nullptr;
202 }
203
210 bool HasType( KICAD_T aType ) const;
211
212 size_t CountType( KICAD_T aType ) const;
213
214 virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
215
216 bool HasReferencePoint() const
217 {
218 return m_referencePoint != std::nullopt;
219 }
220
222 void SetReferencePoint( const VECTOR2I& aP );
223 void ClearReferencePoint();
224
230 bool AreAllItemsIdentical() const;
231
236 bool OnlyContains( std::vector<KICAD_T> aList ) const;
237
238protected:
239 std::optional<VECTOR2I> m_referencePoint;
240 std::deque<EDA_ITEM*> m_items;
241 std::deque<int> m_itemsOrders;
245
246 // mute hidden overloaded virtual function warnings
247 using VIEW_GROUP::Add;
248 using VIEW_GROUP::Remove;
249};
250
251
252#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:98
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition: view_group.h:43
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:46
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:86
std::deque< EDA_ITEM * > m_items
Definition: selection.h:240
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:82
int m_orderCounter
Definition: selection.h:242
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:75
T * FirstOfKind() const
Definition: selection.h:193
ITER end()
Definition: selection.h:80
bool operator==(const SELECTION &aOther) const
Definition: selection.cpp:32
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:126
ITER begin()
Definition: selection.h:79
bool m_isHover
Definition: selection.h:244
VECTOR2I GetReferencePoint() const
Definition: selection.cpp:169
std::deque< int > m_itemsOrders
Definition: selection.h:241
const std::deque< EDA_ITEM * > & Items() const
Definition: selection.h:187
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.h:157
void SetIsHover(bool aIsHover)
Definition: selection.h:84
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:77
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:131
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:60
CITER begin() const
Definition: selection.h:81
bool IsHover() const
Definition: selection.h:89
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:201
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:105
EDA_ITEM * Front() const
Definition: selection.h:177
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:98
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:143
int Size() const
Returns the number of selected parts.
Definition: selection.h:121
wxString GetClass() const override
Return the class name.
Definition: selection.h:69
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.h:164
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: selection.h:149
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:76
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:182
SELECTION()
Definition: selection.h:41
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
Definition: selection.cpp:263
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:169
void ClearReferencePoint()
Definition: selection.cpp:184
bool OnlyContains(std::vector< KICAD_T > aList) const
Checks if all items in the selection have a type in aList.
Definition: selection.cpp:211
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.cpp:178
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:115
EDA_ITEM * m_lastAddedItem
Definition: selection.h:243
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:221
bool HasReferencePoint() const
Definition: selection.h:216
size_t CountType(KICAD_T aType) const
Definition: selection.cpp:155
std::optional< VECTOR2I > m_referencePoint
Definition: selection.h:239
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:84
virtual BOX2I GetBoundingBox() const
Definition: selection.cpp:132
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
Definition: selection.cpp:190
SELECTION(const SELECTION &aOther)
Definition: selection.h:49
The Cairo implementation of the graphics abstraction layer.
Definition: eda_group.h:33
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78