kicad/eeschema/picksymbol.cpp

316 lines
10 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2004-2022 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 2
* 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:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <pgm_base.h>
#include <settings/settings_manager.h>
#include <project/project_file.h>
#include <core/kicad_algo.h>
#include <symbol_library_common.h>
#include <confirm.h>
#include <eeschema_id.h>
#include <general.h>
#include <kiway.h>
2020-12-25 23:37:01 +00:00
#include <symbol_viewer_frame.h>
#include <symbol_tree_model_adapter.h>
#include <symbol_editor/symbol_editor_settings.h>
#include <sch_symbol.h>
#include <symbol_library_common.h>
2018-01-30 10:49:51 +00:00
#include <sch_edit_frame.h>
#include <symbol_lib_table.h>
#include <tool/tool_manager.h>
#include <tools/ee_actions.h>
#include <dialog_choose_symbol.h>
2020-11-07 14:31:50 +00:00
PICKED_SYMBOL SCH_BASE_FRAME::PickSymbolFromLibBrowser( wxTopLevelWindow* aParent,
const SYMBOL_LIBRARY_FILTER* aFilter,
2020-11-07 14:31:50 +00:00
const LIB_ID& aPreselectedLibId,
int aUnit, int aConvert )
{
// Close any open non-modal Lib browser, and open a new one, in "modal" mode:
2020-12-25 23:37:01 +00:00
SYMBOL_VIEWER_FRAME* viewer = (SYMBOL_VIEWER_FRAME*) Kiway().Player( FRAME_SCH_VIEWER, false );
2020-12-25 23:37:01 +00:00
if( viewer )
viewer->Destroy();
2020-12-25 23:37:01 +00:00
viewer = (SYMBOL_VIEWER_FRAME*) Kiway().Player( FRAME_SCH_VIEWER_MODAL, true, aParent );
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
if( aFilter )
2020-12-25 23:37:01 +00:00
viewer->SetFilter( aFilter );
if( aPreselectedLibId.IsValid() )
{
2020-12-25 23:37:01 +00:00
viewer->SetSelectedLibrary( aPreselectedLibId.GetLibNickname() );
2021-03-19 20:27:30 +00:00
viewer->SetSelectedSymbol( aPreselectedLibId.GetLibItemName());
}
2020-12-25 23:37:01 +00:00
viewer->SetUnitAndConvert( aUnit, aConvert );
2020-12-25 23:37:01 +00:00
viewer->Refresh();
2020-11-07 14:31:50 +00:00
PICKED_SYMBOL sel;
wxString symbol;
2020-12-25 23:37:01 +00:00
if( viewer->ShowModal( &symbol, aParent ) )
{
LIB_ID id;
if( id.Parse( symbol ) == -1 )
sel.LibId = id;
2020-12-25 23:37:01 +00:00
sel.Unit = viewer->GetUnit();
sel.Convert = viewer->GetConvert();
}
2020-12-25 23:37:01 +00:00
viewer->Destroy();
2011-04-09 10:52:14 +00:00
return sel;
}
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
PICKED_SYMBOL SCH_BASE_FRAME::PickSymbolFromLibTree( const SYMBOL_LIBRARY_FILTER* aFilter,
2020-11-07 14:31:50 +00:00
std::vector<PICKED_SYMBOL>& aHistoryList,
2020-12-25 23:37:01 +00:00
bool aUseLibBrowser, int aUnit, int aConvert,
bool aShowFootprints, const LIB_ID* aHighlight,
2020-11-07 14:31:50 +00:00
bool aAllowFields )
{
std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_SYMBOL::g_Mutex, std::defer_lock );
SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
PROJECT_FILE& project = Prj().GetProjectFile();
// One DIALOG_CHOOSE_SYMBOL dialog at a time. User probably can't handle more anyway.
if( !dialogLock.try_lock() )
2020-11-07 14:31:50 +00:00
return PICKED_SYMBOL();
// Make sure settings are loaded before we start running multi-threaded symbol loaders
Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> dataPtr
= SYMBOL_TREE_MODEL_ADAPTER::Create( this, libs );
SYMBOL_TREE_MODEL_ADAPTER* modelAdapter
= static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( dataPtr.get() );
bool loaded = false;
if( aFilter )
{
const wxArrayString& liblist = aFilter->GetAllowedLibList();
for( const wxString& nickname : liblist )
{
if( libs->HasLibrary( nickname, true ) )
{
loaded = true;
bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, nickname )
|| alg::contains( project.m_PinnedSymbolLibs, nickname );
if( libs->FindRow( nickname )->GetIsVisible() )
modelAdapter->AddLibrary( nickname, pinned );
}
}
modelAdapter->AssignIntrinsicRanks();
if( aFilter->GetFilterPowerSymbols() )
modelAdapter->SetFilter( SYMBOL_TREE_MODEL_ADAPTER::SYM_FILTER_POWER );
}
std::vector<LIB_SYMBOL> history_list_storage;
std::vector<LIB_TREE_ITEM*> history_list;
history_list_storage.reserve( aHistoryList.size() );
2020-11-07 14:31:50 +00:00
for( const PICKED_SYMBOL& i : aHistoryList )
{
2021-06-17 21:22:10 +00:00
LIB_SYMBOL* symbol = GetLibSymbol( i.LibId );
// This can be null, for example when a symbol has been deleted from a library
if( symbol )
{
history_list_storage.emplace_back( *symbol );
for( const std::pair<int, wxString>& fieldDef : i.Fields )
{
LIB_FIELD* field = history_list_storage.back().GetFieldById( fieldDef.first );
if( field )
field->SetText( fieldDef.second );
}
history_list.push_back( &history_list_storage.back() );
}
}
modelAdapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
history_list, false, true );
if( !aHistoryList.empty() )
modelAdapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit );
const std::vector< wxString > libNicknames = libs->GetLogicalLibs();
if( !loaded )
{
if( !modelAdapter->AddLibraries( libNicknames, this ) )
{
// loading cancelled by user
return PICKED_SYMBOL();
}
}
if( aHighlight && aHighlight->IsValid() )
modelAdapter->SetPreselectNode( *aHighlight, /* aUnit */ 0 );
wxString dialogTitle;
if( modelAdapter->GetFilter() == SYMBOL_TREE_MODEL_ADAPTER::SYM_FILTER_POWER )
dialogTitle.Printf( _( "Choose Power Symbol (%d items loaded)" ), dataPtr->GetItemCount() );
else
dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), dataPtr->GetItemCount() );
DIALOG_CHOOSE_SYMBOL dlg( this, dialogTitle, dataPtr, aConvert, aAllowFields, aShowFootprints,
aUseLibBrowser );
if( dlg.ShowModal() == wxID_CANCEL )
2020-11-07 14:31:50 +00:00
return PICKED_SYMBOL();
2020-11-07 14:31:50 +00:00
PICKED_SYMBOL sel;
LIB_ID id = dlg.GetSelectedLibId( &sel.Unit );
2021-03-19 20:27:30 +00:00
if( dlg.IsExternalBrowserSelected() ) // User requested symbol browser.
{
2020-11-07 14:31:50 +00:00
sel = PickSymbolFromLibBrowser( this, aFilter, id, sel.Unit, sel.Convert );
id = sel.LibId;
}
if( !id.IsValid() ) // Dialog closed by OK button,
// or the selection by lib browser was requested,
// but no symbol selected
2020-11-07 14:31:50 +00:00
return PICKED_SYMBOL();
if( sel.Unit == 0 )
sel.Unit = 1;
sel.Fields = dlg.GetFields();
sel.LibId = id;
if( sel.LibId.IsValid() )
{
alg::delete_if( aHistoryList, [&sel]( PICKED_SYMBOL const& i )
{
return i.LibId == sel.LibId;
} );
aHistoryList.insert( aHistoryList.begin(), sel );
}
return sel;
}
2021-06-10 14:10:55 +00:00
void SCH_EDIT_FRAME::SelectUnit( SCH_SYMBOL* aSymbol, int aUnit )
{
2021-06-17 21:22:10 +00:00
LIB_SYMBOL* symbol = GetLibSymbol( aSymbol->GetLibId() );
2021-06-10 18:51:46 +00:00
if( !symbol )
return;
2021-06-10 18:51:46 +00:00
int unitCount = symbol->GetUnitCount();
2021-03-19 20:27:30 +00:00
if( unitCount <= 1 || aSymbol->GetUnit() == aUnit )
return;
if( aUnit > unitCount )
aUnit = unitCount;
EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
2021-03-19 20:27:30 +00:00
if( !aSymbol->GetEditFlags() ) // No command in progress: save in undo list
SaveCopyInUndoList( GetScreen(), aSymbol, UNDO_REDO::CHANGED, false );
/* Update the unit number. */
2021-03-19 20:27:30 +00:00
aSymbol->SetUnitSelection( &GetCurrentSheet(), aUnit );
aSymbol->SetUnit( aUnit );
aSymbol->ClearFlags();
aSymbol->SetFlags( savedFlags ); // Restore m_Flag modified by SetUnit()
2021-03-19 20:27:30 +00:00
if( !aSymbol->GetEditFlags() ) // No command in progress: update schematic
{
if( eeconfig()->m_AutoplaceFields.enable )
2021-03-19 20:27:30 +00:00
aSymbol->AutoAutoplaceFields( GetScreen() );
TestDanglingEnds();
UpdateItem( aSymbol, false, true );
OnModify();
}
}
2021-06-10 14:10:55 +00:00
void SCH_EDIT_FRAME::ConvertPart( SCH_SYMBOL* aSymbol )
{
if( !aSymbol || !aSymbol->GetLibSymbolRef() )
return;
wxString msg;
if( !aSymbol->GetLibSymbolRef()->HasConversion() )
{
LIB_ID id = aSymbol->GetLibSymbolRef()->GetLibId();
msg.Printf( _( "No alternate body style found for symbol '%s' in library '%s'." ),
id.GetLibItemName().wx_str(),
id.GetLibNickname().wx_str() );
DisplayError( this, msg );
return;
}
EDA_ITEM_FLAGS savedFlags = aSymbol->GetFlags();
2021-03-19 20:27:30 +00:00
aSymbol->SetConvert( aSymbol->GetConvert() + 1 );
2021-01-26 22:54:10 +00:00
// ensure m_convert = 1 or 2
// 1 = shape 1 = not converted
// 2 = shape 2 = first converted shape
2021-03-19 20:27:30 +00:00
// > 2 is not currently supported
2021-01-26 22:54:10 +00:00
// When m_convert = val max, return to the first shape
2021-03-19 20:27:30 +00:00
if( aSymbol->GetConvert() > LIB_ITEM::LIB_CONVERT::DEMORGAN )
aSymbol->SetConvert( LIB_ITEM::LIB_CONVERT::BASE );
TestDanglingEnds();
2021-03-19 20:27:30 +00:00
aSymbol->ClearFlags();
aSymbol->SetFlags( savedFlags ); // Restore m_flags (modified by SetConvert())
// If selected make sure all the now-included pins are selected
2021-03-19 20:27:30 +00:00
if( aSymbol->IsSelected() )
m_toolManager->RunAction( EE_ACTIONS::addItemToSel, true, aSymbol );
UpdateItem( aSymbol, false, true );
OnModify();
}