AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
ATreeModel.h
1// AUI Framework - Declarative UI toolkit for modern C++20
2// Copyright (C) 2020-2024 Alex2772 and Contributors
3//
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2 of the License, or (at your option) any later version.
8//
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library. If not, see <http://www.gnu.org/licenses/>.
16
17//
18// Created by alex2 on 7/1/2021.
19//
20
21
22#pragma once
23
24#include "AUI/Model/ATreeModelIndex.h"
25#include "ITreeModel.h"
26
27
31template<typename T>
32class ATreeModel: public ITreeModel<T> {
33private:
34 struct Node {
35 T value;
36 AVector<_<Node>> children;
38 };
39
40public:
41 struct Item {
42 T value;
43 AVector<Item> children;
44 };
45
46private:
47 static _<Node> itemToNode(Item& item) {
48 auto result = _new<Node>(Node {
49 .value = std::move(item.value),
50 });
51 result->children = item.children.map([&](Item& item) {
52 auto child = itemToNode(item);
53 child->parent = result;
54 return child;
55 });
56
57 return result;
58 }
59
60public:
61 ATreeModel() = default;
62 ATreeModel(AVector<Item> items): mChildren(items.map(itemToNode)) {
63
64 }
65 ~ATreeModel() override = default;
66
67
68 size_t childrenCount(const ATreeModelIndexOrRoot& vertex) override {
69 auto& container = vertex == ATreeModelIndex::ROOT ? mChildren : (*vertex).as<_<Node>>()->children;
70 return container.size();
71 }
72
73 T itemAt(const ATreeModelIndex& index) override {
74 return index.as<_<Node>>()->value;
75 }
76
77 ATreeModelIndex indexOfChild(size_t row, size_t column, const ATreeModelIndexOrRoot& vertex) override {
78 auto& container = vertex == ATreeModelIndex::ROOT ? mChildren : (*vertex).as<_<Node>>()->children;
79 return ATreeModelIndex(row, column, container[row]);
80 }
81
82 ATreeModelIndex makeIndex(_<Node> node) {
83 auto parent = node->parent.lock();
84 auto& children = parent ? parent->children : mChildren;
85 return ATreeModelIndex(children.indexOf(node), 0, std::move(node));
86 }
87
88 ATreeModelIndexOrRoot parent(const ATreeModelIndex& vertex) override {
89 if (auto p = vertex.as<_<Node>>()->parent.lock()) {
90 return makeIndex(std::move(p));
91 }
93 }
94
95private:
96
97 AVector<_<Node>> mChildren;
98};
Valid index of ITreeModel.
Definition: ATreeModelIndex.h:10
static constexpr struct ATreeModelIndex::@2 ROOT
Tag type to define root vertex.
T as() const
any_cast the external user data stored in this vertex.
Definition: ATreeModelIndex.h:33
Basic implementation of ITreeModel.
Definition: ATreeModel.h:32
ATreeModelIndexOrRoot parent(const ATreeModelIndex &vertex) override
Creates index of parent vertex of the specified vertex.
Definition: ATreeModel.h:88
T itemAt(const ATreeModelIndex &index) override
value representation, used by ATreeView.
Definition: ATreeModel.h:73
ATreeModelIndex indexOfChild(size_t row, size_t column, const ATreeModelIndexOrRoot &vertex) override
Creates valid ATreeModelIndex of the child.
Definition: ATreeModel.h:77
size_t childrenCount(const ATreeModelIndexOrRoot &vertex) override
Count of children of the vertex.
Definition: ATreeModel.h:68
A std::vector with AUI extensions.
Definition: AVector.h:38
Tree model.
Definition: ITreeModel.h:35
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Definition: ATreeModel.h:41
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:51