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.

229 lines
7.1 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_VISIBLE,
  31. COL_NICKNAME,
  32. COL_URI,
  33. COL_TYPE,
  34. COL_OPTIONS,
  35. COL_DESCR,
  36. COL_COUNT // keep as last
  37. };
  38. /**
  39. * This abstract base class mixes any object derived from #LIB_TABLE into wxGridTableBase
  40. * so the result can be used as any type of library table within wxGrid.
  41. */
  42. class LIB_TABLE_GRID : public wxGridTableBase
  43. {
  44. public:
  45. //-----<wxGridTableBase overloads>-------------------------------------------
  46. int GetNumberRows() override { return (int) size(); }
  47. int GetNumberCols() override { return COL_COUNT; }
  48. wxString GetValue( int aRow, int aCol ) override
  49. {
  50. if( aRow < (int) size() )
  51. {
  52. const LIB_TABLE_ROW* r = at( (size_t) aRow );
  53. switch( aCol )
  54. {
  55. case COL_NICKNAME: return UnescapeString( r->GetNickName() );
  56. case COL_URI: return r->GetFullURI();
  57. case COL_TYPE: return r->GetType();
  58. case COL_OPTIONS: return r->GetOptions();
  59. case COL_DESCR: return r->GetDescr();
  60. case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
  61. case COL_VISIBLE: return r->GetIsVisible() ? wxT( "1" ) : wxT( "0" );
  62. default: return wxEmptyString;
  63. }
  64. }
  65. return wxEmptyString;
  66. }
  67. bool GetValueAsBool( int aRow, int aCol ) override
  68. {
  69. if( aRow < (int) size() && aCol == COL_ENABLED )
  70. return at( (size_t) aRow )->GetIsEnabled();
  71. else if( aRow < (int) size() && aCol == COL_VISIBLE )
  72. return at( (size_t) aRow )->GetIsVisible();
  73. else
  74. return false;
  75. }
  76. void SetValue( int aRow, int aCol, const wxString& aValue ) override
  77. {
  78. if( aRow < (int) size() )
  79. {
  80. LIB_TABLE_ROW* r = at( (size_t) aRow );
  81. switch( aCol )
  82. {
  83. case COL_NICKNAME: r->SetNickName( EscapeString( aValue, CTX_LIBID ) ); break;
  84. case COL_URI: r->SetFullURI( aValue ); break;
  85. case COL_TYPE: r->SetType( aValue ); break;
  86. case COL_OPTIONS: r->SetOptions( aValue ); break;
  87. case COL_DESCR: r->SetDescr( aValue ); break;
  88. case COL_ENABLED: r->SetEnabled( aValue == wxT( "1" ) ); break;
  89. case COL_VISIBLE: r->SetVisible( aValue == wxT( "1" ) ); break;
  90. }
  91. }
  92. }
  93. void SetValueAsBool( int aRow, int aCol, bool aValue ) override
  94. {
  95. if( aRow < (int) size() && aCol == COL_ENABLED )
  96. at( (size_t) aRow )->SetEnabled( aValue );
  97. else if( aRow < (int) size() && aCol == COL_VISIBLE )
  98. at( (size_t) aRow )->SetVisible( aValue );
  99. }
  100. bool IsEmptyCell( int aRow, int aCol ) override
  101. {
  102. return !GetValue( aRow, aCol );
  103. }
  104. bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
  105. {
  106. if( aPos < size() )
  107. {
  108. for( size_t i = 0; i < aNumRows; i++ )
  109. {
  110. insert( begin() + i, makeNewRow() );
  111. }
  112. // use the (wxGridStringTable) source Luke.
  113. if( GetView() )
  114. {
  115. wxGridTableMessage msg( this,
  116. wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
  117. aPos,
  118. aNumRows );
  119. GetView()->ProcessTableMessage( msg );
  120. }
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool AppendRows( size_t aNumRows = 1 ) override
  126. {
  127. // do not modify aNumRows, original value needed for wxGridTableMessage below
  128. for( int i = aNumRows; i; --i )
  129. push_back( makeNewRow() );
  130. if( GetView() )
  131. {
  132. wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
  133. GetView()->ProcessTableMessage( msg );
  134. }
  135. return true;
  136. }
  137. bool DeleteRows( size_t aPos, size_t aNumRows ) override
  138. {
  139. // aPos may be a large positive, e.g. size_t(-1), and the sum of
  140. // aPos+aNumRows may wrap here, so both ends of the range are tested.
  141. if( aPos < size() && aPos + aNumRows <= size() )
  142. {
  143. LIB_TABLE_ROWS_ITER start = begin() + aPos;
  144. erase( start, start + aNumRows );
  145. if( GetView() )
  146. {
  147. wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
  148. GetView()->ProcessTableMessage( msg );
  149. }
  150. return true;
  151. }
  152. return false;
  153. }
  154. wxString GetColLabelValue( int aCol ) override
  155. {
  156. switch( aCol )
  157. {
  158. case COL_NICKNAME: return _( "Nickname" );
  159. case COL_URI: return _( "Library Path" );
  160. // keep this "Library Format" text fairly long so column is sized wide enough
  161. case COL_TYPE: return _( "Library Format" );
  162. case COL_OPTIONS: return _( "Options" );
  163. case COL_DESCR: return _( "Description" );
  164. case COL_ENABLED: return _( "Active" );
  165. case COL_VISIBLE: return _( "Visible" );
  166. default: return wxEmptyString;
  167. }
  168. }
  169. bool ContainsNickname( const wxString& aNickname )
  170. {
  171. for( size_t i = 0; i < size(); ++i )
  172. {
  173. LIB_TABLE_ROW* row = at( i );
  174. if( row->GetNickName() == aNickname )
  175. return true;
  176. }
  177. return false;
  178. }
  179. protected:
  180. virtual LIB_TABLE_ROW* at( size_t aIndex ) = 0;
  181. virtual size_t size() const = 0;
  182. virtual LIB_TABLE_ROW* makeNewRow() = 0;
  183. virtual LIB_TABLE_ROWS_ITER begin() = 0;
  184. virtual LIB_TABLE_ROWS_ITER insert( LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW* aRow ) = 0;
  185. virtual void push_back( LIB_TABLE_ROW* aRow ) = 0;
  186. virtual LIB_TABLE_ROWS_ITER erase( LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast ) = 0;
  187. };
  188. #endif // __LIB_TABLE_GRID_H__