KiCad PCB EDA Suite
selection_conditions.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) 2014 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include <tool/selection.h>
28 
29 #include <functional>
30 using namespace std::placeholders;
31 
32 
33 bool SELECTION_CONDITIONS::NotEmpty( const SELECTION& aSelection )
34 {
35  return !aSelection.Empty();
36 }
37 
38 
40 {
41  return std::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType );
42 }
43 
44 
46 {
47  return std::bind( &SELECTION_CONDITIONS::onlyTypeFunc, _1, aType );
48 }
49 
50 
51 SELECTION_CONDITION SELECTION_CONDITIONS::OnlyTypes( const std::vector<KICAD_T>& aTypes )
52 {
53  return std::bind( &SELECTION_CONDITIONS::onlyTypesFunc, _1, aTypes );
54 }
55 
56 
58 {
59  return std::bind( &SELECTION_CONDITIONS::onlyTypesFuncArr, _1, aTypes );
60 }
61 
62 
64 {
65  return std::bind( &SELECTION_CONDITIONS::countFunc, _1, aNumber );
66 }
67 
68 
70 {
71  return std::bind( &SELECTION_CONDITIONS::moreThanFunc, _1, aNumber );
72 }
73 
74 
76 {
77  return std::bind( &SELECTION_CONDITIONS::lessThanFunc, _1, aNumber );
78 }
79 
80 
81 bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType )
82 {
83  for( const auto& item : aSelection )
84  {
85  if( item->Type() == aType )
86  return true;
87  }
88 
89  return false;
90 }
91 
92 
93 bool SELECTION_CONDITIONS::onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType )
94 {
95  if( aSelection.Empty() )
96  return false;
97 
98  for( const auto& item : aSelection )
99  {
100  if( item->Type() != aType )
101  return false;
102  }
103 
104  return true;
105 }
106 
107 
108 bool SELECTION_CONDITIONS::onlyTypesFunc( const SELECTION& aSelection, const std::vector<KICAD_T>& aTypes )
109 {
110  if( aSelection.Empty() )
111  return false;
112 
113  for( const auto& item : aSelection )
114  {
115  bool valid = false;
116 
117  for( std::vector<KICAD_T>::const_iterator it = aTypes.begin(); it != aTypes.end(); ++it )
118  {
119  if( item->Type() == *it )
120  {
121  valid = true;
122  break;
123  }
124  }
125 
126  if( !valid )
127  return false;
128  }
129 
130  return true;
131 }
132 
133 
134 bool SELECTION_CONDITIONS::onlyTypesFuncArr( const SELECTION& aSelection, const KICAD_T aTypes[] )
135 {
136  if( aSelection.Empty() )
137  return false;
138 
139  for( const auto& item : aSelection )
140  {
141  bool valid = false;
142  const KICAD_T* type = aTypes;
143 
144  while( *type != EOT )
145  {
146  if( item->Type() == *type )
147  {
148  valid = true;
149  break;
150  }
151 
152  ++type;
153  }
154 
155  if( !valid )
156  return false;
157  }
158 
159  return true;
160 }
161 
162 
163 bool SELECTION_CONDITIONS::countFunc( const SELECTION& aSelection, int aNumber )
164 {
165  return aSelection.Size() == aNumber;
166 }
167 
168 
169 bool SELECTION_CONDITIONS::moreThanFunc( const SELECTION& aSelection, int aNumber )
170 {
171  return aSelection.Size() > aNumber;
172 }
173 
174 
175 bool SELECTION_CONDITIONS::lessThanFunc( const SELECTION& aSelection, int aNumber )
176 {
177  return aSelection.Size() < aNumber;
178 }
179 
180 
182  const SELECTION_CONDITION& aConditionB )
183 {
184  return std::bind( &SELECTION_CONDITIONS::orFunc, aConditionA, aConditionB, _1 );
185 }
186 
187 
189  const SELECTION_CONDITION& aConditionB )
190 {
191  return std::bind( &SELECTION_CONDITIONS::andFunc, aConditionA, aConditionB, _1 );
192 }
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:106
static bool onlyTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by OnlyType()
static bool countFunc(const SELECTION &aSelection, int aNumber)
Helper function used by Count()
static SELECTION_CONDITION MoreThan(int aNumber)
Function MoreThan Creates a functor that tests if the number of selected items is greater than the va...
static bool lessThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by LessThan()
static SELECTION_CONDITION HasType(KICAD_T aType)
Function HasType Creates a functor that tests if among the selected items there is at least one of a ...
static SELECTION_CONDITION Count(int aNumber)
Function Count Creates a functor that tests if the number of selected items is equal to the value giv...
static bool NotEmpty(const SELECTION &aSelection)
Function NotEmpty Tests if there are any items selected.
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
static bool orFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB, const SELECTION &aSelection)
Helper function used by operator||
static bool onlyTypesFunc(const SELECTION &aSelection, const std::vector< KICAD_T > &aTypes)
Helper function used by OnlyTypes()
SELECTION_CONDITION operator&&(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static SELECTION_CONDITION LessThan(int aNumber)
Function LessThan Creates a functor that tests if the number of selected items is smaller than the va...
static SELECTION_CONDITION OnlyTypes(const std::vector< KICAD_T > &aTypes)
Function OnlyTypes Creates a functor that tests if the selected items are only of given types...
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
SELECTION_CONDITION operator||(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static bool hasTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by HasType()
static bool onlyTypesFuncArr(const SELECTION &aSelection, const KICAD_T aTypes[])
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates a functor that tests if the selected items are only of given type...
static bool andFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB, const SELECTION &aSelection)
Helper function used by operator&&
int Size() const
Returns the number of selected parts.
Definition: selection.h:112
static bool moreThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by MoreThan()