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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2010-06-21
 * Description : A simple model to hold a tree structure.
 *
 * SPDX-FileCopyrightText: 2010-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2010-2014 by Michael G. Hansen <mike at mghansen dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "simpletreemodel.h"

namespace Digikam
{

class Q_DECL_HIDDEN SimpleTreeModel::Private
{
public:

    Private() = default;

    SimpleTreeModel::Item* rootItem     = nullptr;
    int                    columnCount  = 1;
};


SimpleTreeModel::SimpleTreeModel(const int columnCount, QObject* const parent)
    : QAbstractItemModel(parent),
      d                 (new Private)
{
    d->columnCount = columnCount;
    d->rootItem    = new Item();
}

SimpleTreeModel::~SimpleTreeModel()
{
    delete d->rootItem;
    delete d;
}

int SimpleTreeModel::columnCount(const QModelIndex& parent) const
{
    const Item* const item = indexToItem(parent);

    if (!item)
    {
        return 0;
    }

    return d->columnCount;
}

bool SimpleTreeModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
    Item* const item = indexToItem(index);

    if (!item)
    {
        return false;
    }

    const int column = index.column();

    if (column < 0)
    {
        return false;
    }

    while (item->dataColumns.count()<column)
    {
        item->dataColumns.append(QMap<int, QVariant>());
    }

    item->dataColumns[column].insert(role, value);

    Q_EMIT dataChanged(index, index);

    return true;
}

QVariant SimpleTreeModel::data(const QModelIndex& index, int role) const
{
    const Item* const item = indexToItem(index);

    if (!item)
    {
        return QVariant();
    }

    if (index.row() > 0)
    {
        return QVariant();
    }

    const int column = index.column();

    if ((column < 0) || (column >= item->dataColumns.count()))
    {
        return QVariant();
    }

    return item->dataColumns.at(column).value(role);
}

QModelIndex SimpleTreeModel::index(int row, int column, const QModelIndex& parent) const
{
    Item* const item = indexToItem(parent);

    if (!item)
    {
        return QModelIndex();
    }

    if (parent.isValid() && (parent.column() != 0))
    {
        return QModelIndex();
    }

    if ((row < 0) || (column != 0))
    {
        return QModelIndex();
    }

    if (row >= item->children.count())
    {
        return QModelIndex();
    }

    return createIndex(row, column, item);
}

QModelIndex SimpleTreeModel::parent(const QModelIndex& index) const
{
    if (!index.isValid())
    {
        return QModelIndex();
    }

    Item* const item = indexToItem(index);<--- Variable 'item' can be declared as pointer to const

    if (!item)
    {
        return QModelIndex();
    }

    if ((item->parent == nullptr) || (item->parent == d->rootItem))
    {
        return QModelIndex();
    }

    return itemToIndex(item->parent);
}

int SimpleTreeModel::rowCount(const QModelIndex& parent) const
{
    const Item* const item = indexToItem(parent);

    if (!item)
    {
        return 0;
    }

    return item->children.count();
}

bool SimpleTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)
{
    Q_UNUSED(section);
    Q_UNUSED(orientation);
    Q_UNUSED(value);
    Q_UNUSED(role);

    return false;
}

QVariant SimpleTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    Q_UNUSED(section);
    Q_UNUSED(orientation);
    Q_UNUSED(role);

    return QVariant();
}

Qt::ItemFlags SimpleTreeModel::flags(const QModelIndex& index) const
{
    return QAbstractItemModel::flags(index);
}

SimpleTreeModel::Item* SimpleTreeModel::addItem(SimpleTreeModel::Item* const parentItem, const int rowNumber)
{
    Item* const myParent    = parentItem ? parentItem : d->rootItem;
    Item* const newItem     = new Item();
    newItem->parent         = myParent;

    const int childrenCount = myParent->children.count();
    int targetRow           = rowNumber;

    if ((rowNumber < 0) || (rowNumber > childrenCount))
    {
        targetRow = childrenCount;
    }

    beginInsertRows(itemToIndex(myParent), targetRow, targetRow);
    myParent->children.insert(targetRow, newItem);
    endInsertRows();

    return newItem;
}

SimpleTreeModel::Item* SimpleTreeModel::indexToItem(const QModelIndex& itemIndex) const
{
    if (!itemIndex.isValid())
    {
        return d->rootItem;
    }

    Item* const item = static_cast<Item*>(itemIndex.internalPointer());
    const int row    = itemIndex.row();

    if ((row < 0) || (row >= item->children.count()))
    {
        return nullptr;
    }

    return item->children.at(row);
}

SimpleTreeModel::Item* SimpleTreeModel::rootItem() const
{
    return d->rootItem;
}

QModelIndex SimpleTreeModel::itemToIndex(const Item* const item) const
{
    if ((!item) || (item == d->rootItem))
    {
        return QModelIndex();
    }

    Item* const parentItem = item->parent;
    const int rowNumber    = parentItem->children.indexOf(const_cast<Item*>(item));

    if (rowNumber < 0)
    {
        return QModelIndex();
    }

    return createIndex(rowNumber, 0, parentItem);
}

} // namespace Digikam

#include "moc_simpletreemodel.cpp"