You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
8.2 KiB

  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-2022 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. #ifndef SELECTION_CONDITIONS_H_
  27. #define SELECTION_CONDITIONS_H_
  28. #include <functional>
  29. #include <core/typeinfo.h>
  30. #include <vector>
  31. #include <tool/selection.h>
  32. ///< Functor type that checks a specific condition for selected items.
  33. typedef std::function<bool (const SELECTION&)> SELECTION_CONDITION;
  34. SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
  35. const SELECTION_CONDITION& aConditionB );
  36. SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
  37. const SELECTION_CONDITION& aConditionB );
  38. SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition );
  39. /// Signature for a reference to a function that takes a SELECTION and returns
  40. /// a boolean. This type is meant to be used to define logical operations between
  41. /// SELECTION_CONDITION functors and non-functor SELECTION_CONDITION-like functions.
  42. /// It should not be used in user code.
  43. typedef bool ( &SELECTION_BOOL )( const SELECTION& );
  44. SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
  45. SELECTION_BOOL aConditionB );
  46. SELECTION_CONDITION operator||( SELECTION_BOOL aConditionA,
  47. const SELECTION_CONDITION& aConditionB );
  48. SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
  49. SELECTION_BOOL aConditionB );
  50. SELECTION_CONDITION operator&&( SELECTION_BOOL aConditionA,
  51. const SELECTION_CONDITION& aConditionB );
  52. /**
  53. * Class that groups generic conditions for selected items.
  54. */
  55. class SELECTION_CONDITIONS
  56. {
  57. public:
  58. /**
  59. * The default condition function (always returns true).
  60. */
  61. static bool ShowAlways( const SELECTION& aSelection ) { return true; }
  62. /**
  63. * Always returns false.
  64. */
  65. static bool ShowNever( const SELECTION& aSelection ) { return false; }
  66. /**
  67. * Test if there are any items selected.
  68. */
  69. static bool NotEmpty( const SELECTION& aSelection );
  70. /**
  71. * Test if there are no items selected.
  72. */
  73. static bool Empty( const SELECTION& aSelection );
  74. /**
  75. * Test if there no items selected or being edited.
  76. */
  77. static bool Idle( const SELECTION& aSelection );
  78. /**
  79. * Test if all selected items are not being edited.
  80. */
  81. static bool IdleSelection( const SELECTION& aSelection );
  82. /**
  83. * Create a functor that tests if among the selected items there is at least one of a
  84. * given type.
  85. *
  86. * @param aType is the type that is searched.
  87. * @return Functor testing for presence of items of a given type.
  88. */
  89. static SELECTION_CONDITION HasType( KICAD_T aType );
  90. /**
  91. * Create a functor that tests if among the selected items there is at least one of a
  92. * given types.
  93. *
  94. * @param aTypes is an array containing types that are searched.
  95. * @return Functor testing for presence of items of a given types.
  96. */
  97. static SELECTION_CONDITION HasTypes( std::vector<KICAD_T> aTypes );
  98. /**
  99. * Create a functor that tests if the selected items are *only* of given types.
  100. *
  101. * @param aTypes is an array containing types that are searched.
  102. * @return Functor testing if selected items are exclusively of the requested types.
  103. */
  104. static SELECTION_CONDITION OnlyTypes( std::vector<KICAD_T> aTypes );
  105. /**
  106. * Create a functor that tests if the number of selected items is equal to the value given as
  107. * parameter.
  108. *
  109. * @param aNumber is the number of expected items.
  110. * @return Functor testing if the number of selected items is equal aNumber.
  111. */
  112. static SELECTION_CONDITION Count( int aNumber );
  113. /**
  114. * Create a functor that tests if the number of selected items is greater than the value given
  115. * as parameter.
  116. *
  117. * @param aNumber is the number used for comparison.
  118. * @return Functor testing if the number of selected items is greater than aNumber.
  119. */
  120. static SELECTION_CONDITION MoreThan( int aNumber );
  121. /**
  122. * Create a functor that tests if the number of selected items is smaller than the value given
  123. * as parameter.
  124. *
  125. * @param aNumber is the number used for comparison.
  126. * @return Functor testing if the number of selected items is smaller than aNumber.
  127. */
  128. static SELECTION_CONDITION LessThan( int aNumber );
  129. private:
  130. ///< Helper function used by HasType()
  131. static bool hasTypeFunc( const SELECTION& aSelection, KICAD_T aType );
  132. ///< Helper function used by HasTypes()
  133. static bool hasTypesFunc( const SELECTION& aSelection, std::vector<KICAD_T> aTypes );
  134. ///< Helper function used by OnlyTypes()
  135. static bool onlyTypesFunc( const SELECTION& aSelection, std::vector<KICAD_T> aTypes );
  136. ///< Helper function used by Count()
  137. static bool countFunc( const SELECTION& aSelection, int aNumber );
  138. ///< Helper function used by MoreThan()
  139. static bool moreThanFunc( const SELECTION& aSelection, int aNumber );
  140. ///< Helper function used by LessThan()
  141. static bool lessThanFunc( const SELECTION& aSelection, int aNumber );
  142. ///< Helper function used by operator||
  143. static bool orFunc( const SELECTION_CONDITION& aConditionA,
  144. const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
  145. {
  146. return aConditionA( aSelection ) || aConditionB( aSelection );
  147. }
  148. ///< Helper function used by operator&&
  149. static bool andFunc( const SELECTION_CONDITION& aConditionA,
  150. const SELECTION_CONDITION& aConditionB, const SELECTION& aSelection )
  151. {
  152. return aConditionA( aSelection ) && aConditionB( aSelection );
  153. }
  154. ///< Helper function used by operator!
  155. static bool notFunc( const SELECTION_CONDITION& aCondition, const SELECTION& aSelection )
  156. {
  157. return !aCondition( aSelection );
  158. }
  159. ///< Helper function used by operator||
  160. static bool orBoolFunc( const SELECTION_CONDITION& aConditionA,
  161. SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
  162. {
  163. return aConditionA( aSelection ) || aConditionB( aSelection );
  164. }
  165. ///< Helper function used by operator&&
  166. static bool andBoolFunc( const SELECTION_CONDITION& aConditionA,
  167. SELECTION_BOOL& aConditionB, const SELECTION& aSelection )
  168. {
  169. return aConditionA( aSelection ) && aConditionB( aSelection );
  170. }
  171. friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
  172. const SELECTION_CONDITION& aConditionB );
  173. friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
  174. const SELECTION_CONDITION& aConditionB );
  175. friend SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition );
  176. friend SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
  177. SELECTION_BOOL aConditionB );
  178. friend SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
  179. SELECTION_BOOL aConditionB );
  180. };
  181. #endif /* SELECTION_CONDITIONS_H_ */