KiCad PCB EDA Suite
FP_TREE_MODEL_ADAPTER Class Reference

#include <fp_tree_model_adapter.h>

Inheritance diagram for FP_TREE_MODEL_ADAPTER:
LIB_TREE_MODEL_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

void AddLibraries ()
 
wxString GenerateInfo (LIB_ID const &aLibId, int aUnit) override
 
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...
 
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 ()
 

Static Public Member Functions

static PTR Create (LIB_TABLE *aLibs)
 Factory function: create a model adapter in a reference-counting container. More...
 

Protected Member Functions

 FP_TREE_MODEL_ADAPTER (LIB_TABLE *aLibs)
 Constructor; takes a set of libraries to be included in the search. More...
 
std::vector< LIB_TREE_ITEM * > getFootprints (const wxString &aLibName)
 
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

FP_LIB_TABLEm_libs
 
LIB_TREE_NODE_ROOT m_tree
 

Detailed Description

Definition at line 31 of file fp_tree_model_adapter.h.

Member Typedef Documentation

◆ PTR

typedef wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> LIB_TREE_MODEL_ADAPTER::PTR
inherited

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

◆ FP_TREE_MODEL_ADAPTER()

FP_TREE_MODEL_ADAPTER::FP_TREE_MODEL_ADAPTER ( LIB_TABLE aLibs)
protected

Constructor; takes a set of libraries to be included in the search.

Definition at line 35 of file fp_tree_model_adapter.cpp.

36  : m_libs( (FP_LIB_TABLE*) aLibs )
37 {}

Referenced by Create().

Member Function Documentation

◆ AddLibraries()

void FP_TREE_MODEL_ADAPTER::AddLibraries ( )

Definition at line 40 of file fp_tree_model_adapter.cpp.

41 {
42  for( const auto& libName : m_libs->GetLogicalLibs() )
43  {
44  const FP_LIB_TABLE_ROW* library = m_libs->FindRow( libName );
45 
46  DoAddLibrary( libName, library->GetDescr(), getFootprints( libName ), true );
47  }
48 
50 }
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.
FP_LIB_TABLE_ROW.
Definition: fp_lib_table.h:42
const wxString & GetDescr() const
Return the description of the library referenced by this row.
std::vector< LIB_TREE_ITEM * > getFootprints(const wxString &aLibName)
const FP_LIB_TABLE_ROW * FindRow(const wxString &aNickName)
Function FindRow.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.

References LIB_TREE_NODE::AssignIntrinsicRanks(), LIB_TREE_MODEL_ADAPTER::DoAddLibrary(), FP_LIB_TABLE::FindRow(), LIB_TABLE_ROW::GetDescr(), getFootprints(), LIB_TABLE::GetLogicalLibs(), m_libs, and LIB_TREE_MODEL_ADAPTER::m_tree.

◆ AssignIntrinsicRanks()

void LIB_TREE_MODEL_ADAPTER::AssignIntrinsicRanks ( )
inlineinherited

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 LIB_TREE_MODEL_ADAPTER::m_tree.

◆ AttachTo()

void LIB_TREE_MODEL_ADAPTER::AttachTo ( wxDataViewCtrl *  aDataViewCtrl)
inherited

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

