2018-07-27 20:47:51 +00:00
|
|
|
/*
|
|
|
|
* 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) 2014 Henner Zeller <h.zeller@acm.org>
|
2022-02-27 11:54:12 +00:00
|
|
|
* Copyright (C) 2014-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-07-27 20:47:51 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-03 12:11:15 +00:00
|
|
|
#include <wx/log.h>
|
2018-07-27 20:47:51 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2020-08-09 10:55:00 +00:00
|
|
|
#include <wx/window.h>
|
2022-07-22 08:26:49 +00:00
|
|
|
#include <core/kicad_algo.h>
|
|
|
|
#include <pgm_base.h>
|
|
|
|
#include <project/project_file.h>
|
2021-08-14 20:05:21 +00:00
|
|
|
#include <widgets/wx_progress_reporters.h>
|
2021-09-14 18:26:03 +00:00
|
|
|
#include <dialogs/html_message_box.h>
|
2018-07-27 20:47:51 +00:00
|
|
|
#include <eda_pattern_match.h>
|
2021-04-02 23:50:50 +00:00
|
|
|
#include <generate_alias_info.h>
|
2022-07-22 08:26:49 +00:00
|
|
|
#include <sch_base_frame.h>
|
2021-03-17 00:48:10 +00:00
|
|
|
#include <locale_io.h>
|
2022-07-22 08:26:49 +00:00
|
|
|
#include <lib_symbol.h>
|
2021-03-17 00:48:10 +00:00
|
|
|
#include <symbol_async_loader.h>
|
2021-04-02 23:50:50 +00:00
|
|
|
#include <symbol_lib_table.h>
|
|
|
|
#include <symbol_tree_model_adapter.h>
|
2022-07-10 02:06:31 +00:00
|
|
|
#include <string_utils.h>
|
2018-07-27 20:47:51 +00:00
|
|
|
|
|
|
|
bool SYMBOL_TREE_MODEL_ADAPTER::m_show_progress = true;
|
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
#define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
|
2018-07-27 20:47:51 +00:00
|
|
|
|
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
|
|
|
|
SYMBOL_TREE_MODEL_ADAPTER::Create( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs )
|
2018-07-27 20:47:51 +00:00
|
|
|
{
|
2020-11-07 14:31:50 +00:00
|
|
|
auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
|
|
|
|
return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
|
2018-07-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-14 14:22:21 +00:00
|
|
|
SYMBOL_TREE_MODEL_ADAPTER::SYMBOL_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
|
2020-05-31 21:42:04 +00:00
|
|
|
LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
|
2020-02-14 14:22:21 +00:00
|
|
|
m_libs( (SYMBOL_LIB_TABLE*) aLibs )
|
2023-01-20 03:14:10 +00:00
|
|
|
{
|
|
|
|
// Symbols may have different value from name
|
|
|
|
m_availableColumns.emplace_back( wxT( "Value" ) );
|
|
|
|
}
|
2018-07-27 20:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
SYMBOL_TREE_MODEL_ADAPTER::~SYMBOL_TREE_MODEL_ADAPTER()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
|
2022-07-22 08:26:49 +00:00
|
|
|
SCH_BASE_FRAME* aFrame )
|
2018-07-27 20:47:51 +00:00
|
|
|
{
|
2022-07-22 08:26:49 +00:00
|
|
|
std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
|
2018-07-27 20:47:51 +00:00
|
|
|
|
|
|
|
if( m_show_progress )
|
|
|
|
{
|
2022-07-22 08:26:49 +00:00
|
|
|
progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame,
|
|
|
|
_( "Loading Symbol Libraries" ),
|
|
|
|
aNicknames.size(), true );
|
2018-07-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 21:50:49 +00:00
|
|
|
// Disable KIID generation: not needed for library parts; sometimes very slow
|
|
|
|
KIID::CreateNilUuids( true );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbols;
|
2018-07-27 20:47:51 +00:00
|
|
|
|
2021-03-20 00:23:54 +00:00
|
|
|
SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs,
|
2021-06-10 14:10:55 +00:00
|
|
|
GetFilter() == LIB_TREE_MODEL_ADAPTER::SYM_FILTER_POWER,
|
2022-07-22 08:26:49 +00:00
|
|
|
&loadedSymbols, progressReporter.get() );
|
2021-03-17 00:48:10 +00:00
|
|
|
|
|
|
|
LOCALE_IO toggle;
|
|
|
|
|
|
|
|
loader.Start();
|
|
|
|
|
|
|
|
while( !loader.Done() )
|
2018-07-27 20:47:51 +00:00
|
|
|
{
|
2022-07-22 08:26:49 +00:00
|
|
|
if( progressReporter && !progressReporter->KeepRefreshing() )
|
2022-02-27 11:54:12 +00:00
|
|
|
break;
|
2020-08-09 10:55:00 +00:00
|
|
|
|
2021-03-17 00:48:10 +00:00
|
|
|
wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
|
|
|
|
}
|
2018-07-27 20:47:51 +00:00
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
loader.Join();
|
|
|
|
|
|
|
|
bool cancelled = false;
|
|
|
|
|
2022-07-22 08:26:49 +00:00
|
|
|
if( progressReporter )
|
|
|
|
cancelled = progressReporter->IsCancelled();
|
2021-03-17 00:48:10 +00:00
|
|
|
|
|
|
|
if( !loader.GetErrors().IsEmpty() )
|
|
|
|
{
|
2022-07-22 08:26:49 +00:00
|
|
|
HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
|
2021-04-02 23:50:50 +00:00
|
|
|
|
2021-06-26 11:33:37 +00:00
|
|
|
dlg.MessageSet( _( "Errors loading symbols:" ) );
|
2021-04-02 23:50:50 +00:00
|
|
|
|
|
|
|
wxString msg = loader.GetErrors();
|
|
|
|
msg.Replace( "\n", "<BR>" );
|
|
|
|
|
|
|
|
dlg.AddHTML_Text( msg );
|
|
|
|
dlg.ShowModal();
|
2021-03-17 00:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( loadedSymbols.size() > 0 )
|
|
|
|
{
|
2022-07-22 08:26:49 +00:00
|
|
|
COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
|
|
|
|
PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
auto addFunc =
|
|
|
|
[&]( const wxString& aLibName, std::vector<LIB_SYMBOL*> aSymbolList,
|
|
|
|
const wxString& aDescription )
|
|
|
|
{
|
|
|
|
std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
|
|
|
|
bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
|
|
|
|
|| alg::contains( project.m_PinnedSymbolLibs, aLibName );
|
|
|
|
|
|
|
|
DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
|
|
|
|
};
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
for( const std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
|
2021-03-17 00:48:10 +00:00
|
|
|
{
|
2022-08-27 03:52:56 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( pair.first );
|
2022-08-27 00:18:16 +00:00
|
|
|
|
2022-12-10 21:04:27 +00:00
|
|
|
wxCHECK2( row, continue );
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
if( !row->GetIsVisible() )
|
|
|
|
continue;
|
2022-07-22 08:26:49 +00:00
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
std::vector<wxString> additionalColumns;
|
|
|
|
row->GetAvailableSymbolFields( additionalColumns );
|
|
|
|
|
|
|
|
for( const wxString& column : additionalColumns )
|
|
|
|
addColumnIfNecessary( column );
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
if( row->SupportsSubLibraries() )
|
|
|
|
{
|
|
|
|
std::vector<wxString> subLibraries;
|
|
|
|
row->GetSubLibraryNames( subLibraries );
|
|
|
|
|
|
|
|
wxString parentDesc = m_libs->GetDescription( pair.first );
|
|
|
|
|
|
|
|
for( const wxString& lib : subLibraries )
|
|
|
|
{
|
2022-12-10 21:35:11 +00:00
|
|
|
wxString suffix = lib.IsEmpty() ? wxT( "" )
|
|
|
|
: wxString::Format( wxT( " - %s" ), lib );
|
|
|
|
wxString name = wxString::Format( wxT( "%s%s" ), pair.first, suffix );
|
2022-08-27 03:52:56 +00:00
|
|
|
wxString desc;
|
|
|
|
|
|
|
|
if( !parentDesc.IsEmpty() )
|
|
|
|
desc = wxString::Format( wxT( "%s (%s)" ), parentDesc, lib );
|
|
|
|
|
|
|
|
std::vector<LIB_SYMBOL*> symbols;
|
|
|
|
|
|
|
|
std::copy_if( pair.second.begin(), pair.second.end(),
|
|
|
|
std::back_inserter( symbols ),
|
|
|
|
[&lib]( LIB_SYMBOL* aSym )
|
|
|
|
{
|
|
|
|
return lib.IsSameAs( aSym->GetLibId().GetSubLibraryName() );
|
|
|
|
} );
|
|
|
|
|
|
|
|
addFunc( name, symbols, desc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addFunc( pair.first, pair.second, m_libs->GetDescription( pair.first ) );
|
|
|
|
}
|
2021-03-17 00:48:10 +00:00
|
|
|
}
|
2018-07-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 21:50:49 +00:00
|
|
|
KIID::CreateNilUuids( false );
|
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
m_tree.AssignIntrinsicRanks();
|
|
|
|
|
2022-07-22 08:26:49 +00:00
|
|
|
if( progressReporter )
|
2018-07-27 20:47:51 +00:00
|
|
|
{
|
2022-09-21 13:44:13 +00:00
|
|
|
// Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
|
|
|
|
// followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
|
|
|
|
// on the event loop manager.
|
|
|
|
// One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
|
|
|
|
// creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
|
2022-07-22 08:26:49 +00:00
|
|
|
progressReporter.reset();
|
2018-07-27 20:47:51 +00:00
|
|
|
m_show_progress = false;
|
|
|
|
}
|
2022-02-27 11:54:12 +00:00
|
|
|
|
|
|
|
return !cancelled;
|
2018-07-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-22 08:26:49 +00:00
|
|
|
void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
|
2018-07-27 20:47:51 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
bool onlyPowerSymbols = ( GetFilter() == SYM_FILTER_POWER );
|
2021-06-10 18:51:46 +00:00
|
|
|
std::vector<LIB_SYMBOL*> symbols;
|
2018-07-27 20:47:51 +00:00
|
|
|
std::vector<LIB_TREE_ITEM*> comp_list;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
|
2018-07-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-26 19:21:30 +00:00
|
|
|
wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
|
|
|
|
aLibNickname,
|
|
|
|
ioe.What() );
|
2018-07-27 20:47:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
if( symbols.size() > 0 )
|
2018-07-27 20:47:51 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
comp_list.assign( symbols.begin(), symbols.end() );
|
2022-07-22 08:26:49 +00:00
|
|
|
DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
|
|
|
|
false );
|
2018-07-27 20:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
|
|
|
|
{
|
|
|
|
return GenerateAliasInfo( m_libs, aLibId, aUnit );
|
|
|
|
}
|
2022-07-10 02:06:31 +00:00
|
|
|
|
|
|
|
|