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.

221 lines
6.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __LIB_TABLE_GRID_H__
  20. #define __LIB_TABLE_GRID_H__
  21. #include <lib_table_base.h>
  22. #include <string_utils.h>
  23. #include <wx/grid.h>
  24. const wxColour COLOUR_ROW_ENABLED( 0, 0, 0 );
  25. const wxColour COLOUR_ROW_DISABLED( 100, 100, 100 );
  26. /// The library table grid column order is established by this sequence.
  27. enum COL_ORDER
  28. {
  29. COL_ENABLED,
  30. COL_NICKNAME,
  31. COL_URI,
  32. COL_TYPE,
  33. COL_OPTIONS,
  34. COL_DESCR,
  35. COL_COUNT // keep as last
  36. };
  37. /**
  38. * This abstract base class mixes any object derived from #LIB_TABLE into wxGridTableBase
  39. * so the result can be used as any type of library table within wxGrid.
  40. */
  41. class LIB_TABLE_GRID : public wxGridTableBase
  42. {
  43. public:
  44. //-----<wxGridTableBase overloads>-------------------------------------------
  45. int GetNumberRows() override { return (int) size(); }
  46. int GetNumberCols() override { return COL_COUNT; }
  47. wxString GetValue( int aRow, int aCol ) override
  48. {
  49. if( aRow < (int) size() )
  50. {
  51. const LIB_TABLE_ROW* r = at( (size_t) aRow );
  52. switch( aCol )
  53. {
  54. case COL_NICKNAME: return UnescapeString( r->GetNickName() );
  55. case COL_URI: return r->GetFullURI();
  56. case COL_TYPE: return r->GetType();
  57. case COL_OPTIONS: return r->GetOptions();
  58. case COL_DESCR: return r->GetDescr();
  59. case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
  60. default: return wxEmptyString;
  61. }
  62. }
  63. return wxEmptyString;
  64. }
  65. bool GetValueAsBool( int aRow, int aCol ) override
  66. {
  67. if( aRow < (int) size() && aCol == COL_ENABLED )
  68. return at( (size_t) aRow )->GetIsEnabled();
  69. else
  70. return false;
  71. }
  72. void SetValue( int aRow, int aCol, const wxString& aValue ) override
  73. {
  74. if( aRow < (int) size() )
  75. {
  76. LIB_TABLE_ROW* r = at( (size_t) aRow );
  77. switch( aCol )
  78. {
  79. case COL_NICKNAME: r->SetNickName( EscapeString( aValue, CTX_LIBID ) ); break;
  80. case COL_URI: r->SetFullURI( aValue ); break;
  81. case COL_TYPE: r->SetType( aValue ); break;
  82. case COL_OPTIONS: r->SetOptions( aValue ); break;
  83. case COL_DESCR: r->SetDescr( aValue ); break;
  84. case COL_ENABLED: r->SetEnabled( aValue == wxT( "1" ) ); break;
  85. }
  86. }
  87. }
  88. void SetValueAsBool( int aRow, int aCol, bool aValue ) override
  89. {
  90. if( aRow < (int) size() && aCol == COL_ENABLED )
  91. at( (size_t) aRow )->SetEnabled( aValue );
  92. }
  93. bool IsEmptyCell( int aRow, int aCol ) override
  94. {
  95. return !GetValue( aRow, aCol );
  96. }
  97. bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
  98. {
  99. if( aPos < size() )
  100. {
  101. for( size_t i = 0; i < aNumRows; i++ )
  102. {
  103. insert( begin() + i, makeNewRow() );
  104. }
  105. // use the (wxGridStringTable) source Luke.
  106. if( GetView() )
  107. {
  108. wxGridTableMessage msg( this,
  109. wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
  110. aPos,
  111. aNumRows );
  112. GetView()->ProcessTableMessage( msg );
  113. }
  114. return true;
  115. }
  116. return false;
  117. }
  118. bool AppendRows( size_t aNumRows = 1 ) override
  119. {
  120. // do not modify aNumRows, original value needed for wxGridTableMessage below
  121. for( int i = aNumRows; i; --i )
  122. push_back( makeNewRow() );
  123. if( GetView() )
  124. {
  125. wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
  126. GetView()->ProcessTableMessage( msg );
  127. }
  128. return true;
  129. }
  130. bool DeleteRows( size_t aPos, size_t aNumRows ) override
  131. {
  132. // aPos may be a large positive, e.g. size_t(-1), and the sum of
  133. // aPos+aNumRows may wrap here, so both ends of the range are tested.
  134. if( aPos < size() && aPos + aNumRows <= size() )
  135. {
  136. LIB_TABLE_ROWS_ITER start = begin() + aPos;
  137. erase( start, start + aNumRows );
  138. if( GetView() )
  139. {
  140. wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
  141. GetView()->ProcessTableMessage( msg );
  142. }
  143. return true;
  144. }
  145. return false;
  146. }
  147. wxString GetColLabelValue( int aCol ) override
  148. {
  149. switch( aCol )
  150. {
  151. case COL_NICKNAME: return _( "Nickname" );
  152. case COL_URI: return _( "Library Path" );
  153. // keep this "Library Format" text fairly long so column is sized wide enough
  154. case COL_TYPE: return _( "Library Format" );
  155. case COL_OPTIONS: return _( "Options" );
  156. case COL_DESCR: return _( "Description" );
  157. case COL_ENABLED: return _( "Active" );
  158. default: return wxEmptyString;
  159. }
  160. }
  161. bool ContainsNickname( const wxString& aNickname )
  162. {
  163. for( size_t i = 0; i < size(); ++i )
  164. {
  165. LIB_TABLE_ROW* row = at( i );
  166. if( row->GetNickName() == aNickname )
  167. return true;
  168. }
  169. return false;
  170. }
  171. protected:
  172. virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
  173. virtual size_t size() const = 0;
  174. virtual LIB_TABLE_ROW* makeNewRow() = 0;
  175. virtual LIB_TABLE_ROWS_ITER begin() = 0;
  176. virtual LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) = 0;
  177. virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
  178. virtual LIB_TABLE_ROWS_ITER erase( LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast ) = 0;
  179. };
  180. #endif // __LIB_TABLE_GRID_H__