Add a simple hash for sch_tables to avoid too much reordering in the saved schematic file

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20918
This commit is contained in:
Marek Roszko 2025-05-23 21:30:51 -04:00
parent 6d1bf799ff
commit f7e851178e
3 changed files with 34 additions and 0 deletions

View File

@ -401,6 +401,15 @@ void SCH_IO_KICAD_SEXPR::Format( SCH_SHEET* aSheet )
if( a->Type() != b->Type() )
return a->Type() < b->Type();
// sorting fix specific for 9.0 to avoid format change
if( a->Type() == SCH_TABLE_T && b->Type() == SCH_TABLE_T )
{
const SCH_TABLE* aTable = static_cast<const SCH_TABLE*>( a );
const SCH_TABLE* bTable = static_cast<const SCH_TABLE*>( b );
return aTable->GetHash() < bTable->GetHash();
}
return a->m_Uuid < b->m_Uuid;
};

View File

@ -628,6 +628,25 @@ double SCH_TABLE::Similarity( const SCH_ITEM& aOther ) const
}
size_t SCH_TABLE::GetHash() const
{
size_t hash = 0;
// The goal here is to generate an hash that would change the least for
// mundane things like just moving the table.
hash_combine( hash, m_colCount );
hash_combine( hash, m_rowHeights.size() );
for( SCH_TABLECELL* cell : m_cells )
{
hash_combine( hash, cell->GetText() );
}
return hash;
}
static struct SCH_TABLE_DESC
{
SCH_TABLE_DESC()

View File

@ -245,6 +245,12 @@ public:
void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif
/**
* This function exists to keep tables sorted on save in 9.0 as the uuid
* is missing.
*/
size_t GetHash() const;
protected:
bool m_strokeExternal;
bool m_StrokeHeaderSeparator;