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.

232 lines
7.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 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 <wx/grid.h>
  23. const wxColour COLOUR_ROW_ENABLED( 0, 0, 0 );
  24. const wxColour COLOUR_ROW_DISABLED( 100, 100, 100 );
  25. /// The library table grid column order is established by this sequence.
  26. enum COL_ORDER
  27. {
  28. COL_ENABLED,
  29. COL_NICKNAME,
  30. COL_URI,
  31. COL_TYPE,
  32. COL_OPTIONS,
  33. COL_DESCR,
  34. COL_COUNT // keep as last
  35. };
  36. /**
  37. * This abstract base class mixes any object derived from #LIB_TABLE into wxGridTableBase
  38. * so the result can be used as any type of library table within wxGrid.
  39. */
  40. class LIB_TABLE_GRID : public wxGridTableBase
  41. {
  42. public:
  43. //-----<wxGridTableBase overloads>-------------------------------------------
  44. int GetNumberRows() override { return (int) size(); }
  45. int GetNumberCols() override { return COL_COUNT; }
  46. wxString GetValue( int aRow, int aCol ) override
  47. {
  48. if( aRow < (int) size() )
  49. {
  50. const LIB_TABLE_ROW* r = at( (size_t) aRow );
  51. switch( aCol )
  52. {
  53. case COL_NICKNAME: return r->GetNickName();
  54. case COL_URI: return r->GetFullURI();
  55. case COL_TYPE: return r->GetType();
  56. case COL_OPTIONS: return r->GetOptions();
  57. case COL_DESCR: return r->GetDescr();
  58. // Render a boolean value as its text equivalent
  59. case COL_ENABLED: return r->GetIsEnabled() ? wxT( "1" ) : wxT( "0" );
  60. default:
  61. ; // fall thru to wxEmptyString
  62. }
  63. }
  64. return wxEmptyString;
  65. }
  66. bool GetValueAsBool( int aRow, int aCol ) override
  67. {
  68. if( aRow < (int) size() && aCol == COL_ENABLED )
  69. return at( (size_t) aRow )->GetIsEnabled();
  70. else
  71. return false;
  72. }
  73. void SetValue( int aRow, int aCol, const wxString &aValue ) override
  74. {
  75. if( aRow < (int) size() )
  76. {
  77. LIB_TABLE_ROW* r = at( (size_t) aRow );
  78. switch( aCol )
  79. {
  80. case COL_NICKNAME: r->SetNickName( aValue ); break;
  81. case COL_URI: r->SetFullURI( aValue ); break;
  82. case COL_TYPE: r->SetType( aValue ); break;
  83. case COL_OPTIONS: r->SetOptions( aValue ); break;
  84. case COL_DESCR: r->SetDescr( aValue ); break;
  85. case COL_ENABLED:
  86. r->SetEnabled( aValue == wxT( "1" ) );
  87. break;
  88. }
  89. }
  90. }
  91. void SetValueAsBool( int aRow, int aCol, bool aValue ) override
  92. {
  93. if( aRow < (int) size() && aCol == COL_ENABLED )
  94. at( (size_t) aRow )->SetEnabled( aValue );
  95. }
  96. bool IsEmptyCell( int aRow, int aCol ) override
  97. {
  98. return !GetValue( aRow, aCol );
  99. }
  100. bool InsertRows( size_t aPos = 0, size_t aNumRows = 1 ) override
  101. {
  102. if( aPos < size() )
  103. {
  104. for( size_t i = 0; i < aNumRows; i++ )
  105. {
  106. insert( begin() + i, makeNewRow() );
  107. }
  108. // use the (wxGridStringTable) source Luke.
  109. if( GetView() )
  110. {
  111. wxGridTableMessage msg( this,
  112. wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
  113. aPos,
  114. aNumRows );
  115. GetView()->ProcessTableMessage( msg );
  116. }
  117. return true;
  118. }
  119. return false;
  120. }
  121. bool AppendRows( size_t aNumRows = 1 ) override
  122. {
  123. // do not modify aNumRows, original value needed for wxGridTableMessage below
  124. for( int i = aNumRows; i; --i )
  125. push_back( makeNewRow() );
  126. if( GetView() )
  127. {
  128. wxGridTableMessage msg( this,
  129. wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
  130. aNumRows );
  131. GetView()->ProcessTableMessage( msg );
  132. }
  133. return true;
  134. }
  135. bool DeleteRows( size_t aPos, size_t aNumRows ) override
  136. {
  137. // aPos may be a large positive, e.g. size_t(-1), and the sum of
  138. // aPos+aNumRows may wrap here, so both ends of the range are tested.
  139. if( aPos < size() && aPos + aNumRows <= size() )
  140. {
  141. LIB_TABLE_ROWS_ITER start = begin() + aPos;
  142. erase( start, start + aNumRows );
  143. if( GetView() )
  144. {
  145. wxGridTableMessage msg( this,
  146. wxGRIDTABLE_NOTIFY_ROWS_DELETED,
  147. aPos,
  148. aNumRows );
  149. GetView()->ProcessTableMessage( msg );
  150. }
  151. return true;
  152. }
  153. return false;
  154. }
  155. wxString GetColLabelValue( int aCol ) override
  156. {
  157. switch( aCol )
  158. {
  159. case COL_NICKNAME: return _( "Nickname" );
  160. case COL_URI: return _( "Library Path" );
  161. // keep this "Plugin Type" text fairly long so column is sized wide enough
  162. case COL_TYPE: return _( "Plugin Type" );
  163. case COL_OPTIONS: return _( "Options" );
  164. case COL_DESCR: return _( "Description" );
  165. case COL_ENABLED: return _( "Active" );
  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__