From a119bd604da66ac2ac703e5226e8ca445e588e59 Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Sun, 28 May 2023 21:10:07 +0200 Subject: [PATCH] Refactor: Move DIALOG_PLUGIN_OPTIONS to common and use enum for event ID --- common/CMakeLists.txt | 2 + common/dialogs/dialog_plugin_options.cpp | 282 ++++++++++++++++ .../dialogs/dialog_plugin_options_base.cpp | 40 +-- .../dialogs/dialog_plugin_options_base.fbp | 6 +- .../dialogs/dialog_plugin_options_base.h | 10 +- common/lib_table_grid_tricks.cpp | 30 +- eeschema/dialogs/panel_sym_lib_table.cpp | 5 + eeschema/sch_io_mgr.h | 2 +- include/dialogs/dialog_plugin_options.h | 76 +++++ include/lib_table_grid.h | 4 + include/lib_table_grid_tricks.h | 9 +- pcbnew/CMakeLists.txt | 2 - pcbnew/dialogs/dialog_fp_plugin_options.cpp | 304 ------------------ pcbnew/dialogs/panel_fp_lib_table.cpp | 44 +-- pcbnew/invoke_pcb_dialog.h | 14 - pcbnew/io_mgr.h | 2 +- 16 files changed, 444 insertions(+), 388 deletions(-) create mode 100644 common/dialogs/dialog_plugin_options.cpp rename pcbnew/dialogs/dialog_fp_plugin_options_base.cpp => common/dialogs/dialog_plugin_options_base.cpp (77%) rename pcbnew/dialogs/dialog_fp_plugin_options_base.fbp => common/dialogs/dialog_plugin_options_base.fbp (99%) rename pcbnew/dialogs/dialog_fp_plugin_options_base.h => common/dialogs/dialog_plugin_options_base.h (82%) create mode 100644 include/dialogs/dialog_plugin_options.h delete mode 100644 pcbnew/dialogs/dialog_fp_plugin_options.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index be6c575c01..983cc84a02 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -125,6 +125,8 @@ set( COMMON_DLG_SRCS dialogs/dialog_page_settings_base.cpp dialogs/dialog_paste_special.cpp dialogs/dialog_paste_special_base.cpp + dialogs/dialog_plugin_options.cpp + dialogs/dialog_plugin_options_base.cpp dialogs/dialog_text_entry_base.cpp dialogs/dialog_page_settings.cpp dialogs/dialog_print_generic.cpp diff --git a/common/dialogs/dialog_plugin_options.cpp b/common/dialogs/dialog_plugin_options.cpp new file mode 100644 index 0000000000..70809e9e70 --- /dev/null +++ b/common/dialogs/dialog_plugin_options.cpp @@ -0,0 +1,282 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck + * Copyright (C) 2013 CERN + * Copyright (C) 2013-2023 KiCad Developers, see change_log.txt for contributors. + * + * 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 +#include +#include +#include +#include +#include +#include + + +#define INITIAL_HELP \ + _( "Select an Option Choice in the listbox above, and then click the Append Selected Option button." ) + + +DIALOG_PLUGIN_OPTIONS::DIALOG_PLUGIN_OPTIONS( wxWindow* aParent, + const wxString& aNickname, + const STRING_UTF8_MAP& aPluginOptions, + const wxString& aFormattedOptions, + wxString* aResult ) : + DIALOG_PLUGIN_OPTIONS_BASE( aParent ), + m_choices( aPluginOptions ), + m_callers_options( aFormattedOptions ), + m_result( aResult ), + m_initial_help( INITIAL_HELP ), + m_grid_widths_dirty( true ) +{ + SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) ); + + // Give a bit more room for combobox editors + m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 ); + + m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows ); + + // add Cut, Copy, and Paste to wxGrid + m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) ); + + // Option Choices Panel: + if( m_choices.size() ) + { + unsigned int row = 0; + + for( STRING_UTF8_MAP::const_iterator it = m_choices.begin(); it != m_choices.end(); + ++it, ++row ) + { + wxString item = FROM_UTF8( it->first.c_str() ); + + m_listbox->InsertItems( 1, &item, row ); + } + } + + m_html->SetPage( m_initial_help ); + + // Configure button logos + m_append_button->SetBitmap( KiBitmap( BITMAPS::small_plus ) ); + m_delete_button->SetBitmap( KiBitmap( BITMAPS::small_trash ) ); + + // initial focus on the grid please. + SetInitialFocus( m_grid ); + + SetupStandardButtons(); +} + + +DIALOG_PLUGIN_OPTIONS ::~DIALOG_PLUGIN_OPTIONS() +{ + // destroy GRID_TRICKS before m_grid. + m_grid->PopEventHandler( true ); +} + + +bool DIALOG_PLUGIN_OPTIONS::TransferDataToWindow() +{ + if( !DIALOG_SHIM::TransferDataToWindow() ) + return false; + + // Fill the grid with existing aOptions + std::string options = TO_UTF8( m_callers_options ); + + STRING_UTF8_MAP* props = LIB_TABLE::ParseOptions( options ); + + if( props ) + { + if( (int) props->size() > m_grid->GetNumberRows() ) + m_grid->AppendRows( props->size() - m_grid->GetNumberRows() ); + + int row = 0; + + for( STRING_UTF8_MAP::const_iterator it = props->begin(); it != props->end(); + ++it, ++row ) + { + m_grid->SetCellValue( row, 0, FROM_UTF8( it->first.c_str() ) ); + m_grid->SetCellValue( row, 1, it->second ); + } + + delete props; + } + + return true; +} + + +bool DIALOG_PLUGIN_OPTIONS::TransferDataFromWindow() +{ + if( !m_grid->CommitPendingChanges() ) + return false; + + if( !DIALOG_SHIM::TransferDataFromWindow() ) + return false; + + STRING_UTF8_MAP props; + const int rowCount = m_grid->GetNumberRows(); + + for( int row = 0; rowGetCellValue( row, 0 ).Trim( false ).Trim() ); + UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim(); + + if( name.size() ) + { + props[name] = value; + } + } + + *m_result = LIB_TABLE::FormatOptions( &props ); + return true; +} + + +int DIALOG_PLUGIN_OPTIONS::appendRow() +{ + int row = m_grid->GetNumberRows(); + + m_grid->AppendRows( 1 ); + + // wx documentation is wrong, SetGridCursor does not make visible. + m_grid->MakeCellVisible( row, 0 ); + m_grid->SetGridCursor( row, 0 ); + + return row; +} + + +void DIALOG_PLUGIN_OPTIONS::appendOption() +{ + int selected_row = m_listbox->GetSelection(); + if( selected_row != wxNOT_FOUND ) + { + wxString option = m_listbox->GetString( selected_row ); + + int row_count = m_grid->GetNumberRows(); + int row; + + for( row=0; rowGetCellValue( row, 0 ); + + if( !col0 ) // empty col0 + break; + } + + if( row == row_count ) + row = appendRow(); + + m_grid->SetCellValue( row, 0, option ); + m_grid_widths_dirty = true; + } +} + + +//----------------------------------------------------------- + +void DIALOG_PLUGIN_OPTIONS::onListBoxItemSelected( wxCommandEvent& event ) +{ + // change the help text based on the m_listbox selection: + if( event.IsSelection() ) + { + std::string option = TO_UTF8( event.GetString() ); + UTF8 help_text; + + if( m_choices.Value( option.c_str(), &help_text ) ) + m_html->SetPage( help_text ); + else + m_html->SetPage( m_initial_help ); + } +} + + +void DIALOG_PLUGIN_OPTIONS::onListBoxItemDoubleClicked( wxCommandEvent& event ) +{ + appendOption(); +} + + +void DIALOG_PLUGIN_OPTIONS::onAppendOption( wxCommandEvent& ) +{ + if( !m_grid->CommitPendingChanges() ) + return; + + appendOption(); +} + + +void DIALOG_PLUGIN_OPTIONS::onAppendRow( wxCommandEvent& ) +{ + if( !m_grid->CommitPendingChanges() ) + return; + + appendRow(); +} + + +void DIALOG_PLUGIN_OPTIONS::onDeleteRow( wxCommandEvent& ) +{ + if( !m_grid->CommitPendingChanges() ) + return; + + int curRow = m_grid->GetGridCursorRow(); + + m_grid->DeleteRows( curRow ); + m_grid_widths_dirty = true; + + curRow = std::max( 0, curRow - 1 ); + m_grid->MakeCellVisible( curRow, m_grid->GetGridCursorCol() ); + m_grid->SetGridCursor( curRow, m_grid->GetGridCursorCol() ); +} + + +void DIALOG_PLUGIN_OPTIONS::onGridCellChange( wxGridEvent& aEvent ) +{ + m_grid_widths_dirty = true; + + aEvent.Skip(); +} + + +void DIALOG_PLUGIN_OPTIONS::onUpdateUI( wxUpdateUIEvent& ) +{ + if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() ) + { + int width = m_grid->GetClientRect().GetWidth(); + + m_grid->AutoSizeColumn( 0 ); + m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) ); + + m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) ); + + m_grid_widths_dirty = false; + } +} + + +void DIALOG_PLUGIN_OPTIONS::onSize( wxSizeEvent& aEvent ) +{ + m_grid_widths_dirty = true; + + aEvent.Skip(); +} diff --git a/pcbnew/dialogs/dialog_fp_plugin_options_base.cpp b/common/dialogs/dialog_plugin_options_base.cpp similarity index 77% rename from pcbnew/dialogs/dialog_fp_plugin_options_base.cpp rename to common/dialogs/dialog_plugin_options_base.cpp index 1e7d35f94c..23a795b519 100644 --- a/pcbnew/dialogs/dialog_fp_plugin_options_base.cpp +++ b/common/dialogs/dialog_plugin_options_base.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b) +// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -8,11 +8,11 @@ #include "widgets/std_bitmap_button.h" #include "widgets/wx_grid.h" -#include "dialog_fp_plugin_options_base.h" +#include "dialog_plugin_options_base.h" /////////////////////////////////////////////////////////////////////////// -DIALOG_FP_PLUGIN_OPTIONS_BASE::DIALOG_FP_PLUGIN_OPTIONS_BASE( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : DIALOG_SHIM( parent, id, title, pos, size, style ) +DIALOG_PLUGIN_OPTIONS_BASE::DIALOG_PLUGIN_OPTIONS_BASE( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : DIALOG_SHIM( parent, id, title, pos, size, style ) { this->SetSizeHints( wxDefaultSize, wxDefaultSize ); @@ -116,26 +116,26 @@ DIALOG_FP_PLUGIN_OPTIONS_BASE::DIALOG_FP_PLUGIN_OPTIONS_BASE( wxWindow* parent, this->Centre( wxBOTH ); // Connect Events - m_grid->Connect( wxEVT_GRID_CELL_CHANGED, wxGridEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onGridCellChange ), NULL, this ); - m_grid->Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onSize ), NULL, this ); - m_grid->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onUpdateUI ), NULL, this ); - m_append_button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onAppendRow ), NULL, this ); - m_delete_button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onDeleteRow ), NULL, this ); - m_listbox->Connect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onListBoxItemSelected ), NULL, this ); - m_listbox->Connect( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onListBoxItemDoubleClicked ), NULL, this ); - m_append_choice_button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onAppendOption ), NULL, this ); + m_grid->Connect( wxEVT_GRID_CELL_CHANGED, wxGridEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onGridCellChange ), NULL, this ); + m_grid->Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onSize ), NULL, this ); + m_grid->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onUpdateUI ), NULL, this ); + m_append_button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onAppendRow ), NULL, this ); + m_delete_button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onDeleteRow ), NULL, this ); + m_listbox->Connect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onListBoxItemSelected ), NULL, this ); + m_listbox->Connect( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onListBoxItemDoubleClicked ), NULL, this ); + m_append_choice_button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onAppendOption ), NULL, this ); } -DIALOG_FP_PLUGIN_OPTIONS_BASE::~DIALOG_FP_PLUGIN_OPTIONS_BASE() +DIALOG_PLUGIN_OPTIONS_BASE::~DIALOG_PLUGIN_OPTIONS_BASE() { // Disconnect Events - m_grid->Disconnect( wxEVT_GRID_CELL_CHANGED, wxGridEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onGridCellChange ), NULL, this ); - m_grid->Disconnect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onSize ), NULL, this ); - m_grid->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onUpdateUI ), NULL, this ); - m_append_button->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onAppendRow ), NULL, this ); - m_delete_button->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onDeleteRow ), NULL, this ); - m_listbox->Disconnect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onListBoxItemSelected ), NULL, this ); - m_listbox->Disconnect( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onListBoxItemDoubleClicked ), NULL, this ); - m_append_choice_button->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_FP_PLUGIN_OPTIONS_BASE::onAppendOption ), NULL, this ); + m_grid->Disconnect( wxEVT_GRID_CELL_CHANGED, wxGridEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onGridCellChange ), NULL, this ); + m_grid->Disconnect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onSize ), NULL, this ); + m_grid->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onUpdateUI ), NULL, this ); + m_append_button->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onAppendRow ), NULL, this ); + m_delete_button->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onDeleteRow ), NULL, this ); + m_listbox->Disconnect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onListBoxItemSelected ), NULL, this ); + m_listbox->Disconnect( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onListBoxItemDoubleClicked ), NULL, this ); + m_append_choice_button->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLUGIN_OPTIONS_BASE::onAppendOption ), NULL, this ); } diff --git a/pcbnew/dialogs/dialog_fp_plugin_options_base.fbp b/common/dialogs/dialog_plugin_options_base.fbp similarity index 99% rename from pcbnew/dialogs/dialog_fp_plugin_options_base.fbp rename to common/dialogs/dialog_plugin_options_base.fbp index ae0cdc2813..95db69a364 100644 --- a/pcbnew/dialogs/dialog_fp_plugin_options_base.fbp +++ b/common/dialogs/dialog_plugin_options_base.fbp @@ -11,13 +11,13 @@ res UTF-8 connect - dialog_fp_plugin_options_base + dialog_plugin_options_base 1000 none 1 - dialog_fp_plugin_options + dialog_plugin_options . @@ -45,7 +45,7 @@ wxID_ANY - DIALOG_FP_PLUGIN_OPTIONS_BASE + DIALOG_PLUGIN_OPTIONS_BASE -1,-1 wxCAPTION|wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxRESIZE_BORDER diff --git a/pcbnew/dialogs/dialog_fp_plugin_options_base.h b/common/dialogs/dialog_plugin_options_base.h similarity index 82% rename from pcbnew/dialogs/dialog_fp_plugin_options_base.h rename to common/dialogs/dialog_plugin_options_base.h index f3e3a49737..0d08cc8d91 100644 --- a/pcbnew/dialogs/dialog_fp_plugin_options_base.h +++ b/common/dialogs/dialog_plugin_options_base.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b) +// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -36,9 +36,9 @@ class WX_GRID; /////////////////////////////////////////////////////////////////////////////// -/// Class DIALOG_FP_PLUGIN_OPTIONS_BASE +/// Class DIALOG_PLUGIN_OPTIONS_BASE /////////////////////////////////////////////////////////////////////////////// -class DIALOG_FP_PLUGIN_OPTIONS_BASE : public DIALOG_SHIM +class DIALOG_PLUGIN_OPTIONS_BASE : public DIALOG_SHIM { private: @@ -66,9 +66,9 @@ class DIALOG_FP_PLUGIN_OPTIONS_BASE : public DIALOG_SHIM public: - DIALOG_FP_PLUGIN_OPTIONS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( -1,-1 ), long style = wxCAPTION|wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxRESIZE_BORDER ); + DIALOG_PLUGIN_OPTIONS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( -1,-1 ), long style = wxCAPTION|wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxRESIZE_BORDER ); - ~DIALOG_FP_PLUGIN_OPTIONS_BASE(); + ~DIALOG_PLUGIN_OPTIONS_BASE(); }; diff --git a/common/lib_table_grid_tricks.cpp b/common/lib_table_grid_tricks.cpp index 59eb4ea3f3..86105b3cf5 100644 --- a/common/lib_table_grid_tricks.cpp +++ b/common/lib_table_grid_tricks.cpp @@ -20,6 +20,7 @@ #include "lib_table_grid_tricks.h" #include "lib_table_grid.h" + LIB_TABLE_GRID_TRICKS::LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid ) : GRID_TRICKS( aGrid ) { } @@ -27,6 +28,14 @@ LIB_TABLE_GRID_TRICKS::LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid ) : GRID_TRICKS( aG void LIB_TABLE_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent ) { + if( m_grid->GetGridCursorCol() == COL_OPTIONS ) + { + menu.Append( LIB_TABLE_GRID_TRICKS_OPTIONS_EDITOR, + _( "Options Editor..." ), + _( "Edit options" ) ); + menu.AppendSeparator(); + } + bool showActivate = false; bool showDeactivate = false; LIB_TABLE_GRID* tbl = static_cast( m_grid->GetTable() ); @@ -73,8 +82,12 @@ void LIB_TABLE_GRID_TRICKS::doPopupSelection( wxCommandEvent& event ) int menu_id = event.GetId(); LIB_TABLE_GRID* tbl = (LIB_TABLE_GRID*) m_grid->GetTable(); - if( menu_id == LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED - || menu_id == LIB_TABLE_GRID_TRICKS_DEACTIVATE_SELECTED ) + if( menu_id == LIB_TABLE_GRID_TRICKS_OPTIONS_EDITOR ) + { + optionsEditor( m_grid->GetGridCursorRow() ); + } + else if( menu_id == LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED + || menu_id == LIB_TABLE_GRID_TRICKS_DEACTIVATE_SELECTED ) { bool selected_state = menu_id == LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED; @@ -95,3 +108,16 @@ void LIB_TABLE_GRID_TRICKS::doPopupSelection( wxCommandEvent& event ) GRID_TRICKS::doPopupSelection( event ); } } + + +bool LIB_TABLE_GRID_TRICKS::handleDoubleClick( wxGridEvent& aEvent ) +{ + if( aEvent.GetCol() == COL_OPTIONS ) + { + optionsEditor( aEvent.GetRow() ); + return true; + } + + return false; +} + diff --git a/eeschema/dialogs/panel_sym_lib_table.cpp b/eeschema/dialogs/panel_sym_lib_table.cpp index 1ac4d8f462..35acf0c394 100644 --- a/eeschema/dialogs/panel_sym_lib_table.cpp +++ b/eeschema/dialogs/panel_sym_lib_table.cpp @@ -138,6 +138,11 @@ public: protected: DIALOG_EDIT_LIBRARY_TABLES* m_dialog; + virtual void optionsEditor( int aRow ) + { + DisplayError( m_dialog, _( "optionsEditor not implemented in SYMBOL_GRID_TRICKS" ) ); + } + /// handle specialized clipboard text, with leading "(sym_lib_table" or /// spreadsheet formatted text. virtual void paste_text( const wxString& cb_text ) override diff --git a/eeschema/sch_io_mgr.h b/eeschema/sch_io_mgr.h index e8a34f9211..8ef1317343 100644 --- a/eeschema/sch_io_mgr.h +++ b/eeschema/sch_io_mgr.h @@ -432,7 +432,7 @@ public: *
This eventually is what shows up into the fp-lib-table "options" * field, possibly combined with others.
*
internationalized description
- *
The internationalized description is displayed in DIALOG_FP_SCH_PLUGIN_OPTIONS. + *
The internationalized description is displayed in DIALOG_PLUGIN_OPTIONS. * It may be multi-line and be quite explanatory of the option.
* *
diff --git a/include/dialogs/dialog_plugin_options.h b/include/dialogs/dialog_plugin_options.h new file mode 100644 index 0000000000..a90c6c9cbc --- /dev/null +++ b/include/dialogs/dialog_plugin_options.h @@ -0,0 +1,76 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. + * + * 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 3 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, see . + */ + + +#ifndef _DIALOG_PLUGIN_OPTIONS_H_ +#define _DIALOG_PLUGIN_OPTIONS_H_ + +#include +#include + +/** + * DIALOG_PLUGIN_OPTIONS + * is an options editor in the form of a two column name/value + * spreadsheet like (table) UI. + */ +class DIALOG_PLUGIN_OPTIONS : public DIALOG_PLUGIN_OPTIONS_BASE +{ +public: + DIALOG_PLUGIN_OPTIONS( wxWindow* aParent, const wxString& aNickname, + const STRING_UTF8_MAP& aPluginOptions, const wxString& aFormattedOptions, + wxString* aResult ); + + ~DIALOG_PLUGIN_OPTIONS() override; + + bool TransferDataToWindow() override; + + bool TransferDataFromWindow() override; + +private: + const wxString& m_callers_options; + wxString* m_result; + STRING_UTF8_MAP m_choices; + wxString m_initial_help; + bool m_grid_widths_dirty; + + int appendRow(); + + void appendOption(); + + //----------------------------------------------------------- + void onListBoxItemSelected( wxCommandEvent& event ) override; + + void onListBoxItemDoubleClicked( wxCommandEvent& event ) override; + + void onAppendOption( wxCommandEvent& ) override; + + void onAppendRow( wxCommandEvent& ) override; + + void onDeleteRow( wxCommandEvent& ) override; + + void onGridCellChange( wxGridEvent& aEvent ) override; + + void onUpdateUI( wxUpdateUIEvent& ) override; + + void onSize( wxSizeEvent& aEvent ) override; +}; + + +#endif // _DIALOG_PLUGIN_OPTIONS_H_ + diff --git a/include/lib_table_grid.h b/include/lib_table_grid.h index 9402a4e2ec..01b46985cb 100644 --- a/include/lib_table_grid.h +++ b/include/lib_table_grid.h @@ -24,6 +24,8 @@ #include #include +class LIB_TABLE_GRID_TRICKS; + const wxColour COLOUR_ROW_ENABLED( 0, 0, 0 ); const wxColour COLOUR_ROW_DISABLED( 100, 100, 100 ); @@ -47,6 +49,8 @@ enum COL_ORDER */ class LIB_TABLE_GRID : public wxGridTableBase { + friend class LIB_TABLE_GRID_TRICKS; + public: //------------------------------------------------ diff --git a/include/lib_table_grid_tricks.h b/include/lib_table_grid_tricks.h index 7a1e4ac249..3faea27ad6 100644 --- a/include/lib_table_grid_tricks.h +++ b/include/lib_table_grid_tricks.h @@ -25,12 +25,19 @@ class LIB_TABLE_GRID_TRICKS : public GRID_TRICKS { LIB_TABLE_GRID_TRICKS_ACTIVATE_SELECTED = GRIDTRICKS_FIRST_CLIENT_ID, LIB_TABLE_GRID_TRICKS_DEACTIVATE_SELECTED, - LIB_TABLE_GRID_TRICKS_LIBRARY_SETTINGS + LIB_TABLE_GRID_TRICKS_LIBRARY_SETTINGS, + LIB_TABLE_GRID_TRICKS_OPTIONS_EDITOR }; public: explicit LIB_TABLE_GRID_TRICKS( WX_GRID* aGrid ); + virtual ~LIB_TABLE_GRID_TRICKS(){}; + void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent ) override; void doPopupSelection( wxCommandEvent& event ) override; + +protected: + virtual void optionsEditor( int aRow ) = 0; + bool handleDoubleClick( wxGridEvent& aEvent ) override; }; diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt index e6e188ff3c..2dafb76af0 100644 --- a/pcbnew/CMakeLists.txt +++ b/pcbnew/CMakeLists.txt @@ -74,8 +74,6 @@ set( PCBNEW_DIALOGS dialogs/dialog_find_base.cpp dialogs/dialog_footprint_wizard_list.cpp dialogs/dialog_footprint_wizard_list_base.cpp - dialogs/dialog_fp_plugin_options.cpp - dialogs/dialog_fp_plugin_options_base.cpp dialogs/dialog_gen_footprint_position.cpp dialogs/dialog_gen_footprint_position_file_base.cpp dialogs/dialog_gencad_export_options.cpp diff --git a/pcbnew/dialogs/dialog_fp_plugin_options.cpp b/pcbnew/dialogs/dialog_fp_plugin_options.cpp deleted file mode 100644 index cf12feb48b..0000000000 --- a/pcbnew/dialogs/dialog_fp_plugin_options.cpp +++ /dev/null @@ -1,304 +0,0 @@ -/* - * This program source code file is part of KiCad, a free EDA CAD application. - * - * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck - * Copyright (C) 2013 CERN - * Copyright (C) 2013-2022 KiCad Developers, see change_log.txt for contributors. - * - * 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 -#include -#include -#include -#include -#include -#include -#include - - -#define INITIAL_HELP \ - _( "Select an Option Choice in the listbox above, and then click the Append Selected Option button." ) - - -using std::string; - - -/** - * DIALOG_FP_PLUGIN_OPTIONS - * 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. - */ -class DIALOG_FP_PLUGIN_OPTIONS : public DIALOG_FP_PLUGIN_OPTIONS_BASE -{ - -public: - DIALOG_FP_PLUGIN_OPTIONS( wxWindow* aParent, const wxString& aNickname, - const wxString& aPluginType, const wxString& aOptions, - wxString* aResult ) : - DIALOG_FP_PLUGIN_OPTIONS_BASE( aParent ), - m_callers_options( aOptions ), - m_result( aResult ), - m_initial_help( INITIAL_HELP ), - m_grid_widths_dirty( true ) - { - SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) ); - - // Give a bit more room for combobox editors - m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 ); - - m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows ); - - // add Cut, Copy, and Paste to wxGrid - m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) ); - - // Option Choices Panel: - - IO_MGR::PCB_FILE_T pi_type = IO_MGR::EnumFromStr( aPluginType ); - PLUGIN::RELEASER pi( IO_MGR::PluginFind( pi_type ) ); - - pi->FootprintLibOptions( &m_choices ); - - if( m_choices.size() ) - { - unsigned int row = 0; - - for( STRING_UTF8_MAP::const_iterator it = m_choices.begin(); it != m_choices.end(); - ++it, ++row ) - { - wxString item = FROM_UTF8( it->first.c_str() ); - - m_listbox->InsertItems( 1, &item, row ); - } - } - - m_html->SetPage( m_initial_help ); - - // Configure button logos - m_append_button->SetBitmap( KiBitmap( BITMAPS::small_plus ) ); - m_delete_button->SetBitmap( KiBitmap( BITMAPS::small_trash ) ); - - // initial focus on the grid please. - SetInitialFocus( m_grid ); - - SetupStandardButtons(); - } - - ~DIALOG_FP_PLUGIN_OPTIONS() override - { - // destroy GRID_TRICKS before m_grid. - m_grid->PopEventHandler( true ); - } - - bool TransferDataToWindow() override - { - if( !DIALOG_SHIM::TransferDataToWindow() ) - return false; - - // Fill the grid with existing aOptions - string options = TO_UTF8( m_callers_options ); - - STRING_UTF8_MAP* props = LIB_TABLE::ParseOptions( options ); - - if( props ) - { - if( (int) props->size() > m_grid->GetNumberRows() ) - m_grid->AppendRows( props->size() - m_grid->GetNumberRows() ); - - int row = 0; - - for( STRING_UTF8_MAP::const_iterator it = props->begin(); it != props->end(); - ++it, ++row ) - { - m_grid->SetCellValue( row, 0, FROM_UTF8( it->first.c_str() ) ); - m_grid->SetCellValue( row, 1, it->second ); - } - - delete props; - } - - return true; - } - - bool TransferDataFromWindow() override - { - if( !m_grid->CommitPendingChanges() ) - return false; - - if( !DIALOG_SHIM::TransferDataFromWindow() ) - return false; - - STRING_UTF8_MAP props; - const int rowCount = m_grid->GetNumberRows(); - - for( int row = 0; rowGetCellValue( row, 0 ).Trim( false ).Trim() ); - UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim(); - - if( name.size() ) - { - props[name] = value; - } - } - - *m_result = LIB_TABLE::FormatOptions( &props ); - return true; - } - -private: - const wxString& m_callers_options; - wxString* m_result; - STRING_UTF8_MAP m_choices; - wxString m_initial_help; - bool m_grid_widths_dirty; - - int appendRow() - { - int row = m_grid->GetNumberRows(); - - m_grid->AppendRows( 1 ); - - // wx documentation is wrong, SetGridCursor does not make visible. - m_grid->MakeCellVisible( row, 0 ); - m_grid->SetGridCursor( row, 0 ); - - return row; - } - - void appendOption() - { - int selected_row = m_listbox->GetSelection(); - if( selected_row != wxNOT_FOUND ) - { - wxString option = m_listbox->GetString( selected_row ); - - int row_count = m_grid->GetNumberRows(); - int row; - - for( row=0; rowGetCellValue( row, 0 ); - - if( !col0 ) // empty col0 - break; - } - - if( row == row_count ) - row = appendRow(); - - m_grid->SetCellValue( row, 0, option ); - m_grid_widths_dirty = true; - } - } - - //----------------------------------------------------------- - - void onListBoxItemSelected( wxCommandEvent& event ) override - { - // change the help text based on the m_listbox selection: - if( event.IsSelection() ) - { - string option = TO_UTF8( event.GetString() ); - UTF8 help_text; - - if( m_choices.Value( option.c_str(), &help_text ) ) - m_html->SetPage( help_text ); - else - m_html->SetPage( m_initial_help ); - } - } - - void onListBoxItemDoubleClicked( wxCommandEvent& event ) override - { - appendOption(); - } - - void onAppendOption( wxCommandEvent& ) override - { - if( !m_grid->CommitPendingChanges() ) - return; - - appendOption(); - } - - void onAppendRow( wxCommandEvent& ) override - { - if( !m_grid->CommitPendingChanges() ) - return; - - appendRow(); - } - - void onDeleteRow( wxCommandEvent& ) override - { - if( !m_grid->CommitPendingChanges() ) - return; - - int curRow = m_grid->GetGridCursorRow(); - - m_grid->DeleteRows( curRow ); - m_grid_widths_dirty = true; - - curRow = std::max( 0, curRow - 1 ); - m_grid->MakeCellVisible( curRow, m_grid->GetGridCursorCol() ); - m_grid->SetGridCursor( curRow, m_grid->GetGridCursorCol() ); - } - - void onGridCellChange( wxGridEvent& aEvent ) override - { - m_grid_widths_dirty = true; - - aEvent.Skip(); - } - - void onUpdateUI( wxUpdateUIEvent& ) override - { - if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() ) - { - int width = m_grid->GetClientRect().GetWidth(); - - m_grid->AutoSizeColumn( 0 ); - m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) ); - - m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) ); - - m_grid_widths_dirty = false; - } - } - - void onSize( wxSizeEvent& aEvent ) override - { - m_grid_widths_dirty = true; - - aEvent.Skip(); - } -}; - - -void InvokePluginOptionsEditor( wxWindow* aCaller, const wxString& aNickname, - const wxString& aPluginType, const wxString& aOptions, - wxString* aResult ) -{ - DIALOG_FP_PLUGIN_OPTIONS dlg( aCaller, aNickname, aPluginType, aOptions, aResult ); - - dlg.ShowModal(); -} diff --git a/pcbnew/dialogs/panel_fp_lib_table.cpp b/pcbnew/dialogs/panel_fp_lib_table.cpp index 7a356606fd..946770d40b 100644 --- a/pcbnew/dialogs/panel_fp_lib_table.cpp +++ b/pcbnew/dialogs/panel_fp_lib_table.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -251,8 +252,6 @@ public: }; -#define MYID_OPTIONS_EDITOR 15151 - class FP_GRID_TRICKS : public LIB_TABLE_GRID_TRICKS { @@ -264,7 +263,7 @@ public: protected: DIALOG_EDIT_LIBRARY_TABLES* m_dialog; - void optionsEditor( int aRow ) + void optionsEditor( int aRow ) override { FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable(); @@ -273,9 +272,14 @@ protected: LIB_TABLE_ROW* row = tbl->at( (size_t) aRow ); const wxString& options = row->GetOptions(); wxString result = options; + STRING_UTF8_MAP choices; - InvokePluginOptionsEditor( m_dialog, row->GetNickName(), row->GetType(), options, - &result ); + IO_MGR::PCB_FILE_T pi_type = IO_MGR::EnumFromStr( row->GetType() ); + PLUGIN::RELEASER pi( IO_MGR::PluginFind( pi_type ) ); + pi->FootprintLibOptions( &choices ); + + DIALOG_PLUGIN_OPTIONS dlg( m_dialog, row->GetNickName(), choices, options, &result ); + dlg.ShowModal(); if( options != result ) { @@ -285,36 +289,6 @@ protected: } } - bool handleDoubleClick( wxGridEvent& aEvent ) override - { - if( aEvent.GetCol() == COL_OPTIONS ) - { - optionsEditor( aEvent.GetRow() ); - return true; - } - - return false; - } - - void showPopupMenu( wxMenu& menu, wxGridEvent& aEvent ) override - { - if( m_grid->GetGridCursorCol() == COL_OPTIONS ) - { - menu.Append( MYID_OPTIONS_EDITOR, _( "Options Editor..." ), _( "Edit options" ) ); - menu.AppendSeparator(); - } - - LIB_TABLE_GRID_TRICKS::showPopupMenu( menu, aEvent ); - } - - void doPopupSelection( wxCommandEvent& event ) override - { - if( event.GetId() == MYID_OPTIONS_EDITOR ) - optionsEditor( m_grid->GetGridCursorRow() ); - else - LIB_TABLE_GRID_TRICKS::doPopupSelection( event ); - } - /// handle specialized clipboard text, with leading "(fp_lib_table", OR /// spreadsheet formatted text. void paste_text( const wxString& cb_text ) override diff --git a/pcbnew/invoke_pcb_dialog.h b/pcbnew/invoke_pcb_dialog.h index c0c7f40036..23e119f838 100644 --- a/pcbnew/invoke_pcb_dialog.h +++ b/pcbnew/invoke_pcb_dialog.h @@ -72,20 +72,6 @@ class KIWAY; */ void InvokePcbLibTableEditor( KIWAY* aKiway, wxWindow* aCaller ); - -/** - * Function InvokePluginOptionsEditor - * calls DIALOG_FP_PLUGIN_OPTIONS dialog so that plugin options set can be edited. - * - * @param aCaller is the wxTopLevelWindow which is invoking the dialog. - * @param aNickname is the footprint library whose options are being edited. - * @param aPluginType is something that will pass through IO_MGR::EnumFromStr(). - * @param aOptions is the options string on calling into this function. - * @param aResult is where to put the result of the editing. - */ -void InvokePluginOptionsEditor( wxWindow* aCaller, const wxString& aNickname, - const wxString& aPluginType, const wxString& aOptions, wxString* aResult ); - /** * Function InvokeExportSVG * shows the Export SVG dialog diff --git a/pcbnew/io_mgr.h b/pcbnew/io_mgr.h index 44d55ec8b5..12854c85c9 100644 --- a/pcbnew/io_mgr.h +++ b/pcbnew/io_mgr.h @@ -538,7 +538,7 @@ public: *
This eventually is what shows up into the fp-lib-table "options" * field, possibly combined with others.
*
internationalized description
- *
The internationalized description is displayed in DIALOG_FP_PLUGIN_OPTIONS. + *
The internationalized description is displayed in DIALOG_PLUGIN_OPTIONS. * It may be multi-line and be quite explanatory of the option.
* *