AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AListModelObserver.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 alex2772 on 7/7/21.
   14//
   15
   16#pragma once
   17
   18#include <AUI/Common/AObject.h>
   19#include "IListModel.h"
   20
   21template<typename T>
   22class AListModelObserver: public AObject {
   23public:
   25    public:
   26        virtual void insertItem(size_t at, const T& value) = 0;
   27        virtual void updateItem(size_t at, const T& value) = 0;
   28        virtual void removeItem(size_t at) = 0;
   29
   30        virtual void onDataCountChanged() = 0;
   31        virtual void onDataChanged() = 0;
   32    };
   33
   34private:
   35    _<IListModel<T>> mModel;
   36    IListModelListener* mListener;
   37
   38public:
   39    explicit AListModelObserver(IListModelListener* listener) : mListener(listener) {
   40        setSlotsCallsOnlyOnMyThread(true);
   41    }
   42
   43
   44    void setModel(const _<IListModel<T>>& model) {
   45        if (mModel) {
   46            mModel->dataInserted.clearAllOutgoingConnectionsWith(this);
   47            mModel->dataChanged.clearAllOutgoingConnectionsWith(this);
   48            mModel->dataRemoved.clearAllOutgoingConnectionsWith(this);
   49        }
   50        mModel = model;
   51
   52        if (model) {
   53            for (size_t i = 0; i < model->listSize(); ++i) {
   54                mListener->insertItem(i, model->listItemAt(i));
   55            }
   56            mListener->onDataCountChanged();
   57            mListener->onDataChanged();
   58
   59            connect(mModel->dataInserted, this, [&](const AListModelRange<T>& data) {
   60                for (const auto& row : data) {
   61                    mListener->insertItem(row.getIndex().getRow(), row.get());
   62                }
   63                mListener->onDataCountChanged();
   64                mListener->onDataChanged();
   65            });
   66            connect(mModel->dataChanged, this, [&](const AListModelRange<T>& data) {
   67                for (const auto& row : data) {
   68                    mListener->updateItem(row.getIndex().getRow(), row.get());
   69                }
   70                mListener->onDataChanged();
   71            });
   72            connect(mModel->dataRemoved, this, [&](const AListModelRange<T>& data) {
   73                for (const auto& row : data) {
   74                    mListener->removeItem(data.begin().getIndex().getRow());
   75                }
   76                mListener->onDataCountChanged();
   77                mListener->onDataChanged();
   78            });
   79        }
   80    }
   81
   82    [[nodiscard]]
   83    const _<IListModel<T>>& getModel() const {
   84        return mModel;
   85    }
   86};
Definition AListModelObserver.h:24
Definition IListModel.h:23
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:86