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
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
#include <symbol_tree_synchronizing_adapter.h>
|
2017-11-12 17:55:20 +00:00
|
|
|
#include <lib_manager.h>
|
2018-02-06 15:14:01 +00:00
|
|
|
#include <symbol_lib_table.h>
|
2017-11-12 17:55:20 +00:00
|
|
|
#include <class_libentry.h>
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_MODEL_ADAPTER::PTR SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create( LIB_MANAGER* aLibMgr )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
return PTR( new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aLibMgr ) );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::SYMBOL_TREE_SYNCHRONIZING_ADAPTER( LIB_MANAGER* aLibMgr )
|
|
|
|
: m_libMgr( aLibMgr ),
|
|
|
|
m_lastSyncHash( -1 )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
const LIB_TREE_NODE* node = ToNode( aItem );
|
|
|
|
return node ? node->Type == LIB_TREE_NODE::LIB : true;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-07 01:17:00 +00:00
|
|
|
#define PROGRESS_INTERVAL_MILLIS 120
|
2018-01-25 23:49:04 +00:00
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( bool aForce, std::function<void(int, int, const wxString&)> aProgressCallback )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-01-29 00:51:07 +00:00
|
|
|
wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
|
2018-01-25 23:49:04 +00:00
|
|
|
|
2017-11-12 13:24:55 +00:00
|
|
|
int libMgrHash = m_libMgr->GetHash();
|
|
|
|
|
2017-11-14 16:21:38 +00:00
|
|
|
if( !aForce && m_lastSyncHash == libMgrHash )
|
2017-11-12 17:55:20 +00:00
|
|
|
return;
|
|
|
|
|
2017-11-12 13:24:55 +00:00
|
|
|
m_lastSyncHash = libMgrHash;
|
2017-11-22 11:06:17 +00:00
|
|
|
int i = 0, max = GetLibrariesCount();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
// Process already stored libraries
|
|
|
|
for( auto it = m_tree.Children.begin(); it != m_tree.Children.end(); /* iteration inside */ )
|
|
|
|
{
|
2017-11-12 13:24:55 +00:00
|
|
|
const wxString& name = it->get()->Name;
|
2018-01-25 23:49:04 +00:00
|
|
|
|
|
|
|
if( wxGetUTCTimeMillis() > nextUpdate )
|
|
|
|
{
|
2018-01-29 00:51:07 +00:00
|
|
|
aProgressCallback( i, max, name );
|
2018-01-25 23:49:04 +00:00
|
|
|
nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2018-04-28 09:43:41 +00:00
|
|
|
if( !m_libMgr->LibraryExists( name, true ) )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2017-11-12 13:24:55 +00:00
|
|
|
it = deleteLibrary( it );
|
2017-11-12 17:55:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-12 13:24:55 +00:00
|
|
|
else if( m_libMgr->GetLibraryHash( name ) != m_libHashes[name] )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
2018-01-29 00:51:07 +00:00
|
|
|
++i;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for new libraries
|
|
|
|
for( const auto& libName : m_libMgr->GetLibraryNames() )
|
|
|
|
{
|
|
|
|
if( m_libHashes.count( libName ) == 0 )
|
2017-11-22 11:06:17 +00:00
|
|
|
{
|
2018-01-25 23:49:04 +00:00
|
|
|
if( wxGetUTCTimeMillis() > nextUpdate )
|
|
|
|
{
|
|
|
|
aProgressCallback( i++, max, libName );
|
|
|
|
nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:14:01 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
|
|
|
|
|
|
|
|
auto& lib_node = m_tree.AddLib( libName, library->GetDescr() );
|
2017-11-30 11:31:36 +00:00
|
|
|
updateLibrary( lib_node );
|
2017-11-22 11:06:17 +00:00
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
2018-08-05 11:56:02 +00:00
|
|
|
|
|
|
|
m_tree.AssignIntrinsicRanks();
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
int SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount() const
|
2017-11-22 11:06:17 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
int count = LIB_TREE_MODEL_ADAPTER::GetLibrariesCount();
|
2017-11-22 11:06:17 +00:00
|
|
|
|
|
|
|
for( const auto& libName : m_libMgr->GetLibraryNames() )
|
|
|
|
{
|
|
|
|
if( m_libHashes.count( libName ) == 0 )
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary( LIB_TREE_NODE_LIB& aLibNode )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-01-10 11:04:34 +00:00
|
|
|
auto hashIt = m_libHashes.find( aLibNode.Name );
|
|
|
|
|
|
|
|
if( hashIt == m_libHashes.end() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2017-11-12 13:24:55 +00:00
|
|
|
// add a new library
|
2017-11-15 10:13:58 +00:00
|
|
|
for( auto alias : m_libMgr->GetAliases( aLibNode.Name ) )
|
2018-07-30 13:18:37 +00:00
|
|
|
aLibNode.AddItem( alias );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
2018-01-10 11:04:34 +00:00
|
|
|
else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.Name ) )
|
2017-11-12 13:24:55 +00:00
|
|
|
{
|
|
|
|
// update an existing libary
|
|
|
|
std::list<LIB_ALIAS*> aliases = m_libMgr->GetAliases( aLibNode.Name );
|
|
|
|
|
|
|
|
// remove the common part from the aliases list
|
|
|
|
for( auto nodeIt = aLibNode.Children.begin(); nodeIt != aLibNode.Children.end(); /**/ )
|
|
|
|
{
|
|
|
|
auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
|
|
|
|
[&] ( const LIB_ALIAS* a ) {
|
|
|
|
return a->GetName() == (*nodeIt)->Name;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if( aliasIt != aliases.end() )
|
|
|
|
{
|
|
|
|
// alias exists both in the component tree and the library manager,
|
2018-01-10 11:04:34 +00:00
|
|
|
// update only the node data
|
2018-07-27 20:47:51 +00:00
|
|
|
static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
|
2017-11-12 13:24:55 +00:00
|
|
|
aliases.erase( aliasIt );
|
|
|
|
++nodeIt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// node does not exist in the library manager, remove the corresponding node
|
|
|
|
nodeIt = aLibNode.Children.erase( nodeIt );
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2017-11-12 13:24:55 +00:00
|
|
|
// now the aliases list contains only new aliases that need to be added to the tree
|
|
|
|
for( auto alias : aliases )
|
2018-07-30 13:18:37 +00:00
|
|
|
aLibNode.AddItem( alias );
|
2017-11-12 13:24:55 +00:00
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2017-11-12 13:24:55 +00:00
|
|
|
aLibNode.AssignIntrinsicRanks();
|
2017-11-12 17:55:20 +00:00
|
|
|
m_libHashes[aLibNode.Name] = m_libMgr->GetLibraryHash( aLibNode.Name );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary(
|
|
|
|
LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
|
2017-11-12 13:24:55 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* node = aLibNodeIt->get();
|
2017-11-14 14:24:50 +00:00
|
|
|
m_libHashes.erase( node->Name );
|
2017-11-12 13:24:55 +00:00
|
|
|
auto it = m_tree.Children.erase( aLibNodeIt );
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* SYMBOL_TREE_SYNCHRONIZING_ADAPTER::findLibrary( const wxString& aLibNickName )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
for( auto& lib : m_tree.Children )
|
|
|
|
{
|
|
|
|
if( lib->Name == aLibNickName )
|
|
|
|
return lib.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
|
|
|
|
unsigned int aCol ) const
|
2018-01-20 17:15:46 +00:00
|
|
|
{
|
2018-04-28 09:43:41 +00:00
|
|
|
if( IsFrozen() )
|
|
|
|
{
|
|
|
|
aVariant = wxEmptyString;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-20 17:15:46 +00:00
|
|
|
auto node = ToNode( aItem );
|
|
|
|
wxASSERT( node );
|
|
|
|
|
|
|
|
switch( aCol )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
aVariant = node->Name;
|
|
|
|
|
|
|
|
// mark modified libs with an asterix
|
2018-07-27 20:47:51 +00:00
|
|
|
if( node->Type == LIB_TREE_NODE::LIB && m_libMgr->IsLibraryModified( node->Name ) )
|
2018-01-20 17:15:46 +00:00
|
|
|
aVariant = node->Name + " *";
|
|
|
|
|
|
|
|
// mark modified parts with an asterix
|
2018-07-27 20:47:51 +00:00
|
|
|
if( node->Type == LIB_TREE_NODE::LIBID
|
2018-01-20 17:15:46 +00:00
|
|
|
&& m_libMgr->IsPartModified( node->Name, node->Parent->Name ) )
|
|
|
|
aVariant = node->Name + " *";
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
aVariant = node->Desc;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // column == -1 is used for default Compare function
|
|
|
|
aVariant = node->Name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
|
|
|
|
wxDataViewItemAttr& aAttr ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-04-28 09:43:41 +00:00
|
|
|
if( IsFrozen() )
|
|
|
|
return false;
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
// change attributes only for the name field
|
|
|
|
if( aCol != 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto node = ToNode( aItem );
|
|
|
|
wxCHECK( node, false );
|
|
|
|
|
|
|
|
switch( node->Type )
|
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
case LIB_TREE_NODE::LIB:
|
2017-11-12 17:55:20 +00:00
|
|
|
// mark modified libs with bold font
|
|
|
|
aAttr.SetBold( m_libMgr->IsLibraryModified( node->Name ) );
|
2017-11-13 11:19:14 +00:00
|
|
|
|
2018-01-20 17:15:46 +00:00
|
|
|
#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....
|
2017-11-13 11:19:14 +00:00
|
|
|
if( node->Name == m_libMgr->GetCurrentLib() )
|
|
|
|
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
|
2018-01-20 17:15:46 +00:00
|
|
|
#else
|
|
|
|
// mark the current library with background color
|
|
|
|
if( node->Name == m_libMgr->GetCurrentLib() )
|
|
|
|
aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
|
|
|
|
#endif
|
2017-11-12 17:55:20 +00:00
|
|
|
break;
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
case LIB_TREE_NODE::LIBID:
|
2017-11-12 17:55:20 +00:00
|
|
|
// mark modified part with bold font
|
|
|
|
aAttr.SetBold( m_libMgr->IsPartModified( node->Name, node->Parent->Name ) );
|
|
|
|
|
|
|
|
// mark aliases with italic font
|
|
|
|
aAttr.SetItalic( !node->IsRoot );
|
2017-11-13 11:19:14 +00:00
|
|
|
|
2018-01-20 17:15:46 +00:00
|
|
|
#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....
|
2017-11-14 11:12:50 +00:00
|
|
|
if( node->LibId == m_libMgr->GetCurrentLibId() )
|
2017-11-13 11:19:14 +00:00
|
|
|
aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
|
2018-01-20 17:15:46 +00:00
|
|
|
#else
|
|
|
|
// mark the current part with background color
|
|
|
|
if( node->LibId == m_libMgr->GetCurrentLibId() )
|
|
|
|
aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
|
|
|
|
#endif
|
2017-11-12 17:55:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|