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 
40 bool SELECTION_CONDITIONS::Empty( const SELECTION& aSelection )
41 {
42  return aSelection.Empty();
43 }
44 
45 
46 bool SELECTION_CONDITIONS::Idle( const SELECTION& aSelection )
47 {
48  return ( !aSelection.Front() || aSelection.Front()->GetEditFlags() == 0 );
49 }
50 
51 
53 {
54  return ( aSelection.Front() && aSelection.Front()->GetEditFlags() == 0 );
55 }
56 
57 
59 {
60  return std::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType );
61 }
62 
63 
65 {
66  return std::bind( &SELECTION_CONDITIONS::onlyTypeFunc, _1, aType );
67 }
68 
69 
71 {
72  return std::bind( &SELECTION_CONDITIONS::onlyTypesFunc, _1, aTypes );
73 }
74 
75 
77 {
78  return std::bind( &SELECTION_CONDITIONS::countFunc, _1, aNumber );
79 }
80 
81 
83 {
84  return std::bind( &SELECTION_CONDITIONS::moreThanFunc, _1, aNumber );
85 }
86 
87 
89 {
90  return std::bind( &SELECTION_CONDITIONS::lessThanFunc, _1, aNumber );
91 }
92 
93 
94 bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType )
95 {
96  for( const auto& item : aSelection )
97  {
98  if( item->Type() == aType )
99  return true;
100  }
101 
102  return false;
103 }
104 
105 
106 bool SELECTION_CONDITIONS::onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType )
107 {
108  if( aSelection.Empty() )
109  return false;
110 
111  KICAD_T types[] = { aType, EOT };
112 
113  for( const auto& item : aSelection )
114  {
115  if( !item->IsType( types ) )
116  return false;
117  }
118 
119  return true;
120 }
121 
122 
123 bool SELECTION_CONDITIONS::onlyTypesFunc( const SELECTION& aSelection, const KICAD_T aTypes[] )
124 {
125  if( aSelection.Empty() )
126  return false;
127 
128  for( const auto& item : aSelection )
129  {
130  if( !item->IsType( aTypes ) )
131  return false;
132  }
133 
134  return true;
135 }
136 
137 
138 bool SELECTION_CONDITIONS::countFunc( const SELECTION& aSelection, int aNumber )
139 {
140  return aSelection.Size() == aNumber;
141 }
142 
143 
144 bool SELECTION_CONDITIONS::moreThanFunc( const SELECTION& aSelection, int aNumber )
145 {
146  return aSelection.Size() > aNumber;
147 }
148 
149 
150 bool SELECTION_CONDITIONS::lessThanFunc( const SELECTION& aSelection, int aNumber )
151 {
152  return aSelection.Size() < aNumber;
153 }
154 
155 
157  const SELECTION_CONDITION& aConditionB )
158 {
159  return std::bind( &SELECTION_CONDITIONS::orFunc, aConditionA, aConditionB, _1 );
160 }
161 
162 
164  const SELECTION_CONDITION& aConditionB )
165 {
166  return std::bind( &SELECTION_CONDITIONS::andFunc, aConditionA, aConditionB, _1 );
167 }
168 
169 
171 {
172  return std::bind( &SELECTION_CONDITIONS::notFunc, aCondition, _1 );
173 }
174 
175 
177  SELECTION_BOOL aConditionB )
178 {
179  return std::bind( &SELECTION_CONDITIONS::orBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
180 }
181 
183  const SELECTION_CONDITION& aConditionB )
184 {
185  return aConditionB || aConditionA;
186 }
187 
188 
190  SELECTION_BOOL aConditionB )
191 {
192  return std::bind( &SELECTION_CONDITIONS::andBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
193 }
194 
196  const SELECTION_CONDITION& aConditionB )
197 {
198  return aConditionB && aConditionA;
199 }
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()
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.
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...
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()
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)
Creates a functor that tests if the number of selected items is smaller than the value given as param...
STATUS_FLAGS GetEditFlags() const
Definition: base_struct.h:237
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:121
static bool hasTypeFunc(const SELECTION &aSelection, KICAD_T aType)
Helper function used by HasType()
static SELECTION_CONDITION OnlyType(KICAD_T aType)
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:127
static bool Empty(const SELECTION &aSelection)
Tests 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:184
static bool moreThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by MoreThan()