diff --git a/eeschema/sch_sheet.cpp b/eeschema/sch_sheet.cpp index 451ee64e2e..d197b4d22f 100644 --- a/eeschema/sch_sheet.cpp +++ b/eeschema/sch_sheet.cpp @@ -1166,7 +1166,24 @@ void SCH_SHEET::Plot( PLOTTER* aPlotter ) } -unsigned SCH_SHEET::GetSheets( std::vector& aSheetList ) const +SCH_SHEET* SCH_SHEET::FindSheetByName( const wxString& aSheetName ) +{ + std::vector< const SCH_SHEET* > sheets; + + GetSheets( sheets ); + + for( unsigned i = 0; i < sheets.size(); i++ ) + { + if( sheets[i]->GetName().CmpNoCase( aSheetName ) == 0 ) + return const_cast< SCH_SHEET*>( sheets[i] ); + } + + return NULL; +} + + +unsigned SCH_SHEET::GetSheets( std::vector< const SCH_SHEET* >& aSheetList, + bool aSortByPath ) const { // Sheet pointers must be unique. wxASSERT( find( aSheetList.begin(), aSheetList.end(), this ) == aSheetList.end() ); @@ -1183,6 +1200,9 @@ unsigned SCH_SHEET::GetSheets( std::vector& aSheetList ) const item = item->Next(); } + if( aSortByPath ) + std::sort( aSheetList.begin(), aSheetList.end(), SortByPath() ); + return aSheetList.size(); } @@ -1225,7 +1245,7 @@ const SCH_SHEET* SCH_SHEET::GetRootSheet() const } -void SCH_SHEET::GetPath( SCH_CONST_SHEETS& aSheetPath ) const +void SCH_SHEET::GetPath( std::vector< const SCH_SHEET* >& aSheetPath ) const { aSheetPath.insert( aSheetPath.begin(), const_cast( this ) ); @@ -1579,7 +1599,7 @@ int SCH_SHEET::operator-( const SCH_SHEET& aRhs ) const if( this == &aRhs ) return 0; - SCH_CONST_SHEETS lhsPath, rhsPath; + std::vector< const SCH_SHEET* > lhsPath, rhsPath; GetPath( lhsPath ); aRhs.GetPath( rhsPath ); diff --git a/eeschema/sch_sheet.h b/eeschema/sch_sheet.h index 3989112115..5e11eb6dab 100644 --- a/eeschema/sch_sheet.h +++ b/eeschema/sch_sheet.h @@ -577,6 +577,25 @@ public: */ bool operator<( const SCH_SHEET& aRhs ) const; + /** + * Structure SortByPath + * + * tests if \a aLhs is less than \a aRhs. + * + * @param aLhs is the left hand side reference to a #SCH_SHEET for comparison. + * @param aRhs is the right hand side reference to a #SCH_SHEET for comparison. + * @return true if \a aLhs is less than \a aRhs otherwise false. + */ + struct SortByPath + { + bool operator()( const SCH_SHEET* aLhs, const SCH_SHEET* aRhs ) + { + wxCHECK( aLhs != NULL && aRhs != NULL, false ); + + return *aLhs < *aRhs; + } + }; + int operator-( const SCH_SHEET& aRhs ) const; wxPoint GetPosition() const { return m_pos; } @@ -591,15 +610,33 @@ public: EDA_ITEM* Clone() const; + /** + * Function FindSheetByName + * + * searches this #SCH_SHEET and all of it's sub-sheets for a sheet named \a aSheetName. + * + * @param aSheetName is the name of the sheet to find. + * @return a pointer to the sheet named \a aSheetName if found or NULL if not found. + */ + SCH_SHEET* FindSheetByName( const wxString& aSheetName ); + /** * Function GetSheets * - * add the point to #SCH_SHEET and all of it's sub-sheets to \a aSheetList. + * add the pointers to the #SCH_SHEET and all of it's sub-sheets to \a aSheetList. + * + * By default no sorting is performed and the #SCH_SHEET pointers are add to the list + * in the order they were loaded when the schematic was parse. When \a aSortByPath is + * true, the list is sorted using the < operator which sort by path length then path + * time stamps. This has the same sorting effect as the old SCH_SHEET_PATH::Cmp() + * function. * * @param aSheetList is a reference to a set containing the #SCH_SHEET pointers. + * @param aSortByPath true to sort by path. False for load order. * @return the number of #SCH_SHEET object pointers in \a aSheetList. */ - unsigned GetSheets( std::vector& aSheetList ) const; + unsigned GetSheets( std::vector< const SCH_SHEET* >& aSheetList, + bool aSortByPath = false ) const; /** * Function GetSheetPaths @@ -647,9 +684,9 @@ public: * recurses up the parent branch up to the root sheet adding a pointer for each * parent sheet to \a aSheetPath. * - * @param aSheetPath is a refernce to an #SCH_SHEETS object to populate. + * @param aSheetPath is a refernce to an #SCH_SHEET object to populate. */ - void GetPath( std::vector& aSheetPath ) const; + void GetPath( std::vector< const SCH_SHEET* >& aSheetPath ) const; /** * Function GetPath @@ -797,9 +834,4 @@ protected: }; -typedef std::vector< SCH_SHEET* > SCH_SHEETS; -typedef std::vector< const SCH_SHEET* > SCH_CONST_SHEETS; -typedef SCH_SHEETS::iterator SCH_SHEETS_ITER; -typedef SCH_SHEETS::const_iterator SCH_SHEETS_CITER; - #endif /* SCH_SHEEET_H */ diff --git a/eeschema/sch_sheet_path.cpp b/eeschema/sch_sheet_path.cpp index f457d1e1f0..7723e5f4f1 100644 --- a/eeschema/sch_sheet_path.cpp +++ b/eeschema/sch_sheet_path.cpp @@ -2,8 +2,8 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com - * Copyright (C) 2011 Wayne Stambaugh - * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2011-2015 Wayne Stambaugh + * Copyright (C) 1992-2015 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 @@ -294,30 +294,6 @@ bool SCH_SHEET_PATH::operator==( const SCH_SHEET_PATH& d1 ) const } -int SCH_SHEET_PATH::FindSheet( const wxString& aFileName ) const -{ - for( unsigned i = 0; i < m_numSheets; i++ ) - { - if( m_sheets[i]->GetFileName().CmpNoCase( aFileName ) == 0 ) - return (int)i; - } - - return SHEET_NOT_FOUND; -} - - -SCH_SHEET* SCH_SHEET_PATH::FindSheetByName( const wxString& aSheetName ) -{ - for( unsigned i = 0; i < m_numSheets; i++ ) - { - if( m_sheets[i]->GetName().CmpNoCase( aSheetName ) == 0 ) - return m_sheets[i]; - } - - return NULL; -} - - /********************************************************************/ /* Class SCH_SHEET_LIST to handle the list of Sheets in a hierarchy */ /********************************************************************/ @@ -535,17 +511,3 @@ SCH_ITEM* SCH_SHEET_LIST::FindPreviousItem( KICAD_T aType, SCH_SHEET_PATH** aShe return NULL; } - - -SCH_SHEET* SCH_SHEET_LIST::FindSheetByName( const wxString& aSheetName ) -{ - for( int i = 0; i < m_count; i++ ) - { - SCH_SHEET* sheet = m_list[i].FindSheetByName( aSheetName ); - - if( sheet ) - return sheet; - } - - return NULL; -} diff --git a/eeschema/sch_sheet_path.h b/eeschema/sch_sheet_path.h index 266684ca53..a1de4fb89c 100644 --- a/eeschema/sch_sheet_path.h +++ b/eeschema/sch_sheet_path.h @@ -220,18 +220,6 @@ public: */ SCH_ITEM* FindPreviousItem( KICAD_T aType, SCH_ITEM* aLastItem = NULL, bool aWrap = false ) const; - int FindSheet( const wxString& aFileName ) const; - - /** - * Function FindSheetByName - * - * searches the #SCH_SHEET_PATH for a sheet named \a aSheetName. - * - * @param aSheetName is the name of the sheet to find. - * @return a pointer to the sheet named \a aSheetName if found or NULL if not found. - */ - SCH_SHEET* FindSheetByName( const wxString& aSheetName ); - SCH_SHEET_PATH& operator=( const SCH_SHEET_PATH& d1 ); bool operator==( const SCH_SHEET_PATH& d1 ) const; @@ -375,16 +363,6 @@ public: SCH_ITEM* FindPreviousItem( KICAD_T aType, SCH_SHEET_PATH** aSheetFound = NULL, SCH_ITEM* aLastItem = NULL, bool aWrap = true ); - /** - * Function FindSheetByName - * - * searches the entire #SCH_SHEET_LIST for a sheet named \a aSheetName. - * - * @param aSheetName is the name of the sheet to find. - * @return a pointer to the sheet named \a aSheetName if found or NULL if not found. - */ - SCH_SHEET* FindSheetByName( const wxString& aSheetName ); - private: /** diff --git a/eeschema/sheet.cpp b/eeschema/sheet.cpp index 0fb98e958a..bd1c935759 100644 --- a/eeschema/sheet.cpp +++ b/eeschema/sheet.cpp @@ -86,7 +86,7 @@ bool SCH_EDIT_FRAME::EditSheet( SCH_SHEET* aSheet, SCH_SHEET* aHierarchy ) } // Duplicate sheet names are not valid. - const SCH_SHEET* sheet = hierarchy.FindSheetByName( dlg.GetSheetName() ); + const SCH_SHEET* sheet = g_RootSheet->FindSheetByName( dlg.GetSheetName() ); if( sheet && (sheet != aSheet) ) {