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>
|
2023-04-14 22:34:14 +00:00
|
|
|
* Copyright (C) 2014-2023 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>
|
2021-09-14 22:45:14 +00:00
|
|
|
#include <kiface_base.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>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const int kDataViewIndent = 20;
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem( const LIB_TREE_NODE* aNode )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent,
|
|
|
|
const wxString& aPinnedKey ) :
|
2020-02-14 14:22:21 +00:00
|
|
|
m_parent( aParent ),
|
2023-05-01 20:26:29 +00:00
|
|
|
m_sort_mode( BEST_MATCH ),
|
2020-02-14 14:22:21 +00:00
|
|
|
m_show_units( true ),
|
|
|
|
m_preselect_unit( 0 ),
|
|
|
|
m_freeze( 0 ),
|
2023-09-29 16:02:57 +00:00
|
|
|
m_widget( nullptr ),
|
|
|
|
m_filter( nullptr )
|
2019-09-18 23:00:48 +00:00
|
|
|
{
|
2022-08-28 23:02:12 +00:00
|
|
|
// Default column widths. Do not translate these names.
|
2023-04-11 22:56:12 +00:00
|
|
|
m_colWidths[ _HKI( "Item" ) ] = 300;
|
|
|
|
m_colWidths[ _HKI( "Description" ) ] = 600;
|
2019-09-18 23:00:48 +00:00
|
|
|
|
2023-04-11 22:56:12 +00:00
|
|
|
m_availableColumns = { _HKI( "Item" ), _HKI( "Description" ) };
|
2022-09-24 03:45:00 +00:00
|
|
|
|
2021-03-30 19:58:35 +00:00
|
|
|
APP_SETTINGS_BASE* cfg = Kiface().KifaceSettings();
|
2022-08-28 23:02:12 +00:00
|
|
|
|
|
|
|
for( const std::pair<const wxString, int>& pair : cfg->m_LibTree.column_widths )
|
|
|
|
m_colWidths[pair.first] = pair.second;
|
2022-09-24 03:45:00 +00:00
|
|
|
|
|
|
|
m_shownColumns = cfg->m_LibTree.columns;
|
|
|
|
|
|
|
|
if( m_shownColumns.empty() )
|
2023-04-11 22:56:12 +00:00
|
|
|
m_shownColumns = { _HKI( "Item" ), _HKI( "Description" ) };
|
2022-09-24 03:45:00 +00:00
|
|
|
|
2023-04-11 22:56:12 +00:00
|
|
|
if( m_shownColumns[0] != _HKI( "Item" ) )
|
|
|
|
m_shownColumns.insert( m_shownColumns.begin(), _HKI( "Item" ) );
|
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
|
|
|
{}
|
|
|
|
|
|
|
|
|
2022-09-24 03:45:00 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::SaveSettings()
|
2019-09-18 23:00:48 +00:00
|
|
|
{
|
|
|
|
if( m_widget )
|
|
|
|
{
|
2021-03-30 19:58:35 +00:00
|
|
|
APP_SETTINGS_BASE* cfg = Kiface().KifaceSettings();
|
2022-08-28 23:02:12 +00:00
|
|
|
|
2022-09-24 03:45:00 +00:00
|
|
|
cfg->m_LibTree.columns = GetShownColumns();
|
2022-08-28 23:02:12 +00:00
|
|
|
cfg->m_LibTree.column_widths.clear();
|
|
|
|
|
|
|
|
for( const std::pair<const wxString, wxDataViewColumn*>& pair : m_colNameMap )
|
|
|
|
cfg->m_LibTree.column_widths[pair.first] = pair.second->GetWidth();
|
2019-09-18 23:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:22:21 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::SetPreselectNode( const LIB_ID& aLibId, int aUnit )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
m_preselect_lib_id = aLibId;
|
|
|
|
m_preselect_unit = aUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
LIB_TREE_NODE_LIB& LIB_TREE_MODEL_ADAPTER::DoAddLibraryNode( const wxString& aNodeName,
|
2022-07-22 08:26:49 +00:00
|
|
|
const wxString& aDesc, bool pinned )
|
2020-02-14 14:22:21 +00:00
|
|
|
{
|
|
|
|
LIB_TREE_NODE_LIB& lib_node = m_tree.AddLib( aNodeName, aDesc );
|
|
|
|
|
2022-07-22 08:26:49 +00:00
|
|
|
lib_node.m_Pinned = pinned;
|
2020-02-14 14:22:21 +00:00
|
|
|
|
|
|
|
return lib_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::DoAddLibrary( const wxString& aNodeName, const wxString& aDesc,
|
|
|
|
const std::vector<LIB_TREE_ITEM*>& aItemList,
|
2022-07-22 08:26:49 +00:00
|
|
|
bool pinned, bool presorted )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2022-07-22 08:26:49 +00:00
|
|
|
LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( aNodeName, aDesc, pinned );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool aState )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2020-02-07 17:06:24 +00:00
|
|
|
{
|
2020-08-18 17:46:50 +00:00
|
|
|
wxWindowUpdateLocker updateLock( m_widget );
|
2020-10-06 23:59:11 +00:00
|
|
|
|
2021-01-04 17:12:18 +00:00
|
|
|
// Even with the updateLock, wxWidgets sometimes ties its knickers in a knot 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-12-03 22:03:43 +00:00
|
|
|
// This collapse is required before the call to "Freeze()" below. Once Freeze()
|
|
|
|
// is called, GetParent() will return nullptr. While this works for some calls, it
|
2021-02-04 21:18:36 +00:00
|
|
|
// segfaults when we have any expanded elements b/c the sub units in the tree don't
|
|
|
|
// have explicit references that are maintained over a search
|
2020-12-03 22:03:43 +00:00
|
|
|
// The tree will be expanded again below when we get our matches
|
2021-01-04 17:12:18 +00:00
|
|
|
//
|
|
|
|
// Also note that this cannot happen when we have deleted a symbol as GTK will also
|
|
|
|
// iterate over the tree in this case and find a symbol that has an invalid link
|
|
|
|
// and crash https://gitlab.com/kicad/code/kicad/-/issues/6910
|
|
|
|
if( !aState && !aSearch.IsNull() && m_tree.m_Children.size() )
|
2021-02-04 21:18:36 +00:00
|
|
|
{
|
|
|
|
for( std::unique_ptr<LIB_TREE_NODE>& child: m_tree.m_Children )
|
|
|
|
m_widget->Collapse( wxDataViewItem( &*child ) );
|
|
|
|
}
|
2020-12-03 22:03:43 +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-21 01:38:14 +00:00
|
|
|
Freeze();
|
2020-10-29 22:44:32 +00:00
|
|
|
BeforeReset();
|
2020-02-07 17:06:24 +00:00
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
m_tree.ResetScore( m_filter );
|
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
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
if( tokenizer.HasMoreTokens() )
|
2020-08-18 17:46:50 +00:00
|
|
|
{
|
2023-09-29 16:02:57 +00:00
|
|
|
while( tokenizer.HasMoreTokens() )
|
2021-12-14 15:18:11 +00:00
|
|
|
{
|
2023-09-29 16:02:57 +00:00
|
|
|
wxString lib;
|
|
|
|
wxString term = tokenizer.GetNextToken().Lower();
|
|
|
|
|
|
|
|
if( term.Contains( ":" ) )
|
|
|
|
{
|
|
|
|
lib = term.BeforeFirst( ':' );
|
|
|
|
term = term.AfterFirst( ':' );
|
|
|
|
}
|
2021-12-14 15:18:11 +00:00
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
EDA_COMBINED_MATCHER matcher( term, CTX_LIBITEM );
|
2017-12-19 13:06:26 +00:00
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
m_tree.UpdateScore( &matcher, lib );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_tree.UpdateScore( nullptr, wxEmptyString );
|
2020-08-18 17:46:50 +00:00
|
|
|
}
|
2017-12-19 13:06:26 +00:00
|
|
|
|
2023-05-01 20:26:29 +00:00
|
|
|
m_tree.SortNodes( m_sort_mode == BEST_MATCH );
|
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
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
const LIB_TREE_NODE* firstMatch = ShowResults();
|
2018-06-10 22:20:06 +00:00
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
if( firstMatch )
|
2018-06-10 22:20:06 +00:00
|
|
|
{
|
2023-04-14 22:34:14 +00:00
|
|
|
wxDataViewItem item = ToItem( firstMatch );
|
2018-06-10 22:20:06 +00:00
|
|
|
m_widget->Select( item );
|
2019-04-28 16:00:00 +00:00
|
|
|
|
2023-07-15 22:22:39 +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).
|
2019-04-28 16:00:00 +00:00
|
|
|
//
|
2023-07-15 22:22:39 +00:00
|
|
|
// This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400 which
|
|
|
|
// appears to be a GTK+3 bug.
|
2019-04-28 16:00:00 +00:00
|
|
|
{
|
|
|
|
wxDataViewItem parent = GetParent( item );
|
|
|
|
|
|
|
|
if( parent.IsOk() )
|
2022-06-10 18:45:25 +00:00
|
|
|
m_widget->EnsureVisible( parent );
|
2019-04-28 16:00:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-08-28 23:02:12 +00:00
|
|
|
m_widget = aDataViewCtrl;
|
|
|
|
aDataViewCtrl->SetIndent( kDataViewIndent );
|
|
|
|
aDataViewCtrl->AssociateModel( this );
|
2022-09-24 03:45:00 +00:00
|
|
|
recreateColumns();
|
|
|
|
}
|
2022-08-28 23:02:12 +00:00
|
|
|
|
|
|
|
|
2022-09-24 03:45:00 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::recreateColumns()
|
|
|
|
{
|
|
|
|
m_widget->ClearColumns();
|
2022-08-28 23:02:12 +00:00
|
|
|
|
2022-09-24 03:45:00 +00:00
|
|
|
m_columns.clear();
|
|
|
|
m_colIdxMap.clear();
|
|
|
|
m_colNameMap.clear();
|
|
|
|
|
|
|
|
// The Item column is always shown
|
|
|
|
doAddColumn( wxT( "Item" ) );
|
|
|
|
|
|
|
|
for( const wxString& colName : m_shownColumns )
|
2022-08-28 23:02:12 +00:00
|
|
|
{
|
2022-09-24 03:45:00 +00:00
|
|
|
if( !m_colNameMap.count( colName ) )
|
2023-04-11 22:51:27 +00:00
|
|
|
doAddColumn( colName, colName == wxT( "Description" ) );
|
2022-08-28 23:02:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-21 13:44:13 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::resortTree()
|
|
|
|
{
|
|
|
|
Freeze();
|
|
|
|
BeforeReset();
|
|
|
|
|
2023-05-01 20:26:29 +00:00
|
|
|
m_tree.SortNodes( m_sort_mode == BEST_MATCH );
|
2022-09-21 13:44:13 +00:00
|
|
|
|
|
|
|
AfterReset();
|
|
|
|
Thaw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_TREE_MODEL_ADAPTER::PinLibrary( LIB_TREE_NODE* aTreeNode )
|
|
|
|
{
|
|
|
|
m_parent->Prj().PinLibrary( aTreeNode->m_LibId.GetLibNickname(), isSymbolModel() );
|
|
|
|
aTreeNode->m_Pinned = true;
|
|
|
|
|
|
|
|
resortTree();
|
|
|
|
m_widget->EnsureVisible( ToItem( aTreeNode ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_TREE_MODEL_ADAPTER::UnpinLibrary( LIB_TREE_NODE* aTreeNode )
|
|
|
|
{
|
|
|
|
m_parent->Prj().UnpinLibrary( aTreeNode->m_LibId.GetLibNickname(), isSymbolModel() );
|
|
|
|
aTreeNode->m_Pinned = false;
|
|
|
|
|
|
|
|
resortTree();
|
|
|
|
// Keep focus at top when unpinning
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::doAddColumn( const wxString& aHeader, bool aTranslate )
|
|
|
|
{
|
|
|
|
wxString translatedHeader = aTranslate ? wxGetTranslation( aHeader ) : aHeader;
|
2019-08-26 10:57:07 +00:00
|
|
|
|
2019-12-10 23:24:56 +00:00
|
|
|
// The extent of the text doesn't take into account the space on either side
|
2021-04-01 09:49:05 +00:00
|
|
|
// in the header, so artificially pad it
|
2022-08-28 23:02:12 +00:00
|
|
|
wxSize headerMinWidth = KIUI::GetTextSize( translatedHeader + wxT( "MMM" ), m_widget );
|
2019-12-10 23:24:56 +00:00
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
if( !m_colWidths.count( aHeader ) || m_colWidths[aHeader] < headerMinWidth.x )
|
|
|
|
m_colWidths[aHeader] = headerMinWidth.x;
|
2019-08-26 10:57:07 +00:00
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
int index = m_columns.size();
|
|
|
|
|
|
|
|
wxDataViewColumn* ret = m_widget->AppendTextColumn( translatedHeader, index,
|
|
|
|
wxDATAVIEW_CELL_INERT,
|
|
|
|
m_colWidths[aHeader] );
|
|
|
|
ret->SetMinWidth( headerMinWidth.x );
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
m_columns.emplace_back( ret );
|
|
|
|
m_colNameMap[aHeader] = ret;
|
|
|
|
m_colIdxMap[m_columns.size() - 1] = aHeader;
|
2019-12-10 23:24:56 +00:00
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_TREE_MODEL_ADAPTER::addColumnIfNecessary( const wxString& aHeader )
|
|
|
|
{
|
|
|
|
if( m_colNameMap.count( aHeader ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Columns will be created later
|
|
|
|
m_colNameMap[aHeader] = nullptr;
|
2022-09-24 03:45:00 +00:00
|
|
|
m_availableColumns.emplace_back( aHeader );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_TREE_MODEL_ADAPTER::SetShownColumns( const std::vector<wxString>& aColumnNames )
|
|
|
|
{
|
|
|
|
bool recreate = m_shownColumns != aColumnNames;
|
|
|
|
|
|
|
|
m_shownColumns = aColumnNames;
|
|
|
|
|
|
|
|
if( recreate && m_widget )
|
|
|
|
recreateColumns();
|
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 );
|
2023-07-15 22:22:39 +00:00
|
|
|
return node ? node->m_LibId : LIB_ID();
|
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
|
|
|
{
|
2021-03-30 19:58:35 +00:00
|
|
|
for( std::unique_ptr<LIB_TREE_NODE>& 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() );
|
|
|
|
|
2021-03-30 19:58:35 +00:00
|
|
|
for( std::unique_ptr<LIB_TREE_NODE>& 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-04 19:51:28 +00:00
|
|
|
wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetCurrentDataViewItem()
|
|
|
|
{
|
|
|
|
return FindItem( m_preselect_lib_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren( const wxDataViewItem& aItem,
|
2018-07-27 20:47:51 +00:00
|
|
|
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 );
|
2023-07-15 22:22:39 +00:00
|
|
|
unsigned int count = 0;
|
2017-11-21 13:54:30 +00:00
|
|
|
|
2021-10-02 09:40:12 +00:00
|
|
|
if( node->m_Type == LIB_TREE_NODE::TYPE::ROOT
|
|
|
|
|| node->m_Type == LIB_TREE_NODE::LIB
|
2020-02-07 17:06:24 +00:00
|
|
|
|| ( m_show_units && node->m_Type == LIB_TREE_NODE::TYPE::LIBID ) )
|
2021-10-02 09:40:12 +00:00
|
|
|
{
|
2023-07-15 22:22:39 +00:00
|
|
|
for( std::unique_ptr<LIB_TREE_NODE> const& child: node->m_Children )
|
|
|
|
{
|
|
|
|
if( child->m_Score > 0 )
|
|
|
|
{
|
|
|
|
aChildren.Add( ToItem( &*child ) );
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
2021-10-02 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 22:22:39 +00:00
|
|
|
return count;
|
2017-11-21 13:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-01 09:49:05 +00:00
|
|
|
void LIB_TREE_MODEL_ADAPTER::FinishTreeInitialization()
|
|
|
|
{
|
2022-08-28 23:02:12 +00:00
|
|
|
wxDataViewColumn* col = nullptr;
|
|
|
|
size_t idx = 0;
|
|
|
|
int totalWidth = 0;
|
|
|
|
wxString header;
|
|
|
|
|
|
|
|
for( ; idx < m_columns.size() - 1; idx++ )
|
|
|
|
{
|
2022-09-06 13:18:34 +00:00
|
|
|
wxASSERT( m_colIdxMap.count( idx ) );
|
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
col = m_columns[idx];
|
2022-09-06 13:18:34 +00:00
|
|
|
header = m_colIdxMap[idx];
|
2022-08-28 23:02:12 +00:00
|
|
|
|
|
|
|
wxASSERT( m_colWidths.count( header ) );
|
|
|
|
|
|
|
|
col->SetWidth( m_colWidths[header] );
|
|
|
|
totalWidth += col->GetWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
int remainingWidth = m_widget->GetSize().x - totalWidth;
|
|
|
|
header = m_columns[idx]->GetTitle();
|
|
|
|
|
|
|
|
m_columns[idx]->SetWidth( std::max( m_colWidths[header], remainingWidth ) );
|
2022-07-28 13:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
std::vector<int> widths;
|
|
|
|
|
|
|
|
for( const wxDataViewColumn* col : m_columns )
|
|
|
|
widths.emplace_back( col->GetWidth() );
|
|
|
|
|
|
|
|
wxASSERT( widths.size() );
|
2020-07-07 18:43:51 +00:00
|
|
|
|
|
|
|
// Only use the widths read back if they are non-zero.
|
|
|
|
// GTK returns the displayed width of the column, which is not calculated immediately
|
2022-08-28 23:02:12 +00:00
|
|
|
if( widths[0] > 0 )
|
2020-07-07 18:43:51 +00:00
|
|
|
{
|
2022-08-28 23:02:12 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
2022-10-16 16:45:10 +00:00
|
|
|
for( const auto& [ colName, colPtr ] : m_colNameMap )
|
|
|
|
m_colWidths[ colName ] = widths[i++];
|
2020-07-07 18:43:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
auto colIt = m_colWidths.begin();
|
|
|
|
|
|
|
|
colIt->second += walk;
|
|
|
|
colIt++;
|
|
|
|
|
|
|
|
if( colIt != m_colWidths.end() )
|
|
|
|
colIt->second -= walk;
|
|
|
|
|
2022-10-16 16:45:10 +00:00
|
|
|
for( const auto& [ colName, colPtr ] : m_colNameMap )
|
2022-08-28 23:02:12 +00:00
|
|
|
{
|
2022-10-16 16:45:10 +00:00
|
|
|
if( colPtr == m_columns[0] )
|
2022-08-28 23:02:12 +00:00
|
|
|
continue;
|
|
|
|
|
2022-10-16 16:45:10 +00:00
|
|
|
wxASSERT( m_colWidths.count( colName ) );
|
|
|
|
colPtr->SetWidth( m_colWidths[ colName ] );
|
2022-08-28 23:02:12 +00:00
|
|
|
}
|
2020-07-07 18:43:51 +00:00
|
|
|
|
2020-03-31 01:09:01 +00:00
|
|
|
walk = -walk;
|
2020-03-08 15:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns( const wxDataViewItem& aItem ) const
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
|
|
|
return IsContainer( aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
bool LIB_TREE_MODEL_ADAPTER::IsContainer( const wxDataViewItem& 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent( const wxDataViewItem& 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,
|
2021-07-27 12:22:27 +00:00
|
|
|
const wxDataViewItem& aItem,
|
2018-07-27 20:47:51 +00:00
|
|
|
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 );
|
2023-07-15 22:22:39 +00:00
|
|
|
wxCHECK( node, /* void */ );
|
2017-10-28 18:28:14 +00:00
|
|
|
|
|
|
|
switch( aCol )
|
|
|
|
{
|
2022-08-28 23:02:12 +00:00
|
|
|
case NAME_COL:
|
2022-09-21 13:44:13 +00:00
|
|
|
if( node->m_Pinned )
|
|
|
|
aVariant = GetPinningSymbol() + UnescapeString( node->m_Name );
|
|
|
|
else
|
|
|
|
aVariant = UnescapeString( node->m_Name );
|
|
|
|
|
2017-10-28 18:28:14 +00:00
|
|
|
break;
|
2022-08-28 23:02:12 +00:00
|
|
|
|
|
|
|
default:
|
2022-09-21 13:44:13 +00:00
|
|
|
if( m_colIdxMap.count( aCol ) )
|
|
|
|
{
|
|
|
|
const wxString& key = m_colIdxMap.at( aCol );
|
2022-08-28 23:02:12 +00:00
|
|
|
|
2022-09-21 13:44:13 +00:00
|
|
|
if( node->m_Fields.count( key ) )
|
|
|
|
aVariant = node->m_Fields.at( key );
|
2022-09-24 03:45:00 +00:00
|
|
|
else if( key == wxT( "Description" ) )
|
|
|
|
aVariant = node->m_Desc;
|
2022-09-21 13:44:13 +00:00
|
|
|
else
|
|
|
|
aVariant = wxEmptyString;
|
|
|
|
}
|
2022-08-28 23:02:12 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
bool LIB_TREE_MODEL_ADAPTER::GetAttr( const wxDataViewItem& aItem,
|
2018-07-27 20:47:51 +00:00
|
|
|
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 );
|
2023-07-15 22:22:39 +00:00
|
|
|
wxCHECK( node, false );
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2023-07-15 22:22:39 +00:00
|
|
|
if( node->m_Type == LIB_TREE_NODE::LIBID )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2023-07-15 22:22:39 +00:00
|
|
|
if( !node->m_IsRoot && aCol == 0 )
|
|
|
|
{
|
|
|
|
// Names of non-root aliases are italicized
|
|
|
|
aAttr.SetItalic( true );
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 22:22:39 +00:00
|
|
|
return false;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
void recursiveDescent( LIB_TREE_NODE& aNode, const std::function<bool( const LIB_TREE_NODE* )>& f )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2021-03-30 19:58:35 +00:00
|
|
|
for( std::unique_ptr<LIB_TREE_NODE>& node: aNode.m_Children )
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2023-04-14 22:34:14 +00:00
|
|
|
if( !f( node.get() ) )
|
|
|
|
break;
|
2018-08-01 00:21:50 +00:00
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
recursiveDescent( *node, f );
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
const LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowResults()
|
2017-10-28 18:28:14 +00:00
|
|
|
{
|
2023-04-14 22:34:14 +00:00
|
|
|
const LIB_TREE_NODE* firstMatch = nullptr;
|
2018-06-10 22:20:06 +00:00
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
// Expand parents of leaf nodes with some level of matching
|
|
|
|
recursiveDescent( m_tree,
|
|
|
|
[&]( const LIB_TREE_NODE* n )
|
|
|
|
{
|
|
|
|
if( n->m_Type == LIB_TREE_NODE::TYPE::LIBID && n->m_Score > 1 )
|
|
|
|
{
|
|
|
|
if( !firstMatch )
|
|
|
|
firstMatch = n;
|
2023-04-30 11:32:19 +00:00
|
|
|
else if( n->m_Score > firstMatch->m_Score )
|
|
|
|
firstMatch = n;
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
m_widget->ExpandAncestors( ToItem( n ) );
|
|
|
|
}
|
2017-10-28 18:28:14 +00:00
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
return true; // keep going to expand ancestors of all found items
|
|
|
|
} );
|
2018-06-10 22:20:06 +00:00
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
// If no matches, find and show the preselect node
|
|
|
|
if( !firstMatch && m_preselect_lib_id.IsValid() )
|
2022-06-10 18:45:25 +00:00
|
|
|
{
|
2023-04-14 22:34:14 +00:00
|
|
|
recursiveDescent( m_tree,
|
|
|
|
[&]( const LIB_TREE_NODE* n )
|
|
|
|
{
|
|
|
|
if( n->m_Type == LIB_TREE_NODE::LIBID
|
|
|
|
&& ( n->m_Children.empty() || !m_preselect_unit )
|
|
|
|
&& m_preselect_lib_id == n->m_LibId )
|
|
|
|
{
|
|
|
|
firstMatch = n;
|
|
|
|
m_widget->ExpandAncestors( ToItem( n ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if( n->m_Type == LIB_TREE_NODE::UNIT
|
|
|
|
&& ( m_preselect_unit && m_preselect_unit == n->m_Unit )
|
|
|
|
&& m_preselect_lib_id == n->m_Parent->m_LibId )
|
|
|
|
{
|
|
|
|
firstMatch = n;
|
|
|
|
m_widget->ExpandAncestors( ToItem( n ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} );
|
2022-06-10 18:45:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
// If still no matches expand a single library if there is only one
|
|
|
|
if( !firstMatch )
|
2022-06-10 18:45:25 +00:00
|
|
|
{
|
2023-04-14 22:34:14 +00:00
|
|
|
recursiveDescent( m_tree,
|
|
|
|
[&]( const LIB_TREE_NODE* n )
|
|
|
|
{
|
|
|
|
if( n->m_Type == LIB_TREE_NODE::TYPE::LIBID
|
|
|
|
&& n->m_Parent->m_Parent->m_Children.size() == 1 )
|
|
|
|
{
|
|
|
|
firstMatch = n;
|
|
|
|
m_widget->ExpandAncestors( ToItem( n ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} );
|
2022-06-10 18:45:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
return firstMatch;
|
2017-10-28 18:28:14 +00:00
|
|
|
}
|
2023-04-14 22:34:14 +00:00
|
|
|
|
|
|
|
|