kicad/eeschema/symbol_tree_synchronizing_a...

330 lines
11 KiB
C++
Raw Normal View History

2017-11-12 17:55:20 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 CERN
* Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
2017-11-12 17:55:20 +00:00
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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, you may find one here:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <symbol_tree_synchronizing_adapter.h>
2017-11-12 17:55:20 +00:00
#include <lib_manager.h>
#include <symbol_lib_table.h>
2017-11-12 17:55:20 +00:00
#include <class_libentry.h>
#include <tools/lib_control.h>
2017-11-12 17:55:20 +00:00
LIB_TREE_MODEL_ADAPTER::PTR SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create( LIB_EDIT_FRAME* aParent,
LIB_MANAGER* aLibMgr )
2017-11-12 17:55:20 +00:00
{
return PTR( new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr ) );
2017-11-12 17:55:20 +00:00
}
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::SYMBOL_TREE_SYNCHRONIZING_ADAPTER( LIB_EDIT_FRAME* aParent,
LIB_MANAGER* aLibMgr ) :
LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
m_frame( aParent ),
m_libMgr( aLibMgr ),
m_lastSyncHash( -1 )
2017-11-12 17:55:20 +00:00
{
}
TOOL_INTERACTIVE* SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetContextMenuTool()
{
return m_frame->GetToolManager()->GetTool<LIB_CONTROL>();
}
bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
2017-11-12 17:55:20 +00:00
{
const LIB_TREE_NODE* node = ToNode( aItem );
return node ? node->m_Type == LIB_TREE_NODE::LIB : true;
2017-11-12 17:55:20 +00:00
}
#define PROGRESS_INTERVAL_MILLIS 120
void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( bool aForce,
std::function<void( int, int, const wxString& )> aProgressCallback )
2017-11-12 17:55:20 +00:00
{
wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
int libMgrHash = m_libMgr->GetHash();
if( !aForce && m_lastSyncHash == libMgrHash )
2017-11-12 17:55:20 +00:00
return;
m_lastSyncHash = libMgrHash;
int i = 0, max = GetLibrariesCount();
2017-11-12 17:55:20 +00:00
// Process already stored libraries
for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
2017-11-12 17:55:20 +00:00
{
const wxString& name = it->get()->m_Name;
if( wxGetUTCTimeMillis() > nextUpdate )
{
aProgressCallback( i, max, name );
nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
}
2017-11-12 17:55:20 +00:00
// There is a bug in LIB_MANAGER::LibraryExists() that uses the buffered modified
// libraries before the symbol library table which prevents the library from being
// removed from the tree control.
if( !m_libMgr->LibraryExists( name, true )
|| !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true ) )
2017-11-12 17:55:20 +00:00
{
it = deleteLibrary( it );
2017-11-12 17:55:20 +00:00
continue;
}
else if( aForce || m_libMgr->GetLibraryHash( name ) != m_libHashes[name] )
2017-11-12 17:55:20 +00:00
{
updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
2017-11-12 17:55:20 +00:00
}
++it;
++i;
2017-11-12 17:55:20 +00:00
}
// Look for new libraries
for( const wxString& libName : m_libMgr->GetLibraryNames() )
2017-11-12 17:55:20 +00:00
{
if( m_libHashes.count( libName ) == 0 )
{
if( wxGetUTCTimeMillis() > nextUpdate )
{
aProgressCallback( i++, max, libName );
nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
}
SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( libName, library->GetDescr() );
updateLibrary( lib_node );
}
2017-11-12 17:55:20 +00:00
}
m_tree.AssignIntrinsicRanks();
2017-11-12 17:55:20 +00:00
}
int SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount() const
{
int count = LIB_TREE_MODEL_ADAPTER::GetLibrariesCount();
for( const wxString& libName : m_libMgr->GetLibraryNames() )
{
if( m_libHashes.count( libName ) == 0 )
++count;
}
return count;
}
void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary( LIB_TREE_NODE_LIB& aLibNode )
2017-11-12 17:55:20 +00:00
{
auto hashIt = m_libHashes.find( aLibNode.m_Name );
if( hashIt == m_libHashes.end() )
2017-11-12 17:55:20 +00:00
{
// add a new library
for( LIB_PART* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
aLibNode.AddItem( alias );
2017-11-12 17:55:20 +00:00
}
else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
{
// update an existing library
std::list<LIB_PART*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
// remove the common part from the aliases list
for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); /**/ )
{
auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
[&] ( const LIB_PART* a )
{
return a->GetName() == (*nodeIt)->m_Name;
} );
if( aliasIt != aliases.end() )
{
// alias exists both in the component tree and the library manager,
// update only the node data
static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
aliases.erase( aliasIt );
++nodeIt;
}
else
{
// node does not exist in the library manager, remove the corresponding node
nodeIt = aLibNode.m_Children.erase( nodeIt );
}
}
2017-11-12 17:55:20 +00:00
// now the aliases list contains only new aliases that need to be added to the tree
for( LIB_PART* alias : aliases )
aLibNode.AddItem( alias );
}
2017-11-12 17:55:20 +00:00
aLibNode.AssignIntrinsicRanks();
m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
2017-11-12 17:55:20 +00:00
}
LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary(
LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
{
LIB_TREE_NODE* node = aLibNodeIt->get();
m_libHashes.erase( node->m_Name );
auto it = m_tree.m_Children.erase( aLibNodeIt );
return it;
}
void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
unsigned int aCol ) const
{
if( IsFrozen() )
{
aVariant = wxEmptyString;
return;
}
LIB_TREE_NODE* node = ToNode( aItem );
wxASSERT( node );
switch( aCol )
{
case 0:
if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
node->m_Name = m_frame->GetCurPart()->GetLibId().GetLibItemName();
if( node->m_Pinned )
aVariant = GetPinningSymbol() + node->m_Name;
else
aVariant = node->m_Name;
// mark modified items with an asterisk
if( node->m_Type == LIB_TREE_NODE::LIB )
{
if( m_libMgr->IsLibraryModified( node->m_Name ) )
aVariant = aVariant.GetString() + " *";
}
else if( node->m_Type == LIB_TREE_NODE::LIBID )
{
if( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) )
aVariant = aVariant.GetString() + " *";
}
break;
case 1:
if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
node->m_Desc = m_frame->GetCurPart()->GetDescription();
aVariant = node->m_Desc;
// Annotate that the library failed to load in the description column
if( node->m_Type == LIB_TREE_NODE::LIB )
{
if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
aVariant = _( "(failed to load) " ) + aVariant.GetString();
}
break;
default: // column == -1 is used for default Compare function
aVariant = node->m_Name;
break;
}
}
bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
wxDataViewItemAttr& aAttr ) const
2017-11-12 17:55:20 +00:00
{
if( IsFrozen() )
return false;
LIB_TREE_NODE* node = ToNode( aItem );
2017-11-12 17:55:20 +00:00
wxCHECK( node, false );
// Mark both columns of unloaded libraries using grey text color (to look disabled)
if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
{
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
return true;
}
// The remaining attributes are only for the name column
if( aCol != 0 )
return false;
switch( node->m_Type )
2017-11-12 17:55:20 +00:00
{
case LIB_TREE_NODE::LIB:
// mark modified libs with bold font
aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
#ifdef __WXGTK__
// The native wxGTK+ impl ignores background colour, so set the text colour instead.
// This works reasonably well in dark themes, and quite poorly in light ones....
if( node->m_Name == m_libMgr->GetCurrentLib() )
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
#else
// mark the current library with background color
if( node->m_Name == m_libMgr->GetCurrentLib() )
{
aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
}
#endif
break;
2017-11-12 17:55:20 +00:00
case LIB_TREE_NODE::LIBID:
// mark modified part with bold font
aAttr.SetBold( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) );
2017-11-12 17:55:20 +00:00
// mark aliases with italic font
aAttr.SetItalic( !node->m_IsRoot );
#ifdef __WXGTK__
// The native wxGTK+ impl ignores background colour, so set the text colour instead.
// This works reasonably well in dark themes, and quite poorly in light ones....
if( node->m_LibId == m_libMgr->GetCurrentLibId() )
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
#else
// mark the current part with background color
if( node->m_LibId == m_libMgr->GetCurrentLibId() )
{
aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
}
#endif
break;
2017-11-12 17:55:20 +00:00
default:
return false;
2017-11-12 17:55:20 +00:00
}
return true;
}