17#include <unordered_set>
29 template <
typename NodeT,
typename ElemT>
30 const NodeT *unwrapNodePtr(
const ElemT &elem)
noexcept
32 using Decayed = std::decay_t<ElemT>;
33 if constexpr (std::is_same_v<Decayed, NodeT>)
35 else if constexpr (std::is_pointer_v<Decayed>)
43 template <
typename NodeT,
typename KeyT>
47 const NodeT *
node{
nullptr};
62 template <
typename NodeT,
typename KeyT>
67 using KeyFn = std::function<KeyT(
const NodeT &)>;
68 using ChildVisitor = std::function<bool(
const NodeT &)>;
69 using ChildrenInvoker = std::function<void(
const NodeT &,
const ChildVisitor &)>;
77 template <
typename KeyFnT,
typename ChildrenFnT>
79 : keyFn_(std::forward<KeyFnT>(keyFn)),
80 visitChildren_([fn = std::forward<ChildrenFnT>(childrenFn)](
82 const ChildVisitor &visitor)
84 for (
const auto &ch : fn(node))
86 if (
const NodeT *ptr = detail::unwrapNodePtr<NodeT>(ch))
101 template <
typename Range>
102 void setRoots(
const Range &roots,
bool notify =
true)
105 for (
const auto &r : roots)
107 if (
const NodeT *ptr = detail::unwrapNodePtr<NodeT>(r))
108 roots_.push_back(ptr);
117 for (
const NodeT *root : roots_)
120 appendSubtree(*root, 0, -1);
122 if (notify && onChanged)
126 size_t count() const noexcept {
return rows_.size(); }
127 bool empty() const noexcept {
return rows_.empty(); }
128 const Row &at(
size_t index)
const {
return rows_.at(index); }
129 const std::vector<Row> &rows() const noexcept {
return rows_; }
134 for (
size_t i = 0; i < rows_.size(); ++i)
135 if (rows_[i].key == key)
136 return static_cast<int>(i);
140 bool isExpanded(
const KeyT &key)
const {
return expanded_.count(key) > 0; }
143 bool setExpanded(
const KeyT &key,
bool expanded,
bool notify =
true)
145 const bool changed = expanded ? expanded_.insert(key).second : (expanded_.erase(key) > 0);
149 if (notify && onItemToggled)
150 onItemToggled(key, expanded);
155 bool toggle(
const KeyT &key,
bool notify =
true)
163 for (
const NodeT *root : roots_)
166 expandAllRecursive(*root);
174 if (expanded_.empty())
180 void select(
const KeyT &key,
bool notify =
true)
182 if (selected_.has_value() && *selected_ == key)
185 if (notify && onSelectionChanged)
186 onSelectionChanged(selected_);
189 void clearSelection(
bool notify =
true)
191 if (!selected_.has_value())
194 if (notify && onSelectionChanged)
195 onSelectionChanged(selected_);
198 bool isSelected(
const KeyT &key)
const
200 return selected_.has_value() && *selected_ == key;
203 const std::optional<KeyT> &selectedKey() const noexcept {
return selected_; }
208 return selected_.has_value() ?
findRowByKey(*selected_) : -1;
221 const int n =
static_cast<int>(rows_.size());
227 select(rows_.front().key,
true);
228 else if (curIdx + 1 < n)
229 select(rows_[
static_cast<size_t>(curIdx + 1)].key,
true);
234 select(rows_.back().key,
true);
236 select(rows_[
static_cast<size_t>(curIdx - 1)].key,
true);
240 select(rows_.front().key,
true);
244 select(rows_.back().key,
true);
250 const Row cur = rows_[
static_cast<size_t>(curIdx)];
255 else if (curIdx + 1 < n && rows_[
static_cast<size_t>(curIdx + 1)].parentRowIndex == curIdx)
256 select(rows_[
static_cast<size_t>(curIdx + 1)].key,
true);
264 const Row cur = rows_[
static_cast<size_t>(curIdx)];
273 if (curIdx >= 0 && onItemActivated)
274 onItemActivated(rows_[
static_cast<size_t>(curIdx)]);
282 std::function<void()> onChanged;
283 std::function<void(
const std::optional<KeyT> &)> onSelectionChanged;
284 std::function<void(
const Row &)> onItemActivated;
285 std::function<void(
const KeyT &,
bool)> onItemToggled;
288 bool nodeHasChildren(
const NodeT &node)
const
291 visitChildren_(node, [&](
const NodeT &) ->
bool
299 void appendSubtree(
const NodeT &node,
int depth,
int parentRowIndex)
301 KeyT key = keyFn_(node);
302 const bool hasKids = nodeHasChildren(node);
303 const bool exp = hasKids && isExpanded(key);
305 const int myIndex =
static_cast<int>(rows_.size());
306 rows_.push_back(Row{std::move(key), &node, depth, parentRowIndex, hasKids, exp});
310 visitChildren_(node, [&](
const NodeT &child) ->
bool
312 appendSubtree(child, depth + 1, myIndex);
318 void expandAllRecursive(
const NodeT &node)
320 bool hasKids =
false;
321 visitChildren_(node, [&](
const NodeT &child) ->
bool
324 expandAllRecursive(child);
328 expanded_.insert(keyFn_(node));
332 ChildrenInvoker visitChildren_;
333 std::vector<const NodeT *> roots_;
334 std::vector<Row> rows_;
335 std::unordered_set<KeyT> expanded_;
336 std::optional<KeyT> selected_;
Centralized standard library includes used throughout ZenitUI.
void expandAll(bool notify=true)
Expand every branch reachable from the current roots.
Definition FlatTreeModel.hpp:161
int findRowByKey(const KeyT &key) const
Index of key in the current flat projection, or -1 if hidden/absent.
Definition FlatTreeModel.hpp:132
bool toggle(const KeyT &key, bool notify=true)
Toggle the expanded state of key.
Definition FlatTreeModel.hpp:155
bool handleKey(int key)
Handle standard tree navigation keys (Up, Down, Left, Right, Home, End, Enter).
Definition FlatTreeModel.hpp:215
FlatTreeModel(KeyFnT &&keyFn, ChildrenFnT &&childrenFn)
Construct a model from a key extractor and a children range accessor.
Definition FlatTreeModel.hpp:78
void setRoots(const Range &roots, bool notify=true)
Set root nodes from any iterable range of NodeT or pointers to NodeT.
Definition FlatTreeModel.hpp:102
bool setExpanded(const KeyT &key, bool expanded, bool notify=true)
Expand or collapse key.
Definition FlatTreeModel.hpp:143
void rebuild(bool notify=true)
Re-project the flat row list from the current roots and expanded set.
Definition FlatTreeModel.hpp:114
void collapseAll(bool notify=true)
Collapse every branch.
Definition FlatTreeModel.hpp:172
int selectedRowIndex() const
Flat index of the selected row, or -1 if nothing is selected or it is collapsed.
Definition FlatTreeModel.hpp:206
One visible row produced by FlatTreeModel.
Definition FlatTreeModel.hpp:45
bool hasChildren
true if the node has at least one child.
Definition FlatTreeModel.hpp:50
int parentRowIndex
Index of the parent row in rows(), or -1 for roots.
Definition FlatTreeModel.hpp:49
int depth
Nesting level (0 for roots).
Definition FlatTreeModel.hpp:48
bool expanded
true if the branch is currently expanded.
Definition FlatTreeModel.hpp:51
const NodeT * node
Non-owning pointer to the user's C++ tree node.
Definition FlatTreeModel.hpp:47