KiCad PCB EDA Suite
SYMBOL_TREE_SYNCHRONIZING_ADAPTER Class Reference

#include <symbol_tree_synchronizing_adapter.h>

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

bool IsContainer (const wxDataViewItem &aItem) const override
 
void Sync (bool aForce=false, std::function< void(int, int, const wxString &)> aProgressCallback=[](int, int, const wxString &){})
 
int GetLibrariesCount () const override
 Return the number of libraries loaded in the tree. More...
 
TOOL_INTERACTIVEGetContextMenuTool () 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...
 
virtual wxString GenerateInfo (LIB_ID const &aLibId, int aUnit)
 
int GetItemCount () const
 Return the number of components 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
 

Static Public Member Functions

static PTR Create (LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibs)
 

Protected Member Functions

void updateLibrary (LIB_TREE_NODE_LIB &aLibNode)
 
LIB_TREE_NODE::PTR_VECTOR::iterator deleteLibrary (LIB_TREE_NODE::PTR_VECTOR::iterator &aLibNodeIt)
 
void GetValue (wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
 
bool GetAttr (wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
 
 SYMBOL_TREE_SYNCHRONIZING_ADAPTER (LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibMgr)
 
bool HasContainerColumns (wxDataViewItem const &aItem) const override
 Check whether a container has columns too. 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...
 
bool SetValue (wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
 Set the value of 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_EDIT_FRAMEm_frame
 
LIB_MANAGERm_libMgr
 
std::map< wxString, int > m_libHashes
 

Hashes to decide whether a library needs an update

More...
 
int m_lastSyncHash
 

LIB_MANAGER hash value returned in the last synchronization

More...
 
LIB_TREE_NODE_ROOT m_tree
 

Detailed Description

Definition at line 34 of file symbol_tree_synchronizing_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

◆ SYMBOL_TREE_SYNCHRONIZING_ADAPTER()

SYMBOL_TREE_SYNCHRONIZING_ADAPTER::SYMBOL_TREE_SYNCHRONIZING_ADAPTER ( LIB_EDIT_FRAME aParent,
LIB_MANAGER aLibMgr 
)
protected

Definition at line 41 of file symbol_tree_synchronizing_adapter.cpp.

42  :
43  m_frame( aParent ),
44  m_libMgr( aLibMgr ),
45  m_lastSyncHash( -1 )
46 {
47 }
int m_lastSyncHash
LIB_MANAGER hash value returned in the last synchronization

Referenced by Create().

Member Function Documentation

◆ 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 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 _, LIB_TREE_MODEL_ADAPTER::DESC_COL, 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()

LIB_TREE_MODEL_ADAPTER::PTR SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create ( LIB_EDIT_FRAME aParent,
LIB_MANAGER aLibs 
)
static

Definition at line 34 of file symbol_tree_synchronizing_adapter.cpp.

36 {
37  return PTR( new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr ) );
38 }
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
SYMBOL_TREE_SYNCHRONIZING_ADAPTER(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibMgr)

References SYMBOL_TREE_SYNCHRONIZING_ADAPTER().

Referenced by LIB_MANAGER::LIB_MANAGER().

◆ deleteLibrary()

LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary ( LIB_TREE_NODE::PTR_VECTOR::iterator &  aLibNodeIt)
protected

Definition at line 191 of file symbol_tree_synchronizing_adapter.cpp.

193 {
194  LIB_TREE_NODE* node = aLibNodeIt->get();
195  m_libHashes.erase( node->Name );
196  auto it = m_tree.Children.erase( aLibNodeIt );
197  return it;
198 }
PTR_VECTOR Children
List of child nodes.
std::map< wxString, int > m_libHashes
Hashes to decide whether a library needs an update
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
wxString Name
Actual name of the part.

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

Referenced by Sync().

◆ 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 FP_TREE_MODEL_ADAPTER::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 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(), LIB_TREE_MODEL_ADAPTER::m_tree, and LIB_TREE_MODEL_ADAPTER::ToItem().

◆ Freeze()

void LIB_TREE_MODEL_ADAPTER::Freeze ( )
inlineinherited

◆ GenerateInfo()

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

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

◆ GetAttr()

bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr ( wxDataViewItem const &  aItem,
unsigned int  aCol,
wxDataViewItemAttr &  aAttr 
) const
overrideprotected

Definition at line 240 of file symbol_tree_synchronizing_adapter.cpp.

242 {
243  if( IsFrozen() )
244  return false;
245 
246  // change attributes only for the name field
247  if( aCol != 0 )
248  return false;
249 
250  auto node = ToNode( aItem );
251  wxCHECK( node, false );
252 
253  switch( node->Type )
254  {
255  case LIB_TREE_NODE::LIB:
256  // mark modified libs with bold font
257  aAttr.SetBold( m_libMgr->IsLibraryModified( node->Name ) );
258 
259 #ifdef __WXGTK__
260  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
261  // This works reasonably well in dark themes, and quite poorly in light ones....
262  if( node->Name == m_libMgr->GetCurrentLib() )
263  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
264 #else
265  // mark the current library with background color
266  if( node->Name == m_libMgr->GetCurrentLib() )
267  {
268  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
269  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
270  }
271 #endif
272  break;
273 
275  // mark modified part with bold font
276  aAttr.SetBold( m_libMgr->IsPartModified( node->Name, node->Parent->Name ) );
277 
278  // mark aliases with italic font
279  aAttr.SetItalic( !node->IsRoot );
280 
281 #ifdef __WXGTK__
282  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
283  // This works reasonably well in dark themes, and quite poorly in light ones....
284  if( node->LibId == m_libMgr->GetCurrentLibId() )
285  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
286 #else
287  // mark the current part with background color
288  if( node->LibId == m_libMgr->GetCurrentLibId() )
289  {
290  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
291  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
292  }
293 #endif
294  break;
295 
296  default:
297  return false;
298  }
299 
300  return true;
301 }
const wxString & GetCurrentLib() const
Returns the currently modified library name.
Definition: lib_manager.h:274
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.
LIB_ID GetCurrentLibId() const
Returns the current library and part name as LIB_ID.
Definition: lib_manager.h:286

References LIB_MANAGER::GetCurrentLib(), LIB_MANAGER::GetCurrentLibId(), LIB_TREE_MODEL_ADAPTER::IsFrozen(), LIB_MANAGER::IsLibraryModified(), LIB_MANAGER::IsPartModified(), LIB_TREE_NODE::LIB, LIB_TREE_NODE::LIBID, m_libMgr, 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 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 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()

TOOL_INTERACTIVE * SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetContextMenuTool ( )
overridevirtual

Reimplemented from LIB_TREE_MODEL_ADAPTER.

Definition at line 50 of file symbol_tree_synchronizing_adapter.cpp.

51 {
53 }
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Class LIB_CONTROL.
Definition: lib_control.h:39

References TOOL_MANAGER::GetTool(), EDA_BASE_FRAME::GetToolManager(), and m_frame.

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

◆ GetItemCount()

int LIB_TREE_MODEL_ADAPTER::GetItemCount ( ) const
inherited

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

◆ GetLibrariesCount()

int SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount ( ) const
overridevirtual

Return the number of libraries loaded in the tree.

Reimplemented from LIB_TREE_MODEL_ADAPTER.

Definition at line 129 of file symbol_tree_synchronizing_adapter.cpp.

130 {
132 
133  for( const auto& libName : m_libMgr->GetLibraryNames() )
134  {
135  if( m_libHashes.count( libName ) == 0 )
136  ++count;
137  }
138 
139  return count;
140 }
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
std::map< wxString, int > m_libHashes
Hashes to decide whether a library needs an update
wxArrayString GetLibraryNames() const
Returns the array of library names.

References LIB_TREE_MODEL_ADAPTER::GetLibrariesCount(), LIB_MANAGER::GetLibraryNames(), m_libHashes, and m_libMgr.

Referenced by Sync().

◆ 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 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 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 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, 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 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 LIB_TREE_MODEL_ADAPTER::ToNode(), and LIB_TREE_NODE::Unit.

◆ GetValue()

void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue ( wxVariant &  aVariant,
wxDataViewItem const &  aItem,
unsigned int  aCol 
) const
overrideprotected

Definition at line 201 of file symbol_tree_synchronizing_adapter.cpp.

203 {
204  if( IsFrozen() )
205  {
206  aVariant = wxEmptyString;
207  return;
208  }
209 
210  auto node = ToNode( aItem );
211  wxASSERT( node );
212 
213  switch( aCol )
214  {
215  case 0:
216  aVariant = node->Name;
217 
218  // mark modified libs with an asterisk
219  if( node->Type == LIB_TREE_NODE::LIB && m_libMgr->IsLibraryModified( node->Name ) )
220  aVariant = node->Name + " *";
221 
222  // mark modified parts with an aster-ix
223  if( node->Type == LIB_TREE_NODE::LIBID
224  && m_libMgr->IsPartModified( node->Name, node->Parent->Name ) )
225  aVariant = node->Name + " *";
226 
227  break;
228 
229  case 1:
230  aVariant = node->Desc;
231  break;
232 
233  default: // column == -1 is used for default Compare function
234  aVariant = node->Name;
235  break;
236  }
237 }
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.

References LIB_TREE_MODEL_ADAPTER::IsFrozen(), LIB_MANAGER::IsLibraryModified(), LIB_MANAGER::IsPartModified(), LIB_TREE_NODE::LIB, LIB_TREE_NODE::LIBID, m_libMgr, 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 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 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 SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer ( const wxDataViewItem &  aItem) const
override

Definition at line 56 of file symbol_tree_synchronizing_adapter.cpp.

57 {
58  const LIB_TREE_NODE* node = ToNode( aItem );
59  return node ? node->Type == LIB_TREE_NODE::LIB : true;
60 }
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::LIB, LIB_TREE_MODEL_ADAPTER::ToNode(), and LIB_TREE_NODE::Type.

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

◆ Sync()

void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync ( bool  aForce = false,
std::function< void(int, int, const wxString &)>  aProgressCallback = [](int, int, const wxString&){} 
)

Definition at line 65 of file symbol_tree_synchronizing_adapter.cpp.

67 {
68  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
69 
70  int libMgrHash = m_libMgr->GetHash();
71 
72  if( !aForce && m_lastSyncHash == libMgrHash )
73  return;
74 
75  m_lastSyncHash = libMgrHash;
76  int i = 0, max = GetLibrariesCount();
77 
78  // Process already stored libraries
79  for( auto it = m_tree.Children.begin(); it != m_tree.Children.end(); /* iteration inside */ )
80  {
81  const wxString& name = it->get()->Name;
82 
83  if( wxGetUTCTimeMillis() > nextUpdate )
84  {
85  aProgressCallback( i, max, name );
86  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
87  }
88 
89  // There is a bug in LIB_MANAGER::LibraryExists() that uses the buffered modified
90  // libraries before the symbol library table which prevents the library from being
91  // removed from the tree control.
92  if( !m_libMgr->LibraryExists( name, true )
93  || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true ) )
94  {
95  it = deleteLibrary( it );
96  continue;
97  }
98  else if( m_libMgr->GetLibraryHash( name ) != m_libHashes[name] )
99  {
100  updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
101  }
102 
103  ++it;
104  ++i;
105  }
106 
107  // Look for new libraries
108  for( const auto& libName : m_libMgr->GetLibraryNames() )
109  {
110  if( m_libHashes.count( libName ) == 0 )
111  {
112  if( wxGetUTCTimeMillis() > nextUpdate )
113  {
114  aProgressCallback( i++, max, libName );
115  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
116  }
117 
118  SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
119 
120  auto& lib_node = m_tree.AddLib( libName, library->GetDescr() );
121  updateLibrary( lib_node );
122  }
123  }
124 
126 }
int m_lastSyncHash
LIB_MANAGER hash value returned in the last synchronization
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
PTR_VECTOR Children
List of child nodes.
std::map< wxString, int > m_libHashes
Hashes to decide whether a library needs an update
int GetLibrariesCount() const override
Return the number of libraries loaded in the tree.
const wxString & GetDescr() const
Return the description of the library referenced by this row.
int GetLibraryHash(const wxString &aLibrary) const
Returns a library hash value to determine if it has changed.
Definition: lib_manager.cpp:88
#define PROGRESS_INTERVAL_MILLIS
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Node type: library.
int GetHash() const
Definition: lib_manager.cpp:77
wxArrayString GetLibraryNames() const
Returns the array of library names.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
LIB_TREE_NODE_LIB & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.
LIB_TREE_NODE::PTR_VECTOR::iterator deleteLibrary(LIB_TREE_NODE::PTR_VECTOR::iterator &aLibNodeIt)
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
const char * name
Definition: DXF_plotter.cpp:61
#define max(a, b)
Definition: auxiliary.h:86
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
size_t i
Definition: json11.cpp:597

References LIB_TREE_NODE_ROOT::AddLib(), LIB_TREE_NODE::AssignIntrinsicRanks(), LIB_TREE_NODE::Children, deleteLibrary(), LIB_TABLE_ROW::GetDescr(), LIB_MANAGER::GetHash(), GetLibrariesCount(), LIB_MANAGER::GetLibrary(), LIB_MANAGER::GetLibraryHash(), LIB_MANAGER::GetLibraryNames(), i, LIB_MANAGER::LibraryExists(), m_frame, m_lastSyncHash, m_libHashes, m_libMgr, LIB_TREE_MODEL_ADAPTER::m_tree, max, name, KIWAY_HOLDER::Prj(), PROGRESS_INTERVAL_MILLIS, and updateLibrary().

Referenced by LIB_MANAGER::Sync().

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

◆ updateLibrary()

void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary ( LIB_TREE_NODE_LIB aLibNode)
protected

Definition at line 143 of file symbol_tree_synchronizing_adapter.cpp.

144 {
145  auto hashIt = m_libHashes.find( aLibNode.Name );
146 
147  if( hashIt == m_libHashes.end() )
148  {
149  // add a new library
150  for( auto alias : m_libMgr->GetAliases( aLibNode.Name ) )
151  aLibNode.AddItem( alias );
152  }
153  else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.Name ) )
154  {
155  // update an existing library
156  std::list<LIB_ALIAS*> aliases = m_libMgr->GetAliases( aLibNode.Name );
157 
158  // remove the common part from the aliases list
159  for( auto nodeIt = aLibNode.Children.begin(); nodeIt != aLibNode.Children.end(); )
160  {
161  auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
162  [&] ( const LIB_ALIAS* a ) {
163  return a->GetName() == (*nodeIt)->Name;
164  } );
165 
166  if( aliasIt != aliases.end() )
167  {
168  // alias exists both in the component tree and the library manager,
169  // update only the node data
170  static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
171  aliases.erase( aliasIt );
172  ++nodeIt;
173  }
174  else
175  {
176  // node does not exist in the library manager, remove the corresponding node
177  nodeIt = aLibNode.Children.erase( nodeIt );
178  }
179  }
180 
181  // now the aliases list contains only new aliases that need to be added to the tree
182  for( auto alias : aliases )
183  aLibNode.AddItem( alias );
184  }
185 
186  aLibNode.AssignIntrinsicRanks();
187  m_libHashes[aLibNode.Name] = m_libMgr->GetLibraryHash( aLibNode.Name );
188 }
Part library alias object definition.
PTR_VECTOR Children
List of child nodes.
std::map< wxString, int > m_libHashes
Hashes to decide whether a library needs an update
std::list< LIB_ALIAS * > GetAliases(const wxString &aLibrary) const
int GetLibraryHash(const wxString &aLibrary) const
Returns a library hash value to determine if it has changed.
Definition: lib_manager.cpp:88
LIB_TREE_NODE_LIB_ID & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
wxString Name
Actual name of the part.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.

