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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef SELECTION_H
27 #define SELECTION_H
28 
29 #include <core/optional.h>
30 #include <core/typeinfo.h>
31 #include <deque>
32 #include <eda_rect.h>
33 #include <view/view_group.h>
34 
35 class EDA_ITEM;
36 
38 {
39 public:
42  {
43  m_isHover = false;
44  }
45 
46  SELECTION( const SELECTION& aOther ) :
48  {
49  m_items = aOther.m_items;
50  m_isHover = aOther.m_isHover;
51  }
52 
53  SELECTION& operator= ( const SELECTION& aOther )
54  {
55  m_items = aOther.m_items;
56  m_isHover = aOther.m_isHover;
57  return *this;
58  }
59 
60  using ITER = std::deque<EDA_ITEM*>::iterator;
61  using CITER = std::deque<EDA_ITEM*>::const_iterator;
62 
63  ITER begin() { return m_items.begin(); }
64  ITER end() { return m_items.end(); }
65  CITER begin() const { return m_items.cbegin(); }
66  CITER end() const { return m_items.cend(); }
67 
68  void SetIsHover( bool aIsHover )
69  {
70  m_isHover = aIsHover;
71  }
72 
73  bool IsHover() const
74  {
75  return m_isHover;
76  }
77 
78  virtual void Add( EDA_ITEM* aItem );
79 
80  virtual void Remove( EDA_ITEM *aItem );
81 
82  virtual void Clear() override
83  {
84  m_items.clear();
85  }
86 
87  virtual unsigned int GetSize() const override
88  {
89  return m_items.size();
90  }
91 
92  virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
93 
94  bool Contains( EDA_ITEM* aItem ) const;
95 
97  bool Empty() const
98  {
99  return m_items.empty();
100  }
101 
103  int Size() const
104  {
105  return m_items.size();
106  }
107 
108  const std::deque<EDA_ITEM*> GetItems() const
109  {
110  return m_items;
111  }
112 
114  virtual VECTOR2I GetCenter() const;
115 
116  virtual const BOX2I ViewBBox() const override
117  {
118  BOX2I r;
119  r.SetMaximum();
120  return r;
121  }
122 
125  {
126  return static_cast<VECTOR2I>( GetBoundingBox().GetPosition() );
127  }
128 
129  virtual EDA_RECT GetBoundingBox() const;
130 
131  virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
132  {
133  return nullptr;
134  }
135 
136  EDA_ITEM* operator[]( const size_t aIdx ) const
137  {
138  if( aIdx < m_items.size() )
139  return m_items[ aIdx ];
140 
141  return nullptr;
142  }
143 
144  EDA_ITEM* Front() const
145  {
146  return m_items.size() ? m_items.front() : nullptr;
147  }
148 
149  std::deque<EDA_ITEM*>& Items()
150  {
151  return m_items;
152  }
153 
154  template<class T>
155  T* FirstOfKind() const
156  {
157  for( auto item : m_items )
158  {
159  if( IsA<T, EDA_ITEM>( item ) )
160  return static_cast<T*> ( item );
161  }
162 
163  return nullptr;
164  }
165 
172  bool HasType( KICAD_T aType ) const;
173 
174  virtual const VIEW_GROUP::ITEMS updateDrawList() const override;
175 
176  bool HasReferencePoint() const
177  {
178  return m_referencePoint != NULLOPT;
179  }
180 
182  {
183  return *m_referencePoint;
184  }
185 
186  void SetReferencePoint( const VECTOR2I& aP )
187  {
188  m_referencePoint = aP;
189  }
190 
192  {
194  }
195 
201  bool AreAllItemsIdentical() const;
202 
203 protected:
205  std::deque<EDA_ITEM*> m_items;
206  bool m_isHover;
207 
208  // mute hidden overloaded virtual function warnings
209  using VIEW_GROUP::Add;
210  using VIEW_GROUP::Remove;
211 };
212 
213 
214 #endif
VECTOR2I GetReferencePoint() const
Definition: selection.h:181
void ClearReferencePoint()
Definition: selection.h:191
bool m_isHover
Definition: selection.h:206
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:70
virtual void Clear() override
Remove all the stored items from the group.
Definition: selection.h:82
bool IsHover() const
Definition: selection.h:73
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:149
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:191
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:110
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:31
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.h:124
virtual const VIEW_GROUP::ITEMS updateDrawList() const override
Layer on which the group is drawn.
Definition: selection.cpp:122
ITER end()
Definition: selection.h:64
Define a general 2D-vector/point.
Definition: vector2d.h:61
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:61
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:43
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:133
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:81
OPT< VECTOR2I > m_referencePoint
Definition: selection.h:204
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:68
CITER end() const
Definition: selection.h:66
ITER begin()
Definition: selection.h:63
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:186
SELECTION(const SELECTION &aOther)
Definition: selection.h:46
const auto NULLOPT
Definition: optional.h:9
std::deque< EDA_ITEM * > m_items
Definition: selection.h:205
SELECTION()
Definition: selection.h:40
void SetMaximum()
Definition: box2.h:57
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:61
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:108
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:60
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.h:131
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:97
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:136
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:87
bool HasReferencePoint() const
Definition: selection.h:176
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:52
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:43
Handle the component boundary box.
Definition: eda_rect.h:42
int Size() const
Returns the number of selected parts.
Definition: selection.h:103
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:155
SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:53
virtual EDA_RECT GetBoundingBox() const
Definition: selection.cpp:99
CITER begin() const
Definition: selection.h:65
EDA_ITEM * Front() const
Definition: selection.h:144
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: selection.h:116