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.

243 lines
7.6 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-2023 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 <string_utils.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 && !cfg->m_Drawing.field_names.IsEmpty() )
  50. m_templateMgr->AddTemplateFieldNames( cfg->m_Drawing.field_names );
  51. }
  52. m_addFieldButton->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
  53. m_deleteFieldButton->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
  54. m_checkboxColWidth = m_grid->GetColSize( 1 );
  55. m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
  56. {
  57. OnAddButtonClick( aEvent );
  58. } ) );
  59. m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
  60. }
  61. PANEL_TEMPLATE_FIELDNAMES::~PANEL_TEMPLATE_FIELDNAMES()
  62. {
  63. // Delete the GRID_TRICKS.
  64. m_grid->PopEventHandler( true );
  65. }
  66. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataToWindow()
  67. {
  68. m_fields = m_templateMgr->GetTemplateFieldNames( m_global );
  69. return TransferDataToGrid();
  70. }
  71. void PANEL_TEMPLATE_FIELDNAMES::OnAddButtonClick( wxCommandEvent& event )
  72. {
  73. if( !m_grid->CommitPendingChanges() )
  74. return;
  75. int row = m_grid->GetNumberRows();
  76. TransferDataFromGrid();
  77. TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
  78. newFieldname.m_Visible = false;
  79. m_fields.insert( m_fields.end(), newFieldname );
  80. TransferDataToGrid();
  81. // wx documentation is wrong, SetGridCursor does not make visible.
  82. m_grid->MakeCellVisible( row, 0 );
  83. m_grid->SetGridCursor( row, 0 );
  84. }
  85. void PANEL_TEMPLATE_FIELDNAMES::OnDeleteButtonClick( wxCommandEvent& event )
  86. {
  87. if( !m_grid->CommitPendingChanges() )
  88. return;
  89. wxArrayInt selectedRows = m_grid->GetSelectedRows();
  90. if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
  91. selectedRows.push_back( m_grid->GetGridCursorRow() );
  92. if( selectedRows.empty() )
  93. return;
  94. // Reverse sort so deleting a row doesn't change the indexes of the other rows.
  95. selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
  96. for( int row : selectedRows )
  97. {
  98. m_fields.erase( m_fields.begin() + row );
  99. m_grid->DeleteRows( row );
  100. m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
  101. m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
  102. }
  103. }
  104. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataToGrid()
  105. {
  106. m_grid->Freeze();
  107. m_grid->ClearRows();
  108. m_grid->AppendRows( m_fields.size() );
  109. for( int row = 0; row < m_grid->GetNumberRows(); ++row )
  110. {
  111. m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
  112. // columns 1 and 2 show a boolean value (in a check box):
  113. m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
  114. m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
  115. // Set cell properties
  116. m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
  117. // Render the Visible and URL columns as check boxes
  118. m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
  119. m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
  120. m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
  121. m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
  122. m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
  123. m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
  124. }
  125. m_grid->Thaw();
  126. return true;
  127. }
  128. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataFromGrid()
  129. {
  130. if( !m_grid->CommitPendingChanges() )
  131. return false;
  132. for( int row = 0; row < m_grid->GetNumberRows(); ++row )
  133. {
  134. m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
  135. m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
  136. m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
  137. }
  138. return true;
  139. }
  140. bool PANEL_TEMPLATE_FIELDNAMES::TransferDataFromWindow()
  141. {
  142. if( !TransferDataFromGrid() )
  143. return false;
  144. m_templateMgr->DeleteAllFieldNameTemplates( m_global );
  145. for( const TEMPLATE_FIELDNAME& field : m_fields )
  146. {
  147. if( !field.m_Name.IsEmpty() )
  148. m_templateMgr->AddTemplateFieldName( field, m_global );
  149. }
  150. if( m_global )
  151. {
  152. EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  153. if( cfg )
  154. {
  155. // Save global fieldname templates
  156. STRING_FORMATTER sf;
  157. m_templateMgr->Format( &sf, 0, true );
  158. wxString record = From_UTF8( sf.GetString().c_str() );
  159. record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
  160. record.Replace( wxT(" "), wxT(" "), true ); // double space to single
  161. cfg->m_Drawing.field_names = record.ToStdString();
  162. }
  163. }
  164. return true;
  165. }
  166. void PANEL_TEMPLATE_FIELDNAMES::AdjustGridColumns( int aWidth )
  167. {
  168. if( aWidth <= 0 )
  169. return;
  170. // Account for scroll bars
  171. aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
  172. m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
  173. m_grid->SetColSize( 1, m_checkboxColWidth );
  174. m_grid->SetColSize( 2, m_checkboxColWidth );
  175. }
  176. void PANEL_TEMPLATE_FIELDNAMES::OnSizeGrid( wxSizeEvent& event )
  177. {
  178. AdjustGridColumns( event.GetSize().GetX() );
  179. event.Skip();
  180. }
  181. void PANEL_TEMPLATE_FIELDNAMES::ImportSettingsFrom( TEMPLATES* templateMgr )
  182. {
  183. m_fields = templateMgr->GetTemplateFieldNames( m_global );
  184. TransferDataToGrid();
  185. }