2023-10-12 13:36:28 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 <author>
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2023-02-28 18:37:04 +00:00
|
|
|
#include <wx/string.h>
|
|
|
|
#include <wx/debug.h>
|
|
|
|
#include <wx/grid.h>
|
2023-07-12 20:40:08 +00:00
|
|
|
#include <common.h>
|
2023-02-28 18:37:04 +00:00
|
|
|
#include <widgets/wx_grid.h>
|
|
|
|
#include <sch_reference_list.h>
|
2023-03-15 00:03:57 +00:00
|
|
|
#include <schematic_settings.h>
|
2023-02-28 18:37:04 +00:00
|
|
|
#include "string_utils.h"
|
|
|
|
|
|
|
|
#include "fields_data_model.h"
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-08-01 14:13:56 +00:00
|
|
|
const wxString FIELDS_EDITOR_GRID_DATA_MODEL::QUANTITY_VARIABLE = wxS( "${QUANTITY}" );
|
|
|
|
const wxString FIELDS_EDITOR_GRID_DATA_MODEL::ITEM_NUMBER_VARIABLE = wxS( "${ITEM_NUMBER}" );
|
2023-03-09 15:54:57 +00:00
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::AddColumn( const wxString& aFieldName, const wxString& aLabel,
|
|
|
|
bool aAddedByUser )
|
|
|
|
{
|
2023-03-15 00:03:57 +00:00
|
|
|
// Don't add a field twice
|
|
|
|
if( GetFieldNameCol( aFieldName ) != -1 )
|
|
|
|
return;
|
|
|
|
|
2023-04-03 17:12:13 +00:00
|
|
|
m_cols.push_back( { aFieldName, aLabel, aAddedByUser, false, false } );
|
2023-02-28 18:37:04 +00:00
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
|
2023-05-03 23:01:14 +00:00
|
|
|
if( SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol() )
|
2023-08-14 14:36:18 +00:00
|
|
|
updateDataStoreSymbolField( *symbol, aFieldName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::updateDataStoreSymbolField( const SCH_SYMBOL& aSymbol,
|
|
|
|
const wxString& aFieldName )
|
|
|
|
{
|
|
|
|
if( const SCH_FIELD* field = aSymbol.GetFieldByName( aFieldName ) )
|
|
|
|
m_dataStore[aSymbol.m_Uuid][aFieldName] = field->GetText();
|
|
|
|
else if( isAttribute( aFieldName ) )
|
|
|
|
m_dataStore[aSymbol.m_Uuid][aFieldName] = getAttributeValue( aSymbol, aFieldName );
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-08-14 14:36:18 +00:00
|
|
|
// Handle fields with variables as names that are not present in the symbol
|
|
|
|
// by giving them the correct value
|
|
|
|
else if( IsTextVar( aFieldName ) )
|
|
|
|
m_dataStore[aSymbol.m_Uuid][aFieldName] = aFieldName;
|
|
|
|
else
|
|
|
|
m_dataStore[aSymbol.m_Uuid][aFieldName] = wxEmptyString;
|
2023-02-28 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::RemoveColumn( int aCol )
|
|
|
|
{
|
|
|
|
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
|
|
|
|
{
|
2023-05-03 23:01:14 +00:00
|
|
|
if( SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol() )
|
|
|
|
m_dataStore[symbol->m_Uuid].erase( m_cols[aCol].m_fieldName );
|
2023-02-28 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_cols.erase( m_cols.begin() + aCol );
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::RenameColumn( int aCol, const wxString& 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( m_cols[aCol].m_fieldName );
|
|
|
|
node.key() = newName;
|
|
|
|
m_dataStore[symbol->m_Uuid].insert( std::move( node ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cols[aCol].m_fieldName = newName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FIELDS_EDITOR_GRID_DATA_MODEL::GetFieldNameCol( wxString aFieldName )
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < m_cols.size(); i++ )
|
|
|
|
{
|
|
|
|
if( m_cols[i].m_fieldName == aFieldName )
|
|
|
|
return (int) i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-27 20:48:37 +00:00
|
|
|
const std::vector<BOM_FIELD> FIELDS_EDITOR_GRID_DATA_MODEL::GetFieldsOrdered()
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
2023-03-27 20:48:37 +00:00
|
|
|
std::vector<BOM_FIELD> fields;
|
2023-02-28 18:37:04 +00:00
|
|
|
|
2023-04-04 14:35:30 +00:00
|
|
|
for( const DATA_MODEL_COL& col : m_cols )
|
2023-04-03 17:12:13 +00:00
|
|
|
fields.push_back( { col.m_fieldName, col.m_label, col.m_show, col.m_group } );
|
2023-02-28 18:37:04 +00:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::SetFieldsOrder( const std::vector<wxString>& aNewOrder )
|
|
|
|
{
|
|
|
|
size_t foundCount = 0;
|
|
|
|
|
|
|
|
for( const wxString& newField : aNewOrder )
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < m_cols.size(); i++ )
|
|
|
|
{
|
|
|
|
if( m_cols[i].m_fieldName == newField )
|
|
|
|
{
|
|
|
|
std::swap( m_cols[foundCount], m_cols[i] );
|
|
|
|
foundCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( int aRow, int aCol )
|
|
|
|
{
|
|
|
|
if( ColIsReference( aCol ) )
|
|
|
|
{
|
|
|
|
// Poor-man's tree controls
|
|
|
|
if( m_rows[aRow].m_Flag == GROUP_COLLAPSED )
|
|
|
|
return wxT( "> " ) + GetValue( m_rows[aRow], aCol );
|
|
|
|
else if( m_rows[aRow].m_Flag == GROUP_EXPANDED )
|
|
|
|
return wxT( "v " ) + GetValue( m_rows[aRow], aCol );
|
|
|
|
else if( m_rows[aRow].m_Flag == CHILD_ITEM )
|
|
|
|
return wxT( " " ) + GetValue( m_rows[aRow], aCol );
|
|
|
|
else
|
|
|
|
return wxT( " " ) + GetValue( m_rows[aRow], aCol );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return GetValue( m_rows[aRow], aCol );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-03-03 13:43:40 +00:00
|
|
|
wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( const DATA_MODEL_ROW& group, int aCol,
|
2023-03-15 00:03:57 +00:00
|
|
|
const wxString& refDelimiter,
|
2023-03-30 14:17:09 +00:00
|
|
|
const wxString& refRangeDelimiter,
|
|
|
|
bool resolveVars )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
|
|
|
std::vector<SCH_REFERENCE> references;
|
|
|
|
wxString fieldValue;
|
|
|
|
|
|
|
|
for( const SCH_REFERENCE& ref : group.m_Refs )
|
|
|
|
{
|
2023-07-12 19:08:32 +00:00
|
|
|
if( ColIsReference( aCol ) || ColIsQuantity( aCol ) || ColIsItemNumber( aCol ) )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
|
|
|
references.push_back( ref );
|
|
|
|
}
|
|
|
|
else // Other columns are either a single value or ROW_MULTI_ITEMS
|
|
|
|
{
|
|
|
|
const KIID& symbolID = ref.GetSymbol()->m_Uuid;
|
|
|
|
|
|
|
|
if( !m_dataStore.count( symbolID )
|
|
|
|
|| !m_dataStore[symbolID].count( m_cols[aCol].m_fieldName ) )
|
|
|
|
{
|
|
|
|
return INDETERMINATE_STATE;
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:59:13 +00:00
|
|
|
wxString refFieldValue;
|
|
|
|
|
|
|
|
if( resolveVars )
|
2023-07-12 20:40:08 +00:00
|
|
|
refFieldValue = getFieldShownText( ref, m_cols[aCol].m_fieldName );
|
2023-05-08 14:59:13 +00:00
|
|
|
else
|
|
|
|
refFieldValue = m_dataStore[symbolID][m_cols[aCol].m_fieldName];
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
if( &ref == &group.m_Refs.front() )
|
2023-05-08 14:59:13 +00:00
|
|
|
fieldValue = refFieldValue;
|
|
|
|
else if( fieldValue != refFieldValue )
|
2023-02-28 18:37:04 +00:00
|
|
|
return INDETERMINATE_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 19:08:32 +00:00
|
|
|
if( ColIsReference( aCol ) || ColIsQuantity( aCol ) || ColIsItemNumber( aCol ) )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
|
|
|
// Remove duplicates (other units of multi-unit parts)
|
|
|
|
std::sort( references.begin(), references.end(),
|
|
|
|
[]( const SCH_REFERENCE& l, const SCH_REFERENCE& r ) -> bool
|
|
|
|
{
|
|
|
|
wxString l_ref( l.GetRef() << l.GetRefNumber() );
|
|
|
|
wxString r_ref( r.GetRef() << r.GetRefNumber() );
|
|
|
|
return StrNumCmp( l_ref, r_ref, true ) < 0;
|
|
|
|
} );
|
|
|
|
|
|
|
|
auto logicalEnd = std::unique( references.begin(), references.end(),
|
|
|
|
[]( const SCH_REFERENCE& l, const SCH_REFERENCE& r ) -> bool
|
|
|
|
{
|
|
|
|
// If unannotated then we can't tell what units belong together
|
|
|
|
// so we have to leave them all
|
|
|
|
if( l.GetRefNumber() == wxT( "?" ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString l_ref( l.GetRef() << l.GetRefNumber() );
|
|
|
|
wxString r_ref( r.GetRef() << r.GetRefNumber() );
|
|
|
|
return l_ref == r_ref;
|
|
|
|
} );
|
|
|
|
|
|
|
|
references.erase( logicalEnd, references.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ColIsReference( aCol ) )
|
2023-03-15 00:03:57 +00:00
|
|
|
fieldValue = SCH_REFERENCE_LIST::Shorthand( references, refDelimiter, refRangeDelimiter );
|
2023-02-28 18:37:04 +00:00
|
|
|
else if( ColIsQuantity( aCol ) )
|
|
|
|
fieldValue = wxString::Format( wxT( "%d" ), (int) references.size() );
|
2023-07-12 19:08:32 +00:00
|
|
|
else if( ColIsItemNumber( aCol ) && group.m_Flag != CHILD_ITEM )
|
|
|
|
fieldValue = wxString::Format( wxT( "%d" ), group.m_ItemNumber );
|
2023-02-28 18:37:04 +00:00
|
|
|
|
|
|
|
return fieldValue;
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::SetValue( int aRow, int aCol, const wxString& aValue )
|
|
|
|
{
|
2023-08-01 14:13:56 +00:00
|
|
|
wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), wxS( "Invalid column number" ) );
|
|
|
|
|
|
|
|
// Can't modify references or text variables column, e.g. ${QUANTITY}
|
2023-08-01 18:29:25 +00:00
|
|
|
if( ColIsReference( aCol )
|
|
|
|
|| ( IsTextVar( m_cols[aCol].m_fieldName ) && !ColIsAttribute( aCol ) ) )
|
|
|
|
{
|
2023-08-01 14:13:56 +00:00
|
|
|
return;
|
2023-08-01 18:29:25 +00:00
|
|
|
}
|
2023-02-28 18:37:04 +00:00
|
|
|
|
|
|
|
DATA_MODEL_ROW& rowGroup = m_rows[aRow];
|
|
|
|
|
|
|
|
for( const SCH_REFERENCE& ref : rowGroup.m_Refs )
|
|
|
|
m_dataStore[ref.GetSymbol()->m_Uuid][m_cols[aCol].m_fieldName] = aValue;
|
|
|
|
|
|
|
|
m_edited = true;
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-03-16 13:53:37 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsReference( int aCol )
|
|
|
|
{
|
|
|
|
wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), false );
|
2023-10-03 16:14:03 +00:00
|
|
|
return m_cols[aCol].m_fieldName == GetCanonicalFieldName( REFERENCE_FIELD );
|
2023-03-16 13:53:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-09-20 13:40:48 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsValue( int aCol )
|
|
|
|
{
|
|
|
|
wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), false );
|
2023-10-03 16:14:03 +00:00
|
|
|
return m_cols[aCol].m_fieldName == GetCanonicalFieldName( VALUE_FIELD );
|
2023-09-20 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-03-16 13:53:37 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsQuantity( int aCol )
|
|
|
|
{
|
|
|
|
wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), false );
|
2023-08-01 14:13:56 +00:00
|
|
|
return m_cols[aCol].m_fieldName == QUANTITY_VARIABLE;
|
2023-03-16 13:53:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-07-12 19:08:32 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsItemNumber( int aCol )
|
|
|
|
{
|
|
|
|
wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), false );
|
2023-08-01 14:13:56 +00:00
|
|
|
return m_cols[aCol].m_fieldName == ITEM_NUMBER_VARIABLE;
|
2023-07-12 19:08:32 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-08-01 18:29:25 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::ColIsAttribute( int aCol )
|
|
|
|
{
|
|
|
|
wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), false );
|
|
|
|
return isAttribute( m_cols[aCol].m_fieldName );
|
|
|
|
}
|
|
|
|
|
2023-03-16 13:53:37 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::cmp( const DATA_MODEL_ROW& lhGroup,
|
|
|
|
const DATA_MODEL_ROW& rhGroup,
|
|
|
|
FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol,
|
|
|
|
bool ascending )
|
|
|
|
{
|
|
|
|
// Empty rows always go to the bottom, whether ascending or descending
|
|
|
|
if( lhGroup.m_Refs.size() == 0 )
|
|
|
|
return true;
|
|
|
|
else if( rhGroup.m_Refs.size() == 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// N.B. To meet the iterator sort conditions, we cannot simply invert the truth
|
|
|
|
// to get the opposite sort. i.e. ~(a<b) != (a>b)
|
|
|
|
auto local_cmp =
|
|
|
|
[ ascending ]( const auto a, const auto b )
|
|
|
|
{
|
|
|
|
if( ascending )
|
|
|
|
return a < b;
|
|
|
|
else
|
|
|
|
return a > b;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Primary sort key is sortCol; secondary is always REFERENCE (column 0)
|
|
|
|
|
|
|
|
wxString lhs = dataModel->GetValue( (DATA_MODEL_ROW&) lhGroup, sortCol );
|
|
|
|
wxString rhs = dataModel->GetValue( (DATA_MODEL_ROW&) rhGroup, sortCol );
|
|
|
|
|
|
|
|
if( lhs == rhs || sortCol == REFERENCE_FIELD )
|
|
|
|
{
|
|
|
|
wxString lhRef = lhGroup.m_Refs[0].GetRef() + lhGroup.m_Refs[0].GetRefNumber();
|
|
|
|
wxString rhRef = rhGroup.m_Refs[0].GetRef() + rhGroup.m_Refs[0].GetRefNumber();
|
|
|
|
return local_cmp( StrNumCmp( lhRef, rhRef, true ), 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return local_cmp( ValueStringCompare( lhs, rhs ), 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::Sort()
|
|
|
|
{
|
2023-02-28 18:37:04 +00:00
|
|
|
CollapseForSort();
|
|
|
|
|
|
|
|
// We're going to sort the rows based on their first reference, so the first reference
|
|
|
|
// had better be the lowest one.
|
|
|
|
for( DATA_MODEL_ROW& row : m_rows )
|
|
|
|
{
|
|
|
|
std::sort( row.m_Refs.begin(), row.m_Refs.end(),
|
|
|
|
[]( const SCH_REFERENCE& lhs, const SCH_REFERENCE& rhs )
|
|
|
|
{
|
|
|
|
wxString lhs_ref( lhs.GetRef() << lhs.GetRefNumber() );
|
|
|
|
wxString rhs_ref( rhs.GetRef() << rhs.GetRefNumber() );
|
|
|
|
return StrNumCmp( lhs_ref, rhs_ref, true ) < 0;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort( m_rows.begin(), m_rows.end(),
|
|
|
|
[this]( const DATA_MODEL_ROW& lhs, const DATA_MODEL_ROW& rhs ) -> bool
|
|
|
|
{
|
|
|
|
return cmp( lhs, rhs, this, m_sortColumn, m_sortAscending );
|
|
|
|
} );
|
|
|
|
|
2023-07-12 19:08:32 +00:00
|
|
|
// Time to renumber the item numbers
|
|
|
|
int itemNumber = 1;
|
|
|
|
for( DATA_MODEL_ROW& row : m_rows )
|
|
|
|
{
|
|
|
|
row.m_ItemNumber = itemNumber++;
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
ExpandAfterSort();
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::unitMatch( const SCH_REFERENCE& lhRef,
|
|
|
|
const SCH_REFERENCE& rhRef )
|
|
|
|
{
|
|
|
|
// If items are unannotated then we can't tell if they're units of the same symbol or not
|
|
|
|
if( lhRef.GetRefNumber() == wxT( "?" ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ( lhRef.GetRef() == rhRef.GetRef() && lhRef.GetRefNumber() == rhRef.GetRefNumber() );
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::groupMatch( const SCH_REFERENCE& lhRef,
|
2023-03-01 15:06:00 +00:00
|
|
|
const SCH_REFERENCE& rhRef )
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
2023-10-03 16:14:03 +00:00
|
|
|
int refCol = GetFieldNameCol( GetCanonicalFieldName( REFERENCE_FIELD ) );
|
2023-02-28 18:37:04 +00:00
|
|
|
bool matchFound = false;
|
|
|
|
|
2023-04-04 14:35:30 +00:00
|
|
|
if( refCol == -1 )
|
|
|
|
return false;
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
// First check the reference column. This can be done directly out of the
|
|
|
|
// SCH_REFERENCEs as the references can't be edited in the grid.
|
2023-03-01 15:06:00 +00:00
|
|
|
if( m_cols[refCol].m_group )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
|
|
|
// if we're grouping by reference, then only the prefix must match
|
|
|
|
if( lhRef.GetRef() != rhRef.GetRef() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
matchFound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check all the other columns. This must be done out of the dataStore
|
|
|
|
// for the refresh button to work after editing.
|
2023-03-01 15:06:00 +00:00
|
|
|
for( size_t i = 0; i < m_cols.size(); ++i )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
2023-03-01 15:06:00 +00:00
|
|
|
//Handled already
|
|
|
|
if( (int) i == refCol )
|
2023-02-28 18:37:04 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
if( !m_cols[i].m_group )
|
|
|
|
continue;
|
|
|
|
|
2023-07-12 20:40:08 +00:00
|
|
|
if( getFieldShownText( lhRef, m_cols[i].m_fieldName )
|
|
|
|
!= getFieldShownText( rhRef, m_cols[i].m_fieldName ) )
|
2023-05-08 14:59:13 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
matchFound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchFound;
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
|
2023-07-12 20:40:08 +00:00
|
|
|
wxString FIELDS_EDITOR_GRID_DATA_MODEL::getFieldShownText( const SCH_REFERENCE& aRef,
|
|
|
|
const wxString& aFieldName )
|
|
|
|
{
|
|
|
|
SCH_FIELD* field = aRef.GetSymbol()->GetFieldByName( aFieldName );
|
|
|
|
|
|
|
|
if( field )
|
|
|
|
return field->GetShownText( &aRef.GetSheetPath(), false );
|
|
|
|
|
|
|
|
// Handle fields with variables as names that are not present in the symbol
|
|
|
|
// by giving them the correct value by resolving against the symbol
|
2023-08-01 14:13:56 +00:00
|
|
|
if( IsTextVar( aFieldName ) )
|
2023-07-12 20:40:08 +00:00
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
const SCH_SHEET_PATH& path = aRef.GetSheetPath();
|
|
|
|
|
|
|
|
std::function<bool( wxString* )> symbolResolver =
|
|
|
|
[&]( wxString* token ) -> bool
|
|
|
|
{
|
|
|
|
return aRef.GetSymbol()->ResolveTextVar( &path, token, depth + 1 );
|
|
|
|
};
|
|
|
|
|
|
|
|
return ExpandTextVars( aFieldName, &symbolResolver );
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-01 18:29:25 +00:00
|
|
|
bool FIELDS_EDITOR_GRID_DATA_MODEL::isAttribute( const wxString& aFieldName )
|
|
|
|
{
|
|
|
|
return aFieldName == wxS( "${DNP}" )
|
|
|
|
|| aFieldName == wxS( "${EXCLUDE_FROM_BOARD}" )
|
|
|
|
|| aFieldName == wxS( "${EXCLUDE_FROM_BOM}" )
|
|
|
|
|| aFieldName == wxS( "${EXCLUDE_FROM_SIM}" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-14 14:36:18 +00:00
|
|
|
wxString FIELDS_EDITOR_GRID_DATA_MODEL::getAttributeValue( const SCH_SYMBOL& aSymbol,
|
|
|
|
const wxString& aAttributeName )
|
2023-08-01 18:29:25 +00:00
|
|
|
{
|
|
|
|
if( aAttributeName == wxS( "${DNP}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
return aSymbol.GetDNP() ? wxS( "1" ) : wxS( "0" );
|
2023-08-01 18:29:25 +00:00
|
|
|
|
|
|
|
if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
return aSymbol.GetExcludedFromBoard() ? wxS( "1" ) : wxS( "0" );
|
2023-08-01 18:29:25 +00:00
|
|
|
|
|
|
|
if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
return aSymbol.GetExcludedFromBOM() ? wxS( "1" ) : wxS( "0" );
|
2023-08-01 18:29:25 +00:00
|
|
|
|
|
|
|
if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
return aSymbol.GetExcludedFromSim() ? wxS( "1" ) : wxS( "0" );
|
2023-08-01 18:29:25 +00:00
|
|
|
|
|
|
|
return wxS( "0" );
|
|
|
|
}
|
|
|
|
|
2023-08-14 14:36:18 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::setAttributeValue( SCH_SYMBOL& aSymbol,
|
|
|
|
const wxString& aAttributeName,
|
|
|
|
const wxString& aValue )
|
2023-08-01 18:29:25 +00:00
|
|
|
{
|
|
|
|
if( aAttributeName == wxS( "${DNP}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
aSymbol.SetDNP( aValue == wxS( "1" ) );
|
2023-08-01 18:29:25 +00:00
|
|
|
else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOARD}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
aSymbol.SetExcludedFromBoard( aValue == wxS( "1" ) );
|
2023-08-01 18:29:25 +00:00
|
|
|
else if( aAttributeName == wxS( "${EXCLUDE_FROM_BOM}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
aSymbol.SetExcludedFromBOM( aValue == wxS( "1" ) );
|
2023-08-01 18:29:25 +00:00
|
|
|
else if( aAttributeName == wxS( "${EXCLUDE_FROM_SIM}" ) )
|
2023-08-14 14:36:18 +00:00
|
|
|
aSymbol.SetExcludedFromSim( aValue == wxS( "1" ) );
|
2023-08-01 18:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-17 16:56:00 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::EnableRebuilds()
|
|
|
|
{
|
|
|
|
m_rebuildsEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::DisableRebuilds()
|
|
|
|
{
|
|
|
|
m_rebuildsEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::RebuildRows()
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
2023-06-17 16:56:00 +00:00
|
|
|
if( !m_rebuildsEnabled )
|
|
|
|
return;
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
if( GetView() )
|
|
|
|
{
|
|
|
|
// Commit any pending in-place edits before the row gets moved out from under
|
|
|
|
// the editor.
|
|
|
|
static_cast<WX_GRID*>( GetView() )->CommitPendingChanges( true );
|
|
|
|
|
|
|
|
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
|
|
|
|
GetView()->ProcessTableMessage( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_rows.clear();
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
|
|
|
|
{
|
|
|
|
SCH_REFERENCE ref = m_symbolsList[i];
|
|
|
|
|
2023-03-01 15:06:00 +00:00
|
|
|
if( !m_filter.IsEmpty() && !WildCompareString( m_filter, ref.GetFullRef(), false ) )
|
2023-02-28 18:37:04 +00:00
|
|
|
continue;
|
2023-03-30 14:17:09 +00:00
|
|
|
|
|
|
|
if( m_excludeDNP && ref.GetSymbol()->GetDNP() )
|
|
|
|
continue;
|
|
|
|
|
2023-06-25 02:03:06 +00:00
|
|
|
if( !m_includeExcluded && ref.GetSymbol()->GetExcludedFromBOM() )
|
2023-03-30 14:17:09 +00:00
|
|
|
continue;
|
2023-02-28 18:37:04 +00:00
|
|
|
|
2023-08-07 19:04:46 +00:00
|
|
|
// Check if the symbol if on the current sheet or, in the sheet path somewhere
|
|
|
|
// depending on scope
|
|
|
|
if( ( m_scope == SCOPE::SCOPE_SHEET && ref.GetSheetPath() != m_path )
|
|
|
|
|| ( m_scope == SCOPE::SCOPE_SHEET_RECURSIVE
|
|
|
|
&& !ref.GetSheetPath().IsContainedWithin( m_path ) ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
bool matchFound = false;
|
|
|
|
|
2023-06-17 16:56:00 +00:00
|
|
|
// Performance optimization for ungrouped case to skip the N^2 for loop
|
|
|
|
if( !m_groupingEnabled && !ref.IsMultiUnit() )
|
|
|
|
{
|
|
|
|
m_rows.emplace_back( DATA_MODEL_ROW( ref, GROUP_SINGLETON ) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
// See if we already have a row which this symbol fits into
|
|
|
|
for( DATA_MODEL_ROW& row : m_rows )
|
|
|
|
{
|
|
|
|
// all group members must have identical refs so just use the first one
|
|
|
|
SCH_REFERENCE rowRef = row.m_Refs[0];
|
|
|
|
|
|
|
|
if( unitMatch( ref, rowRef ) )
|
|
|
|
{
|
|
|
|
matchFound = true;
|
|
|
|
row.m_Refs.push_back( ref );
|
|
|
|
break;
|
|
|
|
}
|
2023-03-01 15:06:00 +00:00
|
|
|
else if( m_groupingEnabled && groupMatch( ref, rowRef ) )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
|
|
|
matchFound = true;
|
|
|
|
row.m_Refs.push_back( ref );
|
|
|
|
row.m_Flag = GROUP_COLLAPSED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !matchFound )
|
|
|
|
m_rows.emplace_back( DATA_MODEL_ROW( ref, GROUP_SINGLETON ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( GetView() )
|
|
|
|
{
|
|
|
|
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_rows.size() );
|
|
|
|
GetView()->ProcessTableMessage( msg );
|
|
|
|
}
|
2023-03-01 15:06:00 +00:00
|
|
|
|
|
|
|
Sort();
|
2023-02-28 18:37:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::ExpandRow( int aRow )
|
|
|
|
{
|
|
|
|
std::vector<DATA_MODEL_ROW> children;
|
|
|
|
|
|
|
|
for( SCH_REFERENCE& ref : m_rows[aRow].m_Refs )
|
|
|
|
{
|
|
|
|
bool matchFound = false;
|
|
|
|
|
|
|
|
// See if we already have a child group which this symbol fits into
|
|
|
|
for( DATA_MODEL_ROW& child : children )
|
|
|
|
{
|
|
|
|
// group members are by definition all matching, so just check
|
|
|
|
// against the first member
|
|
|
|
if( unitMatch( ref, child.m_Refs[0] ) )
|
|
|
|
{
|
|
|
|
matchFound = true;
|
|
|
|
child.m_Refs.push_back( ref );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !matchFound )
|
|
|
|
children.emplace_back( DATA_MODEL_ROW( ref, CHILD_ITEM ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( children.size() < 2 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::sort( children.begin(), children.end(),
|
|
|
|
[this]( const DATA_MODEL_ROW& lhs, const DATA_MODEL_ROW& rhs ) -> bool
|
|
|
|
{
|
|
|
|
return cmp( lhs, rhs, this, m_sortColumn, m_sortAscending );
|
|
|
|
} );
|
|
|
|
|
|
|
|
m_rows[aRow].m_Flag = GROUP_EXPANDED;
|
|
|
|
m_rows.insert( m_rows.begin() + aRow + 1, children.begin(), children.end() );
|
|
|
|
|
|
|
|
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, aRow, children.size() );
|
|
|
|
GetView()->ProcessTableMessage( msg );
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::CollapseRow( int aRow )
|
|
|
|
{
|
|
|
|
auto firstChild = m_rows.begin() + aRow + 1;
|
|
|
|
auto afterLastChild = firstChild;
|
|
|
|
int deleted = 0;
|
|
|
|
|
|
|
|
while( afterLastChild != m_rows.end() && afterLastChild->m_Flag == CHILD_ITEM )
|
|
|
|
{
|
|
|
|
deleted++;
|
|
|
|
afterLastChild++;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_rows[aRow].m_Flag = GROUP_COLLAPSED;
|
|
|
|
m_rows.erase( firstChild, afterLastChild );
|
|
|
|
|
|
|
|
wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow + 1, deleted );
|
|
|
|
GetView()->ProcessTableMessage( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::ExpandCollapseRow( int aRow )
|
|
|
|
{
|
|
|
|
DATA_MODEL_ROW& group = m_rows[aRow];
|
|
|
|
|
|
|
|
if( group.m_Flag == GROUP_COLLAPSED )
|
|
|
|
ExpandRow( aRow );
|
|
|
|
else if( group.m_Flag == GROUP_EXPANDED )
|
|
|
|
CollapseRow( aRow );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::CollapseForSort()
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < m_rows.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_rows[i].m_Flag == GROUP_EXPANDED )
|
|
|
|
{
|
|
|
|
CollapseRow( i );
|
|
|
|
m_rows[i].m_Flag = GROUP_COLLAPSED_DURING_SORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::ExpandAfterSort()
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < m_rows.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_rows[i].m_Flag == GROUP_COLLAPSED_DURING_SORT )
|
|
|
|
ExpandRow( i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::ApplyData(
|
|
|
|
std::function<void( SCH_SYMBOL&, SCH_SHEET_PATH& )> symbolChangeHandler )
|
2023-02-28 18:37:04 +00:00
|
|
|
{
|
|
|
|
for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i )
|
|
|
|
{
|
|
|
|
SCH_SYMBOL& symbol = *m_symbolsList[i].GetSymbol();
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
symbolChangeHandler( symbol, m_symbolsList[i].GetSheetPath() );
|
2023-02-28 18:37:04 +00:00
|
|
|
|
|
|
|
const std::map<wxString, wxString>& fieldStore = m_dataStore[symbol.m_Uuid];
|
|
|
|
|
|
|
|
for( const std::pair<wxString, wxString> srcData : fieldStore )
|
|
|
|
{
|
|
|
|
const wxString& srcName = srcData.first;
|
|
|
|
const wxString& srcValue = srcData.second;
|
2023-08-01 18:29:25 +00:00
|
|
|
|
|
|
|
// Attributes bypass the field logic, so handle them first
|
|
|
|
if( isAttribute( srcName ) )
|
|
|
|
{
|
2023-08-14 14:36:18 +00:00
|
|
|
setAttributeValue( *m_symbolsList[i].GetSymbol(), srcName, srcValue );
|
2023-08-01 18:29:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip special fields with variables as names (e.g. ${QUANTITY}),
|
|
|
|
// they can't be edited
|
|
|
|
if( IsTextVar( srcName ) )
|
|
|
|
continue;
|
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
SCH_FIELD* destField = symbol.FindField( srcName );
|
|
|
|
int col = GetFieldNameCol( srcName );
|
|
|
|
bool userAdded = ( col != -1 && m_cols[col].m_userAdded );
|
|
|
|
|
|
|
|
// Add a not existing field if it has a value for this symbol
|
|
|
|
bool createField = !destField && ( !srcValue.IsEmpty() || userAdded );
|
|
|
|
|
|
|
|
if( createField )
|
|
|
|
{
|
|
|
|
const VECTOR2I symbolPos = symbol.GetPosition();
|
|
|
|
destField = symbol.AddField( SCH_FIELD( symbolPos, -1, &symbol, srcName ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !destField )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( destField->GetId() == REFERENCE_FIELD )
|
|
|
|
{
|
|
|
|
// Reference is not editable from this dialog
|
|
|
|
}
|
|
|
|
else if( destField->GetId() == VALUE_FIELD )
|
|
|
|
{
|
|
|
|
// Value field cannot be empty
|
|
|
|
if( !srcValue.IsEmpty() )
|
|
|
|
symbol.SetValueFieldText( srcValue );
|
|
|
|
}
|
|
|
|
else if( destField->GetId() == FOOTPRINT_FIELD )
|
|
|
|
{
|
|
|
|
symbol.SetFootprintFieldText( srcValue );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
destField->SetText( srcValue );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int ii = symbol.GetFields().size() - 1; ii >= MANDATORY_FIELDS; ii-- )
|
|
|
|
{
|
|
|
|
if( fieldStore.count( symbol.GetFields()[ii].GetName() ) == 0 )
|
|
|
|
symbol.GetFields().erase( symbol.GetFields().begin() + ii );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_edited = false;
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:36:28 +00:00
|
|
|
|
2023-02-28 18:37:04 +00:00
|
|
|
int FIELDS_EDITOR_GRID_DATA_MODEL::GetDataWidth( int aCol )
|
|
|
|
{
|
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
if( ColIsReference( aCol ) )
|
|
|
|
{
|
|
|
|
for( int row = 0; row < GetNumberRows(); ++row )
|
|
|
|
width = std::max( width, KIUI::GetTextSize( GetValue( row, aCol ), GetView() ).x );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString fieldName = GetColFieldName( aCol ); // symbol fieldName or Qty string
|
|
|
|
|
|
|
|
for( unsigned symbolRef = 0; symbolRef < m_symbolsList.GetCount(); ++symbolRef )
|
|
|
|
{
|
|
|
|
const KIID& symbolID = m_symbolsList[symbolRef].GetSymbol()->m_Uuid;
|
|
|
|
wxString text = m_dataStore[symbolID][fieldName];
|
|
|
|
|
|
|
|
width = std::max( width, KIUI::GetTextSize( text, GetView() ).x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
2023-03-03 13:43:40 +00:00
|
|
|
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::ApplyBomPreset( const BOM_PRESET& aPreset )
|
|
|
|
{
|
|
|
|
// Hide and un-group everything by default
|
|
|
|
for( size_t i = 0; i < m_cols.size(); i++ )
|
|
|
|
{
|
|
|
|
SetShowColumn( i, false );
|
|
|
|
SetGroupColumn( i, false );
|
|
|
|
}
|
|
|
|
|
2023-03-27 20:48:37 +00:00
|
|
|
std::vector<wxString> order;
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
// Set columns that are present and shown
|
2023-03-27 20:48:37 +00:00
|
|
|
for( BOM_FIELD field : aPreset.fieldsOrdered )
|
2023-03-15 00:03:57 +00:00
|
|
|
{
|
2023-03-27 20:48:37 +00:00
|
|
|
order.emplace_back( field.name );
|
2023-03-15 00:03:57 +00:00
|
|
|
|
2023-03-27 20:48:37 +00:00
|
|
|
int col = GetFieldNameCol( field.name );
|
2023-03-15 00:03:57 +00:00
|
|
|
|
|
|
|
// Add any missing fields, if the user doesn't add any data
|
2023-08-01 14:13:56 +00:00
|
|
|
// they won't be saved to the symbols anyway
|
2023-03-15 00:03:57 +00:00
|
|
|
if( col == -1 )
|
2023-04-04 14:35:30 +00:00
|
|
|
{
|
2023-03-27 20:48:37 +00:00
|
|
|
AddColumn( field.name, field.label, true );
|
2023-04-04 14:35:30 +00:00
|
|
|
col = GetFieldNameCol( field.name );
|
|
|
|
}
|
2023-03-15 00:03:57 +00:00
|
|
|
else
|
2023-03-27 20:48:37 +00:00
|
|
|
SetColLabelValue( col, field.label );
|
2023-03-15 00:03:57 +00:00
|
|
|
|
2023-03-27 20:48:37 +00:00
|
|
|
SetGroupColumn( col, field.groupBy );
|
|
|
|
SetShowColumn( col, field.show );
|
2023-03-15 00:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set grouping columns
|
|
|
|
SetGroupingEnabled( aPreset.groupSymbols );
|
|
|
|
|
2023-03-27 20:48:37 +00:00
|
|
|
SetFieldsOrder( order );
|
2023-03-15 00:03:57 +00:00
|
|
|
|
|
|
|
// Set our sorting
|
|
|
|
int sortCol = GetFieldNameCol( aPreset.sortField );
|
2023-03-27 20:48:37 +00:00
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
if( sortCol != -1 )
|
|
|
|
SetSorting( sortCol, aPreset.sortAsc );
|
|
|
|
else
|
2023-10-03 16:14:03 +00:00
|
|
|
SetSorting( GetFieldNameCol( GetCanonicalFieldName( REFERENCE_FIELD ) ), aPreset.sortAsc );
|
2023-03-15 00:03:57 +00:00
|
|
|
|
|
|
|
SetFilter( aPreset.filterString );
|
2023-03-30 14:17:09 +00:00
|
|
|
SetExcludeDNP( aPreset.excludeDNP );
|
2023-03-15 00:03:57 +00:00
|
|
|
|
|
|
|
RebuildRows();
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:57:59 +00:00
|
|
|
|
|
|
|
BOM_PRESET FIELDS_EDITOR_GRID_DATA_MODEL::GetBomSettings()
|
|
|
|
{
|
|
|
|
BOM_PRESET current;
|
|
|
|
current.readOnly = false;
|
|
|
|
current.fieldsOrdered = GetFieldsOrdered();
|
|
|
|
current.sortField = GetColFieldName( GetSortCol() );
|
|
|
|
current.sortAsc = GetSortAsc();
|
|
|
|
current.filterString = GetFilter();
|
|
|
|
current.groupSymbols = GetGroupingEnabled();
|
|
|
|
current.excludeDNP = GetExcludeDNP();
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-09 15:54:57 +00:00
|
|
|
wxString FIELDS_EDITOR_GRID_DATA_MODEL::Export( const BOM_FMT_PRESET& settings )
|
2023-03-03 13:43:40 +00:00
|
|
|
{
|
|
|
|
wxString out;
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
if( m_cols.empty() )
|
|
|
|
return out;
|
|
|
|
|
2023-04-04 14:35:30 +00:00
|
|
|
int last_col = -1;
|
2023-03-03 13:43:40 +00:00
|
|
|
|
|
|
|
// Find the location for the line terminator
|
2023-04-04 14:35:30 +00:00
|
|
|
for( size_t col = 0; col < m_cols.size(); col++ )
|
2023-03-03 13:43:40 +00:00
|
|
|
{
|
|
|
|
if( m_cols[col].m_show )
|
2023-04-04 14:35:30 +00:00
|
|
|
last_col = (int) col;
|
2023-03-03 13:43:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 14:35:30 +00:00
|
|
|
// No shown columns
|
|
|
|
if( last_col == -1 )
|
|
|
|
return out;
|
|
|
|
|
2023-03-03 13:43:40 +00:00
|
|
|
auto formatField = [&]( wxString field, bool last ) -> wxString
|
|
|
|
{
|
2023-03-15 00:03:57 +00:00
|
|
|
if( !settings.keepLineBreaks )
|
2023-03-03 13:43:40 +00:00
|
|
|
{
|
|
|
|
field.Replace( wxS( "\r" ), wxS( "" ) );
|
|
|
|
field.Replace( wxS( "\n" ), wxS( "" ) );
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
if( !settings.keepTabs )
|
2023-03-03 13:43:40 +00:00
|
|
|
{
|
|
|
|
field.Replace( wxS( "\t" ), wxS( "" ) );
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:57 +00:00
|
|
|
if( !settings.stringDelimiter.IsEmpty() )
|
|
|
|
field.Replace( settings.stringDelimiter,
|
|
|
|
settings.stringDelimiter + settings.stringDelimiter );
|
2023-03-03 13:43:40 +00:00
|
|
|
|
2023-03-09 15:54:57 +00:00
|
|
|
return settings.stringDelimiter + field + settings.stringDelimiter
|
2023-09-27 19:46:23 +00:00
|
|
|
+ ( last ? wxString( wxS( "\n" ) ) : settings.fieldDelimiter );
|
2023-03-03 13:43:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Column names
|
|
|
|
for( size_t col = 0; col < m_cols.size(); col++ )
|
|
|
|
{
|
|
|
|
if( !m_cols[col].m_show )
|
|
|
|
continue;
|
|
|
|
|
2023-04-04 14:35:30 +00:00
|
|
|
out.Append( formatField( m_cols[col].m_label, col == (size_t) last_col ) );
|
2023-03-03 13:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Data rows
|
|
|
|
for( size_t row = 0; row < m_rows.size(); row++ )
|
|
|
|
{
|
|
|
|
// Don't output child rows
|
|
|
|
if( GetRowFlags( (int) row ) == CHILD_ITEM )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( size_t col = 0; col < m_cols.size(); col++ )
|
|
|
|
{
|
|
|
|
if( !m_cols[col].m_show )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the unanottated version of the field, e.g. no "> " or "v " by
|
2023-03-30 14:17:09 +00:00
|
|
|
out.Append( formatField( GetExportValue( (int) row, (int) col, settings.refDelimiter,
|
|
|
|
settings.refRangeDelimiter ),
|
2023-04-04 14:35:30 +00:00
|
|
|
col == (size_t) last_col ) );
|
2023-03-03 13:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2023-08-14 14:41:28 +00:00
|
|
|
|
|
|
|
|
2023-08-14 18:19:00 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::AddReferences( const SCH_REFERENCE_LIST& aRefs )
|
2023-08-14 14:41:28 +00:00
|
|
|
{
|
|
|
|
for( const SCH_REFERENCE& ref : aRefs )
|
2023-08-14 18:19:00 +00:00
|
|
|
{
|
2023-08-14 14:41:28 +00:00
|
|
|
if( !m_symbolsList.Contains( ref ) )
|
2023-08-14 18:19:00 +00:00
|
|
|
{
|
2023-08-14 14:41:28 +00:00
|
|
|
m_symbolsList.AddItem( ref );
|
2023-08-14 18:19:00 +00:00
|
|
|
|
|
|
|
// Update the fields of every reference
|
|
|
|
for( const SCH_FIELD& field : ref.GetSymbol()->GetFields() )
|
|
|
|
m_dataStore[ref.GetSymbol()->m_Uuid][field.GetCanonicalName()] = field.GetText();
|
|
|
|
}
|
|
|
|
}
|
2023-08-14 14:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::RemoveSymbol( const SCH_SYMBOL& aSymbol )
|
|
|
|
{
|
2023-08-14 18:19:00 +00:00
|
|
|
// The schematic event listener passes us the symbol after it has been removed,
|
2023-08-14 14:41:28 +00:00
|
|
|
// so we can't just work with a SCH_REFERENCE_LIST like the other handlers as the
|
|
|
|
// references are already gone. Instead we need to prune our list.
|
|
|
|
m_dataStore[aSymbol.m_Uuid].clear();
|
|
|
|
|
|
|
|
// Remove all refs that match this symbol using remove_if
|
|
|
|
m_symbolsList.erase( std::remove_if( m_symbolsList.begin(), m_symbolsList.end(),
|
2023-10-30 13:16:24 +00:00
|
|
|
[&aSymbol]( const SCH_REFERENCE& ref ) -> bool
|
2023-08-14 14:41:28 +00:00
|
|
|
{
|
|
|
|
return ref.GetSymbol()->m_Uuid == aSymbol.m_Uuid;
|
|
|
|
} ),
|
|
|
|
m_symbolsList.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-14 18:19:00 +00:00
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::RemoveReferences( const SCH_REFERENCE_LIST& aRefs )
|
2023-08-14 14:41:28 +00:00
|
|
|
{
|
2023-08-14 18:19:00 +00:00
|
|
|
for( const SCH_REFERENCE& ref : aRefs )
|
|
|
|
{
|
|
|
|
int index = m_symbolsList.FindRefByFullPath( ref.GetFullPath() );
|
2023-08-14 14:41:28 +00:00
|
|
|
|
2023-08-14 18:19:00 +00:00
|
|
|
if( index != -1 )
|
|
|
|
{
|
|
|
|
m_symbolsList.RemoveItem( index );
|
2023-08-14 14:41:28 +00:00
|
|
|
|
2023-08-14 18:19:00 +00:00
|
|
|
// If we're out of instances then remove the symbol, too
|
2023-12-28 23:25:47 +00:00
|
|
|
if( ref.GetSymbol()->GetInstances().empty() )
|
2023-08-14 18:19:00 +00:00
|
|
|
m_dataStore.erase( ref.GetSymbol()->m_Uuid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FIELDS_EDITOR_GRID_DATA_MODEL::UpdateReferences( const SCH_REFERENCE_LIST& aRefs )
|
|
|
|
{
|
2023-08-14 14:41:28 +00:00
|
|
|
for( const SCH_REFERENCE& ref : aRefs )
|
2023-08-14 18:19:00 +00:00
|
|
|
{
|
|
|
|
// Update the fields of every reference. Do this by iterating through the data model
|
2023-10-12 13:36:28 +00:00
|
|
|
// columns; we must have all fields in the symbol added to the data model at this point,
|
2023-08-14 18:19:00 +00:00
|
|
|
// and some of the data model columns may be variables that are not present in the symbol
|
|
|
|
for( const DATA_MODEL_COL& col : m_cols )
|
|
|
|
updateDataStoreSymbolField( *ref.GetSymbol(), col.m_fieldName );
|
|
|
|
|
2023-08-14 14:41:28 +00:00
|
|
|
if( !m_symbolsList.Contains( ref ) )
|
|
|
|
m_symbolsList.AddItem( ref );
|
2023-08-14 18:19:00 +00:00
|
|
|
}
|
2023-08-14 14:41:28 +00:00
|
|
|
}
|