217 {
218  wxString partHead = _( "Item" );
219  wxString descHead = _( "Description" );
220 
221  // The extent of the text doesn't take into account the space on either side
222  // in the header, so artificially pad it by M
223  wxSize partHeadMinWidth = GetTextSize( partHead + "M", aDataViewCtrl );
224 
225  if( aDataViewCtrl->GetColumnCount() > 0 )
226  {
227  int partWidth = aDataViewCtrl->GetColumn( PART_COL )->GetWidth();
228  int descWidth = aDataViewCtrl->GetColumn( DESC_COL )->GetWidth();
229 
230  // Only use the widths read back if they are non-zero.
231  // GTK returns the displayed width of the column, which is not calculated immediately
232  // this leads to cases of 0 column width if the user types too fast in the filter
233  if( descWidth > 0 )
234  {
235  m_colWidths[PART_COL] = partWidth;
236  m_colWidths[DESC_COL] = descWidth;
237  }
238  }
239 
240  m_widget = aDataViewCtrl;
241  aDataViewCtrl->SetIndent( kDataViewIndent );
242  aDataViewCtrl->AssociateModel( this );
243  aDataViewCtrl->ClearColumns();
244 
245  m_col_part = aDataViewCtrl->AppendTextColumn( partHead, PART_COL, wxDATAVIEW_CELL_INERT,
247  m_col_desc = aDataViewCtrl->AppendTextColumn( descHead, DESC_COL, wxDATAVIEW_CELL_INERT,
249 
250  // Ensure the part column is wider than the smallest allowable width
251  if( m_colWidths[PART_COL] < partHeadMinWidth.x )
252  {
253  m_colWidths[PART_COL] = partHeadMinWidth.x;
254  m_col_part->SetWidth( partHeadMinWidth.x );
255  }
256 
257  m_col_part->SetMinWidth( partHeadMinWidth.x );
258 }
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: common.cpp:113
static const int kDataViewIndent
#define _(s)
Definition: 3d_actions.cpp:31

References _, LIB_TREE_MODEL_ADAPTER::DESC_COL, GetTextSize(), kDataViewIndent, LIB_TREE_MODEL_ADAPTER::m_col_desc, LIB_TREE_MODEL_ADAPTER::m_col_part, LIB_TREE_MODEL_ADAPTER::m_colWidths, LIB_TREE_MODEL_ADAPTER::m_widget, and LIB_TREE_MODEL_ADAPTER::PART_COL.

Referenced by LIB_TREE_MODEL_ADAPTER::UpdateSearchString().

◆ Create()

FP_TREE_MODEL_ADAPTER::PTR FP_TREE_MODEL_ADAPTER::Create ( LIB_TABLE aLibs)
static

Factory function: create a model adapter in a reference-counting container.

Parameters
aLibslibrary set from which parts will be loaded

Definition at line 29 of file fp_tree_model_adapter.cpp.

30 {
31  return PTR( new FP_TREE_MODEL_ADAPTER( aLibs ) );
32 }
FP_TREE_MODEL_ADAPTER(LIB_TABLE *aLibs)
Constructor; takes a set of libraries to be included in the search.
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.

References FP_TREE_MODEL_ADAPTER().

Referenced by PCB_BASE_FRAME::SelectFootprintFromLibTree().

◆ DoAddLibrary()

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

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 LIB_TREE_MODEL_ADAPTER::m_tree.

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

◆ FindItem()

wxDataViewItem LIB_TREE_MODEL_ADAPTER::FindItem ( const LIB_ID aLibId)
inherited

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

300 {
301  for( auto& lib: m_tree.Children )
302  {
303  if( lib->Name != aLibId.GetLibNickname() )
304  continue;
305 
306  // if part name is not specified, return the library node
307  if( aLibId.GetLibItemName() == "" )
308  return ToItem( lib.get() );
309 
310  for( auto& alias: lib->Children )
311  {
312  if( alias->Name == aLibId.GetLibItemName() )
313  return ToItem( alias.get() );
314  }
315 
316  break; // could not find the part in the requested library
317  }
318 
319  return wxDataViewItem();
320 }
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(), LIB_TREE_MODEL_ADAPTER::m_tree, and LIB_TREE_MODEL_ADAPTER::ToItem().

◆ Freeze()

void LIB_TREE_MODEL_ADAPTER::Freeze ( )
inlineinherited

◆ GenerateInfo()

wxString FP_TREE_MODEL_ADAPTER::GenerateInfo ( LIB_ID const &  aLibId,
int  aUnit 
)
overridevirtual

Reimplemented from LIB_TREE_MODEL_ADAPTER.

Definition at line 75 of file fp_tree_model_adapter.cpp.

76 {
77  return GenerateFootprintInfo( m_libs, aLibId );
78 }
wxString GenerateFootprintInfo(FP_LIB_TABLE *aFpLibTable, LIB_ID const &aLibId)
Return an HTML page describing a LIB_ID in a FP_LIB_TABLE.

References GenerateFootprintInfo(), and m_libs.

◆ GetAliasFor()

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

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

262 {
263  const LIB_TREE_NODE* node = ToNode( aSelection );
264 
265  LIB_ID emptyId;
266 
267  if( !node )
268  return emptyId;
269 
270  return node->LibId;
271 }
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 LIB_TREE_MODEL_ADAPTER::ToNode().

◆ GetAttr()

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

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

392 {
393  if( IsFrozen() )
394  return false;
395 
396  auto node = ToNode( aItem );
397  wxASSERT( node );
398 
399  if( node->Type != LIB_TREE_NODE::LIBID )
400  {
401  // Currently only aliases are formatted at all
402  return false;
403  }
404 
405  if( !node->IsRoot && aCol == 0 )
406  {
407  // Names of non-root aliases are italicized
408  aAttr.SetItalic( true );
409  return true;
410  }
411  else
412  {
413  return false;
414  }
415 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

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

◆ GetChildren()

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

Populate a list of all the children of an item.

Returns
number of children

Definition at line 323 of file lib_tree_model_adapter.cpp.

325 {
326  auto node = ( aItem.IsOk() ? ToNode( aItem ) : &m_tree );
327 
328  if( node->Type != LIB_TREE_NODE::TYPE::LIBID
329  || ( m_show_units && node->Type == LIB_TREE_NODE::TYPE::LIBID ) )
330  return IntoArray( *node, aChildren );
331  else
332  return 0;
333 }
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 LIB_TREE_MODEL_ADAPTER::IntoArray(), LIB_TREE_MODEL_ADAPTER::m_show_units, LIB_TREE_MODEL_ADAPTER::m_tree, and LIB_TREE_MODEL_ADAPTER::ToNode().

◆ GetColumnCount()

unsigned int LIB_TREE_MODEL_ADAPTER::GetColumnCount ( ) const
inlineoverrideprotectedinherited

Definition at line 292 of file lib_tree_model_adapter.h.

292 { return NUM_COLS; }

References LIB_TREE_MODEL_ADAPTER::NUM_COLS.

◆ GetColumnType()

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

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

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
inlineinherited

Return the active filter.

Definition at line 143 of file lib_tree_model_adapter.h.

143 { return m_filter; }

References LIB_TREE_MODEL_ADAPTER::m_filter.

Referenced by SYMBOL_TREE_MODEL_ADAPTER::AddLibrary().

◆ getFootprints()

std::vector< LIB_TREE_ITEM * > FP_TREE_MODEL_ADAPTER::getFootprints ( const wxString &  aLibName)
protected

Definition at line 53 of file fp_tree_model_adapter.cpp.

54 {
55  std::vector<LIB_TREE_ITEM*> libList;
56 
57  auto fullListStart = GFootprintList.GetList().begin();
58  auto fullListEnd = GFootprintList.GetList().end();
59  std::unique_ptr<FOOTPRINT_INFO> dummy( new FOOTPRINT_INFO_IMPL( aLibName, wxEmptyString ) );
60 
61  // List is sorted, so use a binary search to find the range of footnotes for our library
62  auto libBounds = std::equal_range( fullListStart, fullListEnd, dummy,
63  []( const std::unique_ptr<FOOTPRINT_INFO>& a, const std::unique_ptr<FOOTPRINT_INFO>& b )
64  {
65  return StrNumCmp( a->GetLibNickname(), b->GetLibNickname(), false ) < 0;
66  } );
67 
68  for( auto i = libBounds.first; i != libBounds.second; ++i )
69  libList.push_back( i->get() );
70 
71  return libList;
72 }
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:354
const FPILIST & GetList() const
Was forced to add this by modview_frame.cpp.
FOOTPRINT_LIST_IMPL GFootprintList
The global footprint info table.
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.

References dummy(), FOOTPRINT_LIST::GetList(), GFootprintList, and StrNumCmp().

Referenced by AddLibraries(), FP_TREE_SYNCHRONIZING_ADAPTER::Sync(), and FP_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ GetItemCount()

int LIB_TREE_MODEL_ADAPTER::GetItemCount ( ) const
inherited

Return the number of components loaded in the tree.

Definition at line 288 of file lib_tree_model_adapter.cpp.

289 {
290  int n = 0;
291 
292  for( const std::unique_ptr<LIB_TREE_NODE>& lib: m_tree.Children )
293  n += lib->Children.size();
294 
295  return n;
296 }
PTR_VECTOR Children
List of child nodes.

References LIB_TREE_NODE::Children, and LIB_TREE_MODEL_ADAPTER::m_tree.

◆ GetLibrariesCount()

virtual int LIB_TREE_MODEL_ADAPTER::GetLibrariesCount ( ) const
inlinevirtualinherited

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 LIB_TREE_MODEL_ADAPTER::m_tree.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount().

◆ GetParent()

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

Get the parent of an item.

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

Definition at line 349 of file lib_tree_model_adapter.cpp.

350 {
351  auto node = ToNode( aItem );
352  auto parent = node ? node->Parent : nullptr;
353 
354  // wxDataViewModel has no root node, but rather top-level elements have
355  // an invalid (null) parent.
356  if( !node || !parent || parent->Type == LIB_TREE_NODE::TYPE::ROOT )
357  return ToItem( nullptr );
358  else
359  return ToItem( parent );
360 }
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 LIB_TREE_MODEL_ADAPTER::ToItem(), and LIB_TREE_MODEL_ADAPTER::ToNode().

Referenced by LIB_TREE_MODEL_ADAPTER::UpdateSearchString().

◆ GetTypeFor()

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

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

282 {
283  const LIB_TREE_NODE* node = ToNode( aSelection );
284  return node ? node->Type : LIB_TREE_NODE::INVALID;
285 }
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, LIB_TREE_MODEL_ADAPTER::ToNode(), and LIB_TREE_NODE::Type.

◆ GetUnitFor()

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

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

275 {
276  const LIB_TREE_NODE* node = ToNode( aSelection );
277  return node ? node->Unit : 0;
278 }
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 LIB_TREE_MODEL_ADAPTER::ToNode(), and LIB_TREE_NODE::Unit.

◆ GetValue()

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

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

366 {
367  if( IsFrozen() )
368  {
369  aVariant = wxEmptyString;
370  return;
371  }
372 
373  auto node = ToNode( aItem );
374  wxASSERT( node );
375 
376  switch( aCol )
377  {
378  default: // column == -1 is used for default Compare function
379  case 0:
380  aVariant = node->Name;
381  break;
382  case 1:
383  aVariant = node->Desc;
384  break;
385  }
386 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References LIB_TREE_MODEL_ADAPTER::IsFrozen(), and LIB_TREE_MODEL_ADAPTER::ToNode().

◆ HasContainerColumns()

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

Check whether a container has columns too.

Definition at line 336 of file lib_tree_model_adapter.cpp.

337 {
338  return IsContainer( aItem );
339 }
bool IsContainer(wxDataViewItem const &aItem) const override
Check whether an item can have children.

References LIB_TREE_MODEL_ADAPTER::IsContainer().

◆ IntoArray()

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

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 LIB_TREE_MODEL_ADAPTER::ToItem().

Referenced by LIB_TREE_MODEL_ADAPTER::GetChildren().

◆ IsContainer()

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

Check whether an item can have children.

Definition at line 342 of file lib_tree_model_adapter.cpp.

343 {
344  auto node = ToNode( aItem );
345  return node ? node->Children.size() : true;
346 }
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.

References LIB_TREE_MODEL_ADAPTER::ToNode().

Referenced by LIB_TREE_MODEL_ADAPTER::HasContainerColumns().

◆ IsFrozen()

◆ SaveColWidths()

void LIB_TREE_MODEL_ADAPTER::SaveColWidths ( )
inherited

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, LIB_TREE_MODEL_ADAPTER::m_config, LIB_TREE_MODEL_ADAPTER::m_configPrefix, LIB_TREE_MODEL_ADAPTER::m_widget, and LIB_TREE_MODEL_ADAPTER::PART_COL.

◆ SetFilter()

void LIB_TREE_MODEL_ADAPTER::SetFilter ( CMP_FILTER_TYPE  aFilter)
inherited

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 LIB_TREE_MODEL_ADAPTER::m_filter.

◆ SetPreselectNode()

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

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 LIB_TREE_MODEL_ADAPTER::m_preselect_lib_id, and LIB_TREE_MODEL_ADAPTER::m_preselect_unit.

◆ SetValue()

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

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

◆ ShowUnits()

void LIB_TREE_MODEL_ADAPTER::ShowUnits ( bool  aShow)
inherited

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 LIB_TREE_MODEL_ADAPTER::m_show_units.

◆ Thaw()

void LIB_TREE_MODEL_ADAPTER::Thaw ( )
inlineinherited

◆ ToItem()

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

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 LIB_TREE_MODEL_ADAPTER::FindItem(), LIB_TREE_MODEL_ADAPTER::GetParent(), and LIB_TREE_MODEL_ADAPTER::IntoArray().

◆ ToNode()

◆ UpdateSearchString()

void LIB_TREE_MODEL_ADAPTER::UpdateSearchString ( wxString const &  aSearch)
inherited

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 #if defined( __LINUX__ )
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  // On MacOS, this crashes kicad. See https://gitlab.com/kicad/code/kicad/issues/3666
178  // and https://gitlab.com/kicad/code/kicad/issues/3653
179  AttachTo( m_widget );
180 #endif
181  }
182 
183  LIB_TREE_NODE* bestMatch = ShowResults();
184 
185  if( !bestMatch )
186  bestMatch = ShowPreselect();
187 
188  if( !bestMatch )
189  bestMatch = ShowSingleLibrary();
190 
191  if( bestMatch )
192  {
193  auto item = wxDataViewItem( bestMatch );
194  m_widget->Select( item );
195 
196  // Make sure the *parent* item is visible. The selected item is the
197  // first (shown) child of the parent. So it's always right below the parent,
198  // and this way the user can also see what library the selected part belongs to,
199  // without having a case where the selection is off the screen (unless the
200  // window is a single row high, which is unlikely)
201  //
202  // This also happens to circumvent https://bugs.launchpad.net/kicad/+bug/1804400
203  // which appears to be a GTK+3 bug.
204  {
205  wxDataViewItem parent = GetParent( item );
206 
207  if( parent.IsOk() )
208  item = parent;
209  }
210 
211  m_widget->EnsureVisible( item );
212  }
213 }
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 LIB_TREE_MODEL_ADAPTER::AttachTo(), LIB_TREE_MODEL_ADAPTER::GetParent(), LIB_TREE_MODEL_ADAPTER::m_tree, LIB_TREE_MODEL_ADAPTER::m_widget, LIB_TREE_NODE::ResetScore(), LIB_TREE_MODEL_ADAPTER::ShowPreselect(), LIB_TREE_MODEL_ADAPTER::ShowResults(), LIB_TREE_MODEL_ADAPTER::ShowSingleLibrary(), LIB_TREE_NODE::SortNodes(), and LIB_TREE_NODE_ROOT::UpdateScore().

Member Data Documentation

◆ m_libs

FP_LIB_TABLE* FP_TREE_MODEL_ADAPTER::m_libs
protected

◆ m_tree


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