AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AListModelObserver.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//
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>
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.clearAllConnectionsWith(this);
47 mModel->dataChanged.clearAllConnectionsWith(this);
48 mModel->dataRemoved.clearAllConnectionsWith(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: AListModelObserver.h:22
Definition: AListModelRange.h:23
A base object class.
Definition: AObject.h:49
Definition: IListModel.h:23
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177