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 <set>
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::set<EDA_ITEM*>::iterator;
57  using CITER = std::set<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  m_items.insert( aItem );
77  }
78 
79  virtual void Remove( EDA_ITEM *aItem )
80  {
81  m_items.erase( aItem );
82  }
83 
84  virtual void Clear() override
85  {
86  m_items.clear();
87  }
88 
89  virtual unsigned int GetSize() const override
90  {
91  return m_items.size();
92  }
93 
94  virtual KIGFX::VIEW_ITEM* GetItem( unsigned int idx ) const override
95  {
96  auto iter = m_items.begin();
97 
98  std::advance( iter, idx );
99 
100  return *iter;
101  }
102 
103  bool Contains( EDA_ITEM* aItem ) const
104  {
105  return m_items.find( aItem ) != m_items.end();
106  }
107 
109  bool Empty() const
110  {
111  return ( m_items.size() == 0 );
112  }
113 
115  int Size() const
116  {
117  return m_items.size();
118  }
119 
120  const std::set<EDA_ITEM*> GetItems() const
121  {
122  return m_items;
123  }
124 
126  VECTOR2I GetCenter() const;
127 
128  const BOX2I ViewBBox() const override;
129 
131  VECTOR2I GetPosition() const;
132 
133  EDA_RECT GetBoundingBox() const;
134  EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const;
135  EDA_ITEM* GetTopLeftModule() const;
136 
137  EDA_ITEM* operator[]( const int index ) const
138  {
139  if( index < 0 || (unsigned int) index >= m_items.size() )
140  return nullptr;
141 
142  auto iter = m_items.begin();
143  std::advance( iter, index );
144  return *iter;
145  }
146 
147  EDA_ITEM* Front() const
148  {
149  if ( !m_items.size() )
150  return nullptr;
151 
152  return *m_items.begin();
153  }
154 
155  std::set<EDA_ITEM*>& Items()
156  {
157  return m_items;
158  }
159 
160  template<class T>
161  T* FirstOfKind() const
162  {
163  auto refType = T( nullptr ).Type();
164 
165  for( auto item : m_items )
166  {
167  if( item->Type() == refType )
168  return static_cast<T*> ( item );
169  }
170 
171  return nullptr;
172  }
173 
180  bool HasType( KICAD_T aType ) const
181  {
182  for( auto item : m_items )
183  {
184  if( item->Type() == aType )
185  return true;
186  }
187 
188  return false;
189  }
190 
191  virtual const VIEW_GROUP::ITEMS updateDrawList() const override;
192 
193  bool HasReferencePoint() const
194  {
195  return m_referencePoint != NULLOPT;
196  }
197 
199  {
200  return *m_referencePoint;
201  }
202 
203  void SetReferencePoint( const VECTOR2I& aP )
204  {
205  m_referencePoint = aP;
206  }
207 
209  {
211  }
212 
213 private:
214 
216 
218  std::set<EDA_ITEM*> m_items;
219 
220  bool m_isHover;
221 
222  // mute hidden overloaded virtual function warnings
223  using VIEW_GROUP::Add;
224  using VIEW_GROUP::Remove;
225 };
226 
228 {
232 };
233 
234 // Selection type flags for RequestSelection()
236 {
237  // Items that can be deleted (but not necessarily modified, eg. DRC markers)
239  // Items that can be edited (moved, rotated, properties)
241  // Remove pads without a host module
243  // Request a hover-only selection
245  // Select locked parts without asking the user
247 
249 };
250 
251 #endif
void ClearReferencePoint()
Definition: selection.h:208
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:109
bool m_isHover
Definition: selection.h:220
VECTOR2I GetReferencePoint() const
Definition: selection.h:198
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:84
virtual const VIEW_GROUP::ITEMS updateDrawList() const override
Definition: selection.cpp:140
bool Contains(EDA_ITEM *aItem) const
Definition: selection.h:103
EDA_ITEM * Front() const
Definition: selection.h:147
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:180
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int idx) const override
Definition: selection.h:94
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:215
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
const std::set< EDA_ITEM * > GetItems() const
Definition: selection.h:120
ITER begin()
Definition: selection.h:59
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:203
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
SELECTION_TYPE_FLAGS
Definition: selection.h:235
SELECTION()
Definition: selection.h:38
const SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:49
SELECTION_LOCK_FLAGS
Definition: selection.h:227
EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
Definition: selection.cpp:90
std::set< EDA_ITEM * > & Items()
Definition: selection.h:155
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
EDA_RECT GetBoundingBox() const
Definition: selection.cpp:73
std::set< EDA_ITEM * >::iterator ITER
Definition: selection.h:56
T * FirstOfKind() const
Definition: selection.h:161
EDA_ITEM * operator[](const int index) const
Definition: selection.h:137
virtual unsigned int GetSize() const override
Function GetSize() Returns the number of stored items.
Definition: selection.h:89
std::set< EDA_ITEM * > m_items
Set of selected items.
Definition: selection.h:218
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:162
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:115
Basic classes for most KiCad items.
bool HasReferencePoint() const
Definition: selection.h:193
std::set< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:57
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:79