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.

184 lines
5.7 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 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 3
  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 along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef PG_PROPERTIES_H
  21. #define PG_PROPERTIES_H
  22. #include <wx/propgrid/propgrid.h>
  23. #include <wx/propgrid/property.h>
  24. #include <wx/propgrid/props.h>
  25. #include <common.h>
  26. #include <origin_transforms.h>
  27. class PROPERTY_BASE;
  28. class REGEX_VALIDATOR;
  29. wxPGProperty* PGPropertyFactory( const PROPERTY_BASE* aProperty );
  30. ///> Customized abstract wxPGProperty class to handle coordinate/size units
  31. class PGPROPERTY_DISTANCE
  32. {
  33. public:
  34. PGPROPERTY_DISTANCE( const wxString& aRegEx,
  35. ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType = ORIGIN_TRANSFORMS::NOT_A_COORD );
  36. virtual ~PGPROPERTY_DISTANCE() = 0;
  37. void SetCoordType( ORIGIN_TRANSFORMS::COORD_TYPES_T aType ) { m_coordType = aType; }
  38. ORIGIN_TRANSFORMS::COORD_TYPES_T CoordType() const { return m_coordType; }
  39. protected:
  40. bool StringToDistance( wxVariant& aVariant, const wxString& aText, int aArgFlags = 0 ) const;
  41. wxString DistanceToString( wxVariant& aVariant, int aArgFlags = 0 ) const;
  42. std::unique_ptr<REGEX_VALIDATOR> m_regExValidator;
  43. ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType;
  44. };
  45. class PGPROPERTY_SIZE : public wxUIntProperty, public PGPROPERTY_DISTANCE
  46. {
  47. public:
  48. PGPROPERTY_SIZE( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL,
  49. long aValue = 0 );
  50. bool StringToValue( wxVariant& aVariant, const wxString& aText, int aArgFlags = 0 ) const override
  51. {
  52. return StringToDistance( aVariant, aText, aArgFlags );
  53. }
  54. wxString ValueToString( wxVariant& aVariant, int aArgFlags = 0 ) const override
  55. {
  56. return DistanceToString( aVariant, aArgFlags );
  57. }
  58. wxValidator* DoGetValidator() const override;
  59. };
  60. class PGPROPERTY_COORD : public wxIntProperty, public PGPROPERTY_DISTANCE
  61. {
  62. public:
  63. PGPROPERTY_COORD( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL,
  64. long aValue = 0,
  65. ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType = ORIGIN_TRANSFORMS::NOT_A_COORD );
  66. bool StringToValue( wxVariant& aVariant, const wxString& aText, int aArgFlags = 0 ) const override
  67. {
  68. return StringToDistance( aVariant, aText, aArgFlags );
  69. }
  70. wxString ValueToString( wxVariant& aVariant, int aArgFlags = 0 ) const override
  71. {
  72. return DistanceToString( aVariant, aArgFlags );
  73. }
  74. wxValidator* DoGetValidator() const override;
  75. };
  76. ///> Customized wxPGProperty class to handle angles
  77. class PGPROPERTY_ANGLE : public wxFloatProperty
  78. {
  79. public:
  80. PGPROPERTY_ANGLE( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL,
  81. double aValue = 0 )
  82. : wxFloatProperty( aLabel, aName, aValue ), m_scale( 1.0 )
  83. {
  84. }
  85. bool StringToValue( wxVariant& aVariant, const wxString& aText, int aArgFlags = 0 ) const override;
  86. wxString ValueToString( wxVariant& aVariant, int aArgFlags = 0 ) const override;
  87. void SetScale( double aScale )
  88. {
  89. m_scale = aScale;
  90. }
  91. wxValidator* DoGetValidator() const override;
  92. ///> Do not perform PG validation; the UX is not what we want.
  93. bool ValidateValue( wxVariant&, wxPGValidationInfo& ) const override { return true; }
  94. protected:
  95. ///> Scale factor to convert between raw and displayed value
  96. double m_scale;
  97. };
  98. ///> A wxEnumProperty that displays a color next to the enum value
  99. class PGPROPERTY_COLORENUM : public wxEnumProperty
  100. {
  101. public:
  102. PGPROPERTY_COLORENUM( const wxString& aLabel, wxString& aName, wxPGChoices* aChoices,
  103. int aValue = 0 ) :
  104. wxEnumProperty( aLabel, aName, *aChoices, aValue ),
  105. m_colorFunc( []( int aDummy ) { return wxNullColour; } )
  106. {
  107. SetFlag( wxPG_PROP_CUSTOMIMAGE );
  108. }
  109. wxSize OnMeasureImage( int aItem = -1 ) const override;
  110. void OnCustomPaint( wxDC& aDC, const wxRect& aRect, wxPGPaintData& aPaintData ) override;
  111. void SetColorFunc( std::function<wxColour( int aValue )> aFunc )
  112. {
  113. m_colorFunc = aFunc;
  114. }
  115. wxColour GetColor( int aValue )
  116. {
  117. return m_colorFunc( aValue );
  118. }
  119. protected:
  120. std::function<wxColour( int aValue )> m_colorFunc;
  121. };
  122. class PGPROPERTY_STRING : public wxStringProperty
  123. {
  124. public:
  125. PGPROPERTY_STRING( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL,
  126. const wxString& aValue = wxEmptyString ) :
  127. wxStringProperty( aLabel, aName, aValue )
  128. {}
  129. virtual ~PGPROPERTY_STRING() = default;
  130. wxString ValueToString( wxVariant& aValue, int aFlags = 0 ) const override;
  131. bool StringToValue( wxVariant& aVariant, const wxString& aString,
  132. int aFlags = 0 ) const override;
  133. };
  134. class PGPROPERTY_BOOL : public wxBoolProperty
  135. {
  136. public:
  137. PGPROPERTY_BOOL( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL,
  138. bool aValue = false );
  139. virtual ~PGPROPERTY_BOOL() = default;
  140. const wxPGEditor* DoGetEditorClass() const override;
  141. };
  142. #endif /* PG_PROPERTIES_H */