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 
31 using namespace std::placeholders;
32 
33 
34 bool SELECTION_CONDITIONS::NotEmpty( const SELECTION& aSelection )
35 {
36  return !aSelection.Empty();
37 }
38 
39 
41 {
42  return std::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType );
43 }
44 
45 
47 {
48  return std::bind( &SELECTION_CONDITIONS::onlyTypeFunc, _1, aType );
49 }
50 
51 
53 {
54  return std::bind( &SELECTION_CONDITIONS::onlyTypesFunc, _1, aTypes );
55 }
56 
57 
59 {
60  return std::bind( &SELECTION_CONDITIONS::countFunc, _1, aNumber );
61 }
62 
63 
65 {
66  return std::bind( &SELECTION_CONDITIONS::moreThanFunc, _1, aNumber );
67 }
68 
69 
71 {
72  return std::bind( &SELECTION_CONDITIONS::lessThanFunc, _1, aNumber );
73 }
74 
75 
76 bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType )
77 {
78  for( const auto& item : aSelection )
79  {
80  if( item->Type() == aType )
81  return true;
82  }
83 
84  return false;
85 }
86 
87 
88 bool SELECTION_CONDITIONS::onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType )
89 {
90  if( aSelection.Empty() )
91  return false;
92 
93  KICAD_T types[] = { aType, EOT };
94 
95  for( const auto& item : aSelection )
96  {
97  if( !item->IsType( types ) )
98  return false;
99  }
100 
101  return true;
102 }
103 
104 
105 bool SELECTION_CONDITIONS::onlyTypesFunc( const SELECTION& aSelection, const KICAD_T aTypes[] )
106 {
107  if( aSelection.Empty() )
108  return false;
109 
110  for( const auto& item : aSelection )
111  {
112  if( !item->IsType( aTypes ) )
113  return false;
114  }
115 
116  return true;
117 }
118 
119 
120 bool SELECTION_CONDITIONS::countFunc( const SELECTION& aSelection, int aNumber )
121 {
122  return aSelection.Size() == aNumber;
123 }
124 
125 
126 bool SELECTION_CONDITIONS::moreThanFunc( const SELECTION& aSelection, int aNumber )
127 {
128  return aSelection.Size() > aNumber;
129 }
130 
131 
132 bool SELECTION_CONDITIONS::lessThanFunc( const SELECTION& aSelection, int aNumber )
133 {
134  return aSelection.Size() < aNumber;
135 }
136 
137 
139  const SELECTION_CONDITION& aConditionB )
140 {
141  return std::bind( &SELECTION_CONDITIONS::orFunc, aConditionA, aConditionB, _1 );
142 }
143 
144 
146  const SELECTION_CONDITION& aConditionB )
147 {
148  return std::bind( &SELECTION_CONDITIONS::andFunc, aConditionA, aConditionB, _1 );
149 }
150 
151 
153 {
154  return std::bind( &SELECTION_CONDITIONS::notFunc, aCondition, _1 );
155 }
SELECTION_CONDITION operator!(const SELECTION_CONDITION &aCondition)
static bool onlyTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by OnlyType()
SELECTION_CONDITION operator &&(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
static bool countFunc(const SELECTION &aSelection, int aNumber)
Helper function used by Count()
static bool notFunc(const SELECTION_CONDITION &aCondition, const SELECTION &aSelection)
Helper function used by operator!
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 OnlyTypes(const KICAD_T aTypes[])
Function OnlyTypes Creates a functor that tests if the selected items are only of given types.
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.
static bool onlyTypesFunc(const SELECTION &aSelection, const KICAD_T aTypes[])
Helper function used by OnlyTypes()
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 SELECTION_CONDITION LessThan(int aNumber)
Function LessThan Creates a functor that tests if the number of selected items is smaller than the va...
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)
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:119
static bool hasTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by HasType()
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates 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:125
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()