KiCad PCB EDA Suite
LIB_TREE_MODEL_ADAPTER Class Reference

Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture. More...

#include <lib_tree_model_adapter.h>

Inheritance diagram for LIB_TREE_MODEL_ADAPTER:
FP_TREE_MODEL_ADAPTER SYMBOL_TREE_MODEL_ADAPTER SYMBOL_TREE_SYNCHRONIZING_ADAPTER FP_TREE_SYNCHRONIZING_ADAPTER

Public Types

enum  CMP_FILTER_TYPE { CMP_FILTER_NONE, CMP_FILTER_POWER }
 This enum allows a selective filtering of components to list. More...
 
typedef wxObjectDataPtr< LIB_TREE_MODEL_ADAPTERPTR
 Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE. More...
 

Public Member Functions

 ~LIB_TREE_MODEL_ADAPTER ()
 Destructor. More...
 
void SetFilter (CMP_FILTER_TYPE aFilter)
 Set the component filter type. More...
 
CMP_FILTER_TYPE GetFilter () const
 Return the active filter. More...
 
void ShowUnits (bool aShow)
 Whether or not to show units. More...
 
void UpdateWidth (int aCol)
 Update the column size based on the displayed contents. More...
 
void SetPreselectNode (LIB_ID const &aLibId, int aUnit)
 Set the component name to be selected if there are no search results. More...
 
void DoAddLibrary (wxString const &aNodeName, wxString const &aDesc, std::vector< LIB_TREE_ITEM * > const &aItemList, bool presorted)
 Add the given list of components by alias. More...
 
void AssignIntrinsicRanks ()
 Sort the tree and assign ranks after adding libraries. More...
 
void UpdateSearchString (wxString const &aSearch)
 Set the search string provided by the user. More...
 
void AttachTo (wxDataViewCtrl *aDataViewCtrl)
 Attach to a wxDataViewCtrl and initialize it. More...
 
LIB_ID GetAliasFor (const wxDataViewItem &aSelection) const
 Return the alias for the given item. More...
 
int GetUnitFor (const wxDataViewItem &aSelection) const
 Return the unit for the given item. More...
 
LIB_TREE_NODE::TYPE GetTypeFor (const wxDataViewItem &aSelection) const
 Return node type for the given item. More...
 
virtual wxString GenerateInfo (LIB_ID const &aLibId, int aUnit)
 
int GetItemCount () const
 Return the number of components loaded in the tree. More...
 
virtual int GetLibrariesCount () const
 Return the number of libraries loaded in the tree. More...
 
wxDataViewItem FindItem (const LIB_ID &aLibId)
 Returns tree item corresponding to part. More...
 
