2017-10-28 18:28:14 +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>
|
2020-05-21 20:46:00 +00:00
|
|
|
* Copyright (C) 2014-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-10-28 18:28:14 +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/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-14 14:22:21 +00:00
|
|
|
#include <eda_base_frame.h>
|
2017-10-28 18:28:14 +00:00
|
|
|
#include <eda_pattern_match.h>
|
2019-09-18 23:00:48 +00:00
|
|
|
#include <kiface_i.h>
|
2020-02-14 14:22:21 +00:00
|
|
|
#include <config_params.h>
|
2019-09-18 23:00:48 +00:00
|
|
|
#include <lib_tree_model_adapter.h>
|
2020-05-31 21:42:04 +00:00
|
|
|
#include <project/project_file.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <settings/app_settings.h>
|
2020-10-25 16:41:38 +00:00
|
|
|
#include <widgets/ui_common.h>
|
2017-10-28 18:28:14 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2017-11-30 11:31:36 +00:00
|
|
|
#include <wx/wupdlock.h>
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
#define PINNED_ITEMS_KEY wxT( "PinnedItems" )
|
2019-09-18 23:00:48 +00:00
|
|
|
|
2017-10-28 18:28:14 +00:00
|
|
|
static const int kDataViewIndent = 20;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert CMP_TREE_NODE -> wxDataViewItem
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem( LIB_TREE_NODE const* aNode )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert wxDataViewItem -> CMP_TREE_NODE
|
|
|
|
*/
|
2020-02-07 17:06:24 +00:00
|
|
|
LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ToNode( wxDataViewItem aItem )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
return static_cast<LIB_TREE_NODE*>( aItem.GetID() );
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert CMP_TREE_NODE's children to wxDataViewItemArray
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
unsigned int LIB_TREE_MODEL_ADAPTER::IntoArray( LIB_TREE_NODE const& aNode,
|
|
|
|
wxDataViewItemArray& aChildren )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
unsigned int n = 0;
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
for( auto const& child: aNode.m_Children )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
if( child->m_Score > 0 )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
aChildren.Add( ToItem( &*child ) );
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, wxString aPinnedKey ) :
|
2020-02-14 14:22:21 +00:00
|
|
|
m_parent( aParent ),
|
|
|
|
m_filter( CMP_FILTER_NONE ),
|
|
|
|
m_show_units( true ),
|
|
|
|
m_preselect_unit( 0 ),
|
|
|
|
m_freeze( 0 ),
|
|
|
|
m_col_part( nullptr ),
|
|
|
|
m_col_desc( nullptr ),
|
2020-05-31 21:42:04 +00:00
|
|
|
m_widget( nullptr ),
|
|
|
|
m_pinnedLibs(),
|
|
|
|
m_pinnedKey( aPinnedKey )
|
2019-09-18 23:00:48 +00:00
|
|
|
{
|
|
|
|
// Default column widths
|
|
|
|
m_colWidths[PART_COL] = 360;
|
|
|
|
m_colWidths[DESC_COL] = 2000;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
auto cfg = Kiface().KifaceSettings();
|
|
|
|
m_colWidths[PART_COL] = cfg->m_LibTree.column_width;
|
2020-02-07 17:06:24 +00:00
|
|
|
|
2020-02-14 14:22:21 +00:00
|
|
|
// Read the pinned entries from the project config
|
2020-05-31 21:42:04 +00:00
|
|
|
PROJECT_FILE& project = m_parent->Kiway().Prj().GetProjectFile();
|
|
|
|
|
|
|
|
std::vector<wxString>& entries = ( m_pinnedKey == "pinned_symbol_libs" ) ?
|
|
|
|
project.m_PinnedSymbolLibs :
|
|
|
|
project.m_PinnedFootprintLibs;
|
|
|
|
|
|
|
|
for( const wxString& entry : entries )
|
|
|
|
m_pinnedLibs.push_back( entry );
|
2019-09-18 23:00:48 +00:00
|
|
|
}
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_MODEL_ADAPTER::~LIB_TREE_MODEL_ADAPTER()
|
2017-10-28 18:28:14 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2019-09-18 23:00:48 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::SaveColWidths()
|
|
|
|
{
|
|
|
|
if( m_widget )
|
|
|
|
{
|
2020-01-13 01:44:19 +00:00
|
|
|
auto cfg = Kiface().KifaceSettings();
|
|
|
|
cfg->m_LibTree.column_width = m_widget->GetColumn( PART_COL )->GetWidth();
|
2019-09-18 23:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:22:21 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::SavePinnedItems()
|
2020-02-14 14:22:21 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
PROJECT_FILE& project = m_parent->Kiway().Prj().GetProjectFile();
|
2020-02-14 14:22:21 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
std::vector<wxString>& entries = ( m_pinnedKey == "pinned_symbol_libs" ) ?
|
|
|
|
project.m_PinnedSymbolLibs :
|
|
|
|
project.m_PinnedFootprintLibs;
|
2020-02-14 14:22:21 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
entries.clear();
|
2020-02-14 14:22:21 +00:00
|
|
|
m_pinnedLibs.clear();
|
2020-02-07 17:06:24 +00:00
|
|
|
|
|
|
|
for( auto& child: m_tree.m_Children )
|
|
|
|
{
|
|
|
|
if( child->m_Pinned )
|
2020-05-31 21:42:04 +00:00
|
|
|
{
|
2020-02-14 14:22:21 +00:00
|
|
|
m_pinnedLibs.push_back( child->m_LibId.GetLibNickname() );
|
2020-05-31 21:42:04 +00:00
|
|
|
entries.push_back( child->m_LibId.GetLibNickname() );
|
|
|
|
}
|
2020-02-07 17:06:24 +00:00
|
|
|
}
|
2020-02-14 14:22:21 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 23:00:48 +00:00
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::SetFilter( CMP_FILTER_TYPE aFilter )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
m_filter = aFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::ShowUnits( bool aShow )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
m_show_units = aShow;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::SetPreselectNode( LIB_ID const& aLibId, int aUnit )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
m_preselect_lib_id = aLibId;
|
|
|
|
m_preselect_unit = aUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-14 14:22:21 +00:00
|
|
|
LIB_TREE_NODE_LIB& LIB_TREE_MODEL_ADAPTER::DoAddLibraryNode( wxString const& aNodeName,
|
|
|
|
wxString const& aDesc )
|
|
|
|
{
|
|
|
|
LIB_TREE_NODE_LIB& lib_node = m_tree.AddLib( aNodeName, aDesc );
|
|
|
|
|
|
|
|
lib_node.m_Pinned = m_pinnedLibs.Index( lib_node.m_LibId.GetLibNickname() ) != wxNOT_FOUND;
|
|
|
|
|
|
|
|
return lib_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::DoAddLibrary( wxString const& aNodeName, wxString const& aDesc,
|
2018-08-05 11:56:02 +00:00
|
|
|
std::vector<LIB_TREE_ITEM*> const& aItemList,
|
|
|
|
bool presorted )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-14 14:22:21 +00:00
|
|
|
LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( aNodeName, aDesc );
|
2020-02-07 17:06:24 +00:00
|
|
|
|
2019-08-26 10:57:07 +00:00
|
|
|
for( LIB_TREE_ITEM* item: aItemList )
|
|
|
|
lib_node.AddItem( item );
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
lib_node.AssignIntrinsicRanks( presorted );
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( wxString const& aSearch )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
{
|
2020-08-21 01:38:14 +00:00
|
|
|
// DO NOT REMOVE THE FREEZE/THAW. This freeze/thaw is a flag for this model adapter
|
|
|
|
// that tells it when it shouldn't trust any of the data in the model. When set, it will
|
|
|
|
// not return invalid data to the UI, since this invalid data can cause crashes.
|
|
|
|
// This is different than the update locker, which locks the UI aspects only.
|
2020-08-18 17:46:50 +00:00
|
|
|
wxWindowUpdateLocker updateLock( m_widget );
|
2020-10-06 23:59:11 +00:00
|
|
|
|
|
|
|
// Even with the updateLock, wxWidgets sometimes ties its knickers in
|
|
|
|
// a knot when trying to run a wxdataview_selection_changed_callback()
|
|
|
|
// on a row that has been deleted.
|
|
|
|
// https://bugs.launchpad.net/kicad/+bug/1756255
|
|
|
|
m_widget->UnselectAll();
|
2020-08-21 01:38:14 +00:00
|
|
|
Freeze();
|
2020-10-29 22:44:32 +00:00
|
|
|
BeforeReset();
|
2020-02-07 17:06:24 +00:00
|
|
|
|
2020-08-18 17:46:50 +00:00
|
|
|
m_tree.ResetScore();
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2020-08-18 17:46:50 +00:00
|
|
|
for( auto& child: m_tree.m_Children )
|
|
|
|
{
|
|
|
|
if( child->m_Pinned )
|
|
|
|
child->m_Score *= 2;
|
|
|
|
}
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2020-08-18 17:46:50 +00:00
|
|
|
wxStringTokenizer tokenizer( aSearch );
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2020-08-18 17:46:50 +00:00
|
|
|
while( tokenizer.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
const wxString term = tokenizer.GetNextToken().Lower();
|
|
|
|
EDA_COMBINED_MATCHER matcher( term );
|
2017-12-19 13:06:26 +00:00
|
|
|
|
2020-08-18 17:46:50 +00:00
|
|
|
m_tree.UpdateScore( matcher );
|
|
|
|
}
|
2017-12-19 13:06:26 +00:00
|
|
|
|
2020-08-18 17:46:50 +00:00
|
|
|
m_tree.SortNodes();
|
2020-10-06 23:59:11 +00:00
|
|
|
AfterReset();
|
2020-08-21 01:38:14 +00:00
|
|
|
Thaw();
|
2017-12-19 13:06:26 +00:00
|
|
|
}
|
2017-11-30 11:31:36 +00:00
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* bestMatch = ShowResults();
|
2018-06-10 22:20:06 +00:00
|
|
|
|
|
|
|
if( !bestMatch )
|
|
|
|
bestMatch = ShowPreselect();
|
|
|
|
|
|
|
|
if( !bestMatch )
|
|
|
|
bestMatch = ShowSingleLibrary();
|
|
|
|
|
|
|
|
if( bestMatch )
|
|
|
|
{
|
|
|
|
auto item = wxDataViewItem( bestMatch );
|
|
|
|
m_widget->Select( item );
|
2019-04-28 16:00:00 +00:00
|
|
|
|
|
|
|
// Make sure the *parent* item is visible. The selected item is the
|
|
|
|
// first (shown) child of the parent. So it's always right below the parent,
|
|
|
|
// and this way the user can also see what library the selected part belongs to,
|
|
|
|
// without having a case where the selection is off the screen (unless the
|
|
|
|
// window is a single row high, which is unlikely)
|
|
|
|
//
|
|
|
|
// This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400
|
|
|
|
// which appears to be a GTK+3 bug.
|
|
|
|
{
|
|
|
|
wxDataViewItem parent = GetParent( item );
|
|
|
|
|
|
|
|
if( parent.IsOk() )
|
|
|
|
item = parent;
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:20:06 +00:00
|
|
|
m_widget->EnsureVisible( item );
|
|
|
|
}
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2019-08-26 10:57:07 +00:00
|
|
|
wxString partHead = _( "Item" );
|
|
|
|
wxString descHead = _( "Description" );
|
|
|
|
|
2019-12-10 23:24:56 +00:00
|
|
|
// The extent of the text doesn't take into account the space on either side
|
|
|
|
// in the header, so artificially pad it by M
|
2020-10-25 16:41:38 +00:00
|
|
|
wxSize partHeadMinWidth = KIUI::GetTextSize( partHead + "M", aDataViewCtrl );
|
2019-12-10 23:24:56 +00:00
|
|
|
|
2019-08-26 10:57:07 +00:00
|
|
|
if( aDataViewCtrl->GetColumnCount() > 0 )
|
|
|
|
{
|
2019-09-18 23:00:48 +00:00
|
|
|
int partWidth = aDataViewCtrl->GetColumn( PART_COL )->GetWidth();
|
|
|
|
int descWidth = aDataViewCtrl->GetColumn( DESC_COL )->GetWidth();
|
|
|
|
|
|
|
|
// Only use the widths read back if they are non-zero.
|
|
|
|
// GTK returns the displayed width of the column, which is not calculated immediately
|
|
|
|
// this leads to cases of 0 column width if the user types too fast in the filter
|
|
|
|
if( descWidth > 0 )
|
|
|
|
{
|
|
|
|
m_colWidths[PART_COL] = partWidth;
|
|
|
|
m_colWidths[DESC_COL] = descWidth;
|
|
|
|
}
|
2019-08-26 10:57:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 18:28:14 +00:00
|
|
|
m_widget = aDataViewCtrl;
|
|
|
|
aDataViewCtrl->SetIndent( kDataViewIndent );
|
|
|
|
aDataViewCtrl->AssociateModel( this );
|
|
|
|
aDataViewCtrl->ClearColumns();
|
|
|
|
|
2019-09-18 23:00:48 +00:00
|
|
|
m_col_part = aDataViewCtrl->AppendTextColumn( partHead, PART_COL, wxDATAVIEW_CELL_INERT,
|
|
|
|
m_colWidths[PART_COL] );
|
|
|
|
m_col_desc = aDataViewCtrl->AppendTextColumn( descHead, DESC_COL, wxDATAVIEW_CELL_INERT,
|
|
|
|
m_colWidths[DESC_COL] );
|
2019-12-10 23:24:56 +00:00
|
|
|
|
|
|
|
// Ensure the part column is wider than the smallest allowable width
|
|
|
|
if( m_colWidths[PART_COL] < partHeadMinWidth.x )
|
|
|
|
{
|
|
|
|
m_colWidths[PART_COL] = partHeadMinWidth.x;
|
|
|
|
m_col_part->SetWidth( partHeadMinWidth.x );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_col_part->SetMinWidth( partHeadMinWidth.x );
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor( const wxDataViewItem& aSelection ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2019-08-26 10:57:07 +00:00
|
|
|
const LIB_TREE_NODE* node = ToNode( aSelection );
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
LIB_ID emptyId;
|
|
|
|
|
|
|
|
if( !node )
|
|
|
|
return emptyId;
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
return node->m_LibId;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
int LIB_TREE_MODEL_ADAPTER::GetUnitFor( const wxDataViewItem& aSelection ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2019-08-26 10:57:07 +00:00
|
|
|
const LIB_TREE_NODE* node = ToNode( aSelection );
|
2020-02-07 17:06:24 +00:00
|
|
|
return node ? node->m_Unit : 0;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor( const wxDataViewItem& aSelection ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2019-08-26 10:57:07 +00:00
|
|
|
const LIB_TREE_NODE* node = ToNode( aSelection );
|
2020-02-07 17:06:24 +00:00
|
|
|
return node ? node->m_Type : LIB_TREE_NODE::INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::GetTreeNodeFor( const wxDataViewItem& aSelection ) const
|
|
|
|
{
|
|
|
|
return ToNode( aSelection );
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
int LIB_TREE_MODEL_ADAPTER::GetItemCount() const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.m_Children )
|
|
|
|
n += lib->m_Children.size();
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem( const LIB_ID& aLibId )
|
2017-11-14 11:03:19 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
for( auto& lib: m_tree.m_Children )
|
2017-11-14 11:03:19 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
if( lib->m_Name != aLibId.GetLibNickname() )
|
2017-11-14 11:03:19 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// if part name is not specified, return the library node
|
|
|
|
if( aLibId.GetLibItemName() == "" )
|
|
|
|
return ToItem( lib.get() );
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
for( auto& alias: lib->m_Children )
|
2017-11-14 11:03:19 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
if( alias->m_Name == aLibId.GetLibItemName() )
|
2017-11-14 11:03:19 +00:00
|
|
|
return ToItem( alias.get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
break; // could not find the part in the requested library
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxDataViewItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( wxDataViewItem const& aItem,
|
|
|
|
wxDataViewItemArray& aChildren ) const
|
2017-11-21 13:54:30 +00:00
|
|
|
{
|
2020-08-29 23:19:05 +00:00
|
|
|
const LIB_TREE_NODE* node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
|
2017-11-21 13:54:30 +00:00
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
if( node->m_Type != LIB_TREE_NODE::TYPE::LIBID
|
|
|
|
|| ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::LIBID ) )
|
2017-11-21 13:54:30 +00:00
|
|
|
return IntoArray( *node, aChildren );
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-31 01:09:01 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::RefreshTree()
|
2020-03-08 15:14:22 +00:00
|
|
|
{
|
2020-03-31 01:09:01 +00:00
|
|
|
// Yes, this is an enormous hack. But it works on all platforms, it doesn't suffer
|
|
|
|
// the On^2 sorting issues that ItemChanged() does on OSX, and it doesn't lose the
|
|
|
|
// user's scroll position (which re-attaching or deleting/re-inserting columns does).
|
|
|
|
static int walk = 1;
|
|
|
|
|
2020-07-07 18:43:51 +00:00
|
|
|
int partWidth = m_col_part->GetWidth();
|
|
|
|
int descWidth = m_col_desc->GetWidth();
|
|
|
|
|
|
|
|
// Only use the widths read back if they are non-zero.
|
|
|
|
// GTK returns the displayed width of the column, which is not calculated immediately
|
|
|
|
if( descWidth > 0 )
|
|
|
|
{
|
|
|
|
m_colWidths[PART_COL] = partWidth;
|
|
|
|
m_colWidths[DESC_COL] = descWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_colWidths[PART_COL] += walk;
|
|
|
|
m_colWidths[DESC_COL] -= walk;
|
|
|
|
|
|
|
|
m_col_part->SetWidth( m_colWidths[PART_COL] );
|
|
|
|
m_col_desc->SetWidth( m_colWidths[DESC_COL] );
|
2020-03-31 01:09:01 +00:00
|
|
|
walk = -walk;
|
2020-03-08 15:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( wxDataViewItem const& aItem ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
return IsContainer( aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
bool LIB_TREE_MODEL_ADAPTER::IsContainer( wxDataViewItem const& aItem ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-08-29 23:19:05 +00:00
|
|
|
LIB_TREE_NODE* node = ToNode( aItem );
|
2020-02-07 17:06:24 +00:00
|
|
|
return node ? node->m_Children.size() : true;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( wxDataViewItem const& aItem ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-05-21 20:46:00 +00:00
|
|
|
if( m_freeze )
|
|
|
|
return ToItem( nullptr );
|
|
|
|
|
2020-08-29 23:19:05 +00:00
|
|
|
LIB_TREE_NODE* node = ToNode( aItem );
|
|
|
|
LIB_TREE_NODE* parent = node ? node->m_Parent : nullptr;
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
// wxDataViewModel has no root node, but rather top-level elements have
|
|
|
|
// an invalid (null) parent.
|
2020-02-07 17:06:24 +00:00
|
|
|
if( !node || !parent || parent->m_Type == LIB_TREE_NODE::TYPE::ROOT )
|
2017-10-28 18:28:14 +00:00
|
|
|
return ToItem( nullptr );
|
|
|
|
else
|
|
|
|
return ToItem( parent );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::GetValue( wxVariant& aVariant,
|
|
|
|
wxDataViewItem const& aItem,
|
|
|
|
unsigned int aCol ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2018-04-28 09:43:41 +00:00
|
|
|
if( IsFrozen() )
|
|
|
|
{
|
|
|
|
aVariant = wxEmptyString;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-29 23:19:05 +00:00
|
|
|
LIB_TREE_NODE* node = ToNode( aItem );
|
2017-10-28 18:28:14 +00:00
|
|
|
wxASSERT( node );
|
|
|
|
|
|
|
|
switch( aCol )
|
|
|
|
{
|
2017-11-12 13:24:55 +00:00
|
|
|
default: // column == -1 is used for default Compare function
|
2017-10-28 18:28:14 +00:00
|
|
|
case 0:
|
2020-02-07 17:06:24 +00:00
|
|
|
aVariant = node->m_Name;
|
2017-10-28 18:28:14 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2020-02-07 17:06:24 +00:00
|
|
|
aVariant = node->m_Desc;
|
2017-10-28 18:28:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
bool LIB_TREE_MODEL_ADAPTER::GetAttr( wxDataViewItem const& aItem,
|
|
|
|
unsigned int aCol,
|
|
|
|
wxDataViewItemAttr& aAttr ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2018-04-28 09:43:41 +00:00
|
|
|
if( IsFrozen() )
|
|
|
|
return false;
|
|
|
|
|
2020-08-29 23:19:05 +00:00
|
|
|
LIB_TREE_NODE* node = ToNode( aItem );
|
2017-10-28 18:28:14 +00:00
|
|
|
wxASSERT( node );
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
if( node->m_Type != LIB_TREE_NODE::LIBID )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
// Currently only aliases are formatted at all
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
if( !node->m_IsRoot && aCol == 0 )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
// Names of non-root aliases are italicized
|
|
|
|
aAttr.SetItalic( true );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::FindAndExpand( LIB_TREE_NODE& aNode,
|
|
|
|
std::function<bool( LIB_TREE_NODE const* )> aFunc,
|
|
|
|
LIB_TREE_NODE** aHighScore )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
for( auto& node: aNode.m_Children )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
if( aFunc( &*node ) )
|
|
|
|
{
|
2018-06-10 22:20:06 +00:00
|
|
|
auto item = wxDataViewItem( &*node );
|
2017-10-28 18:28:14 +00:00
|
|
|
m_widget->ExpandAncestors( item );
|
2018-06-10 22:20:06 +00:00
|
|
|
|
2020-02-07 17:06:24 +00:00
|
|
|
if( !(*aHighScore) || node->m_Score > (*aHighScore)->m_Score )
|
2018-06-10 22:20:06 +00:00
|
|
|
(*aHighScore) = &*node;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
2018-08-01 00:21:50 +00:00
|
|
|
|
|
|
|
FindAndExpand( *node, aFunc, aHighScore );
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowResults()
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* highScore = nullptr;
|
2018-06-10 22:20:06 +00:00
|
|
|
|
|
|
|
FindAndExpand( m_tree,
|
2018-07-27 20:47:51 +00:00
|
|
|
[]( LIB_TREE_NODE const* n )
|
2018-06-10 22:20:06 +00:00
|
|
|
{
|
2018-08-19 16:57:09 +00:00
|
|
|
// return leaf nodes with some level of matching
|
2020-02-07 17:06:24 +00:00
|
|
|
return n->m_Type == LIB_TREE_NODE::TYPE::LIBID && n->m_Score > 1;
|
2018-06-10 22:20:06 +00:00
|
|
|
},
|
|
|
|
&highScore );
|
|
|
|
|
|
|
|
return highScore;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowPreselect()
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* highScore = nullptr;
|
2018-06-10 22:20:06 +00:00
|
|
|
|
2017-10-28 18:28:14 +00:00
|
|
|
if( !m_preselect_lib_id.IsValid() )
|
2018-06-10 22:20:06 +00:00
|
|
|
return highScore;
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2018-06-10 22:20:06 +00:00
|
|
|
FindAndExpand( m_tree,
|
2018-07-27 20:47:51 +00:00
|
|
|
[&]( LIB_TREE_NODE const* n )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
if( n->m_Type == LIB_TREE_NODE::LIBID && ( n->m_Children.empty() || !m_preselect_unit ) )
|
|
|
|
return m_preselect_lib_id == n->m_LibId;
|
|
|
|
else if( n->m_Type == LIB_TREE_NODE::UNIT && m_preselect_unit )
|
|
|
|
return m_preselect_lib_id == n->m_Parent->m_LibId && m_preselect_unit == n->m_Unit;
|
2017-10-28 18:28:14 +00:00
|
|
|
else
|
|
|
|
return false;
|
2018-06-10 22:20:06 +00:00
|
|
|
},
|
|
|
|
&highScore );
|
|
|
|
|
|
|
|
return highScore;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowSingleLibrary()
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE* highScore = nullptr;
|
2018-06-10 22:20:06 +00:00
|
|
|
|
|
|
|
FindAndExpand( m_tree,
|
2018-07-27 20:47:51 +00:00
|
|
|
[]( LIB_TREE_NODE const* n )
|
2018-06-10 22:20:06 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
return n->m_Type == LIB_TREE_NODE::TYPE::LIBID &&
|
|
|
|
n->m_Parent->m_Parent->m_Children.size() == 1;
|
2018-06-10 22:20:06 +00:00
|
|
|
},
|
|
|
|
&highScore );
|
|
|
|
|
|
|
|
return highScore;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|