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 <eda_item.h>
34 #include <view/view_group.h>
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 
155  virtual EDA_RECT GetBoundingBox() const
156  {
157  EDA_RECT bbox;
158 
159  for( EDA_ITEM* item : m_items )
160  bbox.Merge( item->GetBoundingBox() );
161 
162  return bbox;
163  }
164 
165  virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
166  {
167  return nullptr;
168  }
169 
170  EDA_ITEM* operator[]( const size_t aIdx ) const
171  {
172  if( aIdx < m_items.size() )
173  return m_items[ aIdx ];
174 
175  return nullptr;
176  }
177 
178  EDA_ITEM* Front() const
179  {
180  return m_items.size() ? m_items.front() : nullptr;
181  }
182 
183  std::deque<EDA_ITEM*>& Items()
184  {
185  return m_items;
186  }
187 
188  template<class T>
189  T* FirstOfKind() const
190  {
191  auto refType = T( nullptr ).Type();
192 
193  for( auto item : m_items )
194  {
195  if( item->Type() == refType )
196  return static_cast<T*> ( item );
197  }
198 
199  return nullptr;
200  }
201 
208  bool HasType( KICAD_T aType ) const
209  {
210  for( auto item : m_items )
211  {
212  if( item->Type() == aType )
213  return true;
214  }
215 
216  return false;
217  }
218 
219  virtual const VIEW_GROUP::ITEMS updateDrawList() const override
220  {
221  std::vector<VIEW_ITEM*> items;
222 
223  for( auto item : m_items )
224  items.push_back( item );
225 
226  return items;
227  }
228 
229  bool HasReferencePoint() const
230  {
231  return m_referencePoint != NULLOPT;
232  }
233 
235  {
236  return *m_referencePoint;
237  }
238 
239  void SetReferencePoint( const VECTOR2I& aP )
240  {
241  m_referencePoint = aP;
242  }
243 
245  {
247  }
248 
254  bool AreAllItemsIdentical() const
255  {
256  return ( std::all_of( m_items.begin() + 1, m_items.end(),
257  [&]( const EDA_ITEM* r )
258  {
259  return r->Type() == m_items.front()->Type();
260  } ) );
261  }
262 
263 protected:
265  std::deque<EDA_ITEM*> m_items;
266  bool m_isHover;
267 
268  // mute hidden overloaded virtual function warnings
269  using VIEW_GROUP::Add;
270  using VIEW_GROUP::Remove;
271 };
272 
274 {
278 };
279 
280 
281 #endif
VECTOR2I GetReferencePoint() const
Definition: selection.h:234
void ClearReferencePoint()
Definition: selection.h:244
bool m_isHover
Definition: selection.h:266
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.
Definition: eda_rect.cpp:431
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:183
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:208
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:254
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:264
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:239
SELECTION(const SELECTION &aOther)
Definition: selection.h:44
const auto NULLOPT
Definition: optional.h:9
std::deque< EDA_ITEM * > m_items
Definition: selection.h:265
SELECTION()
Definition: selection.h:39
virtual EDA_RECT GetBoundingBox() const
Definition: selection.h:155
SELECTION_LOCK_FLAGS
Definition: selection.h:273
void SetMaximum()
Definition: box2.h:73
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:165
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:219
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:120
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:170
virtual unsigned int GetSize() const override
Function GetSize() Returns the number of stored items.
Definition: selection.h:99
bool HasReferencePoint() const
Definition: selection.h:229
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: eda_item.h:148
boost::optional< T > OPT
Definition: optional.h:7
T * FirstOfKind() const
Definition: selection.h:189
SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:50
CITER begin() const
Definition: selection.h:62
EDA_ITEM * Front() const
Definition: selection.h:178
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