AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AListModelAdapter.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#pragma once
   13
   14#include <AUI/Model/IListModel.h>
   15#include <AUI/Model/IMutableListModel.h>
   16
   17template<typename ItemTo, typename ItemFrom, typename Adapter>
   18class AListModelAdapter: public IRemovableListModel<ItemTo> {
   19private:
   20    _<IListModel<ItemFrom>> mOther;
   21    IRemovableListModel<ItemFrom>* mOtherMutable;
   22    Adapter mAdapter;
   23
   24public:
   25    using value_type = ItemTo;
   26
   27    explicit AListModelAdapter(const _<IListModel<ItemFrom>>& other, Adapter&& adapter) :
   28            mOther(other),
   29            mAdapter(std::forward<Adapter>(adapter)) {
   30        mOtherMutable = dynamic_cast<IRemovableListModel<ItemFrom>*>(mOther.get());
   31        AObject::connect(other->dataChanged, this, [&](const AListModelRange<ItemFrom>& r){
   32            emit this->dataChanged(this->range(r.getBegin(), r.getEnd()));
   33        });
   34        AObject::connect(other->dataInserted, this, [&](const AListModelRange<ItemFrom>& r){
   35            emit this->dataInserted(this->range(r.getBegin(), r.getEnd()));
   36        });
   37        AObject::connect(other->dataRemoved, this, [&](const AListModelRange<ItemFrom>& r){
   38            emit this->dataRemoved(this->range(r.getBegin(), r.getEnd()));
   39        });
   40    }
   41
   42    ~AListModelAdapter() override = default;
   43
   44    size_t listSize() override {
   45        return mOther->listSize();
   46    }
   47
   48    ItemTo listItemAt(const AListModelIndex& index) override {
   49        return mAdapter(mOther->listItemAt(index));
   50    }
   51
   52
   53    void removeItems(const AListModelRange<ItemTo>& items) override {
   54        AUI_NULLSAFE(mOtherMutable)->removeItems({items.begin().getIndex(), items.end().getIndex(), mOther});
   55    }
   56
   57    void removeItems(const AListModelSelection<ItemTo>& items) override {
   58        AUI_NULLSAFE(mOtherMutable)->removeItems({items.getIndices(), mOther});
   59    }
   60
   61    void removeItem(const AListModelIndex& item) override {
   62        AUI_NULLSAFE(mOtherMutable)->removeItem(item);
   63    }
   64
   65};
   66
   67namespace AModels {
   68    template <typename ItemTo, typename Container, typename Adapter>
   69    auto adapt(const _<Container>& other, Adapter&& adapter) -> _<AListModelAdapter<ItemTo, typename Container::value_type, Adapter>> {
   70        return aui::ptr::manage(new AListModelAdapter<ItemTo, typename Container::value_type, Adapter>(other, std::forward<Adapter>(adapter)));
   71    }
   72}
Definition AListModelAdapter.h:18
Definition AListModelIndex.h:20
Definition AListModelRange.h:23
Definition AListModelSelection.h:28
Definition IListModel.h:23
Definition IMutableListModel.h:20
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
static _< T > manage(T *raw)
Delegates memory management of the raw pointer T* raw to the shared pointer, which is returned.
Definition SharedPtrTypes.h:424