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>
|
2020-03-10 18:46:57 +00:00
|
|
|
* Copyright (C) 1992-2020 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
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>
|
|
|
|
|
|
|
|
#include <panel_eeschema_template_fieldnames.h>
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-04 20:32:14 +00:00
|
|
|
// Yep, it's a hack. I haven't a clue why the global and project versions don't lay
|
|
|
|
// out the same.
|
|
|
|
if( !aGlobal )
|
|
|
|
{
|
|
|
|
m_leftMargin->PrependSpacer( 10 );
|
|
|
|
m_topMargin->PrependSpacer( 10 );
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:34:18 +00:00
|
|
|
m_addFieldButton->SetBitmap( KiBitmap( small_plus_xpm ) );
|
|
|
|
m_deleteFieldButton->SetBitmap( KiBitmap( trash_xpm ) );
|
|
|
|
|
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 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::~PANEL_EESCHEMA_TEMPLATE_FIELDNAMES()
|
|
|
|
{
|
|
|
|
// Delete the GRID_TRICKS.
|
|
|
|
m_grid->PopEventHandler( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataToWindow()
|
|
|
|
{
|
2020-03-10 18:46:57 +00:00
|
|
|
m_fields = m_frame->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;
|
|
|
|
|
2018-05-14 17:34:18 +00:00
|
|
|
int curRow = m_grid->GetGridCursorRow();
|
|
|
|
|
2018-12-07 10:22:03 +00:00
|
|
|
if( curRow >= 0 && curRow < (int)m_fields.size() )
|
2018-05-14 17:34:18 +00:00
|
|
|
{
|
|
|
|
m_fields.erase( m_fields.begin() + curRow );
|
|
|
|
m_grid->DeleteRows( curRow );
|
|
|
|
}
|
|
|
|
|
2018-08-19 16:10:14 +00:00
|
|
|
m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
|
|
|
|
m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataToGrid()
|
|
|
|
{
|
|
|
|
m_grid->Freeze();
|
|
|
|
|
|
|
|
if( m_grid->GetNumberRows() )
|
|
|
|
m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxT( "1" ) : wxEmptyString );
|
2018-08-08 20:57:53 +00:00
|
|
|
m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxT( "1" ) : wxEmptyString );
|
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 )
|
|
|
|
{
|
|
|
|
m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
|
|
|
|
m_fields[row].m_Visible = ( m_grid->GetCellValue( row, 1 ) != wxEmptyString );
|
2018-08-08 20:57:53 +00:00
|
|
|
m_fields[row].m_URL = ( m_grid->GetCellValue( row, 2 ) != wxEmptyString );
|
2018-05-14 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PANEL_EESCHEMA_TEMPLATE_FIELDNAMES::TransferDataFromWindow()
|
|
|
|
{
|
|
|
|
if( !TransferDataFromGrid() )
|
|
|
|
return false;
|
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
m_frame->DeleteAllTemplateFieldNames( m_global );
|
2018-05-14 17:34:18 +00:00
|
|
|
|
|
|
|
for( const TEMPLATE_FIELDNAME& field : m_fields )
|
2020-03-10 18:46:57 +00:00
|
|
|
m_frame->AddTemplateFieldName( field, m_global );
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|