2017-03-06 14:50:48 +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-11-08 12:29:14 +00:00
|
|
|
* Copyright (C) 2023 CERN
|
2023-05-01 20:26:29 +00:00
|
|
|
* Copyright (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-03-06 14:50:48 +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/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
#ifndef LIB_TREE_MODEL_H
|
|
|
|
#define LIB_TREE_MODEL_H
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
#include <vector>
|
2022-08-28 23:02:12 +00:00
|
|
|
#include <map>
|
2017-03-06 14:50:48 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <wx/string.h>
|
2023-05-01 20:26:29 +00:00
|
|
|
#include <eda_pattern_match.h>
|
2018-07-27 20:47:51 +00:00
|
|
|
#include <lib_tree_item.h>
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model class in the component selector Model-View-Adapter (mediated MVC)
|
|
|
|
* architecture. The other pieces are in:
|
|
|
|
*
|
2018-07-27 20:47:51 +00:00
|
|
|
* - Adapter: LIB_TREE_MODEL_ADAPTER in common/lib_tree_model_adapter.h
|
2017-03-06 14:50:48 +00:00
|
|
|
* - View:
|
|
|
|
* - DIALOG_CHOOSE_COMPONENT in eeschema/dialogs/dialog_choose_component.h
|
|
|
|
* - wxDataViewCtrl
|
|
|
|
*
|
|
|
|
* This model is populated from LIB_ALIASes; helper methods in the adapter
|
|
|
|
* can accept entire libraries.
|
|
|
|
*
|
|
|
|
* Quick summary of methods used to populate this class:
|
|
|
|
* - `CMP_TREE_NODE_ROOT::AddLib()` - add a new, empty library to the root
|
|
|
|
* - `CMP_TREE_NODE_LIB::AddAlias()` - add an alias and its units from a
|
|
|
|
* given LIB_ALIAS*
|
|
|
|
*
|
|
|
|
* Quick summary of methods used to drive this class:
|
|
|
|
*
|
|
|
|
* - `UpdateScore()` - accumulate scores recursively given a new search token
|
|
|
|
* - `ResetScore()` - reset scores recursively for a new search string
|
|
|
|
* - `AssignIntrinsicRanks()` - calculate and cache the initial sort order
|
|
|
|
* - `SortNodes()` - recursively sort the tree by score
|
|
|
|
* - `Compare()` - compare two nodes; used by `SortNodes()`
|
|
|
|
*
|
|
|
|
* The data in the model is meant to be accessed directly. Quick summary of
|
|
|
|
* data members:
|
|
|
|
*
|
|
|
|
* - `Parent` - parent node, or nullptr if root
|
|
|
|
* - `Children` - vector of unique_ptrs to children
|
|
|
|
* - `Type` - ROOT, LIB, ALIAS, or UNIT
|
2020-02-07 17:06:24 +00:00
|
|
|
* - `m_IntrinsicRank` - cached initial sort order
|
|
|
|
* - `m_Score` - score taking into account search terms. Zero means irrelevant and
|
2017-03-06 14:50:48 +00:00
|
|
|
* should be hidden.
|
|
|
|
* - `Name` - name of the library/alias/unit, to be displayed
|
|
|
|
* - `Desc` - description of the alias, to be displayed
|
2020-02-07 17:06:24 +00:00
|
|
|
* - `m_MatchName` - Name, normalized to lowercase for matching
|
|
|
|
* - `m_SearchText` - normalized composite of keywords and description
|
2017-09-15 14:17:44 +00:00
|
|
|
* - `LibId` - the #LIB_ID this alias or unit is from, or not valid
|
2017-03-06 14:50:48 +00:00
|
|
|
* - `Unit` - the unit number, or zero for non-units
|
|
|
|
*/
|
2020-02-07 17:06:24 +00:00
|
|
|
class LIB_TREE_NODE
|
|
|
|
{
|
2017-03-06 14:50:48 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Update the score for this part. This is accumulative - it will be
|
|
|
|
* called once per search term.
|
|
|
|
*
|
2017-09-15 14:17:44 +00:00
|
|
|
* @param aMatcher an EDA_COMBINED_MATCHER initialized with the search term
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-11-08 12:29:14 +00:00
|
|
|
virtual void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
|
|
|
|
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) = 0;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
2023-11-08 12:29:14 +00:00
|
|
|
* Initialize scores recursively.
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-11-08 12:29:14 +00:00
|
|
|
virtual void ResetScore();
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2024-04-02 22:51:19 +00:00
|
|
|
virtual void ForceScore( int aScore ) { m_Score = aScore; }
|
|
|
|
|
2017-03-06 14:50:48 +00:00
|
|
|
/**
|
2020-02-07 17:06:24 +00:00
|
|
|
* Store intrinsic ranks on all children of this node. See m_IntrinsicRank
|
2017-03-06 14:50:48 +00:00
|
|
|
* member doc for more information.
|
|
|
|
*/
|
2018-08-05 11:56:02 +00:00
|
|
|
void AssignIntrinsicRanks( bool presorted = false );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort child nodes quickly and recursively (IntrinsicRanks must have been set).
|
|
|
|
*/
|
2023-05-01 20:26:29 +00:00
|
|
|
void SortNodes( bool aUseScores );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
2023-02-16 20:56:19 +00:00
|
|
|
* Compare two nodes. Returns true if aNode1 < aNode2.
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-05-01 20:26:29 +00:00
|
|
|
static bool Compare( LIB_TREE_NODE const& aNode1, LIB_TREE_NODE const& aNode2,
|
|
|
|
bool aUseScores );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE();
|
|
|
|
virtual ~LIB_TREE_NODE() {}
|
2020-12-19 23:29:10 +00:00
|
|
|
|
2024-06-21 13:16:03 +00:00
|
|
|
enum class TYPE
|
2023-05-01 20:26:29 +00:00
|
|
|
{
|
|
|
|
ROOT,
|
2023-11-08 17:34:14 +00:00
|
|
|
LIBRARY,
|
2023-11-17 18:17:50 +00:00
|
|
|
ITEM,
|
2023-05-01 20:26:29 +00:00
|
|
|
UNIT,
|
|
|
|
INVALID
|
2020-12-19 23:29:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<std::unique_ptr<LIB_TREE_NODE>> PTR_VECTOR;
|
|
|
|
|
|
|
|
LIB_TREE_NODE* m_Parent; // Parent node or null
|
|
|
|
PTR_VECTOR m_Children; // List of child nodes
|
|
|
|
enum TYPE m_Type; // Node type
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The rank of the item before any search terms are applied. This is
|
|
|
|
* a fairly expensive sort (involving string compares) so it helps to
|
|
|
|
* store the result of that sort.
|
|
|
|
*/
|
|
|
|
int m_IntrinsicRank;
|
|
|
|
|
|
|
|
int m_Score; // The score of an item resulting from the search algorithm.
|
|
|
|
bool m_Pinned; // Item should appear at top when there is no search string
|
|
|
|
|
|
|
|
wxString m_Name; // Actual name of the part
|
|
|
|
wxString m_Desc; // Description to be displayed
|
2022-06-05 17:58:27 +00:00
|
|
|
wxString m_Footprint; // Footprint ID as a string (ie: the footprint field text)
|
2023-09-29 16:02:57 +00:00
|
|
|
int m_PinCount; // Pin count from symbol, or unique pad count from footprint
|
2020-12-19 23:29:10 +00:00
|
|
|
|
2023-05-01 20:26:29 +00:00
|
|
|
std::vector<SEARCH_TERM> m_SearchTerms; /// List of weighted search terms
|
|
|
|
std::map<wxString, wxString> m_Fields; /// @see LIB_TREE_ITEMS::GetChooserFields
|
2020-12-19 23:29:10 +00:00
|
|
|
|
|
|
|
LIB_ID m_LibId; // LIB_ID determined by the parent library nickname and alias name.
|
|
|
|
int m_Unit; // Actual unit, or zero
|
|
|
|
bool m_IsRoot; // Indicates if the symbol is a root symbol instead of an alias.
|
2017-03-06 14:50:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node type: unit of component.
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
class LIB_TREE_NODE_UNIT: public LIB_TREE_NODE
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The addresses of CMP_TREE_NODEs are used as unique IDs for the
|
|
|
|
* wxDataViewModel, so don't let them be copied around.
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE_UNIT( LIB_TREE_NODE_UNIT const& _ ) = delete;
|
|
|
|
void operator=( LIB_TREE_NODE_UNIT const& _ ) = delete;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a unit node.
|
|
|
|
*
|
|
|
|
* The name of the unit will be "Unit %s", where %s is aUnit formatted
|
|
|
|
* by LIB_PART::SubReference.
|
|
|
|
*
|
|
|
|
* @param aParent parent node, should be a CMP_TREE_NODE_ALIAS
|
2018-07-27 20:47:51 +00:00
|
|
|
* @param aItem parent item
|
2017-03-06 14:50:48 +00:00
|
|
|
* @param aUnit unit number
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE_UNIT( LIB_TREE_NODE* aParent, LIB_TREE_ITEM* aItem, int aUnit );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2023-11-17 18:13:14 +00:00
|
|
|
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
|
|
|
|
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
|
2017-03-06 14:50:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:17:44 +00:00
|
|
|
* Node type: #LIB_ID.
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-11-17 18:17:50 +00:00
|
|
|
class LIB_TREE_NODE_ITEM : public LIB_TREE_NODE
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The addresses of CMP_TREE_NODEs are used as unique IDs for the
|
|
|
|
* wxDataViewModel, so don't let them be copied around.
|
|
|
|
*/
|
2023-11-17 18:17:50 +00:00
|
|
|
LIB_TREE_NODE_ITEM( LIB_TREE_NODE_ITEM const& _ ) = delete;
|
|
|
|
void operator=( LIB_TREE_NODE_ITEM const& _ ) = delete;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-15 14:17:44 +00:00
|
|
|
* Construct a #LIB_ID node.
|
2017-03-06 14:50:48 +00:00
|
|
|
*
|
|
|
|
* All fields will be populated from the LIB_ALIAS, including children
|
2017-09-15 14:17:44 +00:00
|
|
|
* (unit nodes will be generated automatically). This does not keep
|
|
|
|
* the pointer to the #LIB_ALIAS object because at any time, a #LIB_ALIAS
|
2021-06-09 19:32:58 +00:00
|
|
|
* can be remove from a library which will result in an invalid pointer.
|
2017-09-15 14:17:44 +00:00
|
|
|
* The alias must be resolved at the time of use. Anything else is a bug.
|
2017-03-06 14:50:48 +00:00
|
|
|
*
|
|
|
|
* @param aParent parent node, should be a CMP_TREE_NODE_LIB
|
2018-07-27 20:47:51 +00:00
|
|
|
* @param aItem LIB_COMPONENT to populate the node.
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-11-17 18:17:50 +00:00
|
|
|
LIB_TREE_NODE_ITEM( LIB_TREE_NODE* aParent, LIB_TREE_ITEM* aItem );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2018-01-10 11:04:34 +00:00
|
|
|
/**
|
|
|
|
* Update the node using data from a LIB_ALIAS object.
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
void Update( LIB_TREE_ITEM* aItem );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the actual search.
|
|
|
|
*/
|
2023-11-17 18:13:14 +00:00
|
|
|
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
|
|
|
|
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Add a new unit to the component and return it.
|
|
|
|
*
|
|
|
|
* This should not be used directly, as the constructor adds all units.
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE_UNIT& AddUnit( LIB_TREE_ITEM* aItem, int aUnit );
|
2017-03-06 14:50:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node type: library
|
|
|
|
*/
|
2023-11-08 17:34:14 +00:00
|
|
|
class LIB_TREE_NODE_LIBRARY : public LIB_TREE_NODE
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The addresses of CMP_TREE_NODEs are used as unique IDs for the
|
|
|
|
* wxDataViewModel, so don't let them be copied around.
|
|
|
|
*/
|
2023-11-08 17:34:14 +00:00
|
|
|
LIB_TREE_NODE_LIBRARY( LIB_TREE_NODE_LIBRARY const& _ ) = delete;
|
|
|
|
void operator=( LIB_TREE_NODE_LIBRARY const& _ ) = delete;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an empty library node.
|
|
|
|
*
|
|
|
|
* @param aParent parent node, should be a CMP_TREE_NODE_ROOT
|
|
|
|
* @param aName display name of the library
|
2018-02-06 15:14:01 +00:00
|
|
|
* @param aDesc a description of the library
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-11-08 17:34:14 +00:00
|
|
|
LIB_TREE_NODE_LIBRARY( LIB_TREE_NODE* aParent, const wxString& aName, const wxString& aDesc );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new alias node, add it to this library, and return it.
|
|
|
|
*
|
2018-07-30 13:18:37 +00:00
|
|
|
* @param aItem LIB_COMPONENT to provide data
|
2017-03-06 14:50:48 +00:00
|
|
|
*/
|
2023-11-17 18:17:50 +00:00
|
|
|
LIB_TREE_NODE_ITEM& AddItem( LIB_TREE_ITEM* aItem );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2023-11-17 18:13:14 +00:00
|
|
|
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
|
|
|
|
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
|
2017-03-06 14:50:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node type: root
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
class LIB_TREE_NODE_ROOT: public LIB_TREE_NODE
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The addresses of CMP_TREE_NODEs are used as unique IDs for the
|
|
|
|
* wxDataViewModel, so don't let them be copied around.
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE_ROOT( LIB_TREE_NODE_ROOT const& _ ) = delete;
|
|
|
|
void operator=( LIB_TREE_NODE_ROOT const& _ ) = delete;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the root node. Root nodes have no properties.
|
|
|
|
*/
|
2018-07-27 20:47:51 +00:00
|
|
|
LIB_TREE_NODE_ROOT();
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an empty library node, add it to the root, and return it.
|
|
|
|
*/
|
2023-11-08 17:34:14 +00:00
|
|
|
LIB_TREE_NODE_LIBRARY& AddLib( wxString const& aName, wxString const& aDesc );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2023-11-17 18:13:14 +00:00
|
|
|
void UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
|
|
|
|
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override;
|
2017-03-06 14:50:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-07-27 20:47:51 +00:00
|
|
|
#endif // LIB_TREE_MODEL_H
|