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:
38  SELECTION() {};
39 
40  SELECTION( const SELECTION& aOther )
41  {
42  m_items = aOther.m_items;
43  m_isHover = aOther.m_isHover;
44  }
45 
46  const SELECTION& operator= ( const SELECTION& aOther )
47  {
48  m_items = aOther.m_items;
49  m_isHover = aOther.m_isHover;
50  return *this;
51  }
52 
53  using ITER = std::set<EDA_ITEM*>::iterator;
54  using CITER = std::set<EDA_ITEM*>::const_iterator;
55 
56  ITER begin() { return m_items.begin(); }
57  ITER end() { return m_items.end(); }
58  CITER begin() const { return m_items.cbegin(); }
59  CITER end() const { return m_items.cend(); }
60 
61  void SetIsHover( bool aIsHover )
62  {
63  m_isHover = aIsHover;
64  }
65 
66  bool IsHover() const
67  {
68  return m_isHover;
69  }
70 
71  virtual void Add( EDA_ITEM* aItem )
72  {
73  m_items.insert( aItem );
74  }
75 
76  virtual void Remove( EDA_ITEM *aItem )
77  {
78  m_items.erase( aItem );
79  }
80 
81  virtual void Clear() override
82  {
83  m_items.clear();
84  }
85 
86  virtual unsigned int GetSize() const override
87  {
88  return m_items.size();
89  }
90 
91  virtual KIGFX::VIEW_ITEM* GetItem( unsigned int idx ) const override
92  {
93  auto iter = m_items.begin();
94 
95  std::advance( iter, idx );
96 
97  return *iter;
98  }
99 
100  bool Contains( EDA_ITEM* aItem ) const
101  {
102  return m_items.find( aItem ) != m_items.end();
103  }
104 
106  bool Empty() const
107  {
108  return ( m_items.size() == 0 );
109  }
110 
112  int Size() const
113  {
114  return m_items.size();
115  }
116 
117  const std::set<EDA_ITEM*> GetItems() const
118  {
119  return m_items;
120  }
121 
123  VECTOR2I GetCenter() const;
124 
125  const BOX2I ViewBBox() const override;
126 
128  VECTOR2I GetPosition() const;
129 
130  EDA_RECT GetBoundingBox() const;
131  EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const;
132  EDA_ITEM* GetTopLeftModule() const;
133 
134  EDA_ITEM* operator[]( const int index ) const
135  {
136  if( index < 0 || (unsigned int) index >= m_items.size() )
137  return nullptr;
138 
139  auto iter = m_items.begin();
140  std::advance( iter, index );
141  return *iter;
142  }
143 
144  EDA_ITEM* Front() const
145  {
146  if ( !m_items.size() )
147  return nullptr;
148 
149  return *m_items.begin();
150  }
151 
152  std::set<EDA_ITEM*>& Items()
153  {
154  return m_items;
155  }
156 
157  template<class T>
158  T* FirstOfKind() const
159  {
160  auto refType = T( nullptr ).Type();
161 
162  for( auto item : m_items )
163  {
164  if( item->Type() == refType )
165  return static_cast<T*> ( item );
166  }
167 
168  return nullptr;
169  }
170 
171  virtual const VIEW_GROUP::ITEMS updateDrawList() const override;
172 
173  bool HasReferencePoint() const
174  {
175  return m_referencePoint != boost::none;
176  }
177 
179  {
180  return *m_referencePoint;
181  }
182 
183  void SetReferencePoint( const VECTOR2I& aP )
184  {
185  m_referencePoint = aP;
186  }
187 
189  {
190  m_referencePoint = boost::none;
191  }
192 
193 private:
194 
196 
198  std::set<EDA_ITEM*> m_items;
199 
200  bool m_isHover;
201 
202  // mute hidden overloaded virtual function warnings
203  using VIEW_GROUP::Add;
204  using VIEW_GROUP::Remove;
205 };
206 
208 {
212 };
213 
214 // Selection type flags for RequestSelection()
216 {
217  // Items that can be deleted (but not necessarily modified, eg. DRC markers)
219  // Items that can be edited (moved, rotated, properties)
221  // Remove pads without a host module
223  // Request a hover-only selection
226 };
227 
228 #endif
void ClearReferencePoint()
Definition: selection.h:188
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:106
bool m_isHover
Definition: selection.h:200
VECTOR2I GetReferencePoint() const
Definition: selection.h:178
virtual void Clear() override
Function Clear() Removes all the stored items from the group.
Definition: selection.h:81
T
enum T contains all this lexer's tokens.
virtual const VIEW_GROUP::ITEMS updateDrawList() const override
bool Contains(EDA_ITEM *aItem) const
Definition: selection.h:100
EDA_ITEM * Front() const
Definition: selection.h:144
ITER end()
Definition: selection.h:57
CITER begin() const
Definition: selection.h:58
VECTOR2I GetPosition() const
Returns the top left point of the selection area bounding box.
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int idx) const override
Definition: selection.h:91
Class VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW...
Definition: view_item.h:83
void SetIsHover(bool aIsHover)
Definition: selection.h:61
virtual void Add(EDA_ITEM *aItem)
Definition: selection.h:71
const std::set< EDA_ITEM * > GetItems() const
Definition: selection.h:117
ITER begin()
Definition: selection.h:56
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:183
const BOX2I ViewBBox() const override
Function ViewBBox() Returns the bounding box for all stored items covering all its layers...
SELECTION(const SELECTION &aOther)
Definition: selection.h:40
SELECTION_TYPE_FLAGS
Definition: selection.h:215
SELECTION()
Definition: selection.h:38
const SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:46
SELECTION_LOCK_FLAGS
Definition: selection.h:207
boost::optional< VECTOR2I > m_referencePoint
Definition: selection.h:195
EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const
std::set< EDA_ITEM * > & Items()
Definition: selection.h:152
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
EDA_RECT GetBoundingBox() const
std::set< EDA_ITEM * >::iterator ITER
Definition: selection.h:53
T * FirstOfKind() const
Definition: selection.h:158
EDA_ITEM * operator[](const int index) const
Definition: selection.h:134
virtual unsigned int GetSize() const override
Function GetSize() Returns the number of stored items.
Definition: selection.h:86
std::set< EDA_ITEM * > m_items
Set of selected items.
Definition: selection.h:198
Class EDA_RECT handles the component boundary box.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
bool IsHover() const
Definition: selection.h:66
int Size() const
Returns the number of selected parts.
Definition: selection.h:112
Basic classes for most KiCad items.
bool HasReferencePoint() const
Definition: selection.h:173
std::set< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:54
VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
EDA_ITEM * GetTopLeftModule() const
CITER end() const
Definition: selection.h:59
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:76