LibGUI: Add TreeViewModel as a simpler interface for building TreeViews

Having to subclass GUI::Model for even the simplest type of hand-built
TreeView makes them a bit unpleasant to work with at the moment. :^)

This adds such a GUI::Model subclass that is specifically designed for
adding nodes to a TreeView manually, supporting text and an optional
icon by default, and allowing for further data when subclassing the Node
class.
This commit is contained in:
Linus Groh 2022-05-12 21:36:15 +02:00
parent 80174439a9
commit 5bb38296b1
3 changed files with 174 additions and 0 deletions

View file

@ -106,6 +106,7 @@ set(SOURCES
ToolbarContainer.cpp
Tray.cpp
TreeView.cpp
TreeViewModel.cpp
UndoStack.cpp
ValueSlider.cpp
Variant.cpp

View file

@ -0,0 +1,83 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGUI/TreeViewModel.h>
namespace GUI {
ModelIndex TreeViewModel::index(int row, int column, ModelIndex const& parent) const
{
if (!parent.is_valid()) {
if (static_cast<size_t>(row) >= m_nodes.size())
return {};
return create_index(row, column, &m_nodes[row]);
}
auto const& parent_node = *static_cast<Node const*>(parent.internal_data());
if (static_cast<size_t>(row) >= parent_node.child_nodes().size())
return {};
auto const* child = &parent_node.child_nodes()[row];
return create_index(row, column, child);
}
ModelIndex TreeViewModel::parent_index(ModelIndex const& index) const
{
if (!index.is_valid())
return {};
auto const& child_node = *static_cast<Node const*>(index.internal_data());
auto const* parent_node = child_node.parent_node();
if (parent_node == nullptr)
return {};
if (parent_node->parent_node() == nullptr) {
for (size_t row = 0; row < m_nodes.size(); row++)
if (m_nodes.ptr_at(row).ptr() == parent_node)
return create_index(static_cast<int>(row), 0, parent_node);
VERIFY_NOT_REACHED();
}
for (size_t row = 0; row < parent_node->parent_node()->child_nodes().size(); row++) {
auto const* child_node_at_row = parent_node->parent_node()->child_nodes().ptr_at(row).ptr();
if (child_node_at_row == parent_node)
return create_index(static_cast<int>(row), 0, parent_node);
}
VERIFY_NOT_REACHED();
}
int TreeViewModel::row_count(ModelIndex const& index) const
{
if (!index.is_valid())
return static_cast<int>(m_nodes.size());
auto const& node = *static_cast<Node const*>(index.internal_data());
return static_cast<int>(node.child_nodes().size());
}
Variant TreeViewModel::data(ModelIndex const& index, ModelRole role) const
{
auto const& node = *static_cast<Node const*>(index.internal_data());
switch (role) {
case ModelRole::Display:
return node.text();
case ModelRole::Icon:
if (node.icon().has_value())
return *node.icon();
return {};
default:
return {};
}
}
Optional<ModelIndex> TreeViewModel::index_for_node(Node const& node, ModelIndex const& parent) const
{
for (int row = 0; row < row_count(parent); ++row) {
auto row_index = this->index(row, 0);
auto const* row_node = static_cast<TreeViewModel::Node const*>(row_index.internal_data());
if (&node == row_node)
return row_index;
if (auto index = index_for_node(node, row_index); index.has_value())
return index;
}
return {};
}
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/RefPtr.h>
#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <AK/WeakPtr.h>
#include <LibGUI/Model.h>
namespace GUI {
class TreeViewModel final : public Model {
public:
static NonnullRefPtr<TreeViewModel> 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<Node>
, public Weakable<Node> {
public:
Node(String text, Optional<Icon> icon, Node* parent_node = nullptr)
: m_text(move(text))
, m_icon(move(icon))
, m_parent_node(parent_node)
{
}
virtual ~Node() = default;
template<typename NodeType = Node, typename... Args>
NonnullRefPtr<NodeType> add_node(String text, Optional<Icon> icon, Args&&... args) requires(IsBaseOf<Node, NodeType>)
{
auto node = adopt_ref(*new NodeType(move(text), move(icon), this, forward<Args>(args)...));
m_child_nodes.append(*static_cast<Node const*>(node.ptr()));
return node;
}
String const& text() const { return m_text; }
Optional<Icon> const& icon() const { return m_icon; }
Node const* parent_node() const { return m_parent_node; }
Node* parent_node() { return m_parent_node; }
NonnullRefPtrVector<Node> const& child_nodes() const { return m_child_nodes; }
NonnullRefPtrVector<Node>& child_nodes() { return m_child_nodes; }
private:
String m_text;
Optional<Icon> m_icon;
WeakPtr<Node> m_parent_node;
NonnullRefPtrVector<Node> m_child_nodes;
};
NonnullRefPtrVector<Node> const& nodes() const { return m_nodes; }
NonnullRefPtrVector<Node>& nodes() { return m_nodes; }
template<typename NodeType = Node, typename... Args>
NonnullRefPtr<NodeType> add_node(String text, Optional<Icon> icon, Args&&... args) requires(IsBaseOf<Node, NodeType>)
{
auto node = adopt_ref(*new NodeType(move(text), move(icon), nullptr, forward<Args>(args)...));
m_nodes.append(*static_cast<Node const*>(node.ptr()));
return node;
}
Optional<ModelIndex> index_for_node(Node const&, ModelIndex const& parent = {}) const;
private:
TreeViewModel() = default;
NonnullRefPtrVector<Node> m_nodes;
};
}