Ember
Loading...
Searching...
No Matches
TreeHierarchyTab.h
Go to the documentation of this file.
1#pragma once
2
3#include "Interfaces/ITab.h"
6#include <functional>
7#include <memory>
8#include <wx/srchctrl.h>
9#include <wx/treectrl.h>
10#include <wx/wx.h>
11
12namespace EmberUI {
13
15struct TreeNodeItemData : public wxTreeItemData {
18};
19
21struct DummyTreeItemData : public wxTreeItemData {
22 DummyTreeItemData() = default;
23};
24
27 bool showSearchBar = true;
28 bool showNodeIcons = false;
29 bool showTreeLines = false;
31 int fontSize = 9;
32 bool autoExpandOnLoad = true;
34 bool liveSearch = true;
36 wxColour backgroundColour{50, 50, 50};
37 wxColour foregroundColour{255, 255, 255};
38 wxColour panelColour{60, 60, 60};
39};
40
48class TreeHierarchyTab : public wxPanel, public ITab {
49 public:
50 using SelectionCallback = std::function<void(EmberCore::ITreeNode *)>;
51 using LayoutInvalidationCallback = std::function<void()>;
52
53 explicit TreeHierarchyTab(wxWindow *parent, const TreeHierarchyConfig &config = {});
54 ~TreeHierarchyTab() override = default;
55
56 // ITab
57 wxWindow *GetWidget() override { return this; }
58 wxString GetTitle() const override { return "Hierarchy"; }
59 wxString GetTabType() const override { return "Hierarchy"; }
60
62 void SetTree(std::shared_ptr<EmberCore::ITreeStructure> tree);
64 void ClearTree();
66 void RefreshTree();
68 void ExpandAll();
70 void CollapseAll();
71
73 void SelectNodeById(size_t nodeId);
76
81
83 const TreeHierarchyConfig &GetConfig() const { return m_config; }
85 void ApplyConfig(const TreeHierarchyConfig &config);
86
87 protected:
89 virtual wxString GetNodeDisplayText(EmberCore::ITreeNode *node) const;
91 virtual void OnPopulateContextMenu(EmberCore::ITreeNode *node, wxMenu &menu) {}
93 virtual void OnContextMenuCommand(int id, EmberCore::ITreeNode *node) {}
97 virtual void OnSelectionChanged(EmberCore::ITreeNode *node);
99 virtual void OnNodeExpanding(EmberCore::ITreeNode *node);
101 virtual void OnNodeCollapsing(EmberCore::ITreeNode *node);
102
104 wxTreeCtrl *GetTreeCtrl() const { return m_treeCtrl; }
106 wxSearchCtrl *GetSearchCtrl() const { return m_searchCtrl; }
108 std::shared_ptr<EmberCore::ITreeStructure> GetTree() const { return m_tree; }
110 EmberCore::ITreeNode *GetNodeFromItem(const wxTreeItemId &item) const;
112 wxTreeItemId FindItemById(size_t nodeId, const wxTreeItemId &start = wxTreeItemId());
114 wxTreeItemId ExpandPathToNode(EmberCore::ITreeNode *node);
116 void ScrollToItemHorizontally(const wxTreeItemId &item);
118 void SelectRange(const wxTreeItemId &from, const wxTreeItemId &to);
119
121 wxTreeItemId m_lastSelectedItem;
122
123 private:
124 void CreateLayout();
125 void PopulateTree();
126 void AddFilteredNodes(const wxTreeItemId &parentItem, EmberCore::ITreeNode *node);
127 void AddChildrenToItem(const wxTreeItemId &parentItem, EmberCore::ITreeNode *node);
128 void ExpandTreeToDepth(const wxTreeItemId &item, int remainingDepth);
129 void ExpandTreeByVisibility(const wxTreeItemId &item);
130
131 // Event handlers
132 void OnTreeItemExpanding(wxTreeEvent &event);
133 void OnTreeItemCollapsing(wxTreeEvent &event);
134 void OnTreeSelectionChanged(wxTreeEvent &event);
135 void OnTreeItemRightClick(wxTreeEvent &event);
136 void OnTreeItemActivated(wxTreeEvent &event);
137 void OnSearchTextChanged(wxCommandEvent &event);
138 void OnSearchEnter(wxCommandEvent &event);
139
140 wxSearchCtrl *m_searchCtrl = nullptr;
141 wxTreeCtrl *m_treeCtrl = nullptr;
142 wxBoxSizer *m_mainSizer = nullptr;
143
144 std::shared_ptr<EmberCore::ITreeStructure> m_tree;
146
149 bool m_isRefreshing = false;
150
151 enum { ID_TREE_CTRL = wxID_HIGHEST + 200, ID_SEARCH_CTRL };
152
154};
155
156} // namespace EmberUI
Abstract interface for tree nodes that can be visualized.
Definition ITreeNode.h:31
std::function< void(EmberCore::ITreeNode *)> SelectionCallback
EmberCore::ITreeNode * GetSelectedNode() const
Returns the currently selected node, or nullptr.
void SelectNodeById(size_t nodeId)
Selects the tree item with the given node ID.
wxTreeItemId ExpandPathToNode(EmberCore::ITreeNode *node)
Expands path from root to node and returns its item ID.
virtual void OnSelectionChanged(EmberCore::ITreeNode *node)
Called when selection changes; subclasses may override.
virtual void OnNodeActivated(EmberCore::ITreeNode *node)
Hook called when a node is double-clicked/activated.
SelectionCallback m_selectionCallback
void AddFilteredNodes(const wxTreeItemId &parentItem, EmberCore::ITreeNode *node)
TreeHierarchyTab(wxWindow *parent, const TreeHierarchyConfig &config={})
void ExpandTreeToDepth(const wxTreeItemId &item, int remainingDepth)
virtual void OnContextMenuCommand(int id, EmberCore::ITreeNode *node)
Hook to handle context menu command.
~TreeHierarchyTab() override=default
wxTreeCtrl * GetTreeCtrl() const
Returns the tree control widget.
wxString GetTitle() const override
Returns the display title of the tab.
void SetSelectionCallback(SelectionCallback cb)
Sets callback invoked when selection changes.
wxWindow * GetWidget() override
Returns the wxWidgets window used as the tab content.
wxSearchCtrl * GetSearchCtrl() const
Returns the search control widget.
void SetLayoutInvalidationCallback(LayoutInvalidationCallback cb)
Sets callback invoked when layout needs invalidation (e.g. canvas sync).
const TreeHierarchyConfig & GetConfig() const
Returns the current configuration.
void CollapseAll()
Collapses all tree items.
virtual wxString GetNodeDisplayText(EmberCore::ITreeNode *node) const
Returns display text for a node; subclasses may override.
virtual void OnNodeExpanding(EmberCore::ITreeNode *node)
Called when a node is expanding; used for lazy loading.
void RefreshTree()
Rebuilds the tree from the current structure.
std::function< void()> LayoutInvalidationCallback
void ScrollToItemHorizontally(const wxTreeItemId &item)
Scrolls horizontally so the item is visible.
void OnTreeItemCollapsing(wxTreeEvent &event)
void ClearTree()
Clears the tree and removes all items.
EmberCore::ITreeNode * GetNodeFromItem(const wxTreeItemId &item) const
Extracts ITreeNode from a tree item (or nullptr if DummyTreeItemData).
void ApplyConfig(const TreeHierarchyConfig &config)
Applies new configuration and refreshes the tree.
void OnTreeItemRightClick(wxTreeEvent &event)
void OnTreeItemExpanding(wxTreeEvent &event)
virtual void OnNodeCollapsing(EmberCore::ITreeNode *node)
Called when a node is collapsing.
LayoutInvalidationCallback m_layoutInvalidationCb
void OnSearchTextChanged(wxCommandEvent &event)
void ExpandTreeByVisibility(const wxTreeItemId &item)
wxTreeItemId FindItemById(size_t nodeId, const wxTreeItemId &start=wxTreeItemId())
Finds tree item by node ID, optionally starting from given item.
virtual void OnPopulateContextMenu(EmberCore::ITreeNode *node, wxMenu &menu)
Hook to populate context menu for a node.
std::shared_ptr< EmberCore::ITreeStructure > m_tree
TreeHierarchyConfig m_config
void ExpandAll()
Expands all tree items.
wxString GetTabType() const override
Returns the tab type identifier.
void OnSearchEnter(wxCommandEvent &event)
void AddChildrenToItem(const wxTreeItemId &parentItem, EmberCore::ITreeNode *node)
void OnTreeItemActivated(wxTreeEvent &event)
void SelectRange(const wxTreeItemId &from, const wxTreeItemId &to)
Selects a range of items from from to to.
void OnTreeSelectionChanged(wxTreeEvent &event)
std::shared_ptr< EmberCore::ITreeStructure > GetTree() const
Returns the current tree structure.
void SetTree(std::shared_ptr< EmberCore::ITreeStructure > tree)
Sets the tree structure to display.
Interface for tab-based UI components in the application.
Definition ITab.h:7
Definition Panel.h:8
Configuration for TreeHierarchyTab appearance and behavior.
wxColour foregroundColour
Tree text.
int treeIndentationSize
Indentation per level (pixels)
bool autoExpandOnLoad
Auto-expand tree on load.
bool syncSelectionWithCanvas
Sync selection with canvas.
bool showTreeLines
Show connecting lines between nodes.
bool showNodeIcons
Show icons for node types.
bool showSearchBar
Show search bar above tree.
int autoExpandDepth
Depth to auto-expand.
bool liveSearch
Filter as user types.
wxColour backgroundColour
Tree background.
wxColour panelColour
Panel background.
EmberCore::ITreeNode * node
TreeNodeItemData(EmberCore::ITreeNode *n)