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 <deque>
30 #include <eda_rect.h>
31 #include <base_struct.h>
32 #include <view/view_group.h>
33 
34 
36 {
37 public:
39  {
40  m_isHover = false;
41  }
42 
43  SELECTION( const SELECTION& aOther )
44  {
45  m_items = aOther.m_items;
46  m_isHover = aOther.m_isHover;
47  }
48 
49  SELECTION& operator= ( const SELECTION& aOther )
50  {
51  m_items = aOther.m_items;
52  m_isHover = aOther.m_isHover;
53  return *this;
54  }
55 
56  using ITER = std::deque<EDA_ITEM*>::iterator;
57  using CITER = std::deque<EDA_ITEM*>::const_iterator;
58 
59  ITER begin() { return m_items.begin(); }
60  ITER end() { return m_items.end(); }
61  CITER begin() const { return m_items.cbegin(); }
62  CITER end() const { return m_items.cend(); }
63 
64  void SetIsHover( bool aIsHover )
65  {
66  m_isHover = aIsHover;
67  }
68 
69  bool IsHover() const
70  {
71  return m_isHover;
72  }
73 
74  virtual void Add( EDA_ITEM* aItem )
75  {
76  // We're not sorting here; this is just a time-optimized way to do an
77  // inclusion check. std::lower_bound will return the first i >= aItem
78  // and the second i > aItem check rules out i == aItem.
79  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
80 
81  if( i == m_items.end() || *i > aItem )
82  m_items.insert( i, aItem );
83  }
84 
85  virtual void Remove( EDA_ITEM *aItem )
86  {
87  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
88 
89  if( !( i == m_items.end() || *i > aItem ) )
90  m_items.erase( i );
91  }
92 
93  virtual void Clear() override
94  {
95  m_items.clear();
96  }
97 
98  virtual unsigned int GetSize() const override
99  {
100  return m_items.size();
101  }
102 
103  virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override
104  {
105  if( aIdx < m_items.size() )
106  return m_items[ aIdx ];
107 
108  return nullptr;
109  }
110 
111  bool Contains( EDA_ITEM* aItem ) const
112  {
113  CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
114 
115  return !( i == m_items.end() || *i > aItem );
116  }
117 
119  bool Empty() const
120  {
121  return m_items.empty();
122  }
123 
125  int Size() const
126  {
127  return m_items.size();
128  }
129 
130  const std::deque<EDA_ITEM*> GetItems() const
131  {
132  return m_items;
133  }
134 
136  virtual VECTOR2I GetCenter() const
137  {
138  return static_cast<VECTOR2I>( GetBoundingBox().Centre() );
139  }
140 
141  virtual const BOX2I ViewBBox() const override
142  {
143  BOX2I r;
144  r.SetMaximum();
145  return r;
146  }
147 
150  {
151  return static_cast<VECTOR2I>( GetBoundingBox().GetPosition() );
152  }
153 
155  {
156  EDA_RECT bbox;
157 
158  if( Front() )
159  {
160  bbox = Front()->GetBoundingBox();
161 
162  for( auto i = m_items.begin() + 1; i != m_items.end(); ++i )
163  bbox.Merge( (*i)->GetBoundingBox() );
164  }
165 
166  return bbox;
167  }
168 
169  virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
170  {
171  return nullptr;
172  }
173 
174  EDA_ITEM* operator[]( const size_t aIdx ) const
175  {
176  if( aIdx < m_items.size() )
177  return m_items[ aIdx ];
178 
179  return nullptr;
180  }
181 
182  EDA_ITEM* Front() const
183  {
184  return m_items.size() ? m_items.front() : nullptr;
185  }
186 
187  std::deque<EDA_ITEM*>& Items()
188  {
189  return m_items;
190  }
191 
192  template<class T>
193  T* FirstOfKind() const
194  {
195  auto refType = T( nullptr ).Type();
196 
197  for( auto item : m_items )
198  {
199  if( item->Type() == refType )
200  return static_cast<T*> ( item );
201  }
202 
203  return nullptr;
204  }
205 
212  bool HasType( KICAD_T aType ) const
213  {
214  for( auto item : m_items )
215  {
216  if( item->Type() == aType )
217  return true;
218  }
219 
220  return false;
221  }
222 
223  virtual const VIEW_GROUP::ITEMS updateDrawList() const override
224  {
225  std::vector<VIEW_ITEM*> items;
226 
227  for( auto item : m_items )
228  items.push_back( item );
229 
230  return items;
231  }
232 
233  bool HasReferencePoint() const
234  {
235  return m_referencePoint != NULLOPT;
236  }
237 
239  {
240  return *m_referencePoint;
241  }
242 
243  void SetReferencePoint( const VECTOR2I& aP )
244  {
245  m_referencePoint = aP;
246  }
247 
249  {
251  }
252 
253 protected:
255  std::deque<EDA_ITEM*> m_items;
256  bool m_isHover;
257 
258  // mute hidden overloaded virtual function warnings
259  using VIEW_GROUP::Add;
260  using VIEW_GROUP::Remove;
261 };
262 
264 {
268 };
269 
270 
271 #endif
VECTOR2I GetReferencePoint() const
Definition: selection.h:238
void ClearReferencePoint()
Definition: selection.h:248
bool m_isHover
Definition: selection.h:256
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:93
bool IsHover() const
Definition: selection.h:69
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect.
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:187
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.h:212
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.h:149
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.h:136
ITER end()
Definition: selection.h:60
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:57
Class VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:84
OPT< VECTOR2I > m_referencePoint
Definition: selection.h:254
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
void SetIsHover(bool aIsHover)
Definition: selection.h:64
virtual void Add(EDA_ITEM *aItem)
Definition: selection.h:74
CITER end() const
Definition: selection.h:62
ITER begin()
Definition: selection.h:59
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:243
SELECTION(const SELECTION &aOther)
Definition: selection.h:43
const auto NULLOPT
Definition: optional.h:9
std::deque< EDA_ITEM * > m_items
Definition: selection.h:255
SELECTION()
Definition: selection.h:38
SELECTION_LOCK_FLAGS
Definition: selection.h:263
void SetMaximum()
Definition: box2.h:71
bool Contains(EDA_ITEM *aItem) const
Definition: selection.h:111
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:130
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:56
virtual EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.h:169
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.h:103
virtual const VIEW_GROUP::ITEMS updateDrawList() const override
Definition: selection.h:223
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:119
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:174
virtual unsigned int GetSize() const override
Function GetSize() Returns the number of stored items.
Definition: selection.h:98
bool HasReferencePoint() const
Definition: selection.h:233
size_t i
Definition: json11.cpp:649
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
int Size() const
Returns the number of selected parts.
Definition: selection.h:125
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
boost::optional< T > OPT
Definition: optional.h:7
T * FirstOfKind() const
Definition: selection.h:193
SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:49
virtual const EDA_RECT GetBoundingBox() const
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
EDA_RECT GetBoundingBox() const
Definition: selection.h:154
CITER begin() const
Definition: selection.h:61
EDA_ITEM * Front() const
Definition: selection.h:182
virtual const BOX2I ViewBBox() const override
Function ViewBBox() Returns the bounding box for all stored items covering all its layers.
Definition: selection.h:141
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:85