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