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 
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  const 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  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
77 
78  if( i == m_items.end() || *i > aItem )
79  m_items.insert( i, aItem );
80  }
81 
82  virtual void Remove( EDA_ITEM *aItem )
83  {
84  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
85 
86  if( !( i == m_items.end() || *i > aItem ) )
87  m_items.erase( i );
88  }
89 
90  virtual void Clear() override
91  {
92  m_items.clear();
93  }
94 
95  virtual unsigned int GetSize() const override
96  {
97  return m_items.size();
98  }
99 
100  virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override
101  {
102  if( aIdx < m_items.size() )
103  return m_items[ aIdx ];
104 
105  return nullptr;
106  }
107 
108  bool Contains( EDA_ITEM* aItem ) const
109  {
110  CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
111 
112  return !( i == m_items.end() || *i > aItem );
113  }
114 
116  bool Empty() const
117  {
118  return ( m_items.size() == 0 );
119  }
120 
122  int Size() const
123  {
124  return m_items.size();
125  }
126 
127  const std::deque<EDA_ITEM*> GetItems() const
128  {
129  return m_items;
130  }
131 
133  VECTOR2I GetCenter() const;
134 
135  const BOX2I ViewBBox() const override;
136 
138  VECTOR2I GetPosition() const;
139 
140  EDA_RECT GetBoundingBox() const;
141  EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const;
142  EDA_ITEM* GetTopLeftModule() const;
143 
144  EDA_ITEM* operator[]( const size_t aIdx ) const
145  {
146  if( aIdx < m_items.size() )
147  return m_items[ aIdx ];
148 
149  return nullptr;
150  }
151 
152  EDA_ITEM* Front() const
153  {
154  return m_items.front();
155  }
156 
157  std::deque<EDA_ITEM*>& Items()
158  {
159  return m_items;
160  }
161 
162  template<class T>
163  T* FirstOfKind() const
164  {
165  auto refType = T( nullptr ).Type();
166 
167  for( auto item : m_items )
168  {
169  if( item->Type() == refType )
170  return static_cast<T*> ( item );
171  }
172 
173  return nullptr;
174  }
175 
182  bool HasType( KICAD_T aType ) const
183  {
184  for( auto item : m_items )
185  {
186  if( item->Type() == aType )
187  return true;
188  }
189 
190  return false;
191  }
192 
193  virtual const VIEW_GROUP::ITEMS updateDrawList() const override;
194 
195  bool HasReferencePoint() const
196  {
197  return m_referencePoint != NULLOPT;
198  }
199 
201  {
202  return *m_referencePoint;
203  }
204 
205  void SetReferencePoint( const VECTOR2I& aP )
206  {
207  m_referencePoint = aP;
208  }
209 
211  {
213  }
214 
215 private:
216 
218 
220  std::deque<EDA_ITEM*> m_items;
221 
222  bool m_isHover;
223 
224  // mute hidden overloaded virtual function warnings
225  using VIEW_GROUP::Add;
226  using VIEW_GROUP::Remove;
227 };
228 
230 {
234 };
235 
236 
237 #endif
void ClearReferencePoint()
Definition: selection.h:210
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:116
bool m_isHover
Definition: selection.h:222
VECTOR2I GetReferencePoint() const
Definition: selection.h:200
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:90
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:157
EDA_ITEM * operator[](const size_t aIdx) const
Definition: selection.h:144
virtual const VIEW_GROUP::ITEMS updateDrawList() const override
Definition: selection.cpp:140
bool Contains(EDA_ITEM *aItem) const
Definition: selection.h:108
EDA_ITEM * Front() const
Definition: selection.h:152
ITER end()
Definition: selection.h:60
CITER begin() const
Definition: selection.h:61
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
Definition: selection.cpp:61
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.h:182
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:217
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
ITER begin()
Definition: selection.h:59
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:205
const BOX2I ViewBBox() const override
Function ViewBBox() Returns the bounding box for all stored items covering all its layers...
Definition: selection.cpp:132
SELECTION(const SELECTION &aOther)
Definition: selection.h:43
const auto NULLOPT
Definition: optional.h:9
std::deque< EDA_ITEM * > m_items
Set of selected items.
Definition: selection.h:220
SELECTION()
Definition: selection.h:38
const SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:49
SELECTION_LOCK_FLAGS
Definition: selection.h:229
EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.cpp:90
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
EDA_RECT GetBoundingBox() const
Definition: selection.cpp:73
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:56
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.h:100
const std::deque< EDA_ITEM * > GetItems() const
Definition: selection.h:127
T * FirstOfKind() const
Definition: selection.h:163
virtual unsigned int GetSize() const override
Function GetSize() Returns the number of stored items.
Definition: selection.h:95
size_t i
Definition: json11.cpp:597
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
boost::optional< T > OPT
Definition: optional.h:7
bool IsHover() const
Definition: selection.h:69
int Size() const
Returns the number of selected parts.
Definition: selection.h:122
Basic classes for most KiCad items.
bool HasReferencePoint() const
Definition: selection.h:195
VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:67
EDA_ITEM * GetTopLeftModule() const
Definition: selection.cpp:126
CITER end() const
Definition: selection.h:62
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:82