From e53ee9df4b5b79e1dd87b4a114e341bd72547dff Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Fri, 27 Jan 2023 09:18:32 -0500 Subject: [PATCH] Symbol Fields Table: add rename column functionality --- .../dialogs/dialog_symbol_fields_table.cpp | 90 ++++++++++++++++++- eeschema/dialogs/dialog_symbol_fields_table.h | 3 +- .../dialog_symbol_fields_table_base.cpp | 9 +- .../dialog_symbol_fields_table_base.fbp | 76 +++++++++++++++- .../dialogs/dialog_symbol_fields_table_base.h | 2 + 5 files changed, 176 insertions(+), 4 deletions(-) diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 9aff640298..4dadfdb97a 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -241,6 +241,22 @@ public: } } + void RenameColumn( int aCol, const wxString& newName ) + { + wxString fieldName = m_fieldNames[aCol]; + + m_fieldNames[aCol] = newName; + + for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i ) + { + SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol(); + + auto node = m_dataStore[symbol->m_Uuid].extract( fieldName ); + node.key() = newName; + m_dataStore[symbol->m_Uuid].insert( std::move( node ) ); + } + } + int GetNumberRows() override { return m_rows.size(); } // Columns are fieldNames + quantity column @@ -1136,7 +1152,10 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnRemoveField( wxCommandEvent& event ) m_fieldsCtrl->SelectRow( --row ); if( row < MANDATORY_FIELDS ) - m_removeFieldButton->Enable( false ); + { + m_removeFieldButton->Enable( false ); + m_renameFieldButton->Enable( false ); + } wxGridTableMessage msg( m_dataModel, wxGRIDTABLE_NOTIFY_COLS_DELETED, m_fieldsCtrl->GetItemCount(), 1 ); @@ -1153,6 +1172,69 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnRemoveField( wxCommandEvent& event ) } +void DIALOG_SYMBOL_FIELDS_TABLE::OnRenameField( wxCommandEvent& event ) +{ + int col = -1; + int row = m_fieldsCtrl->GetSelectedRow(); + + // Should never occur: "Rename Field..." button should be disabled if invalid selection + // via OnFieldsCtrlSelectionChanged() + wxCHECK_RET( row != -1, wxS( "Some user defined field must be selected first" ) ); + wxCHECK_RET( row >= MANDATORY_FIELDS, wxS( "Mandatory fields cannot be renamed" ) ); + + wxString fieldName = m_fieldsCtrl->GetTextValue( row, 0 ); + + + wxTextEntryDialog dlg( this, _( "New field name:" ), _( "Rename Field" ) ); + + if( dlg.ShowModal() != wxID_OK ) + return; + + wxString newFieldName = dlg.GetValue(); + + if( fieldName.IsEmpty() ) + { + DisplayError( this, _( "Field must have a name." ) ); + return; + } + + for( int i = 0; i < m_dataModel->GetNumberCols(); ++i ) + { + if( fieldName == m_dataModel->GetColLabelValue( i ) ) + { + if( col == -1 ) + { + col = i; + } + else + { + wxString confirm_msg = wxString::Format( + _( "Field name %s already exists. Cannot rename over existing field." ), + fieldName ); + DisplayError( this, confirm_msg ); + return; + } + } + } + + + m_dataModel->RenameColumn( col, newFieldName ); + m_fieldsCtrl->SetTextValue( newFieldName, col, 0 ); + + std::string oldKey( fieldName.ToUTF8() ); + std::string newKey( newFieldName.ToUTF8() ); + + EESCHEMA_SETTINGS* cfg = static_cast( Kiface().KifaceSettings() ); + + //In-place rename map key + auto node = cfg->m_FieldEditorPanel.fields_show.extract( oldKey ); + node.key() = newKey; + cfg->m_FieldEditorPanel.fields_show.insert( std::move( node ) ); + + cfg->m_FieldEditorPanel.fields_show[newKey] = true; +} + + void DIALOG_SYMBOL_FIELDS_TABLE::OnFilterText( wxCommandEvent& aEvent ) { m_dataModel->RebuildRows( m_filter, m_groupSymbolsBox, m_fieldsCtrl ); @@ -1181,9 +1263,15 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnFieldsCtrlSelectionChanged( wxDataViewEvent& int row = m_fieldsCtrl->GetSelectedRow(); if( row >= MANDATORY_FIELDS ) + { m_removeFieldButton->Enable( true ); + m_renameFieldButton->Enable( true ); + } else + { m_removeFieldButton->Enable( false ); + m_renameFieldButton->Enable( false ); + } } void DIALOG_SYMBOL_FIELDS_TABLE::OnColumnItemToggled( wxDataViewEvent& event ) diff --git a/eeschema/dialogs/dialog_symbol_fields_table.h b/eeschema/dialogs/dialog_symbol_fields_table.h index 5508e5c805..7f267d9d65 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.h +++ b/eeschema/dialogs/dialog_symbol_fields_table.h @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2017 Oliver Walters - * Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2017-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 @@ -66,6 +66,7 @@ private: void OnSizeFieldList( wxSizeEvent& event ) override; void OnAddField( wxCommandEvent& event ) override; void OnRemoveField( wxCommandEvent& event ) override; + void OnRenameField( wxCommandEvent& event ) override; void OnExport( wxCommandEvent& aEvent ) override; void OnSaveAndContinue( wxCommandEvent& aEvent ) override; void OnCancel( wxCommandEvent& aEvent ) override; diff --git a/eeschema/dialogs/dialog_symbol_fields_table_base.cpp b/eeschema/dialogs/dialog_symbol_fields_table_base.cpp index e6aa5dc457..e9372efc52 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table_base.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table_base.cpp @@ -37,7 +37,12 @@ DIALOG_SYMBOL_FIELDS_TABLE_BASE::DIALOG_SYMBOL_FIELDS_TABLE_BASE( wxWindow* pare m_removeFieldButton = new wxButton( m_leftPanel, wxID_ANY, _("Remove Field..."), wxDefaultPosition, wxDefaultSize, 0 ); m_removeFieldButton->Enable( false ); - bLeftSizer->Add( m_removeFieldButton, 0, wxEXPAND|wxBOTTOM|wxLEFT, 5 ); + bLeftSizer->Add( m_removeFieldButton, 0, wxEXPAND|wxLEFT, 5 ); + + m_renameFieldButton = new wxButton( m_leftPanel, wxID_ANY, _("Rename Field..."), wxDefaultPosition, wxDefaultSize, 0 ); + m_renameFieldButton->Enable( false ); + + bLeftSizer->Add( m_renameFieldButton, 0, wxBOTTOM|wxEXPAND|wxLEFT|wxTOP, 5 ); m_leftPanel->SetSizer( bLeftSizer ); @@ -156,6 +161,7 @@ DIALOG_SYMBOL_FIELDS_TABLE_BASE::DIALOG_SYMBOL_FIELDS_TABLE_BASE( wxWindow* pare m_fieldsCtrl->Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnSizeFieldList ), NULL, this ); m_addFieldButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnAddField ), NULL, this ); m_removeFieldButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnRemoveField ), NULL, this ); + m_renameFieldButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnRenameField ), NULL, this ); m_filter->Connect( wxEVT_MOTION, wxMouseEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnFilterMouseMoved ), NULL, this ); m_filter->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnFilterText ), NULL, this ); m_groupSymbolsBox->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnGroupSymbolsToggled ), NULL, this ); @@ -180,6 +186,7 @@ DIALOG_SYMBOL_FIELDS_TABLE_BASE::~DIALOG_SYMBOL_FIELDS_TABLE_BASE() m_fieldsCtrl->Disconnect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnSizeFieldList ), NULL, this ); m_addFieldButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnAddField ), NULL, this ); m_removeFieldButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnRemoveField ), NULL, this ); + m_renameFieldButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnRenameField ), NULL, this ); m_filter->Disconnect( wxEVT_MOTION, wxMouseEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnFilterMouseMoved ), NULL, this ); m_filter->Disconnect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnFilterText ), NULL, this ); m_groupSymbolsBox->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnGroupSymbolsToggled ), NULL, this ); diff --git a/eeschema/dialogs/dialog_symbol_fields_table_base.fbp b/eeschema/dialogs/dialog_symbol_fields_table_base.fbp index 56635c06f3..177905519d 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table_base.fbp +++ b/eeschema/dialogs/dialog_symbol_fields_table_base.fbp @@ -286,7 +286,7 @@ 5 - wxEXPAND|wxBOTTOM|wxLEFT + wxEXPAND|wxLEFT 0 1 @@ -358,6 +358,80 @@ OnRemoveField + + 5 + wxBOTTOM|wxEXPAND|wxLEFT|wxTOP + 0 + + 1 + 1 + 1 + 1 + + + + + 0 + + + + + 1 + 0 + 1 + + 1 + + 0 + 0 + + Dock + 0 + Left + 0 + + 1 + + + 0 + 0 + wxID_ANY + Rename Field... + + 0 + + 0 + + + 0 + + 1 + m_renameFieldButton + 1 + + + protected + 1 + + + + Resizable + 1 + + + ; ; forward_declare + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + OnRenameField + + diff --git a/eeschema/dialogs/dialog_symbol_fields_table_base.h b/eeschema/dialogs/dialog_symbol_fields_table_base.h index b5e973fbea..eb1c0f06e8 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table_base.h +++ b/eeschema/dialogs/dialog_symbol_fields_table_base.h @@ -50,6 +50,7 @@ class DIALOG_SYMBOL_FIELDS_TABLE_BASE : public DIALOG_SHIM wxDataViewListCtrl* m_fieldsCtrl; wxButton* m_addFieldButton; wxButton* m_removeFieldButton; + wxButton* m_renameFieldButton; wxPanel* m_rightPanel; wxSearchCtrl* m_filter; BITMAP_BUTTON* m_separator1; @@ -70,6 +71,7 @@ class DIALOG_SYMBOL_FIELDS_TABLE_BASE : public DIALOG_SHIM virtual void OnSizeFieldList( wxSizeEvent& event ) { event.Skip(); } virtual void OnAddField( wxCommandEvent& event ) { event.Skip(); } virtual void OnRemoveField( wxCommandEvent& event ) { event.Skip(); } + virtual void OnRenameField( wxCommandEvent& event ) { event.Skip(); } virtual void OnFilterMouseMoved( wxMouseEvent& event ) { event.Skip(); } virtual void OnFilterText( wxCommandEvent& event ) { event.Skip(); } virtual void OnGroupSymbolsToggled( wxCommandEvent& event ) { event.Skip(); }