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.9 KiB

3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  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 2
  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
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <pgm_base.h>
  25. #include <settings/settings_manager.h>
  26. #include <eeschema_settings.h>
  27. #include <widgets/std_bitmap_button.h>
  28. #include <template_fieldnames.h>
  29. #include <grid_tricks.h>
  30. #include <bitmaps.h>
  31. #include <macros.h>
  32. #include <panel_template_fieldnames.h>
  33. PANEL_TEMPLATE_FIELDNAMES::PANEL_TEMPLATE_FIELDNAMES( wxWindow* aWindow,
  34. TEMPLATES* aProjectTemplateMgr ) :
  35. PANEL_TEMPLATE_FIELDNAMES_BASE( aWindow )
  36. {
  37. if( aProjectTemplateMgr )
  38. {
  39. m_title->SetLabel( _( "Project field name templates:" ) );
  40. m_global = false;
  41. m_templateMgr = aProjectTemplateMgr;
  42. }
  43. else
  44. {
  45. m_title->SetLabel( _( "Global field name templates:" ) );
  46. m_global = true;
  47. m_templateMgr = &m_templateMgrInstance;
  48. EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  49. if( cfg )
  50. {
  51. // Read global fieldname templates
  52. wxString templateFieldNames = cfg->m_Drawing.field_names;
  53. if( !templateFieldNames.IsEmpty() )
  54. {
  55. TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( templateFieldNames ) );
  56. try
  57. {
  58. m_templateMgr->Parse( &field_lexer, true );
  59. }
  60. catch( const IO_ERROR& )
  61. {
  62. }
  63. }
  64. }
  65. }
  66. m_addFieldButton->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
  67. m_deleteFieldButton->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
  68. m_checkboxColWidth = m_grid->GetColSize( 1 );
  69. m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
  70. {
  71. OnAddButtonClick( aEvent );
  72. } ) );
  73. m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
  74. }
  75. PANEL_TEMPLATE_FIELDNAMES::~PANEL_TEMPLATE_FIELDNAMES()
  76. {
  77. // Delete the GRID_TRICKS.
  78. m_grid->PopEventHandler( true );
  79. }
  80. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataToWindow()
  81. {
  82. m_fields = m_templateMgr->GetTemplateFieldNames( m_global );
  83. return TransferDataToGrid();
  84. }
  85. void PANEL_TEMPLATE_FIELDNAMES::OnAddButtonClick( wxCommandEvent& event )
  86. {
  87. if( !m_grid->CommitPendingChanges() )
  88. return;
  89. int row = m_grid->GetNumberRows();
  90. TransferDataFromGrid();
  91. TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
  92. newFieldname.m_Visible = false;
  93. m_fields.insert( m_fields.end(), newFieldname );
  94. TransferDataToGrid();
  95. // wx documentation is wrong, SetGridCursor does not make visible.
  96. m_grid->MakeCellVisible( row, 0 );
  97. m_grid->SetGridCursor( row, 0 );
  98. }
  99. void PANEL_TEMPLATE_FIELDNAMES::OnDeleteButtonClick( wxCommandEvent& event )
  100. {
  101. if( !m_grid->CommitPendingChanges() )
  102. return;
  103. wxArrayInt selectedRows = m_grid->GetSelectedRows();
  104. if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
  105. selectedRows.push_back( m_grid->GetGridCursorRow() );
  106. if( selectedRows.empty() )
  107. return;
  108. // Reverse sort so deleting a row doesn't change the indexes of the other rows.
  109. selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
  110. for( int row : selectedRows )
  111. {
  112. m_fields.erase( m_fields.begin() + row );
  113. m_grid->DeleteRows( row );
  114. m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
  115. m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
  116. }
  117. }
  118. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataToGrid()
  119. {
  120. m_grid->Freeze();
  121. m_grid->ClearRows();
  122. m_grid->AppendRows( m_fields.size() );
  123. for( int row = 0; row < m_grid->GetNumberRows(); ++row )
  124. {
  125. m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
  126. // columns 1 and 2 show a boolean value (in a check box):
  127. m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
  128. m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
  129. // Set cell properties
  130. m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
  131. // Render the Visible and URL columns as check boxes
  132. m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
  133. m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
  134. m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
  135. m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
  136. m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
  137. m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
  138. }
  139. m_grid->Thaw();
  140. return true;
  141. }
  142. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataFromGrid()
  143. {
  144. if( !m_grid->CommitPendingChanges() )
  145. return false;
  146. for( int row = 0; row < m_grid->GetNumberRows(); ++row )
  147. {
  148. m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
  149. m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
  150. m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
  151. }
  152. return true;
  153. }
  154. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataFromWindow()
  155. {
  156. if( !TransferDataFromGrid() )
  157. return false;
  158. m_templateMgr->DeleteAllFieldNameTemplates( m_global );
  159. for( const TEMPLATE_FIELDNAME& field : m_fields )
  160. m_templateMgr->AddTemplateFieldName( field, m_global );
  161. if( m_global )
  162. {
  163. EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  164. if( cfg )
  165. {
  166. // Save global fieldname templates
  167. STRING_FORMATTER sf;
  168. m_templateMgr->Format( &sf, 0, true );
  169. wxString record = FROM_UTF8( sf.GetString().c_str() );
  170. record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
  171. record.Replace( wxT(" "), wxT(" "), true ); // double space to single
  172. cfg->m_Drawing.field_names = record.ToStdString();
  173. }
  174. }
  175. return true;
  176. }
  177. void PANEL_TEMPLATE_FIELDNAMES::AdjustGridColumns( int aWidth )
  178. {
  179. if( aWidth <= 0 )
  180. return;
  181. // Account for scroll bars
  182. aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
  183. m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
  184. m_grid->SetColSize( 1, m_checkboxColWidth );
  185. m_grid->SetColSize( 2, m_checkboxColWidth );
  186. }
  187. void PANEL_TEMPLATE_FIELDNAMES::OnSizeGrid( wxSizeEvent& event )
  188. {
  189. AdjustGridColumns( event.GetSize().GetX() );
  190. event.Skip();
  191. }
  192. void PANEL_TEMPLATE_FIELDNAMES::ImportSettingsFrom( TEMPLATES* templateMgr )
  193. {
  194. m_fields = templateMgr->GetTemplateFieldNames( m_global );
  195. TransferDataToGrid();
  196. }