diff --git a/kicad/pcm/dialogs/panel_package.cpp b/kicad/pcm/dialogs/panel_package.cpp index ad16a91ca7..84df84c193 100644 --- a/kicad/pcm/dialogs/panel_package.cpp +++ b/kicad/pcm/dialogs/panel_package.cpp @@ -111,30 +111,14 @@ void PANEL_PACKAGE::SetState( PCM_PACKAGE_STATE aState ) void PANEL_PACKAGE::OnButtonClicked( wxCommandEvent& event ) { - // Versions are already presorted in descending order if( m_data.state == PPS_AVAILABLE ) { - // Find last stable compatible version - auto ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(), - []( const PACKAGE_VERSION& ver ) - { - return ver.compatible && ver.status == PVS_STABLE; - } ); + wxString version = GetPreferredVersion(); - // If not found then find any compatible version - if( ver_it == m_data.package.versions.end() ) - { - ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(), - []( const PACKAGE_VERSION& ver ) - { - return ver.compatible; - } ); - } + if( version.IsEmpty() ) + return; - if( ver_it == m_data.package.versions.end() ) - return; // Shouldn't happen - - m_actionCallback( m_data, PPA_INSTALL, ver_it->version ); + m_actionCallback( m_data, PPA_INSTALL, version ); } else { @@ -177,3 +161,31 @@ void PANEL_PACKAGE::SetSelected( bool aSelected ) m_selected = aSelected; Refresh(); } + + +wxString PANEL_PACKAGE::GetPreferredVersion() const +{ + // Versions are already presorted in descending order + + // Find last stable compatible version + auto ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(), + []( const PACKAGE_VERSION& ver ) + { + return ver.compatible && ver.status == PVS_STABLE; + } ); + + // If not found then find any compatible version + if( ver_it == m_data.package.versions.end() ) + { + ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(), + []( const PACKAGE_VERSION& ver ) + { + return ver.compatible; + } ); + } + + if( ver_it == m_data.package.versions.end() ) + return wxEmptyString; // Shouldn't happen + + return ver_it->version; +} diff --git a/kicad/pcm/dialogs/panel_package.h b/kicad/pcm/dialogs/panel_package.h index 3f7cd8f124..e320622e4b 100644 --- a/kicad/pcm/dialogs/panel_package.h +++ b/kicad/pcm/dialogs/panel_package.h @@ -75,6 +75,9 @@ public: void OnSize( wxSizeEvent& event ) override; + ///< Get preferred version. If criteria are not met, return wxEmptyString + wxString GetPreferredVersion() const; + const PACKAGE_VIEW_DATA& GetPackageData() const { return m_data; }; private: diff --git a/kicad/pcm/dialogs/panel_packages_view.cpp b/kicad/pcm/dialogs/panel_packages_view.cpp index 0fce0f8bdf..400ec1f61a 100644 --- a/kicad/pcm/dialogs/panel_packages_view.cpp +++ b/kicad/pcm/dialogs/panel_packages_view.cpp @@ -318,7 +318,27 @@ void PANEL_PACKAGES_VIEW::setPackageDetails( const PACKAGE_VIEW_DATA& aPackageDa } if( m_gridVersions->GetNumberRows() >= 1 ) - m_gridVersions->SelectRow( 0 ); + { + wxString version = m_currentSelected->GetPreferredVersion(); + + if( !version.IsEmpty() ) + { + for( int i = 0; i < m_gridVersions->GetNumberRows(); i++ ) + { + if( m_gridVersions->GetCellValue( i, COL_VERSION ) == version ) + { + std::cout << "auto select row: " << i << std::endl; + m_gridVersions->SelectRow( i ); + break; + } + } + } + else + { + // Fall back to first row. + m_gridVersions->SelectRow( 0 ); + } + } m_gridVersions->Thaw();