KiCad PCB EDA Suite
LIB_TREE_MODEL_ADAPTER Class Reference

#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
 
virtual TOOL_INTERACTIVEGetContextMenuTool ()
 

Protected Member Functions

 LIB_TREE_MODEL_ADAPTER ()
 
bool HasContainerColumns (wxDataViewItem const &aItem) const override
 Check whether a container has columns too. More...
 
bool IsContainer (wxDataViewItem const &aItem) const override
 Check whether an item can have children. More...
 
wxDataViewItem GetParent (wxDataViewItem const &aItem) const override
 Get the parent of an item. More...
 
unsigned int GetColumnCount () const override
 
wxString GetColumnType (unsigned int aCol) const override
 Return the type of data stored in the column as indicated by wxVariant::GetType() More...
 
void GetValue (wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
 Get the value of an item. More...
 
bool SetValue (wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
 Set the value of an item. More...
 
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

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

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 208 of file lib_tree_model_adapter.cpp.

209 {
210  m_widget = aDataViewCtrl;
211  aDataViewCtrl->SetIndent( kDataViewIndent );
212  aDataViewCtrl->AssociateModel( this );
213  aDataViewCtrl->ClearColumns();
214 
215  wxString part_head = _( "Item" );
216  wxString desc_head = _( "Description" );
217 
218  m_col_part = aDataViewCtrl->AppendTextColumn( part_head, 0, wxDATAVIEW_CELL_INERT, 360 );
219  m_col_desc = aDataViewCtrl->AppendTextColumn( desc_head, 1, wxDATAVIEW_CELL_INERT, 2000 );
220 }
static const int kDataViewIndent
#define _(s)

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 380 of file lib_tree_model_adapter.cpp.

381 {
382  if( aCol == 0 )
383  {
384  int padding = m_widget->GetTextExtent( "M" ).x;
385  int indent = m_widget->GetIndent();
386  int longest = m_widget->GetTextExtent( aHeading ).x;
387 
388  for( auto& node : aTree.Children )
389  {
390  auto item = ToItem( &*node );
391 
392  if( !item.IsOk() )
393  continue;
394 
395  if( node->Score > 0 )
396  {
397  // Ensure the text size is up to date:
398  if( node->VisLen == 0 )
399  node->VisLen = m_widget->GetTextExtent( node->Name ).x;
400 
401  longest = std::max( longest, node->VisLen + padding + indent );
402  }
403 
404  if( !m_widget->IsExpanded( item ) )
405  continue;
406 
407  for( auto& childNode : node->Children )
408  {
409  if( childNode->Score > 0 )
410  {
411  if( childNode->VisLen == 0 )
412  childNode->VisLen = m_widget->GetTextExtent( childNode->Name ).x;
413 
414  longest = std::max( longest, childNode->VisLen + padding + 2 * indent );
415  }
416  }
417  }
418 
419  return longest;
420  }
421  else
422  return 2000;
423 }
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 426 of file lib_tree_model_adapter.cpp.

429 {
430  for( auto& node: aNode.Children )
431  {
432  if( aFunc( &*node ) )
433  {
434  auto item = wxDataViewItem( &*node );
435  m_widget->ExpandAncestors( item );
436 
437  if( !(*aHighScore) || node->Score > (*aHighScore)->Score )
438  (*aHighScore) = &*node;
439  }
440 
441  FindAndExpand( *node, aFunc, aHighScore );
442  }
443 }
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 261 of file lib_tree_model_adapter.cpp.

262 {
263  for( auto& lib: m_tree.Children )
264  {
265  if( lib->Name != aLibId.GetLibNickname() )
266  continue;
267 
268  // if part name is not specified, return the library node
269  if( aLibId.GetLibItemName() == "" )
270  return ToItem( lib.get() );
271 
272  for( auto& alias: lib->Children )
273  {
274  if( alias->Name == aLibId.GetLibItemName() )
275  return ToItem( alias.get() );
276  }
277 
278  break; // could not find the part in the requested library
279  }
280 
281  return wxDataViewItem();
282 }
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 223 of file lib_tree_model_adapter.cpp.

224 {
225  auto node = ToNode( aSelection );
226 
227  LIB_ID emptyId;
228 
229  if( !node )
230  return emptyId;
231 
232  return node->LibId;
233 }
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
overrideprotected

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

Definition at line 351 of file lib_tree_model_adapter.cpp.

354 {
355  if( IsFrozen() )
356  return false;
357 
358  auto node = ToNode( aItem );
359  wxASSERT( node );
360 
361  if( node->Type != LIB_TREE_NODE::LIBID )
362  {
363  // Currently only aliases are formatted at all
364  return false;
365  }
366 
367  if( !node->IsRoot && aCol == 0 )
368  {
369  // Names of non-root aliases are italicized
370  aAttr.SetItalic( true );
371  return true;
372  }
373  else
374  {
375  return false;
376  }
377 }
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 285 of file lib_tree_model_adapter.cpp.

287 {
288  auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
289 
290  if( node->Type != LIB_TREE_NODE::TYPE::LIBID
291  || ( m_show_units && node->Type == LIB_TREE_NODE::TYPE::LIBID ) )
292  return IntoArray( *node, aChildren );
293  else
294  return 0;
295 }
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()

unsigned int LIB_TREE_MODEL_ADAPTER::GetColumnCount ( ) const
inlineoverrideprotected

Definition at line 283 of file lib_tree_model_adapter.h.

283 { return 2; }

◆ GetColumnType()

wxString LIB_TREE_MODEL_ADAPTER::GetColumnType ( unsigned int  aCol) const
inlineoverrideprotected

Return the type of data stored in the column as indicated by wxVariant::GetType()

Definition at line 288 of file lib_tree_model_adapter.h.

288 { return "string"; }

◆ GetContextMenuTool()

virtual TOOL_INTERACTIVE* LIB_TREE_MODEL_ADAPTER::GetContextMenuTool ( )
inlinevirtual

Reimplemented in SYMBOL_TREE_SYNCHRONIZING_ADAPTER, and FP_TREE_SYNCHRONIZING_ADAPTER.

Definition at line 255 of file lib_tree_model_adapter.h.

255 { return nullptr; }

◆ 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 250 of file lib_tree_model_adapter.cpp.

251 {
252  int n = 0;
253 
254  for( auto& lib: m_tree.Children )
255  n += lib->Children.size();
256 
257  return n;
258 }
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
overrideprotected

Get the parent of an item.

Returns
parent of aItem, or an invalid wxDataViewItem if parent is root

Definition at line 311 of file lib_tree_model_adapter.cpp.

312 {
313  auto node = ToNode( aItem );
314  auto parent = node ? node->Parent : nullptr;
315 
316  // wxDataViewModel has no root node, but rather top-level elements have
317  // an invalid (null) parent.
318  if( !node || !parent || parent->Type == LIB_TREE_NODE::TYPE::ROOT )
319  return ToItem( nullptr );
320  else
321  return ToItem( parent );
322 }
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().

Referenced by UpdateSearchString().

◆ 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 243 of file lib_tree_model_adapter.cpp.

244 {
245  auto node = ToNode( aSelection );
246  return node ? node->Type : LIB_TREE_NODE::INVALID;
247 }
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 236 of file lib_tree_model_adapter.cpp.

237 {
238  auto node = ToNode( aSelection );
239  return node ? node->Unit : 0;
240 }
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
overrideprotected

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

Definition at line 325 of file lib_tree_model_adapter.cpp.

328 {
329  if( IsFrozen() )
330  {
331  aVariant = wxEmptyString;
332  return;
333  }
334 
335  auto node = ToNode( aItem );
336  wxASSERT( node );
337 
338  switch( aCol )
339  {
340  default: // column == -1 is used for default Compare function
341  case 0:
342  aVariant = node->Name;
343  break;
344  case 1:
345  aVariant = node->Desc;
346  break;
347  }
348 }
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
overrideprotected

Check whether a container has columns too.

Definition at line 298 of file lib_tree_model_adapter.cpp.

299 {
300  return IsContainer( aItem );
301 }
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
overrideprotected

Check whether an item can have children.

Definition at line 304 of file lib_tree_model_adapter.cpp.

305 {
306  auto node = ToNode( aItem );
307  return node ? node->Children.size() : true;
308 }
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()

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

Set the value of an item.

Does nothing - this model doesn't support editing.

Definition at line 305 of file lib_tree_model_adapter.h.

307  { 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 462 of file lib_tree_model_adapter.cpp.

463 {
464  LIB_TREE_NODE* highScore = nullptr;
465 
466  if( !m_preselect_lib_id.IsValid() )
467  return highScore;
468 
470  [&]( LIB_TREE_NODE const* n )
471  {
472  if( n->Type == LIB_TREE_NODE::LIBID && ( n->Children.empty() || !m_preselect_unit ) )
473  return m_preselect_lib_id == n->LibId;
474  else if( n->Type == LIB_TREE_NODE::UNIT && m_preselect_unit )
475  return m_preselect_lib_id == n->Parent->LibId && m_preselect_unit == n->Unit;
476  else
477  return false;
478  },
479  &highScore );
480 
481  return highScore;
482 }
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 446 of file lib_tree_model_adapter.cpp.

447 {
448  LIB_TREE_NODE* highScore = nullptr;
449 
451  []( LIB_TREE_NODE const* n )
452  {
453  // return leaf nodes with some level of matching
454  return n->Type == LIB_TREE_NODE::TYPE::LIBID && n->Score > 1;
455  },
456  &highScore );
457 
458  return highScore;
459 }
enum TYPE Type
Node type.
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 FindAndExpand(), m_tree, LIB_TREE_NODE::Score, and LIB_TREE_NODE::Type.

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 485 of file lib_tree_model_adapter.cpp.

486 {
487  LIB_TREE_NODE* highScore = nullptr;
488 
490  []( LIB_TREE_NODE const* n )
491  {
492  return n->Type == LIB_TREE_NODE::TYPE::LIBID &&
493  n->Parent->Parent->Children.size() == 1;
494  },
495  &highScore );
496 
497  return highScore;
498 }
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 
186  // Make sure the *parent* item is visible. The selected item is the
187  // first (shown) child of the parent. So it's always right below the parent,
188  // and this way the user can also see what library the selected part belongs to,
189  // without having a case where the selection is off the screen (unless the
190  // window is a single row high, which is unlikely)
191  //
192  // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400
193  // which appears to be a GTK+3 bug.
194  {
195  wxDataViewItem parent = GetParent( item );
196 
197  if( parent.IsOk() )
198  item = parent;
199  }
200 
201  m_widget->EnsureVisible( item );
202  }
203 
204  UpdateWidth( 0 );
205 }
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.
wxDataViewItem GetParent(wxDataViewItem const &aItem) const override
Get the parent of an item.
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(), GetParent(), 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 329 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 328 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 322 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 326 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 324 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 325 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 323 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: