/* * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace GUI { class TreeViewModel final : public Model { public: static NonnullRefPtr create() { return adopt_ref(*new TreeViewModel()); } virtual ~TreeViewModel() override = default; virtual int row_count(ModelIndex const& = {}) const override; virtual int column_count(ModelIndex const& = {}) const override { return 1; } virtual Variant data(ModelIndex const&, ModelRole role) const override; virtual ModelIndex parent_index(ModelIndex const&) const override; virtual ModelIndex index(int row, int column, ModelIndex const& parent = {}) const override; class Node : public RefCounted , public Weakable { public: Node(DeprecatedString text, Optional icon, Node* parent_node = nullptr) : m_text(move(text)) , m_icon(move(icon)) , m_parent_node(parent_node) { } virtual ~Node() = default; template NonnullRefPtr add_node(DeprecatedString text, Optional icon, Args&&... args) requires(IsBaseOf) { auto node = adopt_ref(*new NodeType(move(text), move(icon), this, forward(args)...)); m_child_nodes.append(*static_cast(node.ptr())); return node; } DeprecatedString const& text() const { return m_text; } Optional const& icon() const { return m_icon; } Node const* parent_node() const { return m_parent_node; } Node* parent_node() { return m_parent_node; } Vector> const& child_nodes() const { return m_child_nodes; } Vector>& child_nodes() { return m_child_nodes; } private: DeprecatedString m_text; Optional m_icon; WeakPtr m_parent_node; Vector> m_child_nodes; }; Vector> const& nodes() const { return m_nodes; } Vector>& nodes() { return m_nodes; } template NonnullRefPtr add_node(DeprecatedString text, Optional icon, Args&&... args) requires(IsBaseOf) { auto node = adopt_ref(*new NodeType(move(text), move(icon), nullptr, forward(args)...)); m_nodes.append(*static_cast(node.ptr())); return node; } Optional index_for_node(Node const&, ModelIndex const& parent = {}) const; private: TreeViewModel() = default; Vector> m_nodes; }; }