KiCad PCB EDA Suite
selection.cpp
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 #include <algorithm>
27 #include <eda_item.h>
28 #include <tool/selection.h>
29 
30 
31 void SELECTION::Add( EDA_ITEM* aItem )
32 {
33  // We're not sorting here; this is just a time-optimized way to do an
34  // inclusion check. std::lower_bound will return the first i >= aItem
35  // and the second i > aItem check rules out i == aItem.
36  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
37 
38  if( i == m_items.end() || *i > aItem )
39  m_items.insert( i, aItem );
40 }
41 
42 
44 {
45  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
46 
47  if( !( i == m_items.end() || *i > aItem ) )
48  m_items.erase( i );
49 }
50 
51 
52 KIGFX::VIEW_ITEM* SELECTION::GetItem( unsigned int aIdx ) const
53 {
54  if( aIdx < m_items.size() )
55  return m_items[aIdx];
56 
57  return nullptr;
58 }
59 
60 
61 bool SELECTION::Contains( EDA_ITEM* aItem ) const
62 {
63  CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
64 
65  return !( i == m_items.end() || *i > aItem );
66 }
67 
68 
71 {
73  bool includeLabels = true;
74 
75  // If the selection contains at least one non-label then don't include labels when
76  // calculating the centerpoint.
77 
78  for( EDA_ITEM* item : m_items )
79  {
80  if( !item->IsType( labelTypes ) )
81  {
82  includeLabels = false;
83  break;
84  }
85  }
86 
87  EDA_RECT bbox;
88 
89  for( EDA_ITEM* item : m_items )
90  {
91  if( !item->IsType( labelTypes ) || includeLabels )
92  bbox.Merge( item->GetBoundingBox() );
93  }
94 
95  return static_cast<VECTOR2I>( bbox.Centre() );
96 }
97 
98 
100 {
101  EDA_RECT bbox;
102 
103  for( EDA_ITEM* item : m_items )
104  bbox.Merge( item->GetBoundingBox() );
105 
106  return bbox;
107 }
108 
109 
110 bool SELECTION::HasType( KICAD_T aType ) const
111 {
112  for( auto item : m_items )
113  {
114  if( item->Type() == aType )
115  return true;
116  }
117 
118  return false;
119 }
120 
121 
123 {
124  std::vector<VIEW_ITEM*> items;
125 
126  for( auto item : m_items )
127  items.push_back( item );
128 
129  return items;
130 }
131 
132 
134 {
135  return ( std::all_of( m_items.begin() + 1, m_items.end(),
136  [&]( const EDA_ITEM* r )
137  {
138  return r->Type() == m_items.front()->Type();
139  } ) );
140 }
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:70
void Merge(const EDA_RECT &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: eda_rect.cpp:432
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
Definition: selection.cpp:110
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:31
virtual const VIEW_GROUP::ITEMS updateDrawList() const override
Layer on which the group is drawn.
Definition: selection.cpp:122
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:61
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:133
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:81
search types array terminator (End Of Types)
Definition: typeinfo.h:81
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
std::deque< EDA_ITEM * > m_items
Definition: selection.h:205
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:61
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:60
std::vector< VIEW_ITEM * > ITEMS
Definition: view_group.h:119
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:52
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:43
Handle the component boundary box.
Definition: eda_rect.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
wxPoint Centre() const
Definition: eda_rect.h:55
virtual EDA_RECT GetBoundingBox() const
Definition: selection.cpp:99