From e26dcbece4b9822e9c5ba5b6e3f56bc93eec75b0 Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Wed, 12 Jul 2023 15:08:32 -0400 Subject: [PATCH] Symbol Fields Table: add item number support --- .../dialogs/dialog_symbol_fields_table.cpp | 19 ++++++++++++++++++- eeschema/fields_data_model.cpp | 19 +++++++++++++++++-- eeschema/fields_data_model.h | 2 ++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 5c0f80972d..e0cebf9201 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -335,7 +335,8 @@ void DIALOG_SYMBOL_FIELDS_TABLE::SetupColumnProperties() attr->SetEditor( new GRID_CELL_URL_EDITOR( this, Prj().SchSearchS() ) ); m_grid->SetColAttr( col, attr ); } - else if( m_dataModel->GetColFieldName( col ) == wxS( "Quantity" ) ) + else if( m_dataModel->GetColFieldName( col ) == wxS( "Quantity" ) + || m_dataModel->GetColFieldName( col ) == wxS( "Item Number" ) ) { attr->SetReadOnly(); m_grid->SetColAttr( col, attr ); @@ -566,6 +567,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::LoadFieldNames() // Generated field that isn't in any symbol AddField( wxS( "Quantity" ), _( "Qty" ), true, false ); + AddField( wxS( "Item Number" ), _( "#" ), true, false ); // User fields second std::set userFieldNames; @@ -807,6 +809,14 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnColumnItemToggled( wxDataViewEvent& event ) m_fieldsCtrl->SetToggleValue( value, row, col ); } + if( m_dataModel->ColIsItemNumber( dataCol ) && value ) + { + DisplayError( this, _( "The Item Number column cannot be grouped by." ) ); + + value = false; + m_fieldsCtrl->SetToggleValue( value, row, col ); + } + wxString fieldName = m_fieldsCtrl->GetTextValue( row, FIELD_NAME_COLUMN ); m_dataModel->SetGroupColumn( m_dataModel->GetFieldNameCol( fieldName ), value ); @@ -849,6 +859,13 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnColSort( wxGridEvent& aEvent ) std::string key( m_dataModel->GetColFieldName( sortCol ).ToUTF8() ); bool ascending; + // Don't sort by item number, it is generated by the sort + if( m_dataModel->ColIsItemNumber( sortCol ) ) + { + aEvent.Veto(); + return; + } + // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the event, and // if we ask it will give us pre-event info. if( m_grid->IsSortingBy( sortCol ) ) diff --git a/eeschema/fields_data_model.cpp b/eeschema/fields_data_model.cpp index 2bc6922010..d0e761469f 100644 --- a/eeschema/fields_data_model.cpp +++ b/eeschema/fields_data_model.cpp @@ -125,7 +125,7 @@ wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( const DATA_MODEL_ROW& group, i for( const SCH_REFERENCE& ref : group.m_Refs ) { - if( ColIsReference( aCol ) || ColIsQuantity( aCol ) ) + if( ColIsReference( aCol ) || ColIsQuantity( aCol ) || ColIsItemNumber( aCol ) ) { references.push_back( ref ); } @@ -158,7 +158,7 @@ wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( const DATA_MODEL_ROW& group, i } } - if( ColIsReference( aCol ) || ColIsQuantity( aCol ) ) + if( ColIsReference( aCol ) || ColIsQuantity( aCol ) || ColIsItemNumber( aCol ) ) { // Remove duplicates (other units of multi-unit parts) std::sort( references.begin(), references.end(), @@ -189,6 +189,8 @@ wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( const DATA_MODEL_ROW& group, i fieldValue = SCH_REFERENCE_LIST::Shorthand( references, refDelimiter, refRangeDelimiter ); else if( ColIsQuantity( aCol ) ) fieldValue = wxString::Format( wxT( "%d" ), (int) references.size() ); + else if( ColIsItemNumber( aCol ) && group.m_Flag != CHILD_ITEM ) + fieldValue = wxString::Format( wxT( "%d" ), group.m_ItemNumber ); return fieldValue; } @@ -220,6 +222,12 @@ bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsQuantity( int aCol ) return m_cols[aCol].m_fieldName == wxS( "Quantity" ); } +bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsItemNumber( int aCol ) +{ + wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), false ); + return m_cols[aCol].m_fieldName == wxS( "Item Number" ); +} + bool FIELDS_EDITOR_GRID_DATA_MODEL::cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup, @@ -284,6 +292,13 @@ void FIELDS_EDITOR_GRID_DATA_MODEL::Sort() return cmp( lhs, rhs, this, m_sortColumn, m_sortAscending ); } ); + // Time to renumber the item numbers + int itemNumber = 1; + for( DATA_MODEL_ROW& row : m_rows ) + { + row.m_ItemNumber = itemNumber++; + } + ExpandAfterSort(); } diff --git a/eeschema/fields_data_model.h b/eeschema/fields_data_model.h index b8b765b090..2efdbe8b18 100644 --- a/eeschema/fields_data_model.h +++ b/eeschema/fields_data_model.h @@ -32,6 +32,7 @@ struct DATA_MODEL_ROW m_Flag = aType; } + int m_ItemNumber; GROUP_TYPE m_Flag; std::vector m_Refs; }; @@ -121,6 +122,7 @@ public: bool ColIsReference( int aCol ); bool ColIsQuantity( int aCol ); + bool ColIsItemNumber( int aCol ); void SetSorting( int aCol, bool ascending ) {