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.

256 lines
7.7 KiB

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