From f179754118bf4fcd6006475721abcf2617acd9aa Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 9 Sep 2022 12:18:05 +0100 Subject: [PATCH] Implement add-new-on-return for some of our grids. Fixes https://gitlab.com/kicad/code/kicad/issues/12335 --- common/dialogs/dialog_configure_paths.cpp | 12 +++- common/dialogs/panel_text_variables.cpp | 5 +- common/grid_tricks.cpp | 68 +++++++++++++------ .../dialogs/dialog_lib_edit_pin_table.cpp | 5 +- eeschema/dialogs/dialog_pin_properties.cpp | 6 +- eeschema/dialogs/panel_setup_buses.cpp | 39 ++++++++++- eeschema/dialogs/panel_setup_buses.h | 3 +- .../dialogs/panel_template_fieldnames.cpp | 5 +- include/grid_tricks.h | 10 ++- .../dialogs/dialog_manage_repositories.cpp | 5 +- .../dialog_footprint_properties_fp_editor.cpp | 12 +++- .../dialogs/panel_fp_properties_3d_model.cpp | 5 +- .../dialogs/panel_setup_tracks_and_vias.cpp | 18 ++++- 13 files changed, 156 insertions(+), 37 deletions(-) diff --git a/common/dialogs/dialog_configure_paths.cpp b/common/dialogs/dialog_configure_paths.cpp index e863c657d0..99cf538365 100644 --- a/common/dialogs/dialog_configure_paths.cpp +++ b/common/dialogs/dialog_configure_paths.cpp @@ -89,8 +89,16 @@ DIALOG_CONFIGURE_PATHS::DIALOG_CONFIGURE_PATHS( wxWindow* aParent, FILENAME_RESO m_EnvVars->SetDefaultRowSize( m_EnvVars->GetDefaultRowSize() + 4 ); m_SearchPaths->SetDefaultRowSize( m_SearchPaths->GetDefaultRowSize() + 4 ); - m_EnvVars->PushEventHandler( new GRID_TRICKS( m_EnvVars ) ); - m_SearchPaths->PushEventHandler( new GRID_TRICKS( m_SearchPaths ) ); + m_EnvVars->PushEventHandler( new GRID_TRICKS( m_EnvVars, + [this]( wxCommandEvent& aEvent ) + { + OnAddEnvVar( aEvent ); + } ) ); + m_SearchPaths->PushEventHandler( new GRID_TRICKS( m_SearchPaths, + [this]( wxCommandEvent& aEvent ) + { + OnAddSearchPath( aEvent ); + } ) ); m_EnvVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows ); m_SearchPaths->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows ); diff --git a/common/dialogs/panel_text_variables.cpp b/common/dialogs/panel_text_variables.cpp index 94f9568b8d..6926968811 100644 --- a/common/dialogs/panel_text_variables.cpp +++ b/common/dialogs/panel_text_variables.cpp @@ -54,7 +54,10 @@ PANEL_TEXT_VARIABLES::PANEL_TEXT_VARIABLES( wxWindow* aParent, PROJECT* aProject m_nameValidator.SetStyle( wxFILTER_EXCLUDE_CHAR_LIST ); m_nameValidator.SetCharExcludes( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) ); - m_TextVars->PushEventHandler( new GRID_TRICKS( m_TextVars ) ); + m_TextVars->PushEventHandler( new GRID_TRICKS( m_TextVars, [this]( wxCommandEvent& aEvent ) + { + OnAddTextVar( aEvent ); + } ) ); m_TextVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows ); // wxFormBuilder doesn't include this event... diff --git a/common/grid_tricks.cpp b/common/grid_tricks.cpp index 4b557087a2..e23fda9e70 100644 --- a/common/grid_tricks.cpp +++ b/common/grid_tricks.cpp @@ -37,34 +37,52 @@ #define ROW_SEP wxT( '\n' ) -GRID_TRICKS::GRID_TRICKS( WX_GRID* aGrid ): - m_grid( aGrid ) +GRID_TRICKS::GRID_TRICKS( WX_GRID* aGrid ) : + m_grid( aGrid ), + m_addHandler( []( wxCommandEvent& ) {} ) +{ + init(); +} + + +GRID_TRICKS::GRID_TRICKS( WX_GRID* aGrid, std::function aAddHandler ) : + m_grid( aGrid ), + m_addHandler( aAddHandler ) +{ + init(); +} + + +void GRID_TRICKS::init() { m_sel_row_start = 0; m_sel_col_start = 0; m_sel_row_count = 0; m_sel_col_count = 0; - aGrid->Connect( wxEVT_GRID_CELL_LEFT_CLICK, - wxGridEventHandler( GRID_TRICKS::onGridCellLeftClick ), nullptr, this ); - aGrid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK, - wxGridEventHandler( GRID_TRICKS::onGridCellLeftDClick ), nullptr, this ); - aGrid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK, - wxGridEventHandler( GRID_TRICKS::onGridCellRightClick ), nullptr, this ); - aGrid->Connect( wxEVT_GRID_LABEL_RIGHT_CLICK, - wxGridEventHandler( GRID_TRICKS::onGridLabelRightClick ), nullptr, this ); - aGrid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK, - wxGridEventHandler( GRID_TRICKS::onGridLabelLeftClick ), nullptr, this ); - aGrid->Connect( GRIDTRICKS_FIRST_ID, GRIDTRICKS_LAST_ID, wxEVT_COMMAND_MENU_SELECTED, - wxCommandEventHandler( GRID_TRICKS::onPopupSelection ), nullptr, this ); - aGrid->Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( GRID_TRICKS::onCharHook ), nullptr, this ); - aGrid->Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( GRID_TRICKS::onKeyDown ), nullptr, this ); - aGrid->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( GRID_TRICKS::onUpdateUI ), - nullptr, this ); + m_grid->Connect( wxEVT_GRID_CELL_LEFT_CLICK, + wxGridEventHandler( GRID_TRICKS::onGridCellLeftClick ), nullptr, this ); + m_grid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK, + wxGridEventHandler( GRID_TRICKS::onGridCellLeftDClick ), nullptr, this ); + m_grid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK, + wxGridEventHandler( GRID_TRICKS::onGridCellRightClick ), nullptr, this ); + m_grid->Connect( wxEVT_GRID_LABEL_RIGHT_CLICK, + wxGridEventHandler( GRID_TRICKS::onGridLabelRightClick ), nullptr, this ); + m_grid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK, + wxGridEventHandler( GRID_TRICKS::onGridLabelLeftClick ), nullptr, this ); + m_grid->Connect( GRIDTRICKS_FIRST_ID, GRIDTRICKS_LAST_ID, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler( GRID_TRICKS::onPopupSelection ), nullptr, this ); + m_grid->Connect( wxEVT_CHAR_HOOK, + wxCharEventHandler( GRID_TRICKS::onCharHook ), nullptr, this ); + m_grid->Connect( wxEVT_KEY_DOWN, + wxKeyEventHandler( GRID_TRICKS::onKeyDown ), nullptr, this ); + m_grid->Connect( wxEVT_UPDATE_UI, + wxUpdateUIEventHandler( GRID_TRICKS::onUpdateUI ), nullptr, this ); // The handlers that control the tooltips must be on the actual grid window, not the grid - aGrid->GetGridWindow()->Connect( wxEVT_MOTION, - wxMouseEventHandler( GRID_TRICKS::onGridMotion ), nullptr, this ); + m_grid->GetGridWindow()->Connect( wxEVT_MOTION, + wxMouseEventHandler( GRID_TRICKS::onGridMotion ), nullptr, + this ); } @@ -400,7 +418,15 @@ void GRID_TRICKS::onCharHook( wxKeyEvent& ev ) { bool handled = false; - if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' ) + if( ev.GetKeyCode() == WXK_RETURN && m_grid->GetGridCursorRow() == m_grid->GetNumberRows() - 1 ) + { + if( m_grid->CommitPendingChanges() ) + { + wxCommandEvent dummy; + m_addHandler( dummy ); + } + } + else if( ev.GetModifiers() == wxMOD_CONTROL && ev.GetKeyCode() == 'V' ) { if( m_grid->IsCellEditControlShown() && wxTheClipboard->Open() ) { diff --git a/eeschema/dialogs/dialog_lib_edit_pin_table.cpp b/eeschema/dialogs/dialog_lib_edit_pin_table.cpp index 5866277929..9d6f31f22a 100644 --- a/eeschema/dialogs/dialog_lib_edit_pin_table.cpp +++ b/eeschema/dialogs/dialog_lib_edit_pin_table.cpp @@ -672,7 +672,10 @@ DIALOG_LIB_EDIT_PIN_TABLE::DIALOG_LIB_EDIT_PIN_TABLE( SYMBOL_EDIT_FRAME* parent, m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 ); m_grid->SetTable( m_dataModel ); - m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) ); + m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent ) + { + OnAddRow( aEvent ); + } ) ); // Show/hide columns according to the user's preference SYMBOL_EDITOR_SETTINGS* cfg = parent->GetSettings(); diff --git a/eeschema/dialogs/dialog_pin_properties.cpp b/eeschema/dialogs/dialog_pin_properties.cpp index 224abd68b5..1a515a8f0d 100644 --- a/eeschema/dialogs/dialog_pin_properties.cpp +++ b/eeschema/dialogs/dialog_pin_properties.cpp @@ -184,7 +184,11 @@ DIALOG_PIN_PROPERTIES::DIALOG_PIN_PROPERTIES( SYMBOL_EDIT_FRAME* parent, LIB_PIN m_alternatesGrid->SetDefaultRowSize( m_alternatesGrid->GetDefaultRowSize() + 4 ); m_alternatesGrid->SetTable( m_alternatesDataModel ); - m_alternatesGrid->PushEventHandler( new GRID_TRICKS( m_alternatesGrid ) ); + m_alternatesGrid->PushEventHandler( new GRID_TRICKS( m_alternatesGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddAlternate( aEvent ); + } ) ); if( aPin->GetParent()->HasConversion() ) { diff --git a/eeschema/dialogs/panel_setup_buses.cpp b/eeschema/dialogs/panel_setup_buses.cpp index 4673fdc681..cb91044965 100644 --- a/eeschema/dialogs/panel_setup_buses.cpp +++ b/eeschema/dialogs/panel_setup_buses.cpp @@ -24,6 +24,7 @@ #include #include #include +#include "grid_tricks.h" PANEL_SETUP_BUSES::PANEL_SETUP_BUSES( wxWindow* aWindow, SCH_EDIT_FRAME* aFrame ) : PANEL_SETUP_BUSES_BASE( aWindow ), @@ -41,6 +42,21 @@ PANEL_SETUP_BUSES::PANEL_SETUP_BUSES( wxWindow* aWindow, SCH_EDIT_FRAME* aFrame m_source->SetFont( KIUI::GetInfoFont( aWindow ) ); + m_aliasesGrid->PushEventHandler( new GRID_TRICKS( m_aliasesGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddAlias( aEvent ); + } ) ); + + m_membersGrid->PushEventHandler( new GRID_TRICKS( m_membersGrid, + [this]( wxCommandEvent& aEvent ) + { + wxIdleEvent dummy; + reloadMembersGridOnIdle( dummy ); + + OnAddMember( aEvent ); + } ) ); + // wxFormBuilder doesn't include this event... m_aliasesGrid->Connect( wxEVT_GRID_CELL_CHANGING, wxGridEventHandler( PANEL_SETUP_BUSES::OnAliasesGridCellChanging ), @@ -53,6 +69,20 @@ PANEL_SETUP_BUSES::PANEL_SETUP_BUSES( wxWindow* aWindow, SCH_EDIT_FRAME* aFrame } +PANEL_SETUP_BUSES::~PANEL_SETUP_BUSES() +{ + // Delete the GRID_TRICKS. + m_aliasesGrid->PopEventHandler( true ); + m_membersGrid->PopEventHandler( true ); + + m_aliasesGrid->Disconnect( wxEVT_GRID_CELL_CHANGING, + wxGridEventHandler( PANEL_SETUP_BUSES::OnAliasesGridCellChanging ), + nullptr, this ); + m_membersGrid->Disconnect( wxEVT_GRID_CELL_CHANGING, + wxGridEventHandler( PANEL_SETUP_BUSES::OnMemberGridCellChanging ), + nullptr, this ); +} + bool PANEL_SETUP_BUSES::TransferDataToWindow() { auto contains = @@ -175,7 +205,7 @@ void PANEL_SETUP_BUSES::OnAddMember( wxCommandEvent& aEvent ) if( !m_membersGrid->CommitPendingChanges() ) return; - int row = m_aliasesGrid->GetNumberRows(); + int row = m_membersGrid->GetNumberRows(); m_membersGrid->AppendRows(); m_membersGrid->MakeCellVisible( row, 0 ); @@ -274,6 +304,7 @@ void PANEL_SETUP_BUSES::OnMemberGridCellChanging( wxGridEvent& event ) } } + m_membersGridDirty = true; Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this ); } } @@ -306,12 +337,15 @@ void PANEL_SETUP_BUSES::doReloadMembersGrid() for( const wxString& member : alias->Members() ) m_membersGrid->SetCellValue( ii++, 0, member ); } + + m_membersGridDirty = false; } void PANEL_SETUP_BUSES::reloadMembersGridOnIdle( wxIdleEvent& aEvent ) { - doReloadMembersGrid(); + if( m_membersGridDirty ) + doReloadMembersGrid(); Unbind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this ); } @@ -401,6 +435,7 @@ void PANEL_SETUP_BUSES::OnUpdateUI( wxUpdateUIEvent& event ) const std::shared_ptr& alias = m_aliases[ row ]; alias->SetName( aliasName ); + m_membersGridDirty = true; Bind( wxEVT_IDLE, &PANEL_SETUP_BUSES::reloadMembersGridOnIdle, this ); } } diff --git a/eeschema/dialogs/panel_setup_buses.h b/eeschema/dialogs/panel_setup_buses.h index 962bc1b70f..76bb9c9738 100644 --- a/eeschema/dialogs/panel_setup_buses.h +++ b/eeschema/dialogs/panel_setup_buses.h @@ -35,7 +35,7 @@ class PANEL_SETUP_BUSES : public PANEL_SETUP_BUSES_BASE public: PANEL_SETUP_BUSES( wxWindow* aWindow, SCH_EDIT_FRAME* aFrame ); - ~PANEL_SETUP_BUSES() {} + ~PANEL_SETUP_BUSES(); bool TransferDataFromWindow() override; bool TransferDataToWindow() override; @@ -61,6 +61,7 @@ private: std::vector< std::shared_ptr > m_aliases; int m_lastAlias; wxString m_lastAliasName; + bool m_membersGridDirty; wxString m_errorMsg; WX_GRID* m_errorGrid; diff --git a/eeschema/dialogs/panel_template_fieldnames.cpp b/eeschema/dialogs/panel_template_fieldnames.cpp index 202a4cf0d2..ef0afe76a8 100644 --- a/eeschema/dialogs/panel_template_fieldnames.cpp +++ b/eeschema/dialogs/panel_template_fieldnames.cpp @@ -75,7 +75,10 @@ PANEL_TEMPLATE_FIELDNAMES::PANEL_TEMPLATE_FIELDNAMES( wxWindow* aWindow, m_checkboxColWidth = m_grid->GetColSize( 1 ); - m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) ); + m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent ) + { + OnAddButtonClick( aEvent ); + } ) ); m_grid->SetSelectionMode( wxGrid::wxGridSelectRows ); } diff --git a/include/grid_tricks.h b/include/grid_tricks.h index 02299f1080..ac34fb8d98 100644 --- a/include/grid_tricks.h +++ b/include/grid_tricks.h @@ -60,6 +60,8 @@ class GRID_TRICKS : public wxEvtHandler public: explicit GRID_TRICKS( WX_GRID* aGrid ); + GRID_TRICKS( WX_GRID* aGrid, std::function aAddHandler ); + /** * Enable the tooltip for a column. * @@ -85,6 +87,9 @@ public: } protected: + /// Shared initialization for various ctors. + void init(); + /// Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above. void getSelectedArea(); @@ -110,6 +115,7 @@ protected: virtual void paste_text( const wxString& cb_text ); virtual void cutcopy( bool doCopy, bool doDelete ); +protected: WX_GRID* m_grid; ///< I don't own the grid, but he owns me // row & col "selection" acquisition @@ -119,7 +125,9 @@ protected: int m_sel_row_count; int m_sel_col_count; - std::bitset m_tooltipEnabled; + std::function m_addHandler; + + std::bitset m_tooltipEnabled; }; #endif // _GRID_TRICKS_H_ diff --git a/kicad/pcm/dialogs/dialog_manage_repositories.cpp b/kicad/pcm/dialogs/dialog_manage_repositories.cpp index 0ebbbc5663..665561d342 100644 --- a/kicad/pcm/dialogs/dialog_manage_repositories.cpp +++ b/kicad/pcm/dialogs/dialog_manage_repositories.cpp @@ -54,7 +54,10 @@ DIALOG_MANAGE_REPOSITORIES::DIALOG_MANAGE_REPOSITORIES( addMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &DIALOG_MANAGE_REPOSITORIES::OnAddDefault, this, menuItem->GetId() ); - m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) ); + m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent ) + { + OnAdd( aEvent ); + } ) ); for( int col = 0; col < m_grid->GetNumberCols(); col++ ) { diff --git a/pcbnew/dialogs/dialog_footprint_properties_fp_editor.cpp b/pcbnew/dialogs/dialog_footprint_properties_fp_editor.cpp index 79b6a88eb8..a2dff55293 100644 --- a/pcbnew/dialogs/dialog_footprint_properties_fp_editor.cpp +++ b/pcbnew/dialogs/dialog_footprint_properties_fp_editor.cpp @@ -161,7 +161,16 @@ DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR( m_privateLayersGrid->SetTable( m_privateLayers ); m_itemsGrid->PushEventHandler( new GRID_TRICKS( m_itemsGrid ) ); - m_privateLayersGrid->PushEventHandler( new GRID_TRICKS( m_itemsGrid ) ); + m_privateLayersGrid->PushEventHandler( new GRID_TRICKS( m_privateLayersGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddLayer( aEvent ); + } ) ); + m_padGroupsGrid->PushEventHandler( new GRID_TRICKS( m_padGroupsGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddPadGroup( aEvent ); + } ) ); m_itemsGrid->SetSelectionMode( wxGrid::wxGridSelectRows ); m_privateLayersGrid->SetSelectionMode( wxGrid::wxGridSelectRows ); @@ -224,6 +233,7 @@ DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::~DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR() // Delete the GRID_TRICKS. m_itemsGrid->PopEventHandler( true ); m_privateLayersGrid->PopEventHandler( true ); + m_padGroupsGrid->PopEventHandler( true ); m_page = static_cast( m_NoteBook->GetSelection() ); diff --git a/pcbnew/dialogs/panel_fp_properties_3d_model.cpp b/pcbnew/dialogs/panel_fp_properties_3d_model.cpp index fb0de97cae..6697d5828e 100644 --- a/pcbnew/dialogs/panel_fp_properties_3d_model.cpp +++ b/pcbnew/dialogs/panel_fp_properties_3d_model.cpp @@ -62,7 +62,10 @@ PANEL_FP_PROPERTIES_3D_MODEL::PANEL_FP_PROPERTIES_3D_MODEL( { m_modelsGrid->SetDefaultRowSize( m_modelsGrid->GetDefaultRowSize() + 4 ); - GRID_TRICKS* trick = new GRID_TRICKS( m_modelsGrid ); + GRID_TRICKS* trick = new GRID_TRICKS( m_modelsGrid, [this]( wxCommandEvent& aEvent ) + { + OnAdd3DRow( aEvent ); + } ); trick->SetTooltipEnable( COL_PROBLEM ); m_modelsGrid->PushEventHandler( trick ); diff --git a/pcbnew/dialogs/panel_setup_tracks_and_vias.cpp b/pcbnew/dialogs/panel_setup_tracks_and_vias.cpp index a4701353c6..5a2c46f59f 100644 --- a/pcbnew/dialogs/panel_setup_tracks_and_vias.cpp +++ b/pcbnew/dialogs/panel_setup_tracks_and_vias.cpp @@ -77,9 +77,21 @@ PANEL_SETUP_TRACKS_AND_VIAS::PANEL_SETUP_TRACKS_AND_VIAS( PAGED_DIALOG* aParent, m_viaSizesGrid->SetDefaultRowSize( m_viaSizesGrid->GetDefaultRowSize() + 4 ); m_diffPairsGrid->SetDefaultRowSize( m_diffPairsGrid->GetDefaultRowSize() + 4 ); - m_trackWidthsGrid->PushEventHandler( new GRID_TRICKS( m_trackWidthsGrid ) ); - m_viaSizesGrid->PushEventHandler( new GRID_TRICKS( m_viaSizesGrid ) ); - m_diffPairsGrid->PushEventHandler( new GRID_TRICKS( m_diffPairsGrid ) ); + m_trackWidthsGrid->PushEventHandler( new GRID_TRICKS( m_trackWidthsGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddTrackWidthsClick( aEvent ); + } ) ); + m_viaSizesGrid->PushEventHandler( new GRID_TRICKS( m_viaSizesGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddViaSizesClick( aEvent ); + } ) ); + m_diffPairsGrid->PushEventHandler( new GRID_TRICKS( m_diffPairsGrid, + [this]( wxCommandEvent& aEvent ) + { + OnAddDiffPairsClick( aEvent ); + } ) ); m_trackWidthsGrid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows ); m_viaSizesGrid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );