Clean up updateSelection() megafunction

This commit is contained in:
Chris Pavlina 2017-02-19 09:00:02 -05:00
parent 3d88cc2a36
commit b4f4ff9353
4 changed files with 222 additions and 91 deletions

View File

@ -104,6 +104,7 @@ set( EESCHEMA_SRCS
find.cpp
getpart.cpp
component_tree_search_container.cpp
generate_alias_info.cpp
hierarch.cpp
highlight_connection.cpp
hotkeys.cpp

View File

@ -31,10 +31,10 @@
#include <class_library.h>
#include <component_tree_search_container.h>
#include <sch_base_frame.h>
#include <kicad_string.h>
#include <widgets/footprint_preview_panel.h>
#include <widgets/two_column_tree_list.h>
#include <template_fieldnames.h> // Field ID definitions
#include <template_fieldnames.h>
#include <generate_alias_info.h>
// Tree navigation helpers.
static wxTreeListItem GetPrevItem( const wxTreeListCtrl& tree, const wxTreeListItem& item );
@ -231,8 +231,6 @@ bool DIALOG_CHOOSE_COMPONENT::updateSelection()
m_componentView->Refresh();
m_componentDetails->SetPage( wxEmptyString );
if( selection == NULL )
{
if( m_footprintPreviewPanel )
@ -240,96 +238,12 @@ bool DIALOG_CHOOSE_COMPONENT::updateSelection()
m_footprintPreviewPanel->SetStatusText( wxEmptyString );
}
m_componentDetails->SetPage( wxEmptyString );
return false;
}
m_componentDetails->Freeze();
const wxString name = selection->GetName();
wxString description = selection->GetDescription();
if ( !name.empty() )
{
m_componentDetails->AppendToPage( "<b>" );
m_componentDetails->AppendToPage( EscapedHTML( name ) );
m_componentDetails->AppendToPage( "</b>" );
}
if( !selection->IsRoot() )
{
LIB_PART* root_part = selection->GetPart();
const wxString root_name( root_part ? root_part->GetName() : _( "Unknown" ) );
m_componentDetails->AppendToPage(
"<br><i>" + _( "Alias of " ) + EscapedHTML( root_name ) + "</i>" );
// For some reason descriptions are a property of aliases, even though
// only the root component's main LIB_ALIAS can actually have a description.
// If the description was empty, go through the alias list and find an alias
// that actually has one.
if( description.empty() )
{
for( size_t i = 0; i < root_part->GetAliasCount(); ++i )
{
LIB_ALIAS* alias = root_part->GetAlias( i );
if( !alias )
continue;
description = alias->GetDescription();
if( !description.empty() )
break;
}
}
}
if( !description.empty() )
{
m_componentDetails->AppendToPage( "<br>" );
m_componentDetails->AppendToPage( EscapedHTML( description ) );
}
wxString keywords = selection->GetKeyWords();
if( !keywords.empty() )
{
m_componentDetails->AppendToPage( "<br>" + _( "Keywords:" ) + " " );
m_componentDetails->AppendToPage( EscapedHTML( keywords ) );
}
m_componentDetails->AppendToPage( "<hr><table border=0>" );
LIB_FIELDS fields;
selection->GetPart()->GetFields( fields );
for( auto const & field: fields )
{
wxString name = field.GetName();
wxString text = field.GetFullText();
m_componentDetails->AppendToPage( "<tr><td><b>" + EscapedHTML( name ) + "</b></td>" );
m_componentDetails->AppendToPage( "<td>" );
if( field.GetId() == DATASHEET )
{
m_componentDetails->AppendToPage( "<a href=\"" + EscapedHTML( text ) + "\">" );
}
m_componentDetails->AppendToPage( EscapedHTML( text ) );
if( field.GetId() == DATASHEET )
{
m_componentDetails->AppendToPage( "</a>" );
}
m_componentDetails->AppendToPage( "</td></tr>" );
}
m_componentDetails->AppendToPage( "</table>" );
m_componentDetails->Thaw();
m_componentDetails->SetPage( GenerateAliasInfo( selection ) );
updateFootprint();
@ -508,6 +422,7 @@ static wxTreeListItem GetNextItem( const wxTreeListCtrl& tree, const wxTreeListI
return nextItem;
}
static wxTreeListItem GetPrevSibling( const wxTreeListCtrl& tree, const wxTreeListItem& item )
{
// Why wxTreeListCtrl has no GetPrevSibling when it does have GetNextSibling

View File

@ -0,0 +1,183 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
* Copyright (C) 2017 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 as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <generate_alias_info.h>
#include <kicad_string.h>
#include <template_fieldnames.h>
static const wxString DescriptionFormat =
"<b>__NAME__</b>"
"__ALIASOF__"
"__DESC__"
"__KEY__"
"<hr><table border=0>"
"__FIELDS__"
"</table>";
static const wxString AliasOfFormat = "<br><i>" + _( "Alias of " ) + "%s</i>";
static const wxString DescFormat = "<br>%s";
static const wxString KeywordsFormat = "<br>" + _( "Keywords:" ) + " %s";
static const wxString FieldFormat =
"<tr>"
" <td><b>__NAME__</b></td>"
" <td>__VALUE__</td>"
"</tr>";
static const wxString DatasheetLinkFormat = "<a href=\"__VALUE__\">__VALUE__</a>";
class ALIAS_INFO_GENERATOR
{
wxString m_html;
LIB_ALIAS const * m_part;
public:
ALIAS_INFO_GENERATOR( LIB_ALIAS const * aAlias )
: m_html( DescriptionFormat ),
m_part( aAlias )
{ }
/**
* Generate the HTML internally.
*/
void GenerateHtml()
{
SetHtmlName();
SetHtmlAliasOf();
SetHtmlDesc();
SetHtmlKeywords();
SetHtmlFieldTable();
}
/**
* Return the generated HTML.
*/
wxString GetHtml()
{
return m_html;
}
protected:
void SetHtmlName()
{
m_html.Replace( "__NAME__", EscapedHTML( m_part->GetName() ) );
}
void SetHtmlAliasOf()
{
if( m_part->IsRoot() )
{
m_html.Replace( "__ALIASOF__", wxEmptyString );
}
else
{
LIB_PART* root = m_part->GetPart();
const wxString root_name = ( root ? root->GetName() : _( "Unknown" ) );
m_html.Replace(
"__ALIASOF__", wxString::Format( AliasOfFormat, EscapedHTML( root_name ) ) );
}
}
void SetHtmlDesc()
{
wxString raw_desc;
if( m_part->IsRoot() )
{
raw_desc = m_part->GetDescription();
}
else
{
LIB_PART* root = m_part->GetPart();
for( size_t i = 0; i < root->GetAliasCount(); ++i )
{
LIB_ALIAS* alias = root->GetAlias( i );
if( alias && !alias->GetDescription().empty() )
{
raw_desc = alias->GetDescription();
break;
}
}
}
m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapedHTML( raw_desc ) ) );
}
void SetHtmlKeywords()
{
wxString keywords = m_part->GetKeyWords();
if( keywords.empty() )
m_html.Replace( "__KEY__", wxEmptyString );
else
m_html.Replace( "__KEY__",
wxString::Format( KeywordsFormat, EscapedHTML( keywords ) ) );
}
wxString GetHtmlFieldRow( LIB_FIELD const & aField )
{
wxString name = aField.GetName();
wxString text = aField.GetFullText();
wxString fieldhtml = FieldFormat;
fieldhtml.Replace( "__NAME__", EscapedHTML( name ) );
if( aField.GetId() == DATASHEET )
{
wxString datasheetlink = DatasheetLinkFormat;
datasheetlink.Replace( "__VALUE__", EscapedHTML( text ) );
fieldhtml.Replace( "__VALUE__", datasheetlink );
}
else
{
fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
}
return fieldhtml;
}
void SetHtmlFieldTable()
{
wxString fieldtable;
LIB_FIELDS fields;
m_part->GetPart()->GetFields( fields );
for( auto const & field: fields )
{
fieldtable += GetHtmlFieldRow( field );
}
m_html.Replace( "__FIELDS__", fieldtable );
}
};
wxString GenerateAliasInfo( LIB_ALIAS const * aAlias )
{
ALIAS_INFO_GENERATOR gen( aAlias );
gen.GenerateHtml();
return gen.GetHtml();
}

View File

@ -0,0 +1,32 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
* Copyright (C) 2017 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 as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GENERATE_ALIAS_INFO_H
#define GENERATE_ALIAS_INFO_H
#include <class_libentry.h>
/**
* Return an HTML page describing a LIB_ALIAS. This is suitable for inclusion
* in a wxHtmlWindow.
*/
wxString GenerateAliasInfo( LIB_ALIAS const * aAlias );
#endif // GENERATE_ALIAS_INFO_H