KiCad PCB EDA Suite
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 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 <core/optional.h>
31 #include <core/typeinfo.h>
32 #include <deque>
33 #include <eda_rect.h>
34 #include <view/view_group.h>
35 
36 class EDA_ITEM;
37 
39 {
40 public:
43  {
44  m_isHover = false;
45  }
46 
47  SELECTION( const SELECTION& aOther ) :
49  {
50  m_items = aOther.m_items;
51  m_isHover = aOther.m_isHover;
52  }
53 
54  SELECTION& operator= ( const SELECTION& aOther )
55  {
56  m_items = aOther.m_items;
57  m_isHover = aOther.m_isHover;
58  return *this;
59  }
60 
61  using ITER = std::deque<EDA_ITEM*>::iterator;
62  using CITER = std::deque<EDA_ITEM*>::const_iterator;
63 
64  ITER begin() { return m_items.begin(); }
65  ITER end() { return m_items.end(); }
66  CITER begin() const { return m_items.cbegin(); }
67  CITER end() const { return m_items.cend(); }
68 
69  void SetIsHover( bool aIsHover )
70  {
71  m_isHover = aIsHover;
72  }
73 
74  bool IsHover() const
75  {
76  return m_isHover;
77  }
78 
79  virtual void Add( EDA_ITEM* aItem );
80 
81  virtual void Remove( EDA_ITEM *aItem );
82 
83  virtual void Clear() override
84  {
85  m_items.clear();
86  }
87 
88  virtual unsigned int GetSize() const override
89  {
90  return m_items.size();
91  }
92 
93  virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
94 
95  bool Contains( EDA_ITEM* aItem ) const;
96 
98  bool Empty() const
99  {
100  return m_items.empty();
101  }
102 
104  int Size() const
105  {
106  return m_items.size();
107  }
108 
109  const std::deque<EDA_ITEM*> GetItems() const
110  {
111  return m_items;
112  }
113 
115  virtual VECTOR2I GetCenter() const;
116 
117  virtual const BOX2I ViewBBox() const override
118  {
119  BOX2I r;
120  r.SetMaximum();
121  return r;
122  }
123 
126  {
127  return static_cast<VECTOR2I>( GetBoundingBox().GetPosition() );
128  }
129 
130  virtual EDA_RECT GetBoundingBox() const;
131 
132  virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
133  {
134  return nullptr;
135  }
136 
137  EDA_ITEM* operator[]( const size_t aIdx ) const
138  {
139  if( aIdx < m_items.size() )
140  return m_items[ aIdx ];
141 
142  return nullptr;
143  }
144 
145  EDA_ITEM* Front() const
146  {
147  return m_items.size() ? m_items.front() : nullptr;
148  }
149 
150  std::deque<EDA_ITEM*>& Items()
151  {
152  return m_items;
153  }
154 
155  template<class T>
156  T* FirstOfKind() const
157  {
158  for( auto item : m_items )
159  {
160  if( IsA<T, EDA_ITEM>( item ) )
161  return static_cast<T*> ( item );
162  }
163 
164  return nullptr;
165  }
166 
173  bool HasType( KICAD_T aType ) const;
174 
175  virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
176 
177  bool HasReferencePoint() const
178  {
179  return m_referencePoint != NULLOPT;
180  }
181 
183  {
184  return *m_referencePoint;
185  }
186 
187  void SetReferencePoint( const VECTOR2I& aP )
188  {
189  m_referencePoint = aP;
190  }
191 
193  {
195  }
196 
202  bool AreAllItemsIdentical() const;
203 
204 protected:
206  std::deque<EDA_ITEM*> m_items;
207  bool m_isHover;
208 
209  // mute hidden overloaded virtual function warnings
210  using VIEW_GROUP::Add;
211  using VIEW_GROUP::Remove;
212 };
213 
214 
215 #endif
VECTOR2I GetReferencePoint() const
Definition: selection.h:182
void ClearReferencePoint()
Definition: selection.h:192
bool m_isHover
Definition: selection.h:207
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:71
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:83
bool IsHover() const
Definition: selection.h:74
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:150
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:111
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:32
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.h:125
ITER end()
Definition: selection.h:65
Define a general 2D-vector/point.
Definition: vector2d.h:61
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:62
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:44
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:134
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:76
OPT< VECTOR2I > m_referencePoint
Definition: selection.h:205
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
void SetIsHover(bool aIsHover)
Definition: selection.h:69
CITER end() const
Definition: selection.h:67
ITER begin()
Definition: selection.h:64
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:187
SELECTION(const SELECTION &aOther)
Definition: selection.h:47
const auto NULLOPT
Definition: optional.h:9
std::deque< EDA_ITEM * > m_items
Definition: selection.h:206
SELECTION()
Definition: selection.h:41
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:62
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:109
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition: view_group.h:46
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:61
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.h:132
E_SERIE r
Definition: eserie.cpp:41
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:98
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:137
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:88
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
Definition: selection.cpp:123
bool HasReferencePoint() const
Definition: selection.h:177
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:53
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:44
Handle the component boundary box.
Definition: eda_rect.h:42
int Size() const
Returns the number of selected parts.
Definition: selection.h:104
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
boost::optional< T > OPT
Definition: optional.h:7
T * FirstOfKind() const
Definition: selection.h:156
SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:54
virtual EDA_RECT GetBoundingBox() const
Definition: selection.cpp:100
CITER begin() const
Definition: selection.h:66
EDA_ITEM * Front() const
Definition: selection.h:145
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: selection.h:117