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 
127  EDA_ITEM* operator[]( const int index ) const
128  {
129  if( index < 0 || (unsigned int) index >= m_items.size() )
130  return nullptr;
131 
132  auto iter = m_items.begin();
133  std::advance( iter, index );
134  return *iter;
135  }
136 
137  EDA_ITEM* Front() const
138  {
139  if ( !m_items.size() )
140  return nullptr;
141 
142  return *m_items.begin();
143  }
144 
145  std::set<EDA_ITEM*>& Items()
146  {
147  return m_items;
148  }
149 
150  template<class T>
151  T* FirstOfKind() const
152  {
153  auto refType = T( nullptr ).Type();
154 
155  for( auto item : m_items )
156  {
157  if( item->Type() == refType )
158  return static_cast<T*> ( item );
159  }
160 
161  return nullptr;
162  }
163 
164  virtual const VIEW_GROUP::ITEMS updateDrawList() const override;
165 
166 private:
168  std::set<EDA_ITEM*> m_items;
169 
170  bool m_isHover;
171 
172  // mute hidden overloaded virtual function warnings
173  using VIEW_GROUP::Add;
174  using VIEW_GROUP::Remove;
175 };
176 
178 {
182 };
183 
184 // Selection type flags for RequestSelection()
186 {
187  // Items that can be deleted (but not necessarily modified, eg. DRC markers)
189  // Items that can be edited (moved, rotated, properties)
191  // Remove pads without a host module
193  // Request a hover-only selection
196 };
197 
198 #endif
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:106
bool m_isHover
Definition: selection.h:170
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:137
ITER end()
Definition: selection.h:57
CITER begin() const
Definition: selection.h:58
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:82
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
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:185
SELECTION()
Definition: selection.h:38
const SELECTION & operator=(const SELECTION &aOther)
Definition: selection.h:46
SELECTION_LOCK_FLAGS
Definition: selection.h:177
std::set< EDA_ITEM * > & Items()
Definition: selection.h:145
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
std::set< EDA_ITEM * >::iterator ITER
Definition: selection.h:53
T * FirstOfKind() const
Definition: selection.h:151
EDA_ITEM * operator[](const int index) const
Definition: selection.h:127
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:168
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
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.
std::set< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:54
VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
CITER end() const
Definition: selection.h:59
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.h:76