AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
IListModel.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 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/Common/AVector.h>
15#include <AUI/Common/ASignal.h>
16#include <AUI/Model/AListModelSelection.h>
17#include <memory>
18#include "AListModelRange.h"
19#include "AUI/Common/AObject.h"
20
21template<typename T>
22class IListModel: public AObject
23{
24public:
25 using value_type = T;
26 ~IListModel() override = default;
27
28 virtual size_t listSize() = 0;
29 virtual T listItemAt(const AListModelIndex& index) = 0;
30
31 using stored_t = T;
32
33 AListModelRange<T> range(const AListModelIndex& begin, const AListModelIndex& end) {
34 return AListModelRange<T>(begin, end, std::dynamic_pointer_cast<IListModel<T>>(this->shared_from_this()));
35 }
36
37 AListModelRange<T> range(const AListModelIndex& item) {
38 return AListModelRange<T>(item, {item.getRow() + 1}, std::dynamic_pointer_cast<IListModel<T>>(this->shared_from_this()));
39 }
40
41
42 AVector<T> toVector() noexcept {
43 AVector<T> result;
44 size_t size = listSize();
45 result.reserve(size);
46 for (size_t i = 0; i < size; ++i) {
47 result << listItemAt(i);
48 }
49 return result;
50 }
51
52 template<typename Filter>
53 AVector<AListModelRange<T>> rangesIncluding(Filter&& filter) {
55 size_t currentBeginning = 0;
56 size_t s = listSize();
57 bool prevValue = false;
58 bool currentValue;
59
60 auto endSliceIfNecessary = [&](size_t i) {
61 if (prevValue) {
62 result << range(currentBeginning, i);
63 }
64 };
65 for (size_t i = 0; i < s; ++i, prevValue = currentValue) {
66 currentValue = filter(i);
67 if (currentValue) {
68 if (!prevValue) {
69 currentBeginning = i;
70 }
71 } else {
72 endSliceIfNecessary(i);
73 }
74 }
75 endSliceIfNecessary(listSize());
76 return result;
77 }
78
79signals:
84
89
94};
Definition: AListModelIndex.h:20
Definition: AListModelRange.h:23
A base object class.
Definition: AObject.h:49
A std::vector with AUI extensions.
Definition: AVector.h:38
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