2014-07-09 13:10:32 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 CERN
|
2020-12-27 00:41:04 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SELECTION_CONDITIONS_H_
|
|
|
|
#define SELECTION_CONDITIONS_H_
|
|
|
|
|
2016-06-29 10:23:11 +00:00
|
|
|
#include <functional>
|
2014-07-09 13:10:32 +00:00
|
|
|
#include <core/typeinfo.h>
|
2015-07-09 11:35:49 +00:00
|
|
|
#include <vector>
|
2017-02-28 03:04:44 +00:00
|
|
|
#include <tool/selection.h>
|
2014-07-09 13:10:32 +00:00
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Functor type that checks a specific condition for selected items.
|
2016-06-29 10:23:11 +00:00
|
|
|
typedef std::function<bool (const SELECTION&)> SELECTION_CONDITION;
|
2014-07-09 13:10:32 +00:00
|
|
|
|
|
|
|
SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB );
|
|
|
|
|
|
|
|
SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB );
|
|
|
|
|
2019-04-29 20:38:05 +00:00
|
|
|
SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition );
|
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
|
2020-07-29 23:50:25 +00:00
|
|
|
/// Signature for a reference to a function that takes a SELECTION and returns
|
|
|
|
/// a boolean. This type is meant to be used to define logical operations between
|
|
|
|
/// SELECTION_CONDITION functors and non-functor SELECTION_CONDITION-like functions.
|
|
|
|
/// It should not be used in user code.
|
|
|
|
typedef bool ( &SELECTION_BOOL )( const SELECTION& );
|
|
|
|
|
|
|
|
SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
|
|
|
|
SELECTION_BOOL aConditionB );
|
|
|
|
|
2020-08-13 23:09:17 +00:00
|
|
|
SELECTION_CONDITION operator||( SELECTION_BOOL aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB );
|
|
|
|
|
2020-07-29 23:50:25 +00:00
|
|
|
SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
|
|
|
|
SELECTION_BOOL aConditionB );
|
|
|
|
|
2020-08-13 23:09:17 +00:00
|
|
|
SELECTION_CONDITION operator&&( SELECTION_BOOL aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB );
|
2020-07-29 23:50:25 +00:00
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
/**
|
|
|
|
* Class that groups generic conditions for selected items.
|
|
|
|
*/
|
|
|
|
class SELECTION_CONDITIONS
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The default condition function (always returns true).
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aSelection is the selection to be tested.
|
2020-07-27 22:42:23 +00:00
|
|
|
* @return Always true.
|
2014-07-09 13:10:32 +00:00
|
|
|
*/
|
|
|
|
static bool ShowAlways( const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-27 22:42:23 +00:00
|
|
|
* Always returns false.
|
|
|
|
*
|
|
|
|
* @param aSelection is the selection to be tested.
|
|
|
|
* @return Always false.
|
|
|
|
*/
|
|
|
|
static bool ShowNever( const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Test if there are any items selected.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aSelection is the selection to be tested.
|
|
|
|
* @return True if there is at least one item selected.
|
|
|
|
*/
|
|
|
|
static bool NotEmpty( const SELECTION& aSelection );
|
|
|
|
|
2020-07-29 23:50:25 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Test if there are no items selected.
|
2020-07-29 23:50:25 +00:00
|
|
|
*
|
|
|
|
* @param aSelection is the selection to be tested.
|
|
|
|
* @return True if there are no items selected.
|
|
|
|
*/
|
|
|
|
static bool Empty( const SELECTION& aSelection );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Test if there no items selected or being edited.
|
2020-07-29 23:50:25 +00:00
|
|
|
*
|
|
|
|
* @param aSelection is the selection to be tested.
|
|
|
|
* @return True if there are no items being edited or no items selected.
|
|
|
|
*/
|
|
|
|
static bool Idle( const SELECTION& aSelection );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Test if all selected items are not being edited.
|
2020-07-29 23:50:25 +00:00
|
|
|
*
|
|
|
|
* @param aSelection is the selection to be tested.
|
|
|
|
* @return True if no selected items are being edited.
|
|
|
|
*/
|
|
|
|
static bool IdleSelection( const SELECTION& aSelection );
|
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a functor that tests if among the selected items there is at least one of a
|
|
|
|
* given type.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aType is the type that is searched.
|
|
|
|
* @return Functor testing for presence of items of a given type.
|
|
|
|
*/
|
|
|
|
static SELECTION_CONDITION HasType( KICAD_T aType );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a functor that tests if the selected items are *only* of given type.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aType is the type that is searched.
|
2015-07-09 11:35:49 +00:00
|
|
|
* @return Functor testing if selected items are exclusively of one type.
|
2014-07-09 13:10:32 +00:00
|
|
|
*/
|
|
|
|
static SELECTION_CONDITION OnlyType( KICAD_T aType );
|
|
|
|
|
2016-05-04 16:35:20 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a functor that tests if the selected items are *only* of given types.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2017-06-22 07:01:29 +00:00
|
|
|
* @param aTypes is an array containing types that are searched. It has to be ended with
|
2020-12-27 00:41:04 +00:00
|
|
|
* #KICAD_T::EOT as end marker.
|
2016-05-04 16:35:20 +00:00
|
|
|
* @return Functor testing if selected items are exclusively of the requested types.
|
|
|
|
*/
|
|
|
|
static SELECTION_CONDITION OnlyTypes( const KICAD_T aTypes[] );
|
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a functor that tests if the number of selected items is equal to the value given as
|
2014-07-09 13:10:32 +00:00
|
|
|
* parameter.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aNumber is the number of expected items.
|
|
|
|
* @return Functor testing if the number of selected items is equal aNumber.
|
|
|
|
*/
|
|
|
|
static SELECTION_CONDITION Count( int aNumber );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a functor that tests if the number of selected items is greater than the value given
|
2014-07-09 13:10:32 +00:00
|
|
|
* as parameter.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aNumber is the number used for comparison.
|
|
|
|
* @return Functor testing if the number of selected items is greater than aNumber.
|
|
|
|
*/
|
|
|
|
static SELECTION_CONDITION MoreThan( int aNumber );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a functor that tests if the number of selected items is smaller than the value given
|
2014-07-09 13:10:32 +00:00
|
|
|
* as parameter.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 13:10:32 +00:00
|
|
|
* @param aNumber is the number used for comparison.
|
|
|
|
* @return Functor testing if the number of selected items is smaller than aNumber.
|
|
|
|
*/
|
|
|
|
static SELECTION_CONDITION LessThan( int aNumber );
|
|
|
|
|
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by HasType()
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool hasTypeFunc( const SELECTION& aSelection, KICAD_T aType );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by OnlyType()
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool onlyTypeFunc( const SELECTION& aSelection, KICAD_T aType );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by OnlyTypes()
|
2019-04-30 13:54:32 +00:00
|
|
|
static bool onlyTypesFunc( const SELECTION& aSelection, const KICAD_T aTypes[] );
|
2015-07-09 11:35:49 +00:00
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by Count()
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool countFunc( const SELECTION& aSelection, int aNumber );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by MoreThan()
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool moreThanFunc( const SELECTION& aSelection, int aNumber );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by LessThan()
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool lessThanFunc( const SELECTION& aSelection, int aNumber );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by operator||
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool orFunc( const SELECTION_CONDITION& aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return aConditionA( aSelection ) || aConditionB( aSelection );
|
|
|
|
}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by operator&&
|
2014-07-09 13:10:32 +00:00
|
|
|
static bool andFunc( const SELECTION_CONDITION& aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return aConditionA( aSelection ) && aConditionB( aSelection );
|
|
|
|
}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by operator!
|
2019-04-29 20:38:05 +00:00
|
|
|
static bool notFunc( const SELECTION_CONDITION& aCondition, const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return !aCondition( aSelection );
|
|
|
|
}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by operator||
|
2020-07-29 23:50:25 +00:00
|
|
|
static bool orBoolFunc( const SELECTION_CONDITION& aConditionA,
|
|
|
|
SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return aConditionA( aSelection ) || aConditionB( aSelection );
|
|
|
|
}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Helper function used by operator&&
|
2020-07-29 23:50:25 +00:00
|
|
|
static bool andBoolFunc( const SELECTION_CONDITION& aConditionA,
|
|
|
|
SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
|
|
|
|
{
|
|
|
|
return aConditionA( aSelection ) && aConditionB( aSelection );
|
|
|
|
}
|
|
|
|
|
2014-07-09 13:10:32 +00:00
|
|
|
friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB );
|
|
|
|
|
|
|
|
friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
|
|
|
|
const SELECTION_CONDITION& aConditionB );
|
2019-04-29 20:38:05 +00:00
|
|
|
|
|
|
|
friend SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition );
|
2020-07-29 23:50:25 +00:00
|
|
|
|
|
|
|
friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
|
|
|
|
SELECTION_BOOL aConditionB );
|
|
|
|
|
|
|
|
friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
|
|
|
|
SELECTION_BOOL aConditionB );
|
2014-07-09 13:10:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SELECTION_CONDITIONS_H_ */
|