You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.5 KiB

  1. /* -*- c++ -*-
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
  5. * Copyright (C) 2014 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http: *www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http: *www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef COMPONENT_TREE_SEARCH_CONTAINER_H
  25. #define COMPONENT_TREE_SEARCH_CONTAINER_H
  26. #include <vector>
  27. #include <wx/string.h>
  28. class LIB_ALIAS;
  29. class PART_LIB;
  30. class PART_LIBS;
  31. class wxTreeCtrl;
  32. class wxArrayString;
  33. // class COMPONENT_TREE_SEARCH_CONTAINER
  34. // A container for components that allows to search them matching their name, keywords
  35. // and descriptions, updating a wxTreeCtrl with the results (toplevel nodes:
  36. // libraries, leafs: components), scored by relevance.
  37. //
  38. // The scored result list is adpated on each update on the search-term: this allows
  39. // to have a search-as-you-type experience.
  40. class COMPONENT_TREE_SEARCH_CONTAINER
  41. {
  42. public:
  43. COMPONENT_TREE_SEARCH_CONTAINER( PART_LIBS* aLibs );
  44. ~COMPONENT_TREE_SEARCH_CONTAINER();
  45. /** Function AddLibrary
  46. * Add all the components and their aliases of this library to be searched.
  47. * To be called in the setup phase to fill this container.
  48. *
  49. * @param aLib containting all the components to be added.
  50. */
  51. void AddLibrary( PART_LIB& aLib );
  52. /** Function AddComponentList
  53. * Add the given list of components, given by name, to be searched.
  54. * To be called in the setup phase to fill this container.
  55. *
  56. * @param aNodeName The parent node name the components will show up as leaf.
  57. * @param aAliasNameList List of alias names.
  58. * @param aOptionalLib Library to look up the component names (if NULL: global lookup)
  59. */
  60. void AddAliasList( const wxString& aNodeName, const wxArrayString& aAliasNameList,
  61. PART_LIB* aOptionalLib );
  62. /** Function SetPreselectNode
  63. * Set the component name to be selected in absence of any search-result.
  64. *
  65. * @param aComponentName the component name to be selected.
  66. * @param aUnit the component unit to be selected (if > 0).
  67. */
  68. void SetPreselectNode( const wxString& aComponentName, int aUnit );
  69. /** Function SetTree
  70. * Set the tree to be manipulated.
  71. * Each update of the search term will update the tree, with the most
  72. * scoring component at the top and selected. If a preselect node is set, this
  73. * is displayed. Does not take ownership of the tree.
  74. *
  75. * @param aTree that is to be modified on search updates.
  76. */
  77. void SetTree( wxTreeCtrl* aTree );
  78. /** Function UpdateSearchTerm
  79. * Update the search string provided by the user and narrow down the result list.
  80. *
  81. * This string is a space-separated list of terms, each of which
  82. * is applied to the components list to narrow it down. Results are scored by
  83. * relevancy (e.g. exact match scores higher than prefix-match which in turn scores
  84. * higher than substring match). This updates the search and tree on each call.
  85. *
  86. * @param aSearch is the user-provided search string.
  87. */
  88. void UpdateSearchTerm( const wxString& aSearch );
  89. /** Function GetSelectedAlias
  90. *
  91. * @param if not-NULL, the selected sub-unit is set here.
  92. * @return the selected alias or NULL if there is none, or there is no tree.
  93. */
  94. LIB_ALIAS* GetSelectedAlias( int* aUnit );
  95. private:
  96. struct TREE_NODE;
  97. static bool scoreComparator( const TREE_NODE* a1, const TREE_NODE* a2 );
  98. std::vector<TREE_NODE*> nodes;
  99. wxTreeCtrl* tree;
  100. int libraries_added;
  101. wxString preselect_node_name;
  102. int preselect_unit_number;
  103. PART_LIBS* m_libs; // no ownership
  104. };
  105. #endif /* COMPONENT_TREE_SEARCH_CONTAINER_H */