AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ITreeModel.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12//
   13// Created by alex2 on 7/1/2021.
   14//
   15
   16
   17#pragma once
   18
   19#include <AUI/Common/ASignal.h>
   20#include "AUI/Common/AObject.h"
   21#include "AUI/Common/AOptional.h"
   22#include <AUI/Model/ATreeModelIndex.h>
   23#include <variant>
   24
   33template<typename T>
   34class ITreeModel: public AObject
   35{
   36public:
   37    virtual ~ITreeModel() = default;
   38
   43    virtual size_t childrenCount(const ATreeModelIndexOrRoot& vertex) = 0;
   44
   48    virtual T itemAt(const ATreeModelIndex& index) = 0;
   49
   57    virtual ATreeModelIndex indexOfChild(size_t row, size_t column,
   58                                         const ATreeModelIndexOrRoot& vertex) = 0;
   59
   63    virtual ATreeModelIndexOrRoot parent(const ATreeModelIndex& vertex) = 0;
   64
   65    using stored_t = T;
   66
   67    template<aui::predicate<ATreeModelIndex> Predicate>
   68    AOptional<ATreeModelIndex> find(const Predicate& predicate) {
   69        return visit(predicate, ATreeModelIndex::ROOT);
   70    }
   71
   72    template<aui::invocable<ATreeModelIndex> Callback>
   73    void forEachDirectChildOf(const ATreeModelIndexOrRoot& vertex, Callback callback) {
   74        auto s = childrenCount(vertex);
   75        for (decltype(s) i = 0; i < s; ++i) {
   76            callback(indexOfChild(i, 0, vertex));
   77        }
   78    }
   79
   80signals:
   85
   90
   95
   96private:
   97    template<aui::predicate<ATreeModelIndex> Predicate>
   98    AOptional<ATreeModelIndex> visit(const Predicate& p, const ATreeModelIndexOrRoot& vertex) {
   99        if (auto index = std::get_if<ATreeModelIndex>(&vertex)) {
  100            if (p(*index)) {
  101                return *index;
  102            }
  103        }
  104        std::size_t count = childrenCount(vertex);
  105        for (std::size_t i = 0; i < count; ++i) {
  106            auto child = indexOfChild(i, 0, vertex);
  107            if (auto v = visit(p, child)) {
  108                return v;
  109            }
  110        }
  111        return {};
  112    };
  113};
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
Valid index of ITreeModel.
Definition ATreeModelIndex.h:10
static constexpr struct ATreeModelIndex::@102253001377275224255007337255000205374254314015 ROOT
Tag type to define root vertex.
Tree model.
Definition ITreeModel.h:35
emits< ATreeModelIndex > dataChanged
Model data was changed.
Definition ITreeModel.h:84
emits< ATreeModelIndex > dataRemoved
Model data about to remove.
Definition ITreeModel.h:94
emits< ATreeModelIndex > dataInserted
Model data was added.
Definition ITreeModel.h:89
virtual ATreeModelIndex indexOfChild(size_t row, size_t column, const ATreeModelIndexOrRoot &vertex)=0
Creates valid ATreeModelIndex of the child.
virtual ATreeModelIndexOrRoot parent(const ATreeModelIndex &vertex)=0
Creates index of parent vertex of the specified vertex.
virtual size_t childrenCount(const ATreeModelIndexOrRoot &vertex)=0
Count of children of the vertex.
virtual T itemAt(const ATreeModelIndex &index)=0
value representation, used by ATreeView.
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:572