References LIB_TREE_NODE_LIB::AddItem(), LIB_TREE_NODE::AssignIntrinsicRanks(), LIB_TREE_NODE::Children, LIB_MANAGER::GetAliases(), LIB_MANAGER::GetLibraryHash(), m_libHashes, m_libMgr, and LIB_TREE_NODE::Name.

Referenced by Sync().

◆ 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 #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 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_frame

LIB_EDIT_FRAME* SYMBOL_TREE_SYNCHRONIZING_ADAPTER::m_frame
protected

Definition at line 61 of file symbol_tree_synchronizing_adapter.h.

Referenced by GetContextMenuTool(), and Sync().

◆ m_lastSyncHash

int SYMBOL_TREE_SYNCHRONIZING_ADAPTER::m_lastSyncHash
protected

LIB_MANAGER hash value returned in the last synchronization

Definition at line 68 of file symbol_tree_synchronizing_adapter.h.

Referenced by Sync().

◆ m_libHashes

std::map<wxString, int> SYMBOL_TREE_SYNCHRONIZING_ADAPTER::m_libHashes
protected

Hashes to decide whether a library needs an update

Definition at line 65 of file symbol_tree_synchronizing_adapter.h.

Referenced by deleteLibrary(), GetLibrariesCount(), Sync(), and updateLibrary().

◆ m_libMgr

LIB_MANAGER* SYMBOL_TREE_SYNCHRONIZING_ADAPTER::m_libMgr
protected

◆ m_tree


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