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...
 
enum  TREE_COLS { PART_COL = 0, DESC_COL, NUM_COLS }
 This enum defines the order of the columns in the tree view. 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 SaveColWidths ()
 Save the column widths to the config file. 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
 
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

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
 
int m_colWidths [NUM_COLS]
 
wxConfigBase * m_config
 
wxString m_configPrefix
 

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.

◆ TREE_COLS

This enum defines the order of the columns in the tree view.

Enumerator
PART_COL 

Part name column.

DESC_COL 

Part description column.

NUM_COLS 

The number of tree columns.

Definition at line 120 of file lib_tree_model_adapter.h.

121  {
122  PART_COL = 0,
123  DESC_COL,
124  NUM_COLS
125  };

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

98 {}

◆ LIB_TREE_MODEL_ADAPTER()

LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER ( )
protected

Definition at line 73 of file lib_tree_model_adapter.cpp.

75  m_show_units( true ),
76  m_preselect_unit( 0 ),
77  m_freeze( 0 ),
78  m_col_part( nullptr ),
79  m_col_desc( nullptr ),
80  m_widget( nullptr )
81 {
82  // Default column widths
83  m_colWidths[PART_COL] = 360;
84  m_colWidths[DESC_COL] = 2000;
85 
87  m_configPrefix = typeid( this ).name();
88 
89  // Read the column width from the config
90  int colWidth = 0;
91 
92  if( m_config->Read( m_configPrefix + LIST_COLUMN_WIDTH_KEY, &colWidth ) )
93  m_colWidths[PART_COL] = colWidth;
94 }
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
#define LIST_COLUMN_WIDTH_KEY
const char * name
Definition: DXF_plotter.cpp:61

References DESC_COL, Kiface(), KIFACE_I::KifaceSettings(), LIST_COLUMN_WIDTH_KEY, m_colWidths, m_config, m_configPrefix, name, and PART_COL.

Member Function Documentation

◆ AssignIntrinsicRanks()

void LIB_TREE_MODEL_ADAPTER::AssignIntrinsicRanks ( )
inline

Sort the tree and assign ranks after adding libraries.

Definition at line 177 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 214 of file lib_tree_model_adapter.cpp.

