AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AListModelFilter.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#include <AUI/Common/AVector.h>
   17
   18template<typename T, typename Filter>
   19class AListModelFilter: public IListModel<T> {
   20private:
   21    _<IListModel<T>> mOther;
   22    Filter mFilter;
   23    AVector<size_t> mMapping;
   24
   25    void fill() {
   26        for (size_t i = 0; i < mOther->listSize(); ++i) {
   27            if (mFilter(mOther->listItemAt(i))) {
   28                mMapping.push_back(i);
   29            }
   30        }
   31    }
   32public:
   33    using value_type = T;
   34
   35    explicit AListModelFilter(const _<IListModel<T>>& other, Filter&& adapter) :
   36            mOther(other),
   37            mFilter(std::forward<Filter>(adapter)) {
   38        fill();
   39
   40        AObject::connect(other->dataChanged, this, [&](const AListModelRange<T>& r){
   41            AUI_ASSERTX(0, "unimplemented");
   42        });
   43        AObject::connect(other->dataInserted, this, [&](const AListModelRange<T>& r){
   44            AUI_ASSERTX(0, "unimplemented");
   45        });
   46        AObject::connect(other->dataRemoved, this, [&](const AListModelRange<T>& r){
   47            AUI_ASSERTX(0, "unimplemented");
   48        });
   49    }
   50
   51
   52    ~AListModelFilter() override = default;
   53
   54    size_t listSize() override {
   55        return mMapping.size();
   56    }
   57
   58    T listItemAt(const AListModelIndex& index) override {
   59        return mOther->listItemAt(mMapping[index.getRow()]);
   60    }
   61
   65    void invalidate() {
   66        size_t prevSize = mMapping.size();
   67        mMapping.clear();
   68        fill();
   69
   70        size_t currentSize = mMapping.size();
   71        if (currentSize == prevSize) {
   72            emit this->dataChanged(this->range(0, currentSize));
   73        } else if (currentSize > prevSize) {
   74            emit this->dataChanged(this->range(0, prevSize));
   75            emit this->dataInserted(this->range(prevSize, currentSize));
   76        } else {
   77            emit this->dataChanged(this->range(0, currentSize));
   78            emit this->dataRemoved(this->range(currentSize, prevSize));
   79        }
   80    }
   81
   87    void lazyInvalidate() {
   88        auto ranges = this->rangesIncluding([&](size_t i) {
   89            return !mFilter(listItemAt(i));
   90        });
   91        int offset = 0;
   92        for (const AListModelRange<T>& r : ranges) {
   93            mMapping.erase(mMapping.begin() + r.getBegin().getRow() - offset, mMapping.begin() + r.getEnd().getRow()) - offset;
   94            offset += r.getEnd().getRow() - r.getBegin().getRow();
   95        }
   96    }
   97};
   98
   99namespace AModels {
  100    template <typename Container, typename Filter>
  101    auto filter(const _<Container>& other, Filter&& filter) -> _<AListModelFilter<typename Container::value_type, Filter>> {
  102        return aui::ptr::manage(new AListModelFilter<typename Container::value_type, Filter>(other, std::forward<Filter>(filter)));
  103    }
  104}
  105
void invalidate()
Definition AListModelFilter.h:65
void lazyInvalidate()
Performs filtering for currently unfiltered elements.
Definition AListModelFilter.h:87
Definition AListModelIndex.h:20
Definition AListModelRange.h:23
A std::vector with AUI extensions.
Definition AVector.h:39
Definition IListModel.h:23
emits< AListModelRange< T > > dataInserted
Model data was added.
Definition IListModel.h:88
emits< AListModelRange< T > > dataChanged
Model data was changed.
Definition IListModel.h:83
emits< AListModelRange< T > > dataRemoved
Model data about to remove.
Definition IListModel.h:93
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
#define emit
emits the specified signal in context of this object.
Definition AObject.h:343
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