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.

237 lines
7.1 KiB

  1. /*
  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-2022 KiCad Developers, see AUTHORS.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 LIB_TREE_H
  25. #define LIB_TREE_H
  26. #include <wx/panel.h>
  27. #include <lib_tree_model_adapter.h>
  28. #include <html_window.h>
  29. class wxDataViewCtrl;
  30. class wxTextCtrl;
  31. class wxHtmlLinkEvent;
  32. class wxSearchCtrl;
  33. class wxTimer;
  34. class wxTimerEvent;
  35. class ACTION_MENU;
  36. class LIB_ID;
  37. class LIB_TABLE;
  38. /**
  39. * Widget displaying a tree of symbols with optional search text control and description panel.
  40. */
  41. class LIB_TREE : public wxPanel
  42. {
  43. public:
  44. ///< Flags to select extra widgets and options
  45. enum FLAGS
  46. {
  47. NONE = 0x00,
  48. SEARCH = 0x01,
  49. DETAILS = 0x02,
  50. ALL_WIDGETS = 0x0F,
  51. MULTISELECT = 0x10
  52. };
  53. /**
  54. * Construct a symbol tree.
  55. *
  56. * @param aParent parent window containing this tree widget
  57. * @param aRecentSearchesKey a key into a global map storing recent searches (usually "power",
  58. * "symbols", or "footprints", but could be further differentiated)
  59. * @param aLibTable table containing libraries and items to display
  60. * @param aAdapter a LIB_TREE_MODEL_ADAPTER instance to use
  61. * @param aFlags selection of sub-widgets to include and other options
  62. * @param aDetails if not null, a custom HTML_WINDOW to hold symbol details. If null this
  63. * will be created inside the LIB_TREE.
  64. */
  65. LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey, LIB_TABLE* aLibTable,
  66. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, int aFlags = ALL_WIDGETS,
  67. HTML_WINDOW* aDetails = nullptr );
  68. ~LIB_TREE() override;
  69. /**
  70. * For multi-unit symbols, if the user selects the symbol itself
  71. * rather than picking an individual unit, 0 will be returned in aUnit.
  72. * Beware that this is an invalid unit number - this should be replaced
  73. * with whatever default is desired (usually 1).
  74. *
  75. * @param aUnit if not NULL, the selected unit is filled in here.
  76. * @return the library id of the symbol that has been selected.
  77. */
  78. LIB_ID GetSelectedLibId( int* aUnit = nullptr ) const;
  79. int GetSelectionCount() const
  80. {
  81. return m_tree_ctrl->GetSelectedItemsCount();
  82. }
  83. /**
  84. * Retrieves a list of selections for trees that allow multi-selection
  85. * @see GetSelectedLibId for details on how aUnit will be filled.
  86. * @param aSelection will be filled with a list of selected LIB_IDs
  87. * @param aUnit is an optional pointer to a list to fill with unit numbers
  88. * @return the number of selected items
  89. */
  90. int GetSelectedLibIds( std::vector<LIB_ID>& aSelection,
  91. std::vector<int>* aUnit = nullptr ) const;
  92. LIB_TREE_NODE* GetCurrentTreeNode() const;
  93. /**
  94. * Select an item in the tree widget.
  95. */
  96. void SelectLibId( const LIB_ID& aLibId );
  97. /**
  98. * Ensure that an item is visible (preferably centered).
  99. */
  100. void CenterLibId( const LIB_ID& aLibId );
  101. /**
  102. * Unselect currently selected item in wxDataViewCtrl
  103. */
  104. void Unselect();
  105. /**
  106. * Expand and item i the tree widget.
  107. */
  108. void ExpandLibId( const LIB_ID& aLibId );
  109. /**
  110. * Save/restore search string.
  111. */
  112. void SetSearchString( const wxString& aSearchString );
  113. wxString GetSearchString() const;
  114. /**
  115. * Regenerate the tree.
  116. */
  117. void Regenerate( bool aKeepState );
  118. /**
  119. * Refreshes the tree (mainly to update highlighting and asterisking)
  120. */
  121. void RefreshLibTree();
  122. wxWindow* GetFocusTarget();
  123. /**
  124. * Focus the search widget if it exists
  125. */
  126. void FocusSearchFieldIfExists();
  127. protected:
  128. /**
  129. * Expand or collapse a node, switching it to the opposite state.
  130. */
  131. void toggleExpand( const wxDataViewItem& aTreeId );
  132. /**
  133. * If a wxDataViewitem is valid, select it and post a selection event.
  134. */
  135. void selectIfValid( const wxDataViewItem& aTreeId );
  136. void centerIfValid( const wxDataViewItem& aTreeId );
  137. void expandIfValid( const wxDataViewItem& aTreeId );
  138. /**
  139. * Post a wxEVT_DATAVIEW_SELECTION_CHANGED to notify the selection handler
  140. * that a new part has been preselected.
  141. */
  142. void postPreselectEvent();
  143. /**
  144. * Post SYMBOL_SELECTED event to notify the selection handler that a part has been selected.
  145. */
  146. void postSelectEvent();
  147. /**
  148. * Structure storing state of the symbol tree widget.
  149. */
  150. struct STATE
  151. {
  152. ///< List of expanded nodes
  153. std::vector<wxDataViewItem> expanded;
  154. std::vector<wxString> pinned;
  155. ///< Current selection, might be not valid if nothing was selected
  156. LIB_ID selection;
  157. };
  158. /**
  159. * Return the symbol tree widget state.
  160. */
  161. STATE getState() const;
  162. /**
  163. * Restore the symbol tree widget state from an object.
  164. */
  165. void setState( const STATE& aState );
  166. void updateRecentSearchMenu();
  167. void onQueryText( wxCommandEvent& aEvent );
  168. void onQueryCharHook( wxKeyEvent& aEvent );
  169. void onQueryMouseMoved( wxMouseEvent& aEvent );
  170. void onTreeSelect( wxDataViewEvent& aEvent );
  171. void onTreeActivate( wxDataViewEvent& aEvent );
  172. void onTreeCharHook( wxKeyEvent& aEvent );
  173. void onSize( wxSizeEvent& aEvent );
  174. void onDetailsLink( wxHtmlLinkEvent& aEvent );
  175. void onPreselect( wxCommandEvent& aEvent );
  176. void onItemContextMenu( wxDataViewEvent& aEvent );
  177. void onHeaderContextMenu( wxDataViewEvent& aEvent );
  178. void onDebounceTimer( wxTimerEvent& aEvent );
  179. protected:
  180. LIB_TABLE* m_lib_table;
  181. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> m_adapter;
  182. wxSearchCtrl* m_query_ctrl;
  183. wxDataViewCtrl* m_tree_ctrl;
  184. HTML_WINDOW* m_details_ctrl;
  185. wxTimer* m_debounceTimer;
  186. bool m_inTimerEvent;
  187. LIB_ID m_last_libid;
  188. wxString m_recentSearchesKey;
  189. bool m_skipNextRightClick;
  190. };
  191. ///< Custom event sent when a new symbol is preselected
  192. wxDECLARE_EVENT( SYMBOL_PRESELECTED, wxCommandEvent );
  193. ///< Custom event sent when a symbol is selected
  194. wxDECLARE_EVENT( SYMBOL_SELECTED, wxCommandEvent );
  195. #endif /* LIB_TREE_H */