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 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

 WX_DECLARE_STRING_HASH_MAP (std::vector< int >, WIDTH_CACHE)
 
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
 

Static Private Attributes

static WIDTH_CACHE m_width_cache
 

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 92 of file lib_tree_model_adapter.h.

Member Typedef Documentation

Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.

Definition at line 99 of file lib_tree_model_adapter.h.

Member Enumeration Documentation

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 110 of file lib_tree_model_adapter.h.

Constructor & Destructor Documentation

LIB_TREE_MODEL_ADAPTER::~LIB_TREE_MODEL_ADAPTER ( )

Destructor.

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

Definition at line 86 of file lib_tree_model_adapter.cpp.

87 {}
LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER ( )
protected

Constructor.

Definition at line 75 of file lib_tree_model_adapter.cpp.

Member Function Documentation

void LIB_TREE_MODEL_ADAPTER::AssignIntrinsicRanks ( )
inline

Sort the tree and assign ranks after adding libraries.

Definition at line 160 of file lib_tree_model_adapter.h.

References LIB_TREE_NODE::AssignIntrinsicRanks(), AttachTo(), GetAliasFor(), GetTypeFor(), GetUnitFor(), m_tree, and UpdateSearchString().

void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
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 173 of file lib_tree_model_adapter.cpp.

References ColWidth(), kDataViewIndent, m_col_desc, m_col_part, m_tree, and m_widget.

Referenced by AssignIntrinsicRanks(), and UpdateSearchString().

174 {
175  m_widget = aDataViewCtrl;
176  aDataViewCtrl->SetIndent( kDataViewIndent );
177  aDataViewCtrl->AssociateModel( this );
178  aDataViewCtrl->ClearColumns();
179 
180  wxString part_head = _( "Item" );
181  wxString desc_head = _( "Description" );
182 
183  m_col_part = aDataViewCtrl->AppendTextColumn( part_head, 0, wxDATAVIEW_CELL_INERT,
184  ColWidth( m_tree, 0, part_head ) );
185  m_col_desc = aDataViewCtrl->AppendTextColumn( desc_head, 1, wxDATAVIEW_CELL_INERT,
186  ColWidth( m_tree, 1, desc_head ) );
187 }
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.
static const int kDataViewIndent
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 347 of file lib_tree_model_adapter.cpp.

Referenced by AttachTo().

348 {
349  // It's too expensive to calculate widths on really big trees, and the user probably
350  // wants it left where they dragged it anyway.
351  if( aCol == 0 )
352  return 360;
353  else
354  return 2000;
355 }
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 109 of file lib_tree_model_adapter.cpp.

References LIB_TREE_NODE_LIB::AddItem(), LIB_TREE_NODE_ROOT::AddLib(), LIB_TREE_NODE::AssignIntrinsicRanks(), and m_tree.

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

112 {
113  auto& lib_node = m_tree.AddLib( aNodeName, aDesc );
114 
115  for( auto item: aItemList )
116  lib_node.AddItem( item );
117 
118  lib_node.AssignIntrinsicRanks( presorted );
119 }
LIB_TREE_NODE_LIB_ID & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
LIB_TREE_NODE_LIB & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
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 358 of file lib_tree_model_adapter.cpp.

References LIB_TREE_NODE::Children, and m_widget.

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

361 {
362  for( auto& node: aNode.Children )
363  {
364  if( aFunc( &*node ) )
365  {
366  auto item = wxDataViewItem( &*node );
367  m_widget->ExpandAncestors( item );
368 
369  if( !(*aHighScore) || node->Score > (*aHighScore)->Score )
370  (*aHighScore) = &*node;
371  }
372 
373  FindAndExpand( *node, aFunc, aHighScore );
374  }
375 }
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...
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 228 of file lib_tree_model_adapter.cpp.

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

Referenced by GetLibrariesCount().

229 {
230  for( auto& lib: m_tree.Children )
231  {
232  if( lib->Name != aLibId.GetLibNickname() )
233  continue;
234 
235  // if part name is not specified, return the library node
236  if( aLibId.GetLibItemName() == "" )
237  return ToItem( lib.get() );
238 
239  for( auto& alias: lib->Children )
240  {
241  if( alias->Name == aLibId.GetLibItemName() )
242  return ToItem( alias.get() );
243  }
244 
245  break; // could not find the part in the requested library
246  }
247 
248  return wxDataViewItem();
249 }
PTR_VECTOR Children
List of child nodes.
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
void LIB_TREE_MODEL_ADAPTER::Freeze ( )
inline

