2013-10-13 21:33:58 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2013-10-23 18:56:03 +00:00
|
|
|
* Copyright (C) 2013 CERN
|
2016-11-19 22:15:34 +00:00
|
|
|
* Copyright (C) 2013-2016 KiCad Developers, see change_log.txt for contributors.
|
2013-10-13 21:33:58 +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 <invoke_pcb_dialog.h>
|
|
|
|
#include <dialog_fp_plugin_options_base.h>
|
|
|
|
#include <fp_lib_table.h>
|
2013-10-23 18:56:03 +00:00
|
|
|
#include <grid_tricks.h>
|
2018-08-19 16:10:14 +00:00
|
|
|
#include <widgets/wx_grid.h>
|
2018-03-14 04:51:59 +00:00
|
|
|
#include <bitmaps.h>
|
2020-10-24 14:45:37 +00:00
|
|
|
#include <macros.h>
|
2013-10-13 21:33:58 +00:00
|
|
|
|
2013-11-12 20:49:17 +00:00
|
|
|
|
|
|
|
#define INITIAL_HELP \
|
|
|
|
_( "Select an <b>Option Choice</b> in the listbox above, and then click the <b>Append Selected Option</b> button." )
|
|
|
|
|
|
|
|
|
2013-10-24 13:58:05 +00:00
|
|
|
using std::string;
|
|
|
|
|
2013-10-13 21:33:58 +00:00
|
|
|
|
2013-10-30 15:33:51 +00:00
|
|
|
/**
|
2020-01-10 14:31:00 +00:00
|
|
|
* DIALOG_FP_PLUGIN_OPTIONS
|
2013-10-30 15:33:51 +00:00
|
|
|
* is an options editor in the form of a two column name/value
|
|
|
|
* spreadsheet like (table) UI. It takes hints from a pcbnew PLUGIN as to
|
|
|
|
* supported options.
|
|
|
|
*/
|
2013-10-13 21:33:58 +00:00
|
|
|
class DIALOG_FP_PLUGIN_OPTIONS : public DIALOG_FP_PLUGIN_OPTIONS_BASE
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2018-03-14 04:51:59 +00:00
|
|
|
DIALOG_FP_PLUGIN_OPTIONS( wxWindow* aParent, const wxString& aNickname,
|
|
|
|
const wxString& aPluginType, const wxString& aOptions,
|
|
|
|
wxString* aResult ) :
|
2013-10-13 21:33:58 +00:00
|
|
|
DIALOG_FP_PLUGIN_OPTIONS_BASE( aParent ),
|
|
|
|
m_callers_options( aOptions ),
|
2013-11-12 20:49:17 +00:00
|
|
|
m_result( aResult ),
|
2018-07-20 17:46:56 +00:00
|
|
|
m_initial_help( INITIAL_HELP ),
|
|
|
|
m_grid_widths_dirty( true )
|
2013-10-13 21:33:58 +00:00
|
|
|
{
|
2021-06-27 13:24:02 +00:00
|
|
|
SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) );
|
2013-10-23 18:56:03 +00:00
|
|
|
|
2018-08-25 14:29:00 +00:00
|
|
|
// Give a bit more room for combobox editors
|
|
|
|
m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
|
|
|
|
|
|
|
|
m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
|
|
|
|
|
2013-10-30 05:14:11 +00:00
|
|
|
// add Cut, Copy, and Paste to wxGrid
|
2013-10-23 18:56:03 +00:00
|
|
|
m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
|
|
|
|
|
2013-11-12 00:17:27 +00:00
|
|
|
// Option Choices Panel:
|
|
|
|
|
|
|
|
IO_MGR::PCB_FILE_T pi_type = IO_MGR::EnumFromStr( aPluginType );
|
|
|
|
PLUGIN::RELEASER pi( IO_MGR::PluginFind( pi_type ) );
|
|
|
|
|
2013-11-12 20:49:17 +00:00
|
|
|
pi->FootprintLibOptions( &m_choices );
|
2013-11-12 00:17:27 +00:00
|
|
|
|
2013-11-12 20:49:17 +00:00
|
|
|
if( m_choices.size() )
|
2013-11-12 00:17:27 +00:00
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
unsigned int row = 0;
|
2022-11-06 16:51:52 +00:00
|
|
|
|
|
|
|
for( STRING_UTF8_MAP::const_iterator it = m_choices.begin(); it != m_choices.end();
|
|
|
|
++it, ++row )
|
2013-11-12 00:17:27 +00:00
|
|
|
{
|
2013-11-12 20:49:17 +00:00
|
|
|
wxString item = FROM_UTF8( it->first.c_str() );
|
|
|
|
|
|
|
|
m_listbox->InsertItems( 1, &item, row );
|
2013-11-12 00:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:49:17 +00:00
|
|
|
m_html->SetPage( m_initial_help );
|
2013-11-12 00:17:27 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
// Configure button logos
|
2021-03-08 02:59:07 +00:00
|
|
|
m_append_button->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
|
|
|
|
m_delete_button->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
|
2013-11-12 20:49:17 +00:00
|
|
|
|
2013-10-23 18:56:03 +00:00
|
|
|
// initial focus on the grid please.
|
2018-03-14 04:51:59 +00:00
|
|
|
SetInitialFocus( m_grid );
|
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
SetupStandardButtons();
|
2013-10-23 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
~DIALOG_FP_PLUGIN_OPTIONS() override
|
2013-10-23 18:56:03 +00:00
|
|
|
{
|
2013-10-24 13:58:05 +00:00
|
|
|
// destroy GRID_TRICKS before m_grid.
|
2013-10-23 18:56:03 +00:00
|
|
|
m_grid->PopEventHandler( true );
|
2013-10-13 21:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
bool TransferDataToWindow() override
|
2013-10-30 15:33:51 +00:00
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
if( !DIALOG_SHIM::TransferDataToWindow() )
|
|
|
|
return false;
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
// Fill the grid with existing aOptions
|
|
|
|
string options = TO_UTF8( m_callers_options );
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2022-11-06 16:51:52 +00:00
|
|
|
STRING_UTF8_MAP* props = LIB_TABLE::ParseOptions( options );
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
if( props )
|
2013-10-30 15:33:51 +00:00
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
if( (int) props->size() > m_grid->GetNumberRows() )
|
|
|
|
m_grid->AppendRows( props->size() - m_grid->GetNumberRows() );
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
int row = 0;
|
2022-11-06 16:51:52 +00:00
|
|
|
|
|
|
|
for( STRING_UTF8_MAP::const_iterator it = props->begin(); it != props->end();
|
|
|
|
++it, ++row )
|
2018-03-14 04:51:59 +00:00
|
|
|
{
|
|
|
|
m_grid->SetCellValue( row, 0, FROM_UTF8( it->first.c_str() ) );
|
|
|
|
m_grid->SetCellValue( row, 1, it->second );
|
|
|
|
}
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
delete props;
|
2013-10-30 15:33:51 +00:00
|
|
|
}
|
2018-03-14 04:51:59 +00:00
|
|
|
|
|
|
|
return true;
|
2013-10-30 15:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
bool TransferDataFromWindow() override
|
2013-10-24 13:58:05 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return false;
|
2018-03-14 04:51:59 +00:00
|
|
|
|
|
|
|
if( !DIALOG_SHIM::TransferDataFromWindow() )
|
|
|
|
return false;
|
|
|
|
|
2022-11-06 16:51:52 +00:00
|
|
|
STRING_UTF8_MAP props;
|
2013-10-24 13:58:05 +00:00
|
|
|
const int rowCount = m_grid->GetNumberRows();
|
|
|
|
|
|
|
|
for( int row = 0; row<rowCount; ++row )
|
|
|
|
{
|
|
|
|
string name = TO_UTF8( m_grid->GetCellValue( row, 0 ).Trim( false ).Trim() );
|
2014-01-02 02:17:07 +00:00
|
|
|
UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim();
|
2013-10-24 13:58:05 +00:00
|
|
|
|
|
|
|
if( name.size() )
|
|
|
|
{
|
|
|
|
props[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
*m_result = LIB_TABLE::FormatOptions( &props );
|
|
|
|
return true;
|
2013-10-24 13:58:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
private:
|
|
|
|
const wxString& m_callers_options;
|
|
|
|
wxString* m_result;
|
2022-11-06 16:51:52 +00:00
|
|
|
STRING_UTF8_MAP m_choices;
|
2018-03-14 04:51:59 +00:00
|
|
|
wxString m_initial_help;
|
2018-07-20 17:46:56 +00:00
|
|
|
bool m_grid_widths_dirty;
|
2013-10-24 13:58:05 +00:00
|
|
|
|
2013-11-12 00:17:27 +00:00
|
|
|
int appendRow()
|
2013-10-13 21:33:58 +00:00
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
int row = m_grid->GetNumberRows();
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
m_grid->AppendRows( 1 );
|
2013-11-12 00:17:27 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
// wx documentation is wrong, SetGridCursor does not make visible.
|
|
|
|
m_grid->MakeCellVisible( row, 0 );
|
|
|
|
m_grid->SetGridCursor( row, 0 );
|
2013-11-12 00:17:27 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
return row;
|
2013-11-12 00:17:27 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 20:49:17 +00:00
|
|
|
void appendOption()
|
2013-11-12 00:17:27 +00:00
|
|
|
{
|
2013-11-12 20:49:17 +00:00
|
|
|
int selected_row = m_listbox->GetSelection();
|
|
|
|
if( selected_row != wxNOT_FOUND )
|
2013-11-12 00:17:27 +00:00
|
|
|
{
|
2013-11-12 20:49:17 +00:00
|
|
|
wxString option = m_listbox->GetString( selected_row );
|
2013-11-12 00:17:27 +00:00
|
|
|
|
|
|
|
int row_count = m_grid->GetNumberRows();
|
|
|
|
int row;
|
|
|
|
|
|
|
|
for( row=0; row<row_count; ++row )
|
|
|
|
{
|
|
|
|
wxString col0 = m_grid->GetCellValue( row, 0 );
|
|
|
|
|
|
|
|
if( !col0 ) // empty col0
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( row == row_count )
|
|
|
|
row = appendRow();
|
|
|
|
|
|
|
|
m_grid->SetCellValue( row, 0, option );
|
2018-07-20 17:46:56 +00:00
|
|
|
m_grid_widths_dirty = true;
|
2013-10-30 15:33:51 +00:00
|
|
|
}
|
2013-10-13 21:33:58 +00:00
|
|
|
}
|
2013-11-12 20:49:17 +00:00
|
|
|
|
|
|
|
//-----<event handlers>------------------------------------------------------
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
void onListBoxItemSelected( wxCommandEvent& event ) override
|
2013-11-12 20:49:17 +00:00
|
|
|
{
|
2013-11-19 20:38:29 +00:00
|
|
|
// change the help text based on the m_listbox selection:
|
2013-11-12 20:49:17 +00:00
|
|
|
if( event.IsSelection() )
|
|
|
|
{
|
2013-11-19 20:38:29 +00:00
|
|
|
string option = TO_UTF8( event.GetString() );
|
2014-01-02 02:17:07 +00:00
|
|
|
UTF8 help_text;
|
2013-11-12 20:49:17 +00:00
|
|
|
|
2013-11-19 20:38:29 +00:00
|
|
|
if( m_choices.Value( option.c_str(), &help_text ) )
|
2018-03-14 04:51:59 +00:00
|
|
|
m_html->SetPage( help_text );
|
2013-11-12 20:49:17 +00:00
|
|
|
else
|
|
|
|
m_html->SetPage( m_initial_help );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
void onListBoxItemDoubleClicked( wxCommandEvent& event ) override
|
2013-11-12 20:49:17 +00:00
|
|
|
{
|
|
|
|
appendOption();
|
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
void onAppendOption( wxCommandEvent& ) override
|
2013-11-12 20:49:17 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return;
|
|
|
|
|
2013-11-12 20:49:17 +00:00
|
|
|
appendOption();
|
|
|
|
}
|
2013-10-13 21:33:58 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
void onAppendRow( wxCommandEvent& ) override
|
2013-11-12 00:17:27 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return;
|
|
|
|
|
2013-11-12 00:17:27 +00:00
|
|
|
appendRow();
|
|
|
|
}
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
void onDeleteRow( wxCommandEvent& ) override
|
2013-10-13 21:33:58 +00:00
|
|
|
{
|
2018-08-19 16:10:14 +00:00
|
|
|
if( !m_grid->CommitPendingChanges() )
|
|
|
|
return;
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
int curRow = m_grid->GetGridCursorRow();
|
2013-10-30 15:33:51 +00:00
|
|
|
|
|
|
|
m_grid->DeleteRows( curRow );
|
2018-07-20 17:46:56 +00:00
|
|
|
m_grid_widths_dirty = true;
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-02-28 09:44:22 +00:00
|
|
|
curRow = std::max( 0, curRow - 1 );
|
|
|
|
m_grid->MakeCellVisible( curRow, m_grid->GetGridCursorCol() );
|
|
|
|
m_grid->SetGridCursor( curRow, m_grid->GetGridCursorCol() );
|
2013-10-13 21:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 17:46:56 +00:00
|
|
|
void onGridCellChange( wxGridEvent& aEvent ) override
|
2013-10-13 21:33:58 +00:00
|
|
|
{
|
2018-07-20 17:46:56 +00:00
|
|
|
m_grid_widths_dirty = true;
|
2013-10-30 15:33:51 +00:00
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
aEvent.Skip();
|
2013-10-13 21:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 17:46:56 +00:00
|
|
|
void onUpdateUI( wxUpdateUIEvent& ) override
|
2013-10-13 21:33:58 +00:00
|
|
|
{
|
2018-07-20 17:46:56 +00:00
|
|
|
if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() )
|
|
|
|
{
|
|
|
|
int width = m_grid->GetClientRect().GetWidth();
|
|
|
|
|
|
|
|
m_grid->AutoSizeColumn( 0 );
|
2022-11-16 12:35:21 +00:00
|
|
|
m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
|
2013-10-13 21:33:58 +00:00
|
|
|
|
2022-11-16 12:35:21 +00:00
|
|
|
m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) );
|
2018-07-20 17:46:56 +00:00
|
|
|
|
|
|
|
m_grid_widths_dirty = false;
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 04:51:59 +00:00
|
|
|
|
2018-07-20 17:46:56 +00:00
|
|
|
void onSize( wxSizeEvent& aEvent ) override
|
|
|
|
{
|
|
|
|
m_grid_widths_dirty = true;
|
2013-10-13 21:33:58 +00:00
|
|
|
|
2018-07-20 17:46:56 +00:00
|
|
|
aEvent.Skip();
|
2013-10-13 21:33:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-03-14 04:51:59 +00:00
|
|
|
void InvokePluginOptionsEditor( wxWindow* aCaller, const wxString& aNickname,
|
|
|
|
const wxString& aPluginType, const wxString& aOptions,
|
|
|
|
wxString* aResult )
|
2013-10-13 21:33:58 +00:00
|
|
|
{
|
2018-03-14 04:51:59 +00:00
|
|
|
DIALOG_FP_PLUGIN_OPTIONS dlg( aCaller, aNickname, aPluginType, aOptions, aResult );
|
2013-10-13 21:33:58 +00:00
|
|
|
|
|
|
|
dlg.ShowModal();
|
|
|
|
}
|