PaginateModel.cpp 11.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 *   Copyright (C) 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library/Lesser General Public License
 *   version 2, or (at your option) any later version, as published by the
 *   Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library/Lesser General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "PaginateModel.h"
21
#include <QtMath>
22
23
24
25
26
27
#include <QDebug>

PaginateModel::PaginateModel(QObject* object)
    : QAbstractListModel(object)
    , m_firstItem(0)
    , m_pageSize(10)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
28
    , m_sourceModel(nullptr)
29
    , m_hasStaticRowCount(false)
30
31
32
{
}

33
34
35
36
PaginateModel::~PaginateModel()
{
}

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
int PaginateModel::firstItem() const
{
    return m_firstItem;
}

void PaginateModel::setFirstItem(int row)
{
    Q_ASSERT(row>=0 && row<m_sourceModel->rowCount());
    if (row!=m_firstItem) {
        beginResetModel();
        m_firstItem = row;
        endResetModel();
        emit firstItemChanged();
    }
}

int PaginateModel::pageSize() const
{
    return m_pageSize;
}

void PaginateModel::setPageSize(int count)
{
    if (count != m_pageSize) {
61
62
63
64
65
66
        const int oldSize = rowsByPageSize(m_pageSize);
        const int newSize = rowsByPageSize(count);
        const int difference = newSize - oldSize;
        if (difference==0) {
            m_pageSize = count;
        } else if(difference>0) {
67
            beginInsertRows(QModelIndex(), m_pageSize, m_pageSize+difference-1);
68
69
70
            m_pageSize = count;
            endInsertRows();
        } else {
71
            beginRemoveRows(QModelIndex(), m_pageSize+difference, m_pageSize-1);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
            m_pageSize = count;
            endRemoveRows();
        }
        emit pageSizeChanged();
    }
}

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

void PaginateModel::setSourceModel(QAbstractItemModel* model)
{
    if(m_sourceModel) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
87
        disconnect(m_sourceModel, nullptr, this, nullptr);
88
89
90
91
92
93
    }

    if(model!=m_sourceModel) {
        beginResetModel();
        m_sourceModel = model;
        if(model) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
            connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeInserted, this, &PaginateModel::_k_sourceRowsAboutToBeInserted);
            connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this, &PaginateModel::_k_sourceRowsInserted);
            connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &PaginateModel::_k_sourceRowsAboutToBeRemoved);
            connect(m_sourceModel, &QAbstractItemModel::rowsRemoved, this, &PaginateModel::_k_sourceRowsRemoved);
            connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeMoved, this, &PaginateModel::_k_sourceRowsAboutToBeMoved);
            connect(m_sourceModel, &QAbstractItemModel::rowsMoved, this, &PaginateModel::_k_sourceRowsMoved);

            connect(m_sourceModel, &QAbstractItemModel::columnsAboutToBeInserted, this, &PaginateModel::_k_sourceColumnsAboutToBeInserted);
            connect(m_sourceModel, &QAbstractItemModel::columnsInserted, this, &PaginateModel::_k_sourceColumnsInserted);
            connect(m_sourceModel, &QAbstractItemModel::columnsAboutToBeRemoved, this, &PaginateModel::_k_sourceColumnsAboutToBeRemoved);
            connect(m_sourceModel, &QAbstractItemModel::columnsRemoved, this, &PaginateModel::_k_sourceColumnsRemoved);
            connect(m_sourceModel, &QAbstractItemModel::columnsAboutToBeMoved, this, &PaginateModel::_k_sourceColumnsAboutToBeMoved);
            connect(m_sourceModel, &QAbstractItemModel::columnsMoved, this, &PaginateModel::_k_sourceColumnsMoved);

            connect(m_sourceModel, &QAbstractItemModel::dataChanged, this, &PaginateModel::_k_sourceDataChanged);
            connect(m_sourceModel, &QAbstractItemModel::headerDataChanged, this, &PaginateModel::_k_sourceHeaderDataChanged);

            connect(m_sourceModel, &QAbstractItemModel::modelAboutToBeReset, this, &PaginateModel::_k_sourceModelAboutToBeReset);
            connect(m_sourceModel, &QAbstractItemModel::modelReset, this, &PaginateModel::_k_sourceModelReset);

            connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this, &PaginateModel::pageCountChanged);
            connect(m_sourceModel, &QAbstractItemModel::rowsRemoved, this, &PaginateModel::pageCountChanged);
            connect(m_sourceModel, &QAbstractItemModel::modelReset, this, &PaginateModel::pageCountChanged);
117
118
119
120
121
122
        }
        endResetModel();
        emit sourceModelChanged();
    }
}

123
124
125
126
127
QHash< int, QByteArray > PaginateModel::roleNames() const
{
    return m_sourceModel ? m_sourceModel->roleNames() : QAbstractItemModel::roleNames();
}

128
129
int PaginateModel::rowsByPageSize(int size) const
{
130
131
132
    return m_hasStaticRowCount ? size
        : !m_sourceModel ? 0
        : qMin(m_sourceModel->rowCount()-m_firstItem, size);
133
134
}

135
136
int PaginateModel::rowCount(const QModelIndex& parent) const
{
137
    return parent.isValid() ? 0 : rowsByPageSize(m_pageSize);
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
}

QModelIndex PaginateModel::mapToSource(const QModelIndex& idx) const
{
    if(!m_sourceModel)
        return QModelIndex();
    return m_sourceModel->index(idx.row()+m_firstItem, idx.column());
}

QModelIndex PaginateModel::mapFromSource(const QModelIndex& idx) const
{
    Q_ASSERT(idx.model() == m_sourceModel);
    if(!m_sourceModel)
        return QModelIndex();
    return index(idx.row()-m_firstItem, idx.column());
}

QVariant PaginateModel::data(const QModelIndex& index, int role) const
{
    if(!m_sourceModel)
        return QVariant();
    QModelIndex idx = mapToSource(index);
    return idx.data(role);
}

void PaginateModel::firstPage()
{
    setFirstItem(0);
}

void PaginateModel::lastPage()
{
170
    setFirstItem((pageCount() - 1)*m_pageSize);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
}

void PaginateModel::nextPage()
{
    setFirstItem(m_firstItem + m_pageSize);
}

void PaginateModel::previousPage()
{
    setFirstItem(m_firstItem - m_pageSize);
}

int PaginateModel::currentPage() const
{
    return m_firstItem/m_pageSize;
}

int PaginateModel::pageCount() const
{
    if(!m_sourceModel)
        return 0;
192
193
194
    const int rc = m_sourceModel->rowCount();
    const int r = (rc%m_pageSize == 0) ? 1 : 0;
    return qMax(qCeil(float(rc)/m_pageSize) - r, 1);
195
196
}

197
198
199
200
201
202
203
204
205
206
207
208
bool PaginateModel::hasStaticRowCount() const
{
    return m_hasStaticRowCount;
}

void PaginateModel::setStaticRowCount(bool src)
{
    if (src == m_hasStaticRowCount) {
        return;
    }

    beginResetModel();
209
    m_hasStaticRowCount = src;
210
211
212
    endResetModel();
}

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
260
261
262
263
264
265
266
267
268
269
270
//////////////////////////////

void PaginateModel::_k_sourceColumnsAboutToBeInserted(const QModelIndex& parent, int start, int end)
{
    Q_UNUSED(end)
    if(parent.isValid() || start!=0) {
        return;
    }
    beginResetModel();
}

void PaginateModel::_k_sourceColumnsAboutToBeMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd, const QModelIndex& destParent, int dest)
{
    Q_UNUSED(sourceParent)
    Q_UNUSED(sourceStart)
    Q_UNUSED(sourceEnd)
    Q_UNUSED(destParent)
    Q_UNUSED(dest)
    beginResetModel();
}

void PaginateModel::_k_sourceColumnsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
{
    Q_UNUSED(end)
    if(parent.isValid() || start!=0) {
        return;
    }
    beginResetModel();
}

void PaginateModel::_k_sourceColumnsInserted(const QModelIndex& parent, int start, int end)
{
    Q_UNUSED(end)
    if(parent.isValid() || start!=0) {
        return;
    }
    endResetModel();
}

void PaginateModel::_k_sourceColumnsMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd, const QModelIndex& destParent, int dest)
{
    Q_UNUSED(sourceParent)
    Q_UNUSED(sourceStart)
    Q_UNUSED(sourceEnd)
    Q_UNUSED(destParent)
    Q_UNUSED(dest)
    endResetModel();
}

void PaginateModel::_k_sourceColumnsRemoved(const QModelIndex& parent, int start, int end)
{
    Q_UNUSED(end)
    if(parent.isValid() || start!=0) {
        return;
    }
    endResetModel();
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
271
void PaginateModel::_k_sourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int> &roles)
272
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
273
    if(topLeft.parent().isValid() || bottomRight.row()<m_firstItem || topLeft.row()>lastItem()) {
274
275
276
277
278
279
280
281
282
283
        return;
    }

    QModelIndex idxTop = mapFromSource(topLeft);
    QModelIndex idxBottom = mapFromSource(bottomRight);
    if(!idxTop.isValid())
        idxTop = index(0);
    if(!idxBottom.isValid())
        idxBottom = index(rowCount()-1);

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
284
    emit dataChanged(idxTop, idxBottom, roles);
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
}

void PaginateModel::_k_sourceHeaderDataChanged(Qt::Orientation orientation, int first, int last)
{
    Q_UNUSED(last)
    if(first==0)
        emit headerDataChanged(orientation, 0, 0);
}

void PaginateModel::_k_sourceModelAboutToBeReset()
{
    beginResetModel();
}

void PaginateModel::_k_sourceModelReset()
{
    endResetModel();
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
304
bool PaginateModel::isIntervalValid(const QModelIndex& parent, int start, int /*end*/) const
305
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
306
307
308
309
310
311
    return !parent.isValid() && start<=lastItem();
}

bool PaginateModel::canSizeChange() const
{
    return !m_hasStaticRowCount && currentPage() == pageCount()-1;
312
313
}

314
315
void PaginateModel::_k_sourceRowsAboutToBeInserted(const QModelIndex& parent, int start, int end)
{
316
    if(!isIntervalValid(parent, start, end)) {
317
318
319
        return;
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
320
    if(canSizeChange()) {
321
322
323
324
        const int insertedCount = end-start;
        const int newStart = qMax(start-m_firstItem, 0);
        beginInsertRows(QModelIndex(), newStart, newStart+insertedCount);
    } else {
325
        beginResetModel();
326
327
328
329
330
331
332
333
334
    }
}

void PaginateModel::_k_sourceRowsInserted(const QModelIndex& parent, int start, int end)
{
    if(!isIntervalValid(parent, start, end)) {
        return;
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
335
    if(canSizeChange()) {
336
        endInsertRows();
337
    } else {
338
        endResetModel();
339
340
341
342
343
344
345
346
347
348
    }
}

void PaginateModel::_k_sourceRowsAboutToBeMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd, const QModelIndex& destParent, int dest)
{
    Q_UNUSED(sourceParent)
    Q_UNUSED(sourceStart)
    Q_UNUSED(sourceEnd)
    Q_UNUSED(destParent)
    Q_UNUSED(dest)
349
    //NOTE could optimize, unsure if it makes sense
350
351
352
    beginResetModel();
}

353
354
355
356
357
358
359
360
361
362
void PaginateModel::_k_sourceRowsMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd, const QModelIndex& destParent, int dest)
{
    Q_UNUSED(sourceParent)
    Q_UNUSED(sourceStart)
    Q_UNUSED(sourceEnd)
    Q_UNUSED(destParent)
    Q_UNUSED(dest)
    endResetModel();
}

363
364
void PaginateModel::_k_sourceRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
{
365
    if(!isIntervalValid(parent, start, end)) {
366
367
368
        return;
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
369
    if(canSizeChange()) {
370
371
        const int removedCount = end-start;
        const int newStart = qMax(start-m_firstItem, 0);
372
373
        beginRemoveRows(QModelIndex(), newStart, newStart+removedCount);
    } else {
374
        beginResetModel();
375
376
377
378
379
    }
}

void PaginateModel::_k_sourceRowsRemoved(const QModelIndex& parent, int start, int end)
{
380
    if(!isIntervalValid(parent, start, end)) {
381
382
383
        return;
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
384
    if(canSizeChange()) {
385
        endRemoveRows();
386
387
    } else {
        beginResetModel();
388
389
    }
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
390
391
392
393
394

int PaginateModel::lastItem() const
{
    return m_firstItem + rowCount();
}