ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ZenitUI::UI::FlatTreeModel< NodeT, KeyT > Class Template Reference

Maintains expanded state, selection, keyboard navigation, and a flat projection of visible rows ready to drive VirtualList. More...

#include <FlatTreeModel.hpp>

Public Types

using Row = FlatTreeRow<NodeT, KeyT>
using KeyFn = std::function<KeyT(const NodeT &)>
using ChildVisitor = std::function<bool(const NodeT &)>
using ChildrenInvoker = std::function<void(const NodeT &, const ChildVisitor &)>

Public Member Functions

template<typename KeyFnT, typename ChildrenFnT>
 FlatTreeModel (KeyFnT &&keyFn, ChildrenFnT &&childrenFn)
 Construct a model from a key extractor and a children range accessor.
template<typename Range>
void setRoots (const Range &roots, bool notify=true)
 Set root nodes from any iterable range of NodeT or pointers to NodeT.
void rebuild (bool notify=true)
 Re-project the flat row list from the current roots and expanded set.
size_t count () const noexcept
bool empty () const noexcept
const Row & at (size_t index) const
const std::vector< Row > & rows () const noexcept
int findRowByKey (const KeyT &key) const
 Index of key in the current flat projection, or -1 if hidden/absent.
bool isExpanded (const KeyT &key) const
bool setExpanded (const KeyT &key, bool expanded, bool notify=true)
 Expand or collapse key.
bool toggle (const KeyT &key, bool notify=true)
 Toggle the expanded state of key.
void expandAll (bool notify=true)
 Expand every branch reachable from the current roots.
void collapseAll (bool notify=true)
 Collapse every branch.
void select (const KeyT &key, bool notify=true)
void clearSelection (bool notify=true)
bool isSelected (const KeyT &key) const
const std::optional< KeyT > & selectedKey () const noexcept
int selectedRowIndex () const
 Flat index of the selected row, or -1 if nothing is selected or it is collapsed.
bool handleKey (int key)
 Handle standard tree navigation keys (Up, Down, Left, Right, Home, End, Enter).

Public Attributes

std::function< void()> onChanged
std::function< void(const std::optional< KeyT > &)> onSelectionChanged
std::function< void(const Row &)> onItemActivated
std::function< void(const KeyT &, bool)> onItemToggled

Detailed Description

template<typename NodeT, typename KeyT>
class ZenitUI::UI::FlatTreeModel< NodeT, KeyT >

Maintains expanded state, selection, keyboard navigation, and a flat projection of visible rows ready to drive VirtualList.

Template Parameters
NodeTUser-defined tree node type (never copied).
KeyTHashable, equality-comparable stable node identifier.
Examples
14_desktop_workspace/main.cpp.

Constructor & Destructor Documentation

◆ FlatTreeModel()

template<typename NodeT, typename KeyT>
template<typename KeyFnT, typename ChildrenFnT>
ZenitUI::UI::FlatTreeModel< NodeT, KeyT >::FlatTreeModel ( KeyFnT && keyFn,
ChildrenFnT && childrenFn )
inline

Construct a model from a key extractor and a children range accessor.

Parameters
keyFnKeyT(const NodeT &) returning a stable unique key per node.
childrenFnReturns any iterable range of NodeT, NodeT *, unique_ptr<NodeT>, or shared_ptr<NodeT>.

Member Function Documentation

◆ handleKey()

template<typename NodeT, typename KeyT>
bool ZenitUI::UI::FlatTreeModel< NodeT, KeyT >::handleKey ( int key)
inline

Handle standard tree navigation keys (Up, Down, Left, Right, Home, End, Enter).

Returns
true if the key was consumed.

◆ setExpanded()

template<typename NodeT, typename KeyT>
bool ZenitUI::UI::FlatTreeModel< NodeT, KeyT >::setExpanded ( const KeyT & key,
bool expanded,
bool notify = true )
inline

Expand or collapse key.

Returns true if the state changed.

Examples
14_desktop_workspace/main.cpp.

◆ setRoots()

template<typename NodeT, typename KeyT>
template<typename Range>
void ZenitUI::UI::FlatTreeModel< NodeT, KeyT >::setRoots ( const Range & roots,
bool notify = true )
inline

Set root nodes from any iterable range of NodeT or pointers to NodeT.

Preserves existing expanded/selected keys across data refreshes.

Examples
14_desktop_workspace/main.cpp.

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