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  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
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 #ifndef SELECTION_CONDITIONS_H_
28 #define SELECTION_CONDITIONS_H_
29 
30 #include <functional>
31 #include <core/typeinfo.h>
32 #include <vector>
33 #include <tool/selection.h>
34 
36 typedef std::function<bool (const SELECTION&)> SELECTION_CONDITION;
37 
39  const SELECTION_CONDITION& aConditionB );
40 
42  const SELECTION_CONDITION& aConditionB );
43 
45 
46 
51 typedef bool ( &SELECTION_BOOL )( const SELECTION& );
52 
54  SELECTION_BOOL aConditionB );
55 
57  const SELECTION_CONDITION& aConditionB );
58 
60  SELECTION_BOOL aConditionB );
61 
63  const SELECTION_CONDITION& aConditionB );
64 
69 {
70 public:
77  static bool ShowAlways( const SELECTION& aSelection )
78  {
79  return true;
80  }
81 
88  static bool ShowNever( const SELECTION& aSelection )
89  {
90  return false;
91  }
92 
99  static bool NotEmpty( const SELECTION& aSelection );
100 
107  static bool Empty( const SELECTION& aSelection );
108 
115  static bool Idle( const SELECTION& aSelection );
116 
123  static bool IdleSelection( const SELECTION& aSelection );
124 
132  static SELECTION_CONDITION HasType( KICAD_T aType );
133 
140  static SELECTION_CONDITION OnlyType( KICAD_T aType );
141 
149  static SELECTION_CONDITION OnlyTypes( const KICAD_T aTypes[] );
150 
158  static SELECTION_CONDITION Count( int aNumber );
159 
167  static SELECTION_CONDITION MoreThan( int aNumber );
168 
176  static SELECTION_CONDITION LessThan( int aNumber );
177 
178 private:
180  static bool hasTypeFunc( const SELECTION& aSelection, KICAD_T aType );
181 
183  static bool onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType );
184 
186  static bool onlyTypesFunc( const SELECTION& aSelection, const KICAD_T aTypes[] );
187 
189  static bool countFunc( const SELECTION& aSelection, int aNumber );
190 
192  static bool moreThanFunc( const SELECTION& aSelection, int aNumber );
193 
195  static bool lessThanFunc( const SELECTION& aSelection, int aNumber );
196 
198  static bool orFunc( const SELECTION_CONDITION& aConditionA,
199  const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
200  {
201  return aConditionA( aSelection ) || aConditionB( aSelection );
202  }
203 
205  static bool andFunc( const SELECTION_CONDITION& aConditionA,
206  const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
207  {
208  return aConditionA( aSelection ) && aConditionB( aSelection );
209  }
210 
212  static bool notFunc( const SELECTION_CONDITION& aCondition, const SELECTION& aSelection )
213  {
214  return !aCondition( aSelection );
215  }
216 
218  static bool orBoolFunc( const SELECTION_CONDITION& aConditionA,
219  SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
220  {
221  return aConditionA( aSelection ) || aConditionB( aSelection );
222  }
223 
225  static bool andBoolFunc( const SELECTION_CONDITION& aConditionA,
226  SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
227  {
228  return aConditionA( aSelection ) && aConditionB( aSelection );
229  }
230 
231  friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
232  const SELECTION_CONDITION& aConditionB );
233 
234  friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
235  const SELECTION_CONDITION& aConditionB );
236 
237  friend SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition );
238 
239  friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
240  SELECTION_BOOL aConditionB );
241 
242  friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
243  SELECTION_BOOL aConditionB );
244 };
245 
246 #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 OnlyTypes()
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.
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)
Create 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)
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()
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...
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)
Create a functor that tests if the selected items are only of given type.
static bool Empty(const SELECTION &aSelection)
Test 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 LessThan()