Definition at line 242 of file lib_tree_model_adapter.h.

References m_freeze.

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

References GetItemCount().

208 { return wxEmptyString; };
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 190 of file lib_tree_model_adapter.cpp.

References ToNode().

Referenced by AssignIntrinsicRanks().

191 {
192  auto node = ToNode( aSelection );
193 
194  LIB_ID emptyId;
195 
196  if( !node )
197  return emptyId;
198 
199  return node->LibId;
200 }
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.
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 318 of file lib_tree_model_adapter.cpp.

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

Referenced by SetValue().

321 {
322  if( IsFrozen() )
323  return false;
324 
325  auto node = ToNode( aItem );
326  wxASSERT( node );
327 
328  if( node->Type != LIB_TREE_NODE::LIBID )
329  {
330  // Currently only aliases are formatted at all
331  return false;
332  }
333 
334  if( !node->IsRoot && aCol == 0 )
335  {
336  // Names of non-root aliases are italicized
337  aAttr.SetItalic( true );
338  return true;
339  }
340  else
341  {
342  return false;
343  }
344 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
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 252 of file lib_tree_model_adapter.cpp.

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

Referenced by GetLibrariesCount().

254 {
255  auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
256 
257  if( node->Type != LIB_TREE_NODE::TYPE::LIBID
258  || ( m_show_units && node->Type == LIB_TREE_NODE::TYPE::LIBID ) )
259  return IntoArray( *node, aChildren );
260  else
261  return 0;
262 }
static unsigned int IntoArray(LIB_TREE_NODE const &aNode, wxDataViewItemArray &aChildren)
Convert CMP_TREE_NODE&#39;s children to wxDataViewItemArray.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
virtual unsigned int LIB_TREE_MODEL_ADAPTER::GetColumnCount ( ) const
inlineoverrideprotectedvirtual

Return the number of columns in the model.

Definition at line 279 of file lib_tree_model_adapter.h.

279 { return 2; }
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 286 of file lib_tree_model_adapter.h.

References GetValue().

286 { return "string"; }
CMP_FILTER_TYPE LIB_TREE_MODEL_ADAPTER::GetFilter ( ) const
inline

Return the active filter.

Definition at line 126 of file lib_tree_model_adapter.h.

References DoAddLibrary(), m_filter, SetPreselectNode(), and ShowUnits().

Referenced by SYMBOL_TREE_MODEL_ADAPTER::AddLibrary().

126 { return m_filter; }
int LIB_TREE_MODEL_ADAPTER::GetItemCount ( ) const

Return the number of components loaded in the tree.

Definition at line 217 of file lib_tree_model_adapter.cpp.

References LIB_TREE_NODE::Children, and m_tree.

Referenced by GenerateInfo().

218 {
219  int n = 0;
220 
221  for( auto& lib: m_tree.Children )
222  n += lib->Children.size();
223 
224  return n;
225 }
PTR_VECTOR Children
List of child nodes.
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 218 of file lib_tree_model_adapter.h.

References LIB_TREE_NODE::Children, FindItem(), GetChildren(), and m_tree.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount().

219  {
220  return m_tree.Children.size();
221  }
PTR_VECTOR Children
List of child nodes.
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 278 of file lib_tree_model_adapter.cpp.

References ToItem(), and ToNode().

279 {
280  auto node = ToNode( aItem );
281  auto parent = node ? node->Parent : nullptr;
282 
283  // wxDataViewModel has no root node, but rather top-level elements have
284  // an invalid (null) parent.
285  if( !node || !parent || parent->Type == LIB_TREE_NODE::TYPE::ROOT )
286  return ToItem( nullptr );
287  else
288  return ToItem( parent );
289 }
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.
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 210 of file lib_tree_model_adapter.cpp.

References LIB_TREE_NODE::INVALID, and ToNode().

Referenced by AssignIntrinsicRanks().

211 {
212  auto node = ToNode( aSelection );
213  return node ? node->Type : LIB_TREE_NODE::INVALID;
214 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
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 203 of file lib_tree_model_adapter.cpp.

References ToNode().

Referenced by AssignIntrinsicRanks().

204 {
205  auto node = ToNode( aSelection );
206  return node ? node->Unit : 0;
207 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
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 292 of file lib_tree_model_adapter.cpp.

References IsFrozen(), and ToNode().

Referenced by GetColumnType().

295 {
296  if( IsFrozen() )
297  {
298  aVariant = wxEmptyString;
299  return;
300  }
301 
302  auto node = ToNode( aItem );
303  wxASSERT( node );
304 
305  switch( aCol )
306  {
307  default: // column == -1 is used for default Compare function
308  case 0:
309  aVariant = node->Name;
310  break;
311  case 1:
312  aVariant = node->Desc;
313  break;
314  }
315 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool LIB_TREE_MODEL_ADAPTER::HasContainerColumns ( wxDataViewItem const &  aItem) const
overrideprotectedvirtual

Check whether a container has columns too.

Definition at line 265 of file lib_tree_model_adapter.cpp.

References IsContainer().

266 {
267  return IsContainer( aItem );
268 }
virtual bool IsContainer(wxDataViewItem const &aItem) const override
Check whether an item can have children.
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 57 of file lib_tree_model_adapter.cpp.

References LIB_TREE_NODE::Children, and ToItem().

Referenced by GetChildren(), and IsFrozen().

59 {
60  unsigned int n = 0;
61 
62  for( auto const& child: aNode.Children )
63  {
64  if( child->Score > 0 )
65  {
66  aChildren.Add( ToItem( &*child ) );
67  ++n;
68  }
69  }
70 
71  return n;
72 }
static wxDataViewItem ToItem(LIB_TREE_NODE const *aNode)
Convert CMP_TREE_NODE -> wxDataViewItem.
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 271 of file lib_tree_model_adapter.cpp.

References ToNode().

Referenced by HasContainerColumns().

272 {
273  auto node = ToNode( aItem );
274  return node ? node->Children.size() : true;
275 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
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 90 of file lib_tree_model_adapter.cpp.

References m_filter.

91 {
92  m_filter = aFilter;
93 }
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 102 of file lib_tree_model_adapter.cpp.

References m_preselect_lib_id, and m_preselect_unit.

Referenced by GetFilter().

103 {
104  m_preselect_lib_id = aLibId;
105  m_preselect_unit = aUnit;
106 }
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 303 of file lib_tree_model_adapter.h.

References GetAttr().

305  { return false; }
LIB_TREE_NODE * LIB_TREE_MODEL_ADAPTER::ShowPreselect ( )
private

Find and expand preselected node.

Return the best match (if any).

Definition at line 394 of file lib_tree_model_adapter.cpp.

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().

395 {
396  LIB_TREE_NODE* highScore = nullptr;
397 
398  if( !m_preselect_lib_id.IsValid() )
399  return highScore;
400 
402  [&]( LIB_TREE_NODE const* n )
403  {
404  if( n->Type == LIB_TREE_NODE::LIBID && ( n->Children.empty() || !m_preselect_unit ) )
405  return m_preselect_lib_id == n->LibId;
406  else if( n->Type == LIB_TREE_NODE::UNIT && m_preselect_unit )
407  return m_preselect_lib_id == n->Parent->LibId && m_preselect_unit == n->Unit;
408  else
409  return false;
410  },
411  &highScore );
412 
413  return highScore;
414 }
bool IsValid() const
Definition: lib_id.h:171
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...
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.
LIB_TREE_NODE * LIB_TREE_MODEL_ADAPTER::ShowResults ( )
private

Find and expand successful search results.

Return the best match (if any).

Definition at line 378 of file lib_tree_model_adapter.cpp.

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

Referenced by UpdateSearchString().

379 {
380  LIB_TREE_NODE* highScore = nullptr;
381 
383  []( LIB_TREE_NODE const* n )
384  {
385  // return leaf nodes with some level of matching
386  return n->Children.size() == 0 && n->Score > 1;
387  },
388  &highScore );
389 
390  return highScore;
391 }
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.
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 417 of file lib_tree_model_adapter.cpp.

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

Referenced by UpdateSearchString().

418 {
419  LIB_TREE_NODE* highScore = nullptr;
420 
422  []( LIB_TREE_NODE const* n )
423  {
424  return n->Type == LIB_TREE_NODE::TYPE::LIBID &&
425  n->Parent->Parent->Children.size() == 1;
426  },
427  &highScore );
428 
429  return highScore;
430 }
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.
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 96 of file lib_tree_model_adapter.cpp.

References m_show_units.

Referenced by GetFilter().

97 {
98  m_show_units = aShow;
99 }
void LIB_TREE_MODEL_ADAPTER::Thaw ( )
inline

Definition at line 243 of file lib_tree_model_adapter.h.

References m_freeze.

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

Convert CMP_TREE_NODE -> wxDataViewItem.

Definition at line 39 of file lib_tree_model_adapter.cpp.

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

40 {
41  return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
42 }
LIB_TREE_NODE const * LIB_TREE_MODEL_ADAPTER::ToNode ( wxDataViewItem  aItem)
staticprotected
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 122 of file lib_tree_model_adapter.cpp.

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

Referenced by AssignIntrinsicRanks().

123 {
124  m_tree.ResetScore();
125 
126  wxStringTokenizer tokenizer( aSearch );
127 
128  while( tokenizer.HasMoreTokens() )
129  {
130  const wxString term = tokenizer.GetNextToken().Lower();
131  EDA_COMBINED_MATCHER matcher( term );
132 
133  m_tree.UpdateScore( matcher );
134  }
135 
136  m_tree.SortNodes();
137 
138  {
139  wxWindowUpdateLocker updateLock( m_widget );
140 
141  // Even with the updateLock, wxWidgets sometimes ties its knickers in
142  // a knot when trying to run a wxdataview_selection_changed_callback()
143  // on a row that has been deleted.
144  // https://bugs.launchpad.net/kicad/+bug/1756255
145  m_widget->UnselectAll();
146 
147  Cleared();
148 #ifndef __WINDOWS__
149  // The fastest method to update wxDataViewCtrl is to rebuild from
150  // scratch by calling Cleared(). Linux requires to reassociate model to
151  // display data, but Windows will create multiple associations.
152  AttachTo( m_widget );
153 #endif
154  }
155 
156  LIB_TREE_NODE* bestMatch = ShowResults();
157 
158  if( !bestMatch )
159  bestMatch = ShowPreselect();
160 
161  if( !bestMatch )
162  bestMatch = ShowSingleLibrary();
163 
164  if( bestMatch )
165  {
166  auto item = wxDataViewItem( bestMatch );
167  m_widget->Select( item );
168  m_widget->EnsureVisible( item );
169  }
170 }
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.
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.
LIB_TREE_MODEL_ADAPTER::WX_DECLARE_STRING_HASH_MAP ( std::vector< int >  ,
WIDTH_CACHE   
)
private

Member Data Documentation

wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::m_col_desc
private

Definition at line 327 of file lib_tree_model_adapter.h.

Referenced by AttachTo().

wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::m_col_part
private

Definition at line 326 of file lib_tree_model_adapter.h.

Referenced by AttachTo().

CMP_FILTER_TYPE LIB_TREE_MODEL_ADAPTER::m_filter
private

Definition at line 320 of file lib_tree_model_adapter.h.

Referenced by GetFilter(), and SetFilter().

int LIB_TREE_MODEL_ADAPTER::m_freeze
private

Definition at line 324 of file lib_tree_model_adapter.h.

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

LIB_ID LIB_TREE_MODEL_ADAPTER::m_preselect_lib_id
private

Definition at line 322 of file lib_tree_model_adapter.h.

Referenced by SetPreselectNode(), and ShowPreselect().

int LIB_TREE_MODEL_ADAPTER::m_preselect_unit
private

Definition at line 323 of file lib_tree_model_adapter.h.

Referenced by SetPreselectNode(), and ShowPreselect().

bool LIB_TREE_MODEL_ADAPTER::m_show_units
private

Definition at line 321 of file lib_tree_model_adapter.h.

Referenced by GetChildren(), and ShowUnits().

wxDataViewCtrl* LIB_TREE_MODEL_ADAPTER::m_widget
private

Definition at line 328 of file lib_tree_model_adapter.h.

Referenced by AttachTo(), FindAndExpand(), and UpdateSearchString().

LIB_TREE_MODEL_ADAPTER::WIDTH_CACHE LIB_TREE_MODEL_ADAPTER::m_width_cache
staticprivate

Definition at line 332 of file lib_tree_model_adapter.h.


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