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