215 {
216  wxString partHead = _( "Item" );
217  wxString descHead = _( "Description" );
218 
219  if( aDataViewCtrl->GetColumnCount() > 0 )
220  {
221  int partWidth = aDataViewCtrl->GetColumn( PART_COL )->GetWidth();
222  int descWidth = aDataViewCtrl->GetColumn( DESC_COL )->GetWidth();
223 
224  // Only use the widths read back if they are non-zero.
225  // GTK returns the displayed width of the column, which is not calculated immediately
226  // this leads to cases of 0 column width if the user types too fast in the filter
227  if( descWidth > 0 )
228  {
229  m_colWidths[PART_COL] = partWidth;
230  m_colWidths[DESC_COL] = descWidth;
231  }
232  }
233 
234  m_widget = aDataViewCtrl;
235  aDataViewCtrl->SetIndent( kDataViewIndent );
236  aDataViewCtrl->AssociateModel( this );
237  aDataViewCtrl->ClearColumns();
238 
239  m_col_part = aDataViewCtrl->AppendTextColumn( partHead, PART_COL, wxDATAVIEW_CELL_INERT,
241  m_col_desc = aDataViewCtrl->AppendTextColumn( descHead, DESC_COL, wxDATAVIEW_CELL_INERT,
243 }
static const int kDataViewIndent
#define _(s)

References _, DESC_COL, kDataViewIndent, m_col_desc, m_col_part, m_colWidths, m_widget, and PART_COL.

Referenced by UpdateSearchString().

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

137 {
138  LIB_TREE_NODE_LIB& lib_node = m_tree.AddLib( aNodeName, aDesc );
139 
140  for( LIB_TREE_ITEM* item: aItemList )
141  lib_node.AddItem( item );
142 
143  lib_node.AssignIntrinsicRanks( presorted );
144 }
A mix-in to provide polymorphism between items stored in libraries (symbols, aliases and footprints).
Definition: lib_tree_item.h:39
LIB_TREE_NODE_LIB_ID & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
Node type: library.
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.

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(), 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 403 of file lib_tree_model_adapter.cpp.

406 {
407  for( auto& node: aNode.Children )
408  {
409  if( aFunc( &*node ) )
410  {
411  auto item = wxDataViewItem( &*node );
412  m_widget->ExpandAncestors( item );
413 
414  if( !(*aHighScore) || node->Score > (*aHighScore)->Score )
415  (*aHighScore) = &*node;
416  }
417 
418  FindAndExpand( *node, aFunc, aHighScore );
419  }
420 }
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 284 of file lib_tree_model_adapter.cpp.

285 {
286  for( auto& lib: m_tree.Children )
287  {
288  if( lib->Name != aLibId.GetLibNickname() )
289  continue;
290 
291  // if part name is not specified, return the library node
292  if( aLibId.GetLibItemName() == "" )
293  return ToItem( lib.get() );
294 
295  for( auto& alias: lib->Children )
296  {
297  if( alias->Name == aLibId.GetLibItemName() )
298  return ToItem( alias.get() );
299  }
300 
301  break; // could not find the part in the requested library
302  }
303 
304  return wxDataViewItem();
305 }
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 259 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 225 of file lib_tree_model_adapter.h.

225 { 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 246 of file lib_tree_model_adapter.cpp.

247 {
248  const LIB_TREE_NODE* node = ToNode( aSelection );
249 
250  LIB_ID emptyId;
251 
252  if( !node )
253  return emptyId;
254 
255  return node->LibId;
256 }
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.
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.

References LIB_TREE_NODE::LibId, and 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 374 of file lib_tree_model_adapter.cpp.

377 {
378  if( IsFrozen() )
379  return false;
380 
381  auto node = ToNode( aItem );
382  wxASSERT( node );
383 
384  if( node->Type != LIB_TREE_NODE::LIBID )
385  {
386  // Currently only aliases are formatted at all
387  return false;
388  }
389 
390  if( !node->IsRoot && aCol == 0 )
391  {
392  // Names of non-root aliases are italicized
393  aAttr.SetItalic( true );
394  return true;
395  }
396  else
397  {
398  return false;
399  }
400 }
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 308 of file lib_tree_model_adapter.cpp.

310 {
311  auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
312 
313  if( node->Type != LIB_TREE_NODE::TYPE::LIBID
314  || ( m_show_units && node->Type == LIB_TREE_NODE::TYPE::LIBID ) )
315  return IntoArray( *node, aChildren );
316  else
317  return 0;
318 }
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 292 of file lib_tree_model_adapter.h.

292 { return NUM_COLS; }

References NUM_COLS.

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

297 { 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 264 of file lib_tree_model_adapter.h.

264 { return nullptr; }

◆ GetFilter()

CMP_FILTER_TYPE LIB_TREE_MODEL_ADAPTER::GetFilter ( ) const
inline

Return the active filter.

Definition at line 143 of file lib_tree_model_adapter.h.

143 { 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 273 of file lib_tree_model_adapter.cpp.

274 {
275  int n = 0;
276 
277  for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.Children )
278  n += lib->Children.size();
279 
280  return n;
281 }
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 235 of file lib_tree_model_adapter.h.

236  {
237  return m_tree.Children.size();
238  }
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 334 of file lib_tree_model_adapter.cpp.

335 {
336  auto node = ToNode( aItem );
337  auto parent = node ? node->Parent : nullptr;
338 
339  // wxDataViewModel has no root node, but rather top-level elements have
340  // an invalid (null) parent.
341  if( !node || !parent || parent->Type == LIB_TREE_NODE::TYPE::ROOT )
342  return ToItem( nullptr );
343  else
344  return ToItem( parent );
345 }
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 266 of file lib_tree_model_adapter.cpp.

267 {
268  const LIB_TREE_NODE* node = ToNode( aSelection );
269  return node ? node->Type : LIB_TREE_NODE::INVALID;
270 }
enum TYPE Type
Node type.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.

References LIB_TREE_NODE::INVALID, ToNode(), and LIB_TREE_NODE::Type.

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

260 {
261  const LIB_TREE_NODE* node = ToNode( aSelection );
262  return node ? node->Unit : 0;
263 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
int Unit
Actual unit, or zero.

References ToNode(), and LIB_TREE_NODE::Unit.

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

351 {
352  if( IsFrozen() )
353  {
354  aVariant = wxEmptyString;
355  return;
356  }
357 
358  auto node = ToNode( aItem );
359  wxASSERT( node );
360 
361  switch( aCol )
362  {
363  default: // column == -1 is used for default Compare function
364  case 0:
365  aVariant = node->Name;
366  break;
367  case 1:
368  aVariant = node->Desc;
369  break;
370  }
371 }
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 321 of file lib_tree_model_adapter.cpp.

322 {
323  return IsContainer( aItem );
324 }
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 327 of file lib_tree_model_adapter.cpp.

328 {
329  auto node = ToNode( aItem );
330  return node ? node->Children.size() : true;
331 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References ToNode().

Referenced by HasContainerColumns().

◆ IsFrozen()

◆ SaveColWidths()

void LIB_TREE_MODEL_ADAPTER::SaveColWidths ( )

Save the column widths to the config file.

This requires the tree view to still be valid.

Definition at line 101 of file lib_tree_model_adapter.cpp.

102 {
103  if( m_widget )
104  {
105  int colWidth = m_widget->GetColumn( PART_COL )->GetWidth();
106  m_config->Write( m_configPrefix + LIST_COLUMN_WIDTH_KEY, colWidth );
107  }
108  else
109  {
110  wxLogDebug( "Error saving column size, tree view doesn't exist" );
111  }
112 }
#define LIST_COLUMN_WIDTH_KEY

References LIST_COLUMN_WIDTH_KEY, m_config, m_configPrefix, m_widget, and PART_COL.

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

116 {
117  m_filter = aFilter;
118 }

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

128 {
129  m_preselect_lib_id = aLibId;
130  m_preselect_unit = aUnit;
131 }

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

316  { 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 439 of file lib_tree_model_adapter.cpp.

440 {
441  LIB_TREE_NODE* highScore = nullptr;
442 
443  if( !m_preselect_lib_id.IsValid() )
444  return highScore;
445 
447  [&]( LIB_TREE_NODE const* n )
448  {
449  if( n->Type == LIB_TREE_NODE::LIBID && ( n->Children.empty() || !m_preselect_unit ) )
450  return m_preselect_lib_id == n->LibId;
451  else if( n->Type == LIB_TREE_NODE::UNIT && m_preselect_unit )
452  return m_preselect_lib_id == n->Parent->LibId && m_preselect_unit == n->Unit;
453  else
454  return false;
455  },
456  &highScore );
457 
458  return highScore;
459 }
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 423 of file lib_tree_model_adapter.cpp.

424 {
425  LIB_TREE_NODE* highScore = nullptr;
426 
428  []( LIB_TREE_NODE const* n )
429  {
430  // return leaf nodes with some level of matching
431  return n->Type == LIB_TREE_NODE::TYPE::LIBID && n->Score > 1;
432  },
433  &highScore );
434 
435  return highScore;
436 }
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 462 of file lib_tree_model_adapter.cpp.

463 {
464  LIB_TREE_NODE* highScore = nullptr;
465 
467  []( LIB_TREE_NODE const* n )
468  {
469  return n->Type == LIB_TREE_NODE::TYPE::LIBID &&
470  n->Parent->Parent->Children.size() == 1;
471  },
472  &highScore );
473 
474  return highScore;
475 }
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 121 of file lib_tree_model_adapter.cpp.

122 {
123  m_show_units = aShow;
124 }

References m_show_units.

◆ Thaw()

void LIB_TREE_MODEL_ADAPTER::Thaw ( )
inline

Definition at line 260 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 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 147 of file lib_tree_model_adapter.cpp.

148 {
149  m_tree.ResetScore();
150 
151  wxStringTokenizer tokenizer( aSearch );
152 
153  while( tokenizer.HasMoreTokens() )
154  {
155  const wxString term = tokenizer.GetNextToken().Lower();
156  EDA_COMBINED_MATCHER matcher( term );
157 
158  m_tree.UpdateScore( matcher );
159  }
160 
161  m_tree.SortNodes();
162 
163  {
164  wxWindowUpdateLocker updateLock( m_widget );
165 
166  // Even with the updateLock, wxWidgets sometimes ties its knickers in
167  // a knot when trying to run a wxdataview_selection_changed_callback()
168  // on a row that has been deleted.
169  // https://bugs.launchpad.net/kicad/+bug/1756255
170  m_widget->UnselectAll();
171 
172  Cleared();
173 #ifndef __WINDOWS__
174  // The fastest method to update wxDataViewCtrl is to rebuild from
175  // scratch by calling Cleared(). Linux requires to reassociate model to
176  // display data, but Windows will create multiple associations.
177  AttachTo( m_widget );
178 #endif
179  }
180 
181  LIB_TREE_NODE* bestMatch = ShowResults();
182 
183  if( !bestMatch )
184  bestMatch = ShowPreselect();
185 
186  if( !bestMatch )
187  bestMatch = ShowSingleLibrary();
188 
189  if( bestMatch )
190  {
191  auto item = wxDataViewItem( bestMatch );
192  m_widget->Select( item );
193 
194  // Make sure the *parent* item is visible. The selected item is the
195  // first (shown) child of the parent. So it's always right below the parent,
196  // and this way the user can also see what library the selected part belongs to,
197  // without having a case where the selection is off the screen (unless the
198  // window is a single row high, which is unlikely)
199  //
200  // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400
201  // which appears to be a GTK+3 bug.
202  {
203  wxDataViewItem parent = GetParent( item );
204 
205  if( parent.IsOk() )
206  item = parent;
207  }
208 
209  m_widget->EnsureVisible( item );
210  }
211 }
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.
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(), and LIB_TREE_NODE_ROOT::UpdateScore().

Member Data Documentation

◆ m_col_desc

wxDataViewColumn* LIB_TREE_MODEL_ADAPTER::m_col_desc
private

Definition at line 338 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 337 of file lib_tree_model_adapter.h.

Referenced by AttachTo().

◆ m_colWidths

int LIB_TREE_MODEL_ADAPTER::m_colWidths[NUM_COLS]
private

Definition at line 341 of file lib_tree_model_adapter.h.

Referenced by AttachTo(), and LIB_TREE_MODEL_ADAPTER().

◆ m_config

wxConfigBase* LIB_TREE_MODEL_ADAPTER::m_config
private

Definition at line 343 of file lib_tree_model_adapter.h.

Referenced by LIB_TREE_MODEL_ADAPTER(), and SaveColWidths().

◆ m_configPrefix

wxString LIB_TREE_MODEL_ADAPTER::m_configPrefix
private

Definition at line 344 of file lib_tree_model_adapter.h.

Referenced by LIB_TREE_MODEL_ADAPTER(), and SaveColWidths().

◆ m_filter

CMP_FILTER_TYPE LIB_TREE_MODEL_ADAPTER::m_filter
private

Definition at line 331 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 335 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 333 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 334 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 332 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: