aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/effectcomposer/tableheaderlengthmodel.cpp
blob: 55ce97abd208c2d8fc88cddf5f7fda3dda53fe15 (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
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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "tableheaderlengthmodel.h"

TableHeaderLengthModel::TableHeaderLengthModel(QObject *parent)
    : QAbstractListModel(parent)
{}

QHash<int, QByteArray> TableHeaderLengthModel::roleNames() const
{
    static const QHash<int, QByteArray> result = {
        {LengthRole, "length"},
        {HiddenRole, "hidden"},
        {NameRole, "name"},
    };
    return result;
}

int TableHeaderLengthModel::rowCount(const QModelIndex &) const
{
    return m_data.size();
}

QVariant TableHeaderLengthModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return {};

    switch (role) {
    case Roles::LengthRole:
        return m_data.at(index.row()).length;
    case Roles::HiddenRole:
        return !m_data.at(index.row()).visible;
    case Roles::NameRole:
        return m_sourceModel->headerData(index.row(), orientation(), Qt::DisplayRole);
    default:
        return {};
    }
}

bool TableHeaderLengthModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (data(index, role) == value)
        return false;

    switch (role) {
    case Roles::LengthRole: {
        m_data[index.row()].length = value.toInt();
        emit dataChanged(index, index, {role});
    } break;
    case Roles::HiddenRole: {
        m_data[index.row()].visible = !value.toBool();
        emit dataChanged(index, index, {role});
        emit sectionVisibilityChanged(index.row());
    } break;
    default:
        return false;
    }
    return true;
}

void TableHeaderLengthModel::setSourceModel(QAbstractItemModel *sourceModel)
{
    if (m_sourceModel.get() == sourceModel)
        return;

    // Disconnect old model if exists
    if (m_sourceModel)
        disconnect(m_sourceModel.get(), 0, this, nullptr);

    m_sourceModel = sourceModel;
    emit sourceModelChanged();
    setupModel();
}

QAbstractItemModel *TableHeaderLengthModel::sourceModel() const
{
    return m_sourceModel;
}

void TableHeaderLengthModel::setOrientation(Qt::Orientation orientation)
{
    if (m_orientation == orientation)
        return;

    m_orientation = orientation;
    emit orientationChanged();
    setupModel();
}

Qt::Orientation TableHeaderLengthModel::orientation() const
{
    return m_orientation;
}

void TableHeaderLengthModel::onSourceItemsInserted(const QModelIndex &, int first, int last)
{
    beginInsertRows({}, first, last);
    const Item defaultItem{true, m_defaultLength};
    m_data.insert(first, last - first + 1, defaultItem);
    endInsertRows();
}

void TableHeaderLengthModel::onSourceItemsRemoved(const QModelIndex &, int first, int last)
{
    beginRemoveRows({}, first, last);
    m_data.remove(first, last - first + 1);
    endRemoveRows();
}

void TableHeaderLengthModel::onSourceItemsMoved(
    const QModelIndex &, int sourceStart, int sourceEnd, const QModelIndex &, int destinationRow)
{
    beginMoveRows({}, sourceStart, sourceEnd, {}, destinationRow);
    QList<Item> pack = m_data.mid(sourceStart, sourceEnd - sourceStart + 1);
    m_data.remove(sourceStart, sourceEnd - sourceStart + 1);
    while (!pack.isEmpty())
        m_data.insert(destinationRow, pack.takeLast());
    endMoveRows();
}

void TableHeaderLengthModel::checkModelReset()
{
    int availableItems = 0;
    if (m_sourceModel)
        availableItems = (orientation() == Qt::Horizontal) ? m_sourceModel->columnCount()
                                                           : m_sourceModel->rowCount();
    if (availableItems != rowCount())
        setupModel();
}

void TableHeaderLengthModel::setupModel()
{
    beginResetModel();
    m_data.clear();

    QAbstractItemModel *source = m_sourceModel.get();
    if (!source) {
        endResetModel();
        return;
    }

    disconnect(source, 0, this, nullptr);

    connect(source, &QAbstractItemModel::modelReset, this, &TableHeaderLengthModel::checkModelReset);

    const Item defaultItem{true, m_defaultLength};
    if (orientation() == Qt::Horizontal) {
        connect(
            source,
            &QAbstractItemModel::columnsInserted,
            this,
            &TableHeaderLengthModel::onSourceItemsInserted);

        connect(
            source,
            &QAbstractItemModel::columnsRemoved,
            this,
            &TableHeaderLengthModel::onSourceItemsRemoved);

        connect(
            source,
            &QAbstractItemModel::columnsMoved,
            this,
            &TableHeaderLengthModel::onSourceItemsMoved);

        m_data.insert(0, source->columnCount(), defaultItem);
    } else {
        connect(
            source,
            &QAbstractItemModel::rowsInserted,
            this,
            &TableHeaderLengthModel::onSourceItemsInserted);

        connect(
            source,
            &QAbstractItemModel::rowsRemoved,
            this,
            &TableHeaderLengthModel::onSourceItemsRemoved);

        connect(
            source,
            &QAbstractItemModel::rowsMoved,
            this,
            &TableHeaderLengthModel::onSourceItemsMoved);
        m_data.insert(0, source->rowCount(), defaultItem);
    }
    endResetModel();
}

bool TableHeaderLengthModel::invalidSection(int section) const
{
    return (section < 0 || section >= m_data.length());
}

int TableHeaderLengthModel::defaultLength() const
{
    return m_defaultLength;
}

void TableHeaderLengthModel::setDefaultLength(int value)
{
    if (m_defaultLength == value)
        return;
    m_defaultLength = value;
    emit defaultLengthChanged();
}

int TableHeaderLengthModel::length(int section) const
{
    if (invalidSection(section))
        return 0;
    return m_data.at(section).length;
}

void TableHeaderLengthModel::setLength(int section, int len)
{
    QModelIndex idx = index(section, 0);
    setData(idx, len, Roles::LengthRole);
}

bool TableHeaderLengthModel::isVisible(int section) const
{
    if (invalidSection(section))
        return false;

    return m_data.at(section).visible;
}

void TableHeaderLengthModel::setVisible(int section, bool visible)
{
    QModelIndex idx = index(section, 0);
    setData(idx, !visible, Roles::HiddenRole);
}

int TableHeaderLengthModel::minimumLength(int section) const
{
    if (invalidSection(section))
        return 0;

    // ToDo: handle it by data
    if (orientation() == Qt::Horizontal)
        return 70;

    return 20;
}