Clean up some of the PDF hypertext stuff, and add opening HTTP[S] urls.

Fixes https://gitlab.com/kicad/code/kicad/issues/12153
This commit is contained in:
Jeff Young 2022-09-06 11:18:09 +01:00
parent 59dc6c8e45
commit b80824951e
4 changed files with 65 additions and 28 deletions

View File

@ -955,52 +955,70 @@ bool PDF_PLOTTER::EndPlot()
{
const BOX2D& box = menuPair.first;
const std::vector<wxString>& urls = menuPair.second;
// We currently only support menu links for internal pages and property lists.
// This vector holds the menu titles and (optional) page numbers.
std::vector<std::pair<wxString, int>> menuItems;
wxString js = wxT( "var aParams = [ " );
for( const wxString& url : urls )
{
wxString pageNumber;
if( url.StartsWith( "!" ) )
{
menuItems.push_back( { url.AfterFirst( '!' ), -1 } );
}
else if( EDA_TEXT::IsGotoPageHref( url, &pageNumber ) )
{
for( size_t ii = 0; ii < m_pageNumbers.size(); ++ii )
{
if( m_pageNumbers[ii] == pageNumber )
menuItems.push_back( { pageNumber, ii } );
}
}
}
wxString property = url.AfterFirst( '!' );
wxString js = wxT( "var aParams = [ " );
if( property.Find( "http:" ) >= 0 )
{
wxString href = property.substr( property.Find( "http:" ) );
for( const std::pair<wxString, int>& menuItem : menuItems )
js += wxString::Format( wxT( "{ cName: '%s', cReturn: '%s' }, " ),
EscapeString( property, CTX_JS_STR ),
EscapeString( href, CTX_JS_STR ) );
}
else if( property.Find( "https:" ) >= 0 )
{
if( menuItem.second < 0 )
{
js += wxString::Format( wxT( "{ cName: '%s', cReturn: null }, " ),
EscapeString( menuItem.first, CTX_JS_STR ) );
wxString href = property.substr( property.Find( "https:" ) );
js += wxString::Format( wxT( "{ cName: '%s', cReturn: '%s' }, " ),
EscapeString( property, CTX_JS_STR ),
EscapeString( href, CTX_JS_STR ) );
}
else
{
wxString menuText = wxString::Format( _( "Show Page %s" ), menuItem.first );
js += wxString::Format( wxT( "{ cName: '%s', cReturn: null }, " ),
EscapeString( property, CTX_JS_STR ) );
}
}
else if( url.StartsWith( "#" ) )
{
wxString pageNumber = url.AfterFirst( '#' );
js += wxString::Format( wxT( "{ cName: '%s', cReturn: '%d' }, " ),
for( size_t ii = 0; ii < m_pageNumbers.size(); ++ii )
{
if( m_pageNumbers[ii] == pageNumber )
{
wxString menuText = wxString::Format( _( "Show Page %s" ), pageNumber );
js += wxString::Format( wxT( "{ cName: '%s', cReturn: '#%d' }, " ),
EscapeString( menuText, CTX_JS_STR ),
menuItem.second );
static_cast<int>( ii ) );
break;
}
}
}
else if( url.StartsWith( "http:" ) || url.StartsWith( "https:" ) )
{
wxString menuText = wxString::Format( _( "Open %s" ), url );
js += wxString::Format( wxT( "{ cName: '%s', cReturn: '%s' }, " ),
EscapeString( menuText, CTX_JS_STR ),
EscapeString( url, CTX_JS_STR ) );
}
}
js += wxT( "]; " );
js += wxT( "var cChoice = app.popUpMenuEx.apply\\( app, aParams \\); " );
js += wxT( "if\\( cChoice != null \\) this.pageNum = cChoice; " );
js += wxT( "if\\( cChoice != null && cChoice.startsWith\\( '#' \\) \\)"
" this.pageNum = cChoice.slice\\( 1 \\); " );
js += wxT( "else if\\( cChoice != null && cChoice.startsWith\\( 'http' \\) \\)"
" app.launchURL\\( cChoice \\);" );
startPdfObject( menuHandle );

View File

@ -983,13 +983,16 @@ void SCH_FIELD::Plot( PLOTTER* aPlotter, bool aBackground ) const
SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( m_parent );
std::vector<std::pair<wxString, wxString>> pages;
std::vector<wxString> pageHrefs;
BOX2I bbox = GetBoundingBox();
label->GetIntersheetRefs( &pages );
for( const std::pair<wxString, wxString>& page : pages )
pageHrefs.push_back( wxT( "#" ) + page.first );
aPlotter->HyperlinkMenu( GetBoundingBox(), pageHrefs );
bbox.Offset( label->GetSchematicTextOffset( settings ) );
aPlotter->HyperlinkMenu( bbox, pageHrefs );
}
}

View File

@ -882,6 +882,7 @@ void SCH_LABEL_BASE::Plot( PLOTTER* aPlotter, bool aBackground ) const
static std::vector<VECTOR2I> s_poly;
RENDER_SETTINGS* settings = aPlotter->RenderSettings();
SCHEMATIC* schematic = Schematic();
SCH_CONNECTION* connection = Connection();
int layer = ( connection && connection->IsBus() ) ? LAYER_BUS : m_layer;
COLOR4D color = settings->GetLayerColor( layer );
@ -917,6 +918,21 @@ void SCH_LABEL_BASE::Plot( PLOTTER* aPlotter, bool aBackground ) const
{
std::vector<wxString> properties;
if( schematic && connection )
{
auto& netSettings = schematic->Prj().GetProjectFile().m_NetSettings;
wxString netName = connection->Name();
wxString className = netSettings->GetEffectiveNetClass( netName )->GetName();
properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
_( "Net" ),
UnescapeString( netName ) ) );
properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
_( "Resolved netclass" ),
UnescapeString( className ) ) );
}
for( const SCH_FIELD& field : GetFields() )
{
properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
@ -924,7 +940,7 @@ void SCH_LABEL_BASE::Plot( PLOTTER* aPlotter, bool aBackground ) const
field.GetShownText() ) );
}
aPlotter->HyperlinkMenu( GetBoundingBox(), properties );
aPlotter->HyperlinkMenu( GetBodyBoundingBox(), properties );
}
}

View File

@ -858,7 +858,7 @@ void SCH_LINE::Plot( PLOTTER* aPlotter, bool aBackground ) const
UnescapeString( netName ) ) );
properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
_( "Net class" ),
_( "Resolved netclass" ),
UnescapeString( className ) ) );
}
}