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>
|
2022-01-06 17:59:34 +00:00
|
|
|
* Copyright (C) 1992-2022 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
|
|
|
|
*/
|
|
|
|
|
2018-05-30 10:52:19 +00:00
|
|
|
#include <widgets/wx_grid.h>
|
2018-05-14 17:34:18 +00:00
|
|
|
#include <template_fieldnames.h>
|
|
|
|
#include <grid_tricks.h>
|
|
|
|
#include <sch_edit_frame.h>
|
|
|
|
#include <bitmaps.h>
|
2020-08-30 17:57:10 +00:00
|
|
|
#include <schematic.h>
|
2018-05-14 17:34:18 +00:00
|
|
|
#include <panel_eeschema_template_fieldnames.h>
|
2021-09-14 22:45:14 +00:00
|
|
|
#include <kiface_base.h>
|
2018-05-14 17:34:18 +00:00
|
|
|
|
|
|
|
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( SCH_EDIT_FRAME* aFrame,
|
2020-03-10 18:46:57 +00:00
|
|
|
wxWindow* aWindow,
|
|
|
|
bool aGlobal ) :
|
2018-05-14 17:34:18 +00:00
|
|
|
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES_BASE( aWindow ),
|
2020-03-10 18:46:57 +00:00
|
|
|
m_frame( aFrame ),
|
|
|
|
m_global( aGlobal )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
2020-04-11 06:54:34 +00:00
|
|
|
m_title->SetLabel( aGlobal ? _( "Global field name templates:" )
|
|
|
|
: _( "Project field name templates:" ) );
|
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
|
|
|
|
|
|
|
m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
|
2021-12-12 17:27:40 +00:00
|
|
|
m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::~PANEL_EESCHEMA_TEMPLATE_FIELDNAMES()
|
|
|
|
{
|
|
|
|
// Delete the GRID_TRICKS.
|
|
|
|
m_grid->PopEventHandler( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataToWindow()
|
|
|
|
{
|
2020-08-30 17:57:10 +00:00
|
|
|
SCHEMATIC& schematic = m_frame->Schematic();
|
|
|
|
|
|
|
|
m_fields = schematic.Settings().m_TemplateFieldNames.GetTemplateFieldNames( m_global );
|
2018-05-14 17:34:18 +00:00
|
|
|
return TransferDataToGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::OnAddButtonClick( wxCommandEvent& event )
|
|
|
|
{
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::OnDeleteButtonClick( wxCommandEvent& event )
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataToGrid()
|
|
|
|
{
|
|
|
|
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):
|
|
|
|
m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? "1" : "0" );
|
|
|
|
m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? "1" : "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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataFromGrid()
|
|
|
|
{
|
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 );
|
2022-01-06 17:59:34 +00:00
|
|
|
m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) == "1";
|
|
|
|
m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) == "1";
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataFromWindow()
|
|
|
|
{
|
|
|
|
if( !TransferDataFromGrid() )
|
|
|
|
return false;
|
|
|
|
|
2020-08-30 17:57:10 +00:00
|
|
|
SCHEMATIC& schematic = m_frame->Schematic();
|
|
|
|
|
|
|
|
schematic.Settings().m_TemplateFieldNames.DeleteAllFieldNameTemplates( m_global );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
|
|
|
for( const TEMPLATE_FIELDNAME& field : m_fields )
|
2020-08-30 17:57:10 +00:00
|
|
|
schematic.Settings().m_TemplateFieldNames.AddTemplateFieldName( field, m_global );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
2020-11-21 23:09:57 +00:00
|
|
|
if( m_global )
|
|
|
|
{
|
|
|
|
auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
|
|
|
|
|
|
|
|
if( cfg )
|
|
|
|
{
|
|
|
|
// Save global fieldname templates
|
|
|
|
STRING_FORMATTER sf;
|
|
|
|
schematic.Settings().m_TemplateFieldNames.Format( &sf, 0, true );
|
|
|
|
|
|
|
|
wxString record = FROM_UTF8( sf.GetString().c_str() );
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::AdjustGridColumns( int aWidth )
|
|
|
|
{
|
|
|
|
if( aWidth <= 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Account for scroll bars
|
|
|
|
aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
|
|
|
|
|
2018-08-08 20:57:53 +00:00
|
|
|
m_grid->SetColSize( 0, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::OnSizeGrid( wxSizeEvent& event )
|
|
|
|
{
|
|
|
|
AdjustGridColumns( event.GetSize().GetX() );
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
2020-03-13 09:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
void PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::ImportSettingsFrom( TEMPLATES* templateMgr )
|
|
|
|
{
|
|
|
|
m_fields = templateMgr->GetTemplateFieldNames( m_global );
|
|
|
|
TransferDataToGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|