summaryrefslogtreecommitdiff
path: root/src/libs/utils/listmodel.h
blob: 5f4de811e67ff084200af5990091a93626625426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "utils_global.h"
#include "treemodel.h"

namespace Utils {

template <class ChildType>
class BaseListModel : public TreeModel<TypedTreeItem<ChildType>, ChildType>
{
public:
    using BaseModel = TreeModel<TypedTreeItem<ChildType>, ChildType>;
    using BaseModel::rootItem;

    explicit BaseListModel(QObject *parent = nullptr) : BaseModel(parent) {}

    int itemCount() const { return rootItem()->childCount(); }
    ChildType *itemAt(int row) const { return rootItem()->childAt(row); }

    void appendItem(ChildType *item) { rootItem()->appendChild(item); }

    template <class Predicate>
    void forItems(const Predicate &pred) const
    {
        rootItem()->forFirstLevelChildren(pred);
    }

    template <class Predicate>
    ChildType *findItem(const Predicate &pred) const
    {
        return rootItem()->findFirstLevelChild(pred);
    }

    void sortItems(const std::function<bool(const ChildType *, const ChildType *)> &lessThan)
    {
        return rootItem()->sortChildren([lessThan](const TreeItem *a, const TreeItem *b) {
            return lessThan(static_cast<const ChildType *>(a), static_cast<const ChildType *>(b));
        });
    }

    int indexOf(const ChildType *item) const { return rootItem()->indexOf(item); }

    void clear() { rootItem()->removeChildren(); }

    using const_iterator = typename QVector<TreeItem *>::const_iterator;
    const_iterator begin() const { return rootItem()->begin(); }
    const_iterator end() const { return rootItem()->end(); }

};

template <class ItemData>
class ListItem : public TreeItem
{
public:
    ItemData itemData;
};

template <class ItemData>
class ListModel : public BaseListModel<ListItem<ItemData>>
{
public:
    using ChildType = ListItem<ItemData>;
    using BaseModel = BaseListModel<ChildType>;

    explicit ListModel(QObject *parent = nullptr) : BaseModel(parent) {}

    const ItemData &dataAt(int row) const
    {
        static const ItemData dummyData = {};
        auto item = BaseModel::itemAt(row);
        return item ? item->itemData : dummyData;
    }

    ChildType *findItemByData(const std::function<bool(const ItemData &)> &pred) const
    {
        return BaseModel::rootItem()->findFirstLevelChild([pred](ChildType *child) {
            return pred(child->itemData);
        });
    }

    void destroyItems(const std::function<bool(const ItemData &)> &pred)
    {
        QList<ChildType *> toDestroy;
        BaseModel::rootItem()->forFirstLevelChildren([pred, &toDestroy](ChildType *item) {
            if (pred(item->itemData))
                toDestroy.append(item);
        });
        for (ChildType *item : toDestroy)
            this->destroyItem(item);
    }

    ItemData *findData(const std::function<bool(const ItemData &)> &pred) const
    {
        ChildType *item = findItemByData(pred);
        return item ? &item->itemData : nullptr;
    }

    void forItems(const std::function<void(ItemData &)> &func) const
    {
        BaseModel::rootItem()->forFirstLevelChildren([func](ChildType *child) {
            func(child->itemData);
        });
    }

    ChildType *appendItem(const ItemData &data)
    {
        auto item = new ChildType;
        item->itemData = data;
        BaseModel::rootItem()->appendChild(item);
        return item;
    }

    QVariant data(const QModelIndex &idx, int role) const override
    {
        TreeItem *item = BaseModel::itemForIndex(idx);
        if (item && item->parent() == BaseModel::rootItem())
            return itemData(static_cast<ChildType *>(item)->itemData, idx.column(), role);
        return {};
    }

    Qt::ItemFlags flags(const QModelIndex &idx) const override
    {
        TreeItem *item = BaseModel::itemForIndex(idx);
        if (item && item->parent() == BaseModel::rootItem())
            return itemFlags(static_cast<ChildType *>(item)->itemData, idx.column());
        return {};
    }

    virtual QVariant itemData(const ItemData &idata, int column, int role) const
    {
        if (m_dataAccessor)
            return m_dataAccessor(idata, column, role);
        return {};
    }

    virtual Qt::ItemFlags itemFlags(const ItemData &idata, int column) const
    {
        if (m_flagsAccessor)
            return m_flagsAccessor(idata, column);
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }

    void setDataAccessor(const std::function<QVariant(const ItemData &, int, int)> &accessor)
    {
        m_dataAccessor = accessor;
    }

    void setFlagsAccessor(const std::function<Qt::ItemFlags(const ItemData &, int)> &accessor)
    {
        m_flagsAccessor = accessor;
    }

private:
    std::function<QVariant(const ItemData &, int, int)> m_dataAccessor;
    std::function<Qt::ItemFlags(const ItemData &, int)> m_flagsAccessor;
};

} // namespace Utils