KiCad PCB EDA Suite
selection_conditions.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) 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 #ifndef SELECTION_CONDITIONS_H_
26 #define SELECTION_CONDITIONS_H_
27 
28 #include <functional>
29 #include <core/typeinfo.h>
30 #include <vector>
31 #include <tool/selection.h>
32 
34 typedef std::function<bool (const SELECTION&)> SELECTION_CONDITION;
35 
37  const SELECTION_CONDITION& aConditionB );
38 
40  const SELECTION_CONDITION& aConditionB );
41 
43 
44 
49 typedef bool ( &SELECTION_BOOL )( const SELECTION& );
50 
52  SELECTION_BOOL aConditionB );
53 
55  const SELECTION_CONDITION& aConditionB );
56 
58  SELECTION_BOOL aConditionB );
59 
61  const SELECTION_CONDITION& aConditionB );
62 
67 {
68 public:
75  static bool ShowAlways( const SELECTION& aSelection )
76  {
77  return true;
78  }
79 
86  static bool ShowNever( const SELECTION& aSelection )
87  {
88  return false;
89  }
90 
97  static bool NotEmpty( const SELECTION& aSelection );
98 
105  static bool Empty( const SELECTION& aSelection );
106 
113  static bool Idle( const SELECTION& aSelection );
114 
121  static bool IdleSelection( const SELECTION& aSelection );
122 
129  static SELECTION_CONDITION HasType( KICAD_T aType );
130 
137  static SELECTION_CONDITION OnlyType( KICAD_T aType );
138 
146  static SELECTION_CONDITION OnlyTypes( const KICAD_T aTypes[] );
147 
155  static SELECTION_CONDITION Count( int aNumber );
156 
164  static SELECTION_CONDITION MoreThan( int aNumber );
165 
173  static SELECTION_CONDITION LessThan( int aNumber );
174 
175 private:
177  static bool hasTypeFunc( const SELECTION& aSelection, KICAD_T aType );
178 
180  static bool onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType );
181 
183  static bool onlyTypesFunc( const SELECTION& aSelection, const KICAD_T aTypes[] );
184 
186  static bool countFunc( const SELECTION& aSelection, int aNumber );
187 
189  static bool moreThanFunc( const SELECTION& aSelection, int aNumber );
190 
192  static bool lessThanFunc( const SELECTION& aSelection, int aNumber );
193 
195  static bool orFunc( const SELECTION_CONDITION& aConditionA,
196  const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
197  {
198  return aConditionA( aSelection ) || aConditionB( aSelection );
199  }
200 
202  static bool andFunc( const SELECTION_CONDITION& aConditionA,
203  const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
204  {
205  return aConditionA( aSelection ) && aConditionB( aSelection );
206  }
207 
209  static bool notFunc( const SELECTION_CONDITION& aCondition, const SELECTION& aSelection )
210  {
211  return !aCondition( aSelection );
212  }
213 
215  static bool orBoolFunc( const SELECTION_CONDITION& aConditionA,
216  SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
217  {
218  return aConditionA( aSelection ) || aConditionB( aSelection );
219  }
220 
222  static bool andBoolFunc( const SELECTION_CONDITION& aConditionA,
223  SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
224  {
225  return aConditionA( aSelection ) && aConditionB( aSelection );
226  }
227 
228  friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
229  const SELECTION_CONDITION& aConditionB );
230 
231  friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
232  const SELECTION_CONDITION& aConditionB );
233 
234  friend SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition );
235 
236  friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
237  SELECTION_BOOL aConditionB );
238 
239  friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
240  SELECTION_BOOL aConditionB );
241 };
242 
243 #endif /* SELECTION_CONDITIONS_H_ */
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
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()
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)
Helper function used by operator&&
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)
Creates a functor that tests if the number of selected items is greater than the value given as param...
static bool lessThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by LessThan()
static bool Idle(const SELECTION &aSelection)
Tests if there no items selected or being edited.
static SELECTION_CONDITION OnlyTypes(const KICAD_T aTypes[])
Creates a functor that tests if the selected items are only of given types.
static SELECTION_CONDITION HasType(KICAD_T aType)
Creates a functor that tests if among the selected items there is at least one of a given type.
Class that groups generic conditions for selected items.
friend SELECTION_CONDITION operator||(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static SELECTION_CONDITION Count(int aNumber)
Creates a functor that tests if the number of selected items is equal to the value given as parameter...
SELECTION_CONDITION operator||(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static bool IdleSelection(const SELECTION &aSelection)
Tests if all selected items are not being edited.
static bool NotEmpty(const SELECTION &aSelection)
Tests if there are any items selected.
static bool onlyTypesFunc(const SELECTION &aSelection, const KICAD_T aTypes[])
Helper function used by OnlyTypes()
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 SELECTION_CONDITION LessThan(int aNumber)
Creates a functor that tests if the number of selected items is smaller than the value given as param...
friend SELECTION_CONDITION operator &&(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
SELECTION_CONDITION operator!(const SELECTION_CONDITION &aCondition)
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
friend SELECTION_CONDITION operator!(const SELECTION_CONDITION &aCondition)
static bool hasTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by HasType()
SELECTION_CONDITION operator &&(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Creates a functor that tests if the selected items are only of given type.
static bool Empty(const SELECTION &aSelection)
Tests if there are no items selected.
static bool ShowNever(const SELECTION &aSelection)
Always returns false.
static bool andFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB, const SELECTION &aSelection)
Helper function used by operator&&
static bool moreThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by MoreThan()