From da1a89fc7e23c93243c671a7a2341e617330cc10 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sun, 3 May 2020 23:20:31 +0100 Subject: [PATCH] Allow users to control the order of sheets through sheet fields. Fixes https://gitlab.com/kicad/code/kicad/issues/2394 --- eeschema/dialogs/dialog_sch_sheet_props.cpp | 6 +++- eeschema/sch_screen.cpp | 32 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/eeschema/dialogs/dialog_sch_sheet_props.cpp b/eeschema/dialogs/dialog_sch_sheet_props.cpp index 3aadd5a01b..fcd19f9f2e 100644 --- a/eeschema/dialogs/dialog_sch_sheet_props.cpp +++ b/eeschema/dialogs/dialog_sch_sheet_props.cpp @@ -318,7 +318,11 @@ bool DIALOG_SCH_SHEET_PROPS::TransferDataFromWindow() m_sheet->SetBackgroundColor( m_backgroundSwatch->GetSwatchColor() ); m_frame->TestDanglingEnds(); - m_frame->RefreshItem( m_sheet ); + + // Refresh all sheets in case ordering changed. + for( SCH_ITEM* item : m_frame->GetScreen()->Items().OfType( SCH_SHEET_T ) ) + m_frame->RefreshItem( item ); + m_frame->OnModify(); return true; diff --git a/eeschema/sch_screen.cpp b/eeschema/sch_screen.cpp index 7d73d66bc3..9ddd82b6a8 100644 --- a/eeschema/sch_screen.cpp +++ b/eeschema/sch_screen.cpp @@ -777,10 +777,36 @@ void SCH_SCREEN::GetSheets( std::vector* aItems ) std::sort( aItems->begin(), aItems->end(), []( EDA_ITEM* a, EDA_ITEM* b ) -> bool { - if( a->GetPosition().x == b->GetPosition().x ) - return a->GetPosition().y < b->GetPosition().y; - else + long a_order = 0; + long b_order = 0; + + for( const SCH_FIELD& field : static_cast( a )->GetFields() ) + { + if( field.GetName().CmpNoCase( wxT( "Order" ) ) == 0 ) + { + field.GetText().ToLong( &a_order ); + break; + } + } + + for( const SCH_FIELD& field : static_cast( b )->GetFields() ) + { + if( field.GetName().CmpNoCase( wxT( "Order" ) ) == 0 ) + { + field.GetText().ToLong( &b_order ); + break; + } + } + + if( a_order == b_order ) + { + if( a->GetPosition().x == b->GetPosition().x ) + return a->GetPosition().y < b->GetPosition().y; + return a->GetPosition().x < b->GetPosition().x; + } + + return a_order < b_order; } ); }