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.

206 lines
5.3 KiB

  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. #include <tool/selection.h>
  25. #include <tool/selection_conditions.h>
  26. #include <functional>
  27. #include <eda_item.h>
  28. using namespace std::placeholders;
  29. bool SELECTION_CONDITIONS::NotEmpty( const SELECTION& aSelection )
  30. {
  31. return !aSelection.Empty();
  32. }
  33. bool SELECTION_CONDITIONS::Empty( const SELECTION& aSelection )
  34. {
  35. return aSelection.Empty();
  36. }
  37. bool SELECTION_CONDITIONS::Idle( const SELECTION& aSelection )
  38. {
  39. constexpr int busyMask = ( IS_NEW | IS_PASTED | IS_MOVING );
  40. return !aSelection.Front() || ( aSelection.Front()->GetEditFlags() & busyMask ) == 0;
  41. }
  42. bool SELECTION_CONDITIONS::IdleSelection( const SELECTION& aSelection )
  43. {
  44. return ( aSelection.Front() && aSelection.Front()->GetEditFlags() == 0 );
  45. }
  46. SELECTION_CONDITION SELECTION_CONDITIONS::HasType( KICAD_T aType )
  47. {
  48. return std::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType );
  49. }
  50. SELECTION_CONDITION SELECTION_CONDITIONS::HasTypes( std::vector<KICAD_T> aTypes )
  51. {
  52. return std::bind( &SELECTION_CONDITIONS::hasTypesFunc, _1, aTypes );
  53. }
  54. SELECTION_CONDITION SELECTION_CONDITIONS::OnlyTypes( std::vector<KICAD_T> aTypes )
  55. {
  56. return std::bind( &SELECTION_CONDITIONS::onlyTypesFunc, _1, aTypes );
  57. }
  58. SELECTION_CONDITION SELECTION_CONDITIONS::Count( int aNumber )
  59. {
  60. return std::bind( &SELECTION_CONDITIONS::countFunc, _1, aNumber );
  61. }
  62. SELECTION_CONDITION SELECTION_CONDITIONS::MoreThan( int aNumber )
  63. {
  64. return std::bind( &SELECTION_CONDITIONS::moreThanFunc, _1, aNumber );
  65. }
  66. SELECTION_CONDITION SELECTION_CONDITIONS::LessThan( int aNumber )
  67. {
  68. return std::bind( &SELECTION_CONDITIONS::lessThanFunc, _1, aNumber );
  69. }
  70. bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType )
  71. {
  72. if( aSelection.Empty() )
  73. return false;
  74. for( const EDA_ITEM* item : aSelection )
  75. {
  76. if( item->Type() == aType )
  77. return true;
  78. }
  79. return false;
  80. }
  81. bool SELECTION_CONDITIONS::hasTypesFunc( const SELECTION& aSelection, std::vector<KICAD_T> aTypes )
  82. {
  83. if( aSelection.Empty() )
  84. return false;
  85. for( const EDA_ITEM* item : aSelection )
  86. {
  87. if( item->IsType( aTypes ) )
  88. return true;
  89. }
  90. return false;
  91. }
  92. bool SELECTION_CONDITIONS::onlyTypesFunc( const SELECTION& aSelection, std::vector<KICAD_T> aTypes )
  93. {
  94. if( aSelection.Empty() )
  95. return false;
  96. for( const EDA_ITEM* item : aSelection )
  97. {
  98. if( !item->IsType( aTypes ) )
  99. return false;
  100. }
  101. return true;
  102. }
  103. bool SELECTION_CONDITIONS::countFunc( const SELECTION& aSelection, int aNumber )
  104. {
  105. return aSelection.Size() == aNumber;
  106. }
  107. bool SELECTION_CONDITIONS::moreThanFunc( const SELECTION& aSelection, int aNumber )
  108. {
  109. return aSelection.Size() > aNumber;
  110. }
  111. bool SELECTION_CONDITIONS::lessThanFunc( const SELECTION& aSelection, int aNumber )
  112. {
  113. return aSelection.Size() < aNumber;
  114. }
  115. SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
  116. const SELECTION_CONDITION& aConditionB )
  117. {
  118. return std::bind( &SELECTION_CONDITIONS::orFunc, aConditionA, aConditionB, _1 );
  119. }
  120. SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
  121. const SELECTION_CONDITION& aConditionB )
  122. {
  123. return std::bind( &SELECTION_CONDITIONS::andFunc, aConditionA, aConditionB, _1 );
  124. }
  125. SELECTION_CONDITION operator!( const SELECTION_CONDITION& aCondition )
  126. {
  127. return std::bind( &SELECTION_CONDITIONS::notFunc, aCondition, _1 );
  128. }
  129. SELECTION_CONDITION operator||( const SELECTION_CONDITION& aConditionA,
  130. SELECTION_BOOL aConditionB )
  131. {
  132. return std::bind( &SELECTION_CONDITIONS::orBoolFunc, aConditionA, std::ref( aConditionB ), _1 );
  133. }
  134. SELECTION_CONDITION operator||( SELECTION_BOOL aConditionA,
  135. const SELECTION_CONDITION& aConditionB )
  136. {
  137. return aConditionB || aConditionA;
  138. }
  139. SELECTION_CONDITION operator&&( const SELECTION_CONDITION& aConditionA,
  140. SELECTION_BOOL aConditionB )
  141. {
  142. return std::bind( &SELECTION_CONDITIONS::andBoolFunc, aConditionA,
  143. std::ref( aConditionB ), _1 );
  144. }
  145. SELECTION_CONDITION operator&&( SELECTION_BOOL aConditionA,
  146. const SELECTION_CONDITION& aConditionB )
  147. {
  148. return aConditionB && aConditionA;
  149. }