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 <[email protected]>
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
32using namespace std::placeholders;
33
34
36{
37 return !aSelection.Empty();
38}
39
40
41bool SELECTION_CONDITIONS::Empty( const SELECTION& aSelection )
42{
43 return aSelection.Empty();
44}
45
46
47bool SELECTION_CONDITIONS::Idle( const SELECTION& aSelection )
48{
49 constexpr int busyMask = ( IS_NEW | IS_PASTED | IS_MOVING | IS_RESIZING | IS_DRAGGING );
50
51 return !aSelection.Front() || ( aSelection.Front()->GetEditFlags() & busyMask ) == 0;
52}
53
54
56{
57 return ( aSelection.Front() && aSelection.Front()->GetEditFlags() == 0 );
58}
59
60
62{
63 return std::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType );
64}
65
66
68{
69 return std::bind( &SELECTION_CONDITIONS::hasTypesFunc, _1, aTypes );
70}
71
72
74{
75 return std::bind( &SELECTION_CONDITIONS::onlyTypesFunc, _1, aTypes );
76}
77
78
80{
81 return std::bind( &SELECTION_CONDITIONS::countFunc, _1, aNumber );
82}
83
84
86{
87 return std::bind( &SELECTION_CONDITIONS::moreThanFunc, _1, aNumber );
88}
89
90
92{
93 return std::bind( &SELECTION_CONDITIONS::lessThanFunc, _1, aNumber );
94}
95
96
97bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType )
98{
99 if( aSelection.Empty() )
100 return false;
101
102 for( const EDA_ITEM* item : aSelection )
103 {
104 if( item->Type() == aType )
105 return true;
106 }
107
108 return false;
109}
110
111
112bool SELECTION_CONDITIONS::hasTypesFunc( const SELECTION& aSelection, std::vector<KICAD_T> aTypes )
113{
114 if( aSelection.Empty() )
115 return false;
116
117 for( const EDA_ITEM* item : aSelection )
118 {
119 if( item->IsType( aTypes ) )
120 return true;
121 }
122
123 return false;
124}
125
126
127bool SELECTION_CONDITIONS::onlyTypesFunc( const SELECTION& aSelection, std::vector<KICAD_T> aTypes )
128{
129 if( aSelection.Empty() )
130 return false;
131
132 for( const EDA_ITEM* item : aSelection )
133 {
134 if( !item->IsType( aTypes ) )
135 return false;
136 }
137
138 return true;
139}
140
141
142bool SELECTION_CONDITIONS::countFunc( const SELECTION& aSelection, int aNumber )
143{
144 return aSelection.Size() == aNumber;
145}
146
147
148bool SELECTION_CONDITIONS::moreThanFunc( const SELECTION& aSelection, int aNumber )
149{
150 return aSelection.Size() > aNumber;
151}
152
153
154bool SELECTION_CONDITIONS::lessThanFunc( const SELECTION& aSelection, int aNumber )
155{
156 return aSelection.Size() < aNumber;
157}
158
159
161 const SELECTION_CONDITION& aConditionB )
162{
163 return std::bind( &SELECTION_CONDITIONS::orFunc, aConditionA, aConditionB, _1 );
164}
165
166
168 const SELECTION_CONDITION& aConditionB )
169{
170 return std::bind( &SELECTION_CONDITIONS::andFunc, aConditionA, aConditionB, _1 );
171}
172
173
175{
176 return std::bind( &SELECTION_CONDITIONS::notFunc, aCondition, _1 );
177}
178
179
181 SELECTION_BOOL aConditionB )
182{
183 return std::bind( &SELECTION_CONDITIONS::orBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
184}
185
187 const SELECTION_CONDITION& aConditionB )
188{
189 return aConditionB || aConditionA;
190}
191
192
194 SELECTION_BOOL aConditionB )
195{
196 return std::bind( &SELECTION_CONDITIONS::andBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
197}
198
200 const SELECTION_CONDITION& aConditionB )
201{
202 return aConditionB && aConditionA;
203}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:147
static bool hasTypesFunc(const SELECTION &aSelection, std::vector< KICAD_T > aTypes)
Helper function used by OnlyTypes()
static SELECTION_CONDITION HasTypes(std::vector< KICAD_T > aTypes)
Create a functor that tests if among the selected items there is at least one of a 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 bool Empty(const SELECTION &aSelection)
Test if there are no items selected.
static bool moreThanFunc(const SELECTION &aSelection, int aNumber)
Helper function used by LessThan()
static bool NotEmpty(const SELECTION &aSelection)
Test if there are any items selected.
static bool countFunc(const SELECTION &aSelection, int aNumber)
Helper function used by MoreThan()
static bool orFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &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 orBoolFunc(const SELECTION_CONDITION &aConditionA, SELECTION_BOOL &aConditionB, const SELECTION &aSelection)
Helper function used by operator&&.
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 bool IdleSelection(const SELECTION &aSelection)
Test if all selected items are not being edited.
static bool andBoolFunc(const SELECTION_CONDITION &aConditionA, SELECTION_BOOL &aConditionB, const SELECTION &aSelection)
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...
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 onlyTypesFunc(const SELECTION &aSelection, std::vector< KICAD_T > aTypes)
Helper function used by Count()
static bool andFunc(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &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 SELECTION_CONDITION OnlyTypes(std::vector< KICAD_T > aTypes)
Create a functor that tests if the selected items are only of given types.
static bool hasTypeFunc(const SELECTION &aSelection, KICAD_T aType)
< Helper function used by HasType()
EDA_ITEM * Front() const
Definition: selection.h:206
int Size() const
Returns the number of selected parts.
Definition: selection.h:113
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:107
#define IS_PASTED
Modifier on IS_NEW which indicates it came from clipboard.
#define IS_NEW
New item, just created.
#define IS_DRAGGING
Item being dragged.
#define IS_MOVING
Item being moved.
#define IS_RESIZING
Item being resized.
SELECTION_CONDITION operator||(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
SELECTION_CONDITION operator&&(const SELECTION_CONDITION &aConditionA, const SELECTION_CONDITION &aConditionB)
SELECTION_CONDITION operator!(const SELECTION_CONDITION &aCondition)
bool(&) SELECTION_BOOL(const SELECTION &)
Signature for a reference to a function that takes a SELECTION and returns a boolean.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78