2018-05-14 17:34:18 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Wayne Stambaugh <stambaughw@verizon.net>
|
2023-01-23 23:40:58 +00:00
|
|
|
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-05-14 17:34:18 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
#include <pgm_base.h>
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
#include <eeschema_settings.h>
|
2022-12-13 20:47:28 +00:00
|
|
|
#include <widgets/std_bitmap_button.h>
|
2018-05-14 17:34:18 +00:00
|
|
|
#include <template_fieldnames.h>
|
|
|
|
#include <grid_tricks.h>
|
|
|
|
#include <bitmaps.h>
|
2023-09-09 04:10:57 +00:00
|
|
|
#include <string_utils.h>
|
2021-08-29 23:33:08 +00:00
|
|
|
#include <panel_template_fieldnames.h>
|
|
|
|
|
|
|
|
PANEL_TEMPLATE_FIELDNAMES::PANEL_TEMPLATE_FIELDNAMES( wxWindow* aWindow,
|
|
|
|
TEMPLATES* aProjectTemplateMgr ) :
|
|
|
|
PANEL_TEMPLATE_FIELDNAMES_BASE( aWindow )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2021-08-29 23:33:08 +00:00
|
|
|
if( aProjectTemplateMgr )
|
|
|
|
{
|
|
|
|
m_title->SetLabel( _( "Project field name templates:" ) );
|
|
|
|
m_global = false;
|
|
|
|
m_templateMgr = aProjectTemplateMgr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_title->SetLabel( _( "Global field name templates:" ) );
|
|
|
|
m_global = true;
|
|
|
|
m_templateMgr = &m_templateMgrInstance;
|
|
|
|
|
|
|
|
EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
|
|
|
|
|
2023-01-23 23:40:58 +00:00
|
|
|
if( cfg && !cfg->m_Drawing.field_names.IsEmpty() )
|
|
|
|
m_templateMgr->AddTemplateFieldNames( cfg->m_Drawing.field_names );
|
2021-08-29 23:33:08 +00:00
|
|
|
}
|
2020-03-10 18:46:57 +00:00
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
m_addFieldButton->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
|
|
|
|
m_deleteFieldButton->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
m_checkboxColWidth = m_grid->GetColSize( 1 );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2022-09-09 11:18:05 +00:00
|
|
|
m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
OnAddButtonClick( aEvent );
|
|
|
|
} ) );
|
2021-12-12 17:27:40 +00:00
|
|
|
m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
PANEL_TEMPLATE_FIELDNAMES::~PANEL_TEMPLATE_FIELDNAMES()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
|
|
|
// Delete the GRID_TRICKS.
|
|
|
|
m_grid->PopEventHandler( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
bool PANEL_TEMPLATE_FIELDNAMES::TransferDataToWindow()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2022-02-17 12:35:46 +00:00
|
|
|
m_fields = m_templateMgr->GetTemplateFieldNames( m_global );
|
2020-08-30 17:57:10 +00:00
|
|
|
|
2018-05-14 17:34:18 +00:00
|
|
|
return TransferDataToGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void PANEL_TEMPLATE_FIELDNAMES::OnAddButtonClick( wxCommandEvent& event )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return;
|
|
|
|
|
2018-05-14 17:34:18 +00:00
|
|
|
int row = m_grid->GetNumberRows();
|
|
|
|
TransferDataFromGrid();
|
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
|
2018-05-14 17:34:18 +00:00
|
|
|
newFieldname.m_Visible = false;
|
|
|
|
m_fields.insert( m_fields.end(), newFieldname );
|
|
|
|
TransferDataToGrid();
|
|
|
|
|
|
|
|
// wx documentation is wrong, SetGridCursor does not make visible.
|
|
|
|
m_grid->MakeCellVisible( row, 0 );
|
|
|
|
m_grid->SetGridCursor( row, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void PANEL_TEMPLATE_FIELDNAMES::OnDeleteButtonClick( wxCommandEvent& event )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return;
|
|
|
|
|
2021-12-12 17:27:40 +00:00
|
|
|
wxArrayInt selectedRows = m_grid->GetSelectedRows();
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2021-12-12 17:27:40 +00:00
|
|
|
if( selectedRows.empty() && m_grid->GetGridCursorRow() >= 0 )
|
|
|
|
selectedRows.push_back( m_grid->GetGridCursorRow() );
|
|
|
|
|
|
|
|
if( selectedRows.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Reverse sort so deleting a row doesn't change the indexes of the other rows.
|
|
|
|
selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
|
|
|
|
|
|
|
|
for( int row : selectedRows )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2021-12-12 17:27:40 +00:00
|
|
|
m_fields.erase( m_fields.begin() + row );
|
|
|
|
m_grid->DeleteRows( row );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2021-12-12 17:27:40 +00:00
|
|
|
m_grid->MakeCellVisible( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
|
|
|
|
m_grid->SetGridCursor( std::max( 0, row-1 ), m_grid->GetGridCursorCol() );
|
|
|
|
}
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
bool PANEL_TEMPLATE_FIELDNAMES::TransferDataToGrid()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
|
|
|
m_grid->Freeze();
|
|
|
|
|
2021-08-10 11:59:53 +00:00
|
|
|
m_grid->ClearRows();
|
2018-05-14 17:34:18 +00:00
|
|
|
m_grid->AppendRows( m_fields.size() );
|
|
|
|
|
|
|
|
for( int row = 0; row < m_grid->GetNumberRows(); ++row )
|
|
|
|
{
|
|
|
|
m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
|
2022-01-06 17:59:34 +00:00
|
|
|
// columns 1 and 2 show a boolean value (in a check box):
|
2023-01-17 04:14:38 +00:00
|
|
|
m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxS( "1" ) : wxS( "0" ) );
|
|
|
|
m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxS( "1" ) : wxS( "0" ) );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
|
|
|
// Set cell properties
|
|
|
|
m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
|
|
|
|
|
2018-08-08 20:57:53 +00:00
|
|
|
// Render the Visible and URL columns as check boxes
|
2018-05-14 17:34:18 +00:00
|
|
|
m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
|
|
|
|
m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
|
|
|
|
m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
|
2018-08-08 20:57:53 +00:00
|
|
|
|
|
|
|
m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
|
|
|
|
m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
|
|
|
|
m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_grid->Thaw();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
bool PANEL_TEMPLATE_FIELDNAMES::TransferDataFromGrid()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return false;
|
2018-05-14 17:34:18 +00:00
|
|
|
|
|
|
|
for( int row = 0; row < m_grid->GetNumberRows(); ++row )
|
|
|
|
{
|
2020-05-08 18:10:47 +00:00
|
|
|
m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
|
2023-01-17 04:14:38 +00:00
|
|
|
m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == wxS( "1" );
|
|
|
|
m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == wxS( "1" );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
bool PANEL_TEMPLATE_FIELDNAMES::TransferDataFromWindow()
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
|
|
|
if( !TransferDataFromGrid() )
|
|
|
|
return false;
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
m_templateMgr->DeleteAllFieldNameTemplates( m_global );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
|
|
|
for( const TEMPLATE_FIELDNAME& field : m_fields )
|
2023-04-18 12:45:53 +00:00
|
|
|
{
|
|
|
|
if( !field.m_Name.IsEmpty() )
|
|
|
|
m_templateMgr->AddTemplateFieldName( field, m_global );
|
|
|
|
}
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2020-11-21 23:09:57 +00:00
|
|
|
if( m_global )
|
|
|
|
{
|
2021-08-29 23:33:08 +00:00
|
|
|
EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
|
2020-11-21 23:09:57 +00:00
|
|
|
|
|
|
|
if( cfg )
|
|
|
|
{
|
|
|
|
// Save global fieldname templates
|
|
|
|
STRING_FORMATTER sf;
|
2021-08-29 23:33:08 +00:00
|
|
|
m_templateMgr->Format( &sf, 0, true );
|
2020-11-21 23:09:57 +00:00
|
|
|
|
2023-09-09 04:10:57 +00:00
|
|
|
wxString record = From_UTF8( sf.GetString().c_str() );
|
2020-11-21 23:09:57 +00:00
|
|
|
record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
|
|
|
|
record.Replace( wxT(" "), wxT(" "), true ); // double space to single
|
|
|
|
|
|
|
|
cfg->m_Drawing.field_names = record.ToStdString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:34:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void PANEL_TEMPLATE_FIELDNAMES::AdjustGridColumns( int aWidth )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
|
|
|
if( aWidth <= 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Account for scroll bars
|
|
|
|
aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
|
|
|
|
|
2022-11-16 12:35:21 +00:00
|
|
|
m_grid->SetColSize( 0, std::max( 72, aWidth - 2 * m_checkboxColWidth ) );
|
2018-05-30 10:52:19 +00:00
|
|
|
m_grid->SetColSize( 1, m_checkboxColWidth );
|
2018-08-08 20:57:53 +00:00
|
|
|
m_grid->SetColSize( 2, m_checkboxColWidth );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void PANEL_TEMPLATE_FIELDNAMES::OnSizeGrid( wxSizeEvent& event )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
|
|
|
AdjustGridColumns( event.GetSize().GetX() );
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
2020-03-13 09:53:44 +00:00
|
|
|
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
void PANEL_TEMPLATE_FIELDNAMES::ImportSettingsFrom( TEMPLATES* templateMgr )
|
2020-03-13 09:53:44 +00:00
|
|
|
{
|
|
|
|
m_fields = templateMgr->GetTemplateFieldNames( m_global );
|
|
|
|
TransferDataToGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|