unsigned int GetChildren (wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
 Populate a list of all the children of an item. More...
 
void Freeze ()
 
void Thaw ()
 
bool IsFrozen () const
 

Protected Member Functions

 LIB_TREE_MODEL_ADAPTER ()
 Constructor. More...
 
virtual bool HasContainerColumns (wxDataViewItem const &aItem) const override
 Check whether a container has columns too. More...
 
virtual bool IsContainer (wxDataViewItem const &aItem) const override
 Check whether an item can have children. More...
 
virtual wxDataViewItem GetParent (wxDataViewItem const &aItem) const override
 Get the parent of an item. More...
 
virtual unsigned int GetColumnCount () const override
 Return the number of columns in the model. More...
 
virtual wxString GetColumnType (unsigned int aCol) const override
 Return the type of data stored in the column. More...
 
virtual void GetValue (wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
 Get the value of an item. More...
 
virtual bool SetValue (wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
 Set the value of an item. More...
 
virtual bool GetAttr (wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
 Get any formatting for an item. More...
 

Static Protected Member Functions

static wxDataViewItem ToItem (LIB_TREE_NODE const *aNode)
 Convert CMP_TREE_NODE -> wxDataViewItem. More...
 
static LIB_TREE_NODE const * ToNode (wxDataViewItem aItem)
 Convert wxDataViewItem -> CMP_TREE_NODE. More...
 
static unsigned int IntoArray (LIB_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
 Convert CMP_TREE_NODE's children to wxDataViewItemArray. More...
 

Protected Attributes

LIB_TREE_NODE_ROOT m_tree
 

Private Member Functions

int ColWidth (LIB_TREE_NODE &aTree, int aCol, wxString const &aHeading)
 Compute the width required for the given column of a node and its children. More...
 
void FindAndExpand (LIB_TREE_NODE &aNode, std::function< bool(LIB_TREE_NODE const *)> aFunc, LIB_TREE_NODE **aHighScore)
 Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE const*) -> bool) The highest-scoring node is written to aHighScore. More...
 
LIB_TREE_NODEShowResults ()
 Find and expand successful search results. More...
 
LIB_TREE_NODEShowPreselect ()
 Find and expand preselected node. More...
 
LIB_TREE_NODEShowSingleLibrary ()
 Find and expand a library if there is only one. More...
 

Private Attributes

CMP_FILTER_TYPE m_filter
 
bool m_show_units
 
LIB_ID m_preselect_lib_id
 
int m_preselect_unit
 
int m_freeze
 
wxDataViewColumn * m_col_part
 
wxDataViewColumn * m_col_desc
 
wxDataViewCtrl * m_widget
 

Detailed Description

Adapter class in the component selector Model-View-Adapter (mediated MVC) architecture.

The other pieces are in:

This adapter presents the interface specified by wxDataViewModel to the wxDataViewCtrl:

                  +---+                      +------------------+
+---+  Generates  | A |                      |       VIEW       |
| M |  from libs  | D |   wxDataViewModel    |------------------|
| O | <---------- | A | <------------------> |  wxDataViewCtrl  |
| D |             | P |                      |------------------|
| E | <---------> | T | <------------------- |    wxTextCtrl    |
| L | UpdateScore | E | UpdateSearchString() |------------------|
+---+             | R |                      |                  |
                  +---+                      +------------------+

Because this adapter is a wxDataViewModel, it is reference-counted by wxObject. To ensure this interface is used correctly, the constructor is private; LIB_TREE_MODEL_ADAPTER should be created by the static factory method LIB_TREE_MODEL_ADAPTER::Create().

Quick summary of methods used to drive this class:

  • SetFilter() - set whether the view is restricted to power parts
  • ShowUnits() - set whether units are displayed
  • SetPreselectNode() - set a node to highlight when not searching
  • AddLibrary() - populate the model with all aliases in a library
  • AddAliasList() - populate the model with a specific list of aliases

Quick summary of methods used by the View:

Methods implemented as part of wxDataViewModel:

Definition at line 93 of file lib_tree_model_adapter.h.

Member Typedef Documentation

◆ PTR

Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.

Definition at line 100 of file lib_tree_model_adapter.h.

Member Enumeration Documentation

◆ CMP_FILTER_TYPE

This enum allows a selective filtering of components to list.

Enumerator
CMP_FILTER_NONE 

no filtering

CMP_FILTER_POWER 

list components flagged PWR

Definition at line 111 of file lib_tree_model_adapter.h.

Constructor & Destructor Documentation

◆ ~LIB_TREE_MODEL_ADAPTER()

LIB_TREE_MODEL_ADAPTER::~LIB_TREE_MODEL_ADAPTER ( )

Destructor.

Do NOT delete this class manually; it is reference-counted by wxObject.

Definition at line 84 of file lib_tree_model_adapter.cpp.

85 {}

◆ LIB_TREE_MODEL_ADAPTER()

LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER ( )
protected

Constructor.

Definition at line 73 of file lib_tree_model_adapter.cpp.

Member Function Documentation

◆ AssignIntrinsicRanks()

void LIB_TREE_MODEL_ADAPTER::AssignIntrinsicRanks ( )
inline

Sort the tree and assign ranks after adding libraries.

Definition at line 168 of file lib_tree_model_adapter.h.

void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.

References LIB_TREE_NODE::AssignIntrinsicRanks(), and m_tree.

◆ AttachTo()

void LIB_TREE_MODEL_ADAPTER::AttachTo ( wxDataViewCtrl *  aDataViewCtrl)

Attach to a wxDataViewCtrl and initialize it.

This will set up columns and associate the model via the adapter.

Parameters
aDataViewCtrlthe view component in the dialog

Definition at line 192 of file lib_tree_model_adapter.cpp.

193 {
194  m_widget = aDataViewCtrl;
195  aDataViewCtrl->SetIndent( kDataViewIndent );
196  aDataViewCtrl->AssociateModel( this );
197  aDataViewCtrl->ClearColumns();
198 
199  wxString part_head = _( "Item" );
200  wxString desc_head = _( "Description" );
201 
202  m_col_part = aDataViewCtrl->AppendTextColumn( part_head, 0, wxDATAVIEW_CELL_INERT, 360 );
203  m_col_desc = aDataViewCtrl->AppendTextColumn( desc_head, 1, wxDATAVIEW_CELL_INERT, 2000 );
204 }
static const int kDataViewIndent

References kDataViewIndent, m_col_desc, m_col_part, and m_widget.

Referenced by UpdateSearchString().

◆ ColWidth()

int LIB_TREE_MODEL_ADAPTER::ColWidth ( LIB_TREE_NODE aTree,
int  aCol,
wxString const &  aHeading 
)
private

Compute the width required for the given column of a node and its children.

Parameters
aTree- root node of the tree
aCol- column number
aHeading- heading text, to set the minimum width

Definition at line 364 of file lib_tree_model_adapter.cpp.

365 {
366  if( aCol == 0 )
367  {
368  int padding = m_widget->GetTextExtent( "M" ).x;
369  int indent = m_widget->GetIndent();
370  int longest = m_widget->GetTextExtent( aHeading ).x;
371 
372  for( auto& node : aTree.Children )
373  {
374  auto item = ToItem( &*node );
375 
376  if( !item.IsOk() )
377  continue;
378 
379  if( node->Score > 0 )
380  {
381  // Ensure the text size is up to date:
382  if( node->VisLen == 0 )
383  node->VisLen = m_widget->GetTextExtent( node->Name ).x;
384 
385  longest = std::max( longest, node->VisLen + padding + indent );
386  }
387 
388  if( !m_widget->IsExpanded( item ) )
389  continue;
390 
391  for( auto& childNode : node->Children )
392  {
393  if( childNode->Score > 0 )
394  {
395  if( childNode->VisLen == 0 )
396  childNode->VisLen = m_widget->GetTextExtent( childNode->Name ).x;
397 
398  longest = std::max( longest, childNode->VisLen + padding + 2 * indent );
399  }
400  }
401  }
402 
403  return longest;
404  }
405  else
406  return 2000;
407 }
PTR_VECTOR Children
List of child nodes.
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
#define max(a, b)
Definition: auxiliary.h:86

References LIB_TREE_NODE::Children, m_widget, max, and ToItem().

Referenced by UpdateWidth().

◆ DoAddLibrary()

void LIB_TREE_MODEL_ADAPTER::DoAddLibrary ( wxString const &  aNodeName,
wxString const &  aDesc,
std::vector< LIB_TREE_ITEM * > const &  aItemList,
bool  presorted 
)

Add the given list of components by alias.

To be called in the setup phase.

Parameters
aNodeNamethe parent node the components will appear under
aDescthe description field of the parent node
aItemListlist of components

Definition at line 118 of file lib_tree_model_adapter.cpp.

121 {
122  auto& lib_node = m_tree.AddLib( aNodeName, aDesc );
123 
124  lib_node.VisLen = wxTheApp->GetTopWindow()->GetTextExtent( lib_node.Name ).x;
125 
126  for( auto item: aItemList )
127  {
128  if( item )
129  {
130  auto& child_node = lib_node.AddItem( item );
131  child_node.VisLen = wxTheApp->GetTopWindow()->GetTextExtent( child_node.Name ).x;
132  }
133  }
134 
135  lib_node.AssignIntrinsicRanks( presorted );
136 }
int VisLen
Length of the string as shown on screen.
LIB_TREE_NODE_LIB & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.

References LIB_TREE_NODE_ROOT::AddLib(), m_tree, and LIB_TREE_NODE::VisLen.

Referenced by FP_TREE_MODEL_ADAPTER::AddLibraries(), SYMBOL_TREE_MODEL_ADAPTER::AddLibrary(), and FP_TREE_SYNCHRONIZING_ADAPTER::Sync().

◆ FindAndExpand()

void LIB_TREE_MODEL_ADAPTER::FindAndExpand ( LIB_TREE_NODE aNode,
std::function< bool(LIB_TREE_NODE const *)>  aFunc,
LIB_TREE_NODE **  aHighScore 
)
private

Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE const*) -> bool) The highest-scoring node is written to aHighScore.

Definition at line 410 of file lib_tree_model_adapter.cpp.

413 {
414  for( auto& node: aNode.Children )
415  {
416  if( aFunc( &*node ) )
417  {
418  auto item = wxDataViewItem( &*node );
419  m_widget->ExpandAncestors( item );
420 
421  if( !(*aHighScore) || node->Score > (*aHighScore)->Score )
422  (*aHighScore) = &*node;
423  }
424 
425  FindAndExpand( *node, aFunc, aHighScore );
426  }
427 }
PTR_VECTOR Children
List of child nodes.
void FindAndExpand(LIB_TREE_NODE &aNode, std::function< bool(LIB_TREE_NODE const *)> aFunc, LIB_TREE_NODE **aHighScore)
Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE con...

References LIB_TREE_NODE::Children, and m_widget.

Referenced by ShowPreselect(), ShowResults(), and ShowSingleLibrary().

◆ FindItem()

wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem ( const LIB_ID aLibId)

Returns tree item corresponding to part.

Parameters
aLibIdspecifies the part and library name to be searched for.
Returns
Tree data item representing the part. Might be invalid if nothings was found.

Definition at line 245 of file lib_tree_model_adapter.cpp.

246 {
247  for( auto& lib: m_tree.Children )
248  {
249  if( lib->Name != aLibId.GetLibNickname() )
250  continue;
251 
252  // if part name is not specified, return the library node
253  if( aLibId.GetLibItemName() == "" )
254  return ToItem( lib.get() );
255 
256  for( auto& alias: lib->Children )
257  {
258  if( alias->Name == aLibId.GetLibItemName() )
259  return ToItem( alias.get() );
260  }
261 
262  break; // could not find the part in the requested library
263  }
264 
265  return wxDataViewItem();
266 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
PTR_VECTOR Children
List of child nodes.
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97

References LIB_TREE_NODE::Children, LIB_ID::GetLibItemName(), LIB_ID::GetLibNickname(), m_tree, and ToItem().

◆ Freeze()

void LIB_TREE_MODEL_ADAPTER::Freeze ( )
inline

Definition at line 250 of file lib_tree_model_adapter.h.

References m_freeze.

◆ GenerateInfo()

virtual wxString LIB_TREE_MODEL_ADAPTER::GenerateInfo ( LIB_ID const &  aLibId,
int  aUnit 
)
inlinevirtual

Reimplemented in SYMBOL_TREE_MODEL_ADAPTER, and FP_TREE_MODEL_ADAPTER.

Definition at line 216 of file lib_tree_model_adapter.h.

216 { return wxEmptyString; };

◆ GetAliasFor()

LIB_ID LIB_TREE_MODEL_ADAPTER::GetAliasFor ( const wxDataViewItem &  aSelection) const

Return the alias for the given item.

Parameters
aSelectionitem from the wxDataViewCtrl (see wxDataViewCtrl::GetSelection())
Returns
alias, or nullptr if none is selected

Definition at line 207 of file lib_tree_model_adapter.cpp.

208 {
209  auto node = ToNode( aSelection );
210 
211  LIB_ID emptyId;
212 
213  if( !node )
214  return emptyId;
215 
216  return node->LibId;
217 }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References ToNode().

◆ GetAttr()

bool LIB_TREE_MODEL_ADAPTER::GetAttr ( wxDataViewItem const &  aItem,
unsigned int  aCol,
wxDataViewItemAttr &  aAttr 
) const
overrideprotectedvirtual

Get any formatting for an item.

Parameters
aItemitem to get formatting for
aColcolumn number of interest
aAttrreceiver for attributes
Returns
true iff the item has non-default attributes

Reimplemented in SYMBOL_TREE_SYNCHRONIZING_ADAPTER, and FP_TREE_SYNCHRONIZING_ADAPTER.

Definition at line 335 of file lib_tree_model_adapter.cpp.

338 {
339  if( IsFrozen() )
340  return false;
341 
342  auto node = ToNode( aItem );
343  wxASSERT( node );
344 
345  if( node->Type != LIB_TREE_NODE::LIBID )
346  {
347  // Currently only aliases are formatted at all
348  return false;
349  }
350 
351  if( !node->IsRoot && aCol == 0 )
352  {
353  // Names of non-root aliases are italicized
354  aAttr.SetItalic( true );
355  return true;
356  }
357  else
358  {
359  return false;
360  }
361 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References IsFrozen(), LIB_TREE_NODE::LIBID, and ToNode().

◆ GetChildren()

unsigned int LIB_TREE_MODEL_ADAPTER::GetChildren ( wxDataViewItem const &  aItem,
wxDataViewItemArray &  aChildren 
) const
override

Populate a list of all the children of an item.

Returns
number of children

Definition at line 269 of file lib_tree_model_adapter.cpp.

271 {
272  auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
273 
274  if( node->Type != LIB_TREE_NODE::TYPE::LIBID
275  || ( m_show_units && node->Type == LIB_TREE_NODE::TYPE::LIBID ) )
276  return IntoArray( *node, aChildren );
277  else
278  return 0;
279 }
static unsigned int IntoArray(LIB_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
Convert CMP_TREE_NODE's children to wxDataViewItemArray.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References IntoArray(), m_show_units, m_tree, and ToNode().

◆ GetColumnCount()

virtual unsigned int LIB_TREE_MODEL_ADAPTER::GetColumnCount ( ) const
inlineoverrideprotectedvirtual

Return the number of columns in the model.

Definition at line 287 of file lib_tree_model_adapter.h.

287 { return 2; }

◆ GetColumnType()

virtual wxString LIB_TREE_MODEL_ADAPTER::GetColumnType ( unsigned int  aCol) const
inlineoverrideprotectedvirtual

Return the type of data stored in the column.

Returns
type of data as indicated by wxVariant::GetType()

Definition at line 294 of file lib_tree_model_adapter.h.

294 { return "string"; }

◆ GetFilter()

CMP_FILTER_TYPE LIB_TREE_MODEL_ADAPTER::GetFilter ( ) const
inline

Return the active filter.

Definition at line 127 of file lib_tree_model_adapter.h.

127 { return m_filter; }

References m_filter.

Referenced by SYMBOL_TREE_MODEL_ADAPTER::AddLibrary().

◆ GetItemCount()

int LIB_TREE_MODEL_ADAPTER::GetItemCount ( ) const

Return the number of components loaded in the tree.

Definition at line 234 of file lib_tree_model_adapter.cpp.

235 {
236  int n = 0;
237 
238  for( auto& lib: m_tree.Children )
239  n += lib->Children.size();
240 
241  return n;
242 }
PTR_VECTOR Children
List of child nodes.

References LIB_TREE_NODE::Children, and m_tree.

◆ GetLibrariesCount()

virtual int LIB_TREE_MODEL_ADAPTER::GetLibrariesCount ( ) const
inlinevirtual

Return the number of libraries loaded in the tree.

Reimplemented in SYMBOL_TREE_SYNCHRONIZING_ADAPTER, and FP_TREE_SYNCHRONIZING_ADAPTER.

Definition at line 226 of file lib_tree_model_adapter.h.

227  {
228  return m_tree.Children.size();
229  }
PTR_VECTOR Children
List of child nodes.

References LIB_TREE_NODE::Children, and m_tree.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount().

◆ GetParent()

wxDataViewItem LIB_TREE_MODEL_ADAPTER::GetParent ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Get the parent of an item.

Parameters
aItemitem to get the parent of
Returns
parent of aItem, or an invalid wxDataViewItem if parent is root

Definition at line 295 of file lib_tree_model_adapter.cpp.

296 {
297  auto node = ToNode( aItem );
298  auto parent = node ? node->Parent : nullptr;
299 
300  // wxDataViewModel has no root node, but rather top-level elements have
301  // an invalid (null) parent.
302  if( !node || !parent || parent->Type == LIB_TREE_NODE::TYPE::ROOT )
303  return ToItem( nullptr );
304  else
305  return ToItem( parent );
306 }
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References ToItem(), and ToNode().

◆ GetTypeFor()

LIB_TREE_NODE::TYPE LIB_TREE_MODEL_ADAPTER::GetTypeFor ( const wxDataViewItem &  aSelection) const

Return node type for the given item.

Parameters
aSelectionitem from the wxDataViewCtrl (see wxDataViewCtrl::GetSelection())
Returns
Type of the selected node, might be INVALID.

Definition at line 227 of file lib_tree_model_adapter.cpp.

228 {
229  auto node = ToNode( aSelection );
230  return node ? node->Type : LIB_TREE_NODE::INVALID;
231 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References LIB_TREE_NODE::INVALID, and ToNode().

◆ GetUnitFor()

int LIB_TREE_MODEL_ADAPTER::GetUnitFor ( const wxDataViewItem &  aSelection) const

Return the unit for the given item.

Parameters
aSelectionitem from the wxDataViewCtrl (see wxDataViewCtrl::GetSelection())
Returns
Unit, or zero if the alias itself is selected. Return valid is invalid if GetAliasFor() returns nullptr.

Definition at line 220 of file lib_tree_model_adapter.cpp.

221 {
222  auto node = ToNode( aSelection );
223  return node ? node->Unit : 0;
224 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References ToNode().

◆ GetValue()

void LIB_TREE_MODEL_ADAPTER::GetValue ( wxVariant &  aVariant,
wxDataViewItem const &  aItem,
unsigned int  aCol 
) const
overrideprotectedvirtual

Get the value of an item.

Parameters
aVariantwxVariant to receive the data
aItemitem whose data will be placed into aVariant
aColcolumn number of the data

Reimplemented in SYMBOL_TREE_SYNCHRONIZING_ADAPTER, and FP_TREE_SYNCHRONIZING_ADAPTER.

Definition at line 309 of file lib_tree_model_adapter.cpp.

312 {
313  if( IsFrozen() )
314  {
315  aVariant = wxEmptyString;
316  return;
317  }
318 
319  auto node = ToNode( aItem );
320  wxASSERT( node );
321 
322  switch( aCol )
323  {
324  default: // column == -1 is used for default Compare function
325  case 0:
326  aVariant = node->Name;
327  break;
328  case 1:
329  aVariant = node->Desc;
330  break;
331  }
332 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References IsFrozen(), and ToNode().

◆ HasContainerColumns()

bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Check whether a container has columns too.

Definition at line 282 of file lib_tree_model_adapter.cpp.

283 {
284  return IsContainer( aItem );
285 }
virtual bool IsContainer(wxDataViewItem const &aItem) const override
Check whether an item can have children.

References IsContainer().

◆ IntoArray()

unsigned int LIB_TREE_MODEL_ADAPTER::IntoArray ( LIB_TREE_NODE const &  aNode,
wxDataViewItemArray &  aChildren 
)
staticprotected

Convert CMP_TREE_NODE's children to wxDataViewItemArray.

Definition at line 55 of file lib_tree_model_adapter.cpp.

57 {
58  unsigned int n = 0;
59 
60  for( auto const& child: aNode.Children )
61  {
62  if( child->Score > 0 )
63  {
64  aChildren.Add( ToItem( &*child ) );
65  ++n;
66  }
67  }
68 
69  return n;
70 }
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.

References LIB_TREE_NODE::Children, and ToItem().

Referenced by GetChildren().

◆ IsContainer()

bool LIB_TREE_MODEL_ADAPTER::IsContainer ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Check whether an item can have children.

Reimplemented in FP_TREE_SYNCHRONIZING_ADAPTER, and SYMBOL_TREE_SYNCHRONIZING_ADAPTER.

Definition at line 288 of file lib_tree_model_adapter.cpp.

289 {
290  auto node = ToNode( aItem );
291  return node ? node->Children.size() : true;
292 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References ToNode().

Referenced by HasContainerColumns().

◆ IsFrozen()

◆ SetFilter()

void LIB_TREE_MODEL_ADAPTER::SetFilter ( CMP_FILTER_TYPE  aFilter)

Set the component filter type.

Must be set before adding libraries

Parameters
aFilterif CMP_FILTER_POWER, only power parts are loaded

Definition at line 88 of file lib_tree_model_adapter.cpp.

89 {
90  m_filter = aFilter;
91 }

References m_filter.

◆ SetPreselectNode()

void LIB_TREE_MODEL_ADAPTER::SetPreselectNode ( LIB_ID const &  aLibId,
int  aUnit 
)

Set the component name to be selected if there are no search results.

May be set at any time; updates at the next UpdateSearchString().

Parameters
aLibIdsymbol LIB_ID to be selected
aUnitunit to be selected, if > 0 (0 selects the alias itself)

Definition at line 111 of file lib_tree_model_adapter.cpp.

112 {
113  m_preselect_lib_id = aLibId;
114  m_preselect_unit = aUnit;
115 }

References m_preselect_lib_id, and m_preselect_unit.

◆ SetValue()

virtual bool LIB_TREE_MODEL_ADAPTER::SetValue ( wxVariant const &  aVariant,
wxDataViewItem const &  aItem,
unsigned int  aCol 
)
inlineoverrideprotectedvirtual

Set the value of an item.

Does nothing - this model doesn't support editing.

Definition at line 311 of file lib_tree_model_adapter.h.

313  { return false; }

◆ ShowPreselect()

LIB_TREE_NODE * LIB_TREE_MODEL_ADAPTER::ShowPreselect ( )
private

Find and expand preselected node.

Return the best match (if any).

Definition at line 446 of file lib_tree_model_adapter.cpp.

447 {
448  LIB_TREE_NODE* highScore = nullptr;
449 
450  if( !m_preselect_lib_id.IsValid() )
451  return highScore;
452 
454  [&]( LIB_TREE_NODE const* n )
455  {
456  if( n->Type == LIB_TREE_NODE::LIBID && ( n->Children.empty() || !m_preselect_unit ) )
457  return m_preselect_lib_id == n->LibId;
458  else if( n->Type == LIB_TREE_NODE::UNIT && m_preselect_unit )
459  return m_preselect_lib_id == n->Parent->LibId && m_preselect_unit == n->Unit;
460  else
461  return false;
462  },
463  &highScore );
464 
465  return highScore;
466 }
PTR_VECTOR Children
List of child nodes.
enum TYPE Type
Node type.
bool IsValid() const
Definition: lib_id.h:171
LIB_TREE_NODE * Parent
Parent node or null.
void FindAndExpand(LIB_TREE_NODE &aNode, std::function< bool(LIB_TREE_NODE const *)> aFunc, LIB_TREE_NODE **aHighScore)
Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE con...
LIB_ID LibId
LIB_ID determined by the parent library nickname and alias name.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
int Unit
Actual unit, or zero.

References LIB_TREE_NODE::Children, FindAndExpand(), LIB_ID::IsValid(), LIB_TREE_NODE::LIBID, LIB_TREE_NODE::LibId, m_preselect_lib_id, m_preselect_unit, m_tree, LIB_TREE_NODE::Parent, LIB_TREE_NODE::Type, LIB_TREE_NODE::UNIT, and LIB_TREE_NODE::Unit.

Referenced by UpdateSearchString().

◆ ShowResults()

LIB_TREE_NODE * LIB_TREE_MODEL_ADAPTER::ShowResults ( )
private

Find and expand successful search results.

Return the best match (if any).

Definition at line 430 of file lib_tree_model_adapter.cpp.

431 {
432  LIB_TREE_NODE* highScore = nullptr;
433 
435  []( LIB_TREE_NODE const* n )
436  {
437  // return leaf nodes with some level of matching
438  return n->Children.size() == 0 && n->Score > 1;
439  },
440  &highScore );
441 
442  return highScore;
443 }
PTR_VECTOR Children
List of child nodes.
int Score
The score of an item resulting from the search algorithm.
void FindAndExpand(LIB_TREE_NODE &aNode, std::function< bool(LIB_TREE_NODE const *)> aFunc, LIB_TREE_NODE **aHighScore)
Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE con...
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.

References LIB_TREE_NODE::Children, FindAndExpand(), m_tree, and LIB_TREE_NODE::Score.

Referenced by UpdateSearchString().

◆ ShowSingleLibrary()

LIB_TREE_NODE * LIB_TREE_MODEL_ADAPTER::ShowSingleLibrary ( )
private

Find and expand a library if there is only one.

Return the best match (if any).

Definition at line 469 of file lib_tree_model_adapter.cpp.

470 {
471  LIB_TREE_NODE* highScore = nullptr;
472 
474  []( LIB_TREE_NODE const* n )
475  {
476  return n->Type == LIB_TREE_NODE::TYPE::LIBID &&
477  n->Parent->Parent->Children.size() == 1;
478  },
479  &highScore );
480 
481  return highScore;
482 }
PTR_VECTOR Children
List of child nodes.
enum TYPE Type
Node type.
LIB_TREE_NODE * Parent
Parent node or null.
void FindAndExpand(LIB_TREE_NODE &aNode, std::function< bool(LIB_TREE_NODE const *)> aFunc, LIB_TREE_NODE **aHighScore)
Find any results worth highlighting and expand them, according to given criteria (f(CMP_TREE_NODE con...
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.

References LIB_TREE_NODE::Children, FindAndExpand(), m_tree, LIB_TREE_NODE::Parent, and LIB_TREE_NODE::Type.

Referenced by UpdateSearchString().

◆ ShowUnits()

void LIB_TREE_MODEL_ADAPTER::ShowUnits ( bool  aShow)

Whether or not to show units.

May be set at any time; updates at the next UpdateSearchString()

Parameters
aShowif true, units are displayed

Definition at line 94 of file lib_tree_model_adapter.cpp.

95 {
96  m_show_units = aShow;
97 }

References m_show_units.

◆ Thaw()

void LIB_TREE_MODEL_ADAPTER::Thaw ( )
inline

Definition at line 251 of file lib_tree_model_adapter.h.

References m_freeze.

◆ ToItem()

wxDataViewItem LIB_TREE_MODEL_ADAPTER::ToItem ( LIB_TREE_NODE const *  aNode)
staticprotected

Convert CMP_TREE_NODE -> wxDataViewItem.

Definition at line 37 of file lib_tree_model_adapter.cpp.

38 {
39  return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
40 }

Referenced by ColWidth(), FindItem(), GetParent(), and IntoArray().

◆ ToNode()

LIB_TREE_NODE const * LIB_TREE_MODEL_ADAPTER::ToNode ( wxDataViewItem  aItem)
staticprotected

◆ UpdateSearchString()

void LIB_TREE_MODEL_ADAPTER::UpdateSearchString ( wxString const &  aSearch)

Set the search string provided by the user.

Parameters
aSearchfull, unprocessed search text

Definition at line 139 of file lib_tree_model_adapter.cpp.

140 {
141  m_tree.ResetScore();
142 
143  wxStringTokenizer tokenizer( aSearch );
144 
145  while( tokenizer.HasMoreTokens() )
146  {
147  const wxString term = tokenizer.GetNextToken().Lower();
148  EDA_COMBINED_MATCHER matcher( term );
149 
150  m_tree.UpdateScore( matcher );
151  }
152 
153  m_tree.SortNodes();
154 
155  {
156  wxWindowUpdateLocker updateLock( m_widget );
157 
158  // Even with the updateLock, wxWidgets sometimes ties its knickers in
159  // a knot when trying to run a wxdataview_selection_changed_callback()
160  // on a row that has been deleted.
161  // https://bugs.launchpad.net/kicad/+bug/1756255
162  m_widget->UnselectAll();
163 
164  Cleared();
165 #ifndef __WINDOWS__
166  // The fastest method to update wxDataViewCtrl is to rebuild from
167  // scratch by calling Cleared(). Linux requires to reassociate model to
168  // display data, but Windows will create multiple associations.
169  AttachTo( m_widget );
170 #endif
171  }
172 
173  LIB_TREE_NODE* bestMatch = ShowResults();
174 
175  if( !bestMatch )
176  bestMatch = ShowPreselect();
177 
178  if( !bestMatch )
179  bestMatch = ShowSingleLibrary();
180 
181  if( bestMatch )
182  {
183  auto item = wxDataViewItem( bestMatch );
184  m_widget->Select( item );
185  m_widget->EnsureVisible( item );
186  }
187 
188  UpdateWidth( 0 );
189 }
void SortNodes()
Sort child nodes quickly and recursively (IntrinsicRanks must have been set).
LIB_TREE_NODE * ShowResults()
Find and expand successful search results.
LIB_TREE_NODE * ShowPreselect()
Find and expand preselected node.
void ResetScore()
Initialize score to kLowestDefaultScore, recursively.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
void UpdateWidth(int aCol)
Update the column size based on the displayed contents.
virtual void UpdateScore(EDA_COMBINED_MATCHER &aMatcher) override
Update the score for this part.
LIB_TREE_NODE * ShowSingleLibrary()
Find and expand a library if there is only one.
void AttachTo(wxDataViewCtrl *aDataViewCtrl)
Attach to a wxDataViewCtrl and initialize it.

References AttachTo(), m_tree, m_widget, LIB_TREE_NODE::ResetScore(), ShowPreselect(), ShowResults(), ShowSingleLibrary(), LIB_TREE_NODE::SortNodes(), LIB_TREE_NODE_ROOT::UpdateScore(), and UpdateWidth().

◆ UpdateWidth()

void LIB_TREE_MODEL_ADAPTER::UpdateWidth ( int  aCol)

Update the column size based on the displayed contents.

Parameters
aColWhich column to resize

Definition at line 100 of file lib_tree_model_adapter.cpp.

101 {
102  auto col = m_widget->GetColumn( aCol );
103 
104  if( col )
105  {
106  col->SetWidth( ColWidth( m_tree, aCol, col->GetTitle() ) );
107  }
108 }
int ColWidth(LIB_TREE_NODE &aTree, int aCol, wxString const &aHeading)
Compute the width required for the given column of a node and its children.

References ColWidth(), m_tree, and m_widget.

Referenced by UpdateSearchString().

Member Data Documentation

◆ m_col_desc

wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::m_col_desc
private

Definition at line 335 of file lib_tree_model_adapter.h.

Referenced by AttachTo().

◆ m_col_part

wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::m_col_part
private

Definition at line 334 of file lib_tree_model_adapter.h.

Referenced by AttachTo().

◆ m_filter

CMP_FILTER_TYPE LIB_TREE_MODEL_ADAPTER::m_filter
private

Definition at line 328 of file lib_tree_model_adapter.h.

Referenced by GetFilter(), and SetFilter().

◆ m_freeze

int LIB_TREE_MODEL_ADAPTER::m_freeze
private

Definition at line 332 of file lib_tree_model_adapter.h.

Referenced by Freeze(), IsFrozen(), and Thaw().

◆ m_preselect_lib_id

LIB_ID LIB_TREE_MODEL_ADAPTER::m_preselect_lib_id
private

Definition at line 330 of file lib_tree_model_adapter.h.

Referenced by SetPreselectNode(), and ShowPreselect().

◆ m_preselect_unit

int LIB_TREE_MODEL_ADAPTER::m_preselect_unit
private

Definition at line 331 of file lib_tree_model_adapter.h.

Referenced by SetPreselectNode(), and ShowPreselect().

◆ m_show_units

bool LIB_TREE_MODEL_ADAPTER::m_show_units
private

Definition at line 329 of file lib_tree_model_adapter.h.

Referenced by GetChildren(), and ShowUnits().

◆ m_tree

◆ m_widget

wxDataViewCtrl* LIB_TREE_MODEL_ADAPTER::m_widget
private

The documentation for this class was generated from the following files: