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 #include <eda_item.h>
31 
32 using namespace std::placeholders;
33 
34 
35 bool SELECTION_CONDITIONS::NotEmpty( const SELECTION& aSelection )
36 {
37  return !aSelection.Empty();
38 }
39 
40 
41 bool SELECTION_CONDITIONS::Empty( const SELECTION& aSelection )
42 {
43  return aSelection.Empty();
44 }
45 
46 
47 bool SELECTION_CONDITIONS::Idle( const SELECTION& aSelection )
48 {
49  return ( !aSelection.Front() || aSelection.Front()->GetEditFlags() == 0 );
50 }
51 
52 
54 {
55  return ( aSelection.Front() && aSelection.Front()->GetEditFlags() == 0 );
56 }
57 
58 
60 {
61  return std::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType );
62 }
63 
64 
66 {
67  return std::bind( &SELECTION_CONDITIONS::onlyTypeFunc, _1, aType );
68 }
69 
70 
72 {
73  return std::bind( &SELECTION_CONDITIONS::onlyTypesFunc, _1, aTypes );
74 }
75 
76 
78 {
79  return std::bind( &SELECTION_CONDITIONS::countFunc, _1, aNumber );
80 }
81 
82 
84 {
85  return std::bind( &SELECTION_CONDITIONS::moreThanFunc, _1, aNumber );
86 }
87 
88 
90 {
91  return std::bind( &SELECTION_CONDITIONS::lessThanFunc, _1, aNumber );
92 }
93 
94 
95 bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType )
96 {
97  for( const auto& item : aSelection )
98  {
99  if( item->Type() == aType )
100  return true;
101  }
102 
103  return false;
104 }
105 
106 
107 bool SELECTION_CONDITIONS::onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType )
108 {
109  if( aSelection.Empty() )
110  return false;
111 
112  KICAD_T types[] = { aType, EOT };
113 
114  for( const auto& item : aSelection )
115  {
116  if( !item->IsType( types ) )
117  return false;
118  }
119 
120  return true;
121 }
122 
123 
124 bool SELECTION_CONDITIONS::onlyTypesFunc( const SELECTION& aSelection, const KICAD_T aTypes[] )
125 {
126  if( aSelection.Empty() )
127  return false;
128 
129  for( const auto& item : aSelection )
130  {
131  if( !item->IsType( aTypes ) )
132  return false;
133  }
134 
135  return true;
136 }
137 
138 
139 bool SELECTION_CONDITIONS::countFunc( const SELECTION& aSelection, int aNumber )
140 {
141  return aSelection.Size() == aNumber;
142 }
143 
144 
145 bool SELECTION_CONDITIONS::moreThanFunc( const SELECTION& aSelection, int aNumber )
146 {
147  return aSelection.Size() > aNumber;
148 }
149 
150 
151 bool SELECTION_CONDITIONS::lessThanFunc( const SELECTION& aSelection, int aNumber )
152 {
153  return aSelection.Size() < aNumber;
154 }
155 
156 
158  const SELECTION_CONDITION& aConditionB )
159 {
160  return std::bind( &SELECTION_CONDITIONS::orFunc, aConditionA, aConditionB, _1 );
161 }
162 
163 
165  const SELECTION_CONDITION& aConditionB )
166 {
167  return std::bind( &SELECTION_CONDITIONS::andFunc, aConditionA, aConditionB, _1 );
168 }
169 
170 
172 {
173  return std::bind( &SELECTION_CONDITIONS::notFunc, aCondition, _1 );
174 }
175 
176 
178  SELECTION_BOOL aConditionB )
179 {
180  return std::bind( &SELECTION_CONDITIONS::orBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
181 }
182 
184  const SELECTION_CONDITION& aConditionB )
185 {
186  return aConditionB || aConditionA;
187 }
188 
189 
191  SELECTION_BOOL aConditionB )
192 {
193  return std::bind( &SELECTION_CONDITIONS::andBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
194 }
195 
197  const SELECTION_CONDITION& aConditionB )
198 {
199  return aConditionB && aConditionA;
200 }
SELECTION_CONDITION operator!(const SELECTION_CONDITION &aCondition)
static bool onlyTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by OnlyTypes()
SELECTION_CONDITION operator &&(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static bool countFunc(const SELECTION &aSelection, int aNumber)
Helper function used by MoreThan()
bool(&) SELECTION_BOOL(const SELECTION &)
Signature for a reference to a function that takes a SELECTION and returns a boolean.
static bool andBoolFunc(const SELECTION_CONDITION &aConditionA, SELECTION_BOOL &aConditionB, const SELECTION &aSelection)
static bool notFunc(const SELECTION_CONDITION &aCondition, const SELECTION &aSelection)
Helper function used by operator||.
static bool orBoolFunc(const SELECTION_CONDITION &aConditionA, SELECTION_BOOL &aConditionB, const SELECTION &aSelection)
Helper function used by operator&&.
static SELECTION_CONDITION MoreThan(int aNumber)
Create a functor that tests if the number of selected items is greater than the value given as parame...
static bool lessThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by operator||.
static bool Idle(const SELECTION &aSelection)
Test if there no items selected or being edited.
static SELECTION_CONDITION OnlyTypes(const KICAD_T aTypes[])
Create a functor that tests if the selected items are only of given types.
static SELECTION_CONDITION HasType(KICAD_T aType)
Create a functor that tests if among the selected items there is at least one of a given type.
static SELECTION_CONDITION Count(int aNumber)
Create a functor that tests if the number of selected items is equal to the value given as parameter.
static bool IdleSelection(const SELECTION &aSelection)
Test if all selected items are not being edited.
static bool NotEmpty(const SELECTION &aSelection)
Test if there are any items selected.
static bool onlyTypesFunc(const SELECTION &aSelection, const KICAD_T aTypes[])
Helper function used by Count()
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
static bool orFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB, const SELECTION &aSelection)
Helper function used by operator&&.
static SELECTION_CONDITION LessThan(int aNumber)
Create a functor that tests if the number of selected items is smaller than the value given as parame...
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:158
SELECTION_CONDITION operator||(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:97
static bool hasTypeFunc(const SELECTION &aSelection, KICAD_T aType)
< Helper function used by HasType()
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Create a functor that tests if the selected items are only of given type.
int Size() const
Returns the number of selected parts.
Definition: selection.h:103
static bool Empty(const SELECTION &aSelection)
Test if there are no items selected.
static bool andFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB, const SELECTION &aSelection)
Helper function used by operator!
EDA_ITEM * Front() const
Definition: selection.h:144
static bool moreThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by LessThan()