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.

167 lines
5.3 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. protected:
  92. ///> Scale factor to convert between raw and displayed value
  93. double m_scale;
  94. };
  95. ///> A wxEnumProperty that displays a color next to the enum value
  96. class PGPROPERTY_COLORENUM : public wxEnumProperty
  97. {
  98. public:
  99. PGPROPERTY_COLORENUM( const wxString& aLabel, wxString& aName, const wxPGChoices& aChoices,
  100. int aValue = 0 ) :
  101. wxEnumProperty( aLabel, aName, const_cast<wxPGChoices&>( aChoices ), aValue ),
  102. m_colorFunc( []( const wxString& aChoice ) { return wxNullColour; } )
  103. {
  104. SetFlag( wxPG_PROP_CUSTOMIMAGE );
  105. }
  106. wxSize OnMeasureImage( int aItem = -1 ) const override;
  107. void OnCustomPaint( wxDC& aDC, const wxRect& aRect, wxPGPaintData& aPaintData ) override;
  108. void SetColorFunc( std::function<wxColour( const wxString& aChoice )> aFunc )
  109. {
  110. m_colorFunc = aFunc;
  111. }
  112. wxColour GetColor( const wxString& aChoice )
  113. {
  114. return m_colorFunc( aChoice );
  115. }
  116. protected:
  117. std::function<wxColour( const wxString& aChoice )> m_colorFunc;
  118. };
  119. class PGPROPERTY_STRING : public wxStringProperty
  120. {
  121. public:
  122. PGPROPERTY_STRING( const wxString& aLabel = wxPG_LABEL, const wxString& aName = wxPG_LABEL,
  123. const wxString& aValue = wxEmptyString ) :
  124. wxStringProperty( aLabel, aName, aValue )
  125. {}
  126. virtual ~PGPROPERTY_STRING() = default;
  127. wxString ValueToString( wxVariant& aValue, int aFlags = 0 ) const override;
  128. bool StringToValue( wxVariant& aVariant, const wxString& aString,
  129. int aFlags = 0 ) const override;
  130. };
  131. #endif /* PG_PROPERTIES_H */