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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <algorithm>
28 #include <eda_item.h>
29 #include <tool/selection.h>
30 
31 
32 void SELECTION::Add( EDA_ITEM* aItem )
33 {
34  // We're not sorting here; this is just a time-optimized way to do an
35  // inclusion check. std::lower_bound will return the first i >= aItem
36  // and the second i > aItem check rules out i == aItem.
37  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
38 
39  if( i == m_items.end() || *i > aItem )
40  m_items.insert( i, aItem );
41 }
42 
43 
45 {
46  ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
47 
48  if( !( i == m_items.end() || *i > aItem ) )
49  m_items.erase( i );
50 }
51 
52 
53 KIGFX::VIEW_ITEM* SELECTION::GetItem( unsigned int aIdx ) const
54 {
55  if( aIdx < m_items.size() )
56  return m_items[aIdx];
57 
58  return nullptr;
59 }
60 
61 
62 bool SELECTION::Contains( EDA_ITEM* aItem ) const
63 {
64  CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
65 
66  return !( i == m_items.end() || *i > aItem );
67 }
68 
69 
72 {
74  bool includeLabels = true;
75 
76  // If the selection contains at least one non-label then don't include labels when
77  // calculating the centerpoint.
78 
79  for( EDA_ITEM* item : m_items )
80  {
81  if( !item->IsType( labelTypes ) )
82  {
83  includeLabels = false;
84  break;
85  }
86  }
87 
88  EDA_RECT bbox;
89 
90  for( EDA_ITEM* item : m_items )
91  {
92  if( !item->IsType( labelTypes ) || includeLabels )
93  bbox.Merge( item->GetBoundingBox() );
94  }
95 
96  return static_cast<VECTOR2I>( bbox.Centre() );
97 }
98 
99 
101 {
102  EDA_RECT bbox;
103 
104  for( EDA_ITEM* item : m_items )
105  bbox.Merge( item->GetBoundingBox() );
106 
107  return bbox;
108 }
109 
110 
111 bool SELECTION::HasType( KICAD_T aType ) const
112 {
113  for( auto item : m_items )
114  {
115  if( item->Type() == aType )
116  return true;
117  }
118 
119  return false;
120 }
121 
122 
123 const std::vector<KIGFX::VIEW_ITEM*> SELECTION::updateDrawList() const
124 {
125  std::vector<VIEW_ITEM*> items;
126 
127  for( auto item : m_items )
128  items.push_back( item );
129 
130  return items;
131 }
132 
133 
135 {
136  return ( std::all_of( m_items.begin() + 1, m_items.end(),
137  [&]( const EDA_ITEM* r )
138  {
139  return r->Type() == m_items.front()->Type();
140  } ) );
141 }
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:71
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:111
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:32
std::deque< EDA_ITEM * >::const_iterator CITER
Definition: selection.h:62
bool AreAllItemsIdentical() const
Checks if all items in the selection are the same KICAD_T type.
Definition: selection.cpp:134
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:76
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:206
bool Contains(EDA_ITEM *aItem) const
Definition: selection.cpp:62
std::deque< EDA_ITEM * >::iterator ITER
Definition: selection.h:61
E_SERIE r
Definition: eserie.cpp:41
virtual const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
Definition: selection.cpp:123
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:53
virtual void Remove(EDA_ITEM *aItem)
Definition: selection.cpp:44
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:100