Fix SortByPageNumbers: use Sheet Name when page numbers are equal

This commit is contained in:
Roberto Fernandez Bautista 2021-04-11 16:24:11 +01:00
parent 258946d8b1
commit 2c75911669
4 changed files with 45 additions and 21 deletions

View File

@ -166,10 +166,7 @@ int HIERARCHY_TREE::OnCompareItems( const wxTreeItemId& item1, const wxTreeItemI
SCH_SHEET_PATH* item1Path = &static_cast<TreeItemData*>( GetItemData( item1 ) )->m_SheetPath; SCH_SHEET_PATH* item1Path = &static_cast<TreeItemData*>( GetItemData( item1 ) )->m_SheetPath;
SCH_SHEET_PATH* item2Path = &static_cast<TreeItemData*>( GetItemData( item2 ) )->m_SheetPath; SCH_SHEET_PATH* item2Path = &static_cast<TreeItemData*>( GetItemData( item2 ) )->m_SheetPath;
wxString item1PageNo = item1Path->Last()->GetPageNumber( *item1Path ); return item1Path->ComparePageNumAndName( *item2Path );
wxString item2PageNo = item2Path->Last()->GetPageNumber( *item2Path );
return SCH_SHEET::ComparePageNum( item1PageNo, item2PageNo );
} }

View File

@ -1150,7 +1150,7 @@ void SCH_SHEET::SetPageNumber( const SCH_SHEET_PATH& aInstance, const wxString&
int SCH_SHEET::ComparePageNum( const wxString& aPageNumberA, const wxString aPageNumberB ) int SCH_SHEET::ComparePageNum( const wxString& aPageNumberA, const wxString aPageNumberB )
{ {
if( aPageNumberA == aPageNumberB ) if( aPageNumberA == aPageNumberB )
return 1; return 0; // A == B
// First sort numerically if the page numbers are integers // First sort numerically if the page numbers are integers
long pageA, pageB; long pageA, pageB;
@ -1159,29 +1159,25 @@ int SCH_SHEET::ComparePageNum( const wxString& aPageNumberA, const wxString aPag
if( isIntegerPageA && isIntegerPageB ) if( isIntegerPageA && isIntegerPageB )
{ {
if( pageA > pageB ) if( pageA < pageB )
return 1; return -1; //A < B
else if( pageA == pageB )
return 0;
else else
return -1; return 1; // A > B
} }
// Numerical page numbers always before strings // Numerical page numbers always before strings
if( isIntegerPageA ) if( isIntegerPageA )
return -1; return -1; //A < B
else if( isIntegerPageB ) else if( isIntegerPageB )
return 1; return 1; // A > B
// If not numeric, then sort as strings // If not numeric, then sort as strings
int result = aPageNumberA.Cmp( aPageNumberB ); int result = aPageNumberA.Cmp( aPageNumberB );
if( result == 0 ) if( result > 0 )
return 0; return 1; // A > B
else if( result > 0 )
return 1;
return -1; return -1; //A < B
} }

View File

@ -156,6 +156,27 @@ int SCH_SHEET_PATH::Cmp( const SCH_SHEET_PATH& aSheetPathToTest ) const
} }
int SCH_SHEET_PATH::ComparePageNumAndName( const SCH_SHEET_PATH& aSheetPathToTest ) const
{
wxString pageA = GetPageNumber();
wxString pageB = aSheetPathToTest.GetPageNumber();
int pageNumComp = SCH_SHEET::ComparePageNum( pageA, pageB );
if( pageNumComp == 0 )
{
wxString nameA = Last()->GetName();
wxString nameB = aSheetPathToTest.Last()->GetName();
return nameA.Cmp( nameB );
}
else
{
return pageNumComp;
}
}
SCH_SHEET* SCH_SHEET_PATH::Last() const SCH_SHEET* SCH_SHEET_PATH::Last() const
{ {
if( !empty() ) if( !empty() )
@ -499,10 +520,7 @@ void SCH_SHEET_LIST::SortByPageNumbers( bool aUpdateVirtualPageNums )
std::sort( begin(), end(), std::sort( begin(), end(),
[]( SCH_SHEET_PATH a, SCH_SHEET_PATH b ) -> bool []( SCH_SHEET_PATH a, SCH_SHEET_PATH b ) -> bool
{ {
wxString pageA = a.GetPageNumber(); return a.ComparePageNumAndName(b) < 0;
wxString pageB = b.GetPageNumber();
return SCH_SHEET::ComparePageNum( pageA, pageB ) < 0;
} ); } );
if( aUpdateVirtualPageNums ) if( aUpdateVirtualPageNums )

View File

@ -213,6 +213,16 @@ public:
*/ */
int Cmp( const SCH_SHEET_PATH& aSheetPathToTest ) const; int Cmp( const SCH_SHEET_PATH& aSheetPathToTest ) const;
/**
* Compare sheets by their page number and then by their name. Finally
* compare using #Cmp()
*
* @return -1 if aSheetPathToTest is greater than this (should appear later in the sort order)
* 0 if aSheetPathToTest is equal to this
* 1 if aSheetPathToTest is less than this (should appear earlier in the sort order)
*/
int ComparePageNumAndName( const SCH_SHEET_PATH& aSheetPathToTest ) const;
/** /**
* Return a pointer to the last #SCH_SHEET of the list. * Return a pointer to the last #SCH_SHEET of the list.
* *
@ -469,6 +479,9 @@ public:
/** /**
* Sort the list of sheets by page number. This should be called after #BuildSheetList * Sort the list of sheets by page number. This should be called after #BuildSheetList
* *
* If page numbers happen to be equal, then it compares the sheet names to ensure deterministic
* ordering.
*
* @param aUpdateVirtualPageNums If true, updates the virtual page numbers to match the new * @param aUpdateVirtualPageNums If true, updates the virtual page numbers to match the new
* ordering * ordering
*/ */