1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "randomsortmodel.h"
30#include <QRandomGenerator>
31
32RandomSortModel::RandomSortModel(QObject* parent):
33 QAbstractListModel(parent)
34{
35 for (int i = 0; i < 10; ++i) {
36 mData.append(t: qMakePair(x: QString::fromLatin1(str: "Item %1").arg(a: i), y: i * 10));
37 }
38}
39
40QHash<int, QByteArray> RandomSortModel::roleNames() const
41{
42 QHash<int,QByteArray> roles = QAbstractItemModel::roleNames();
43 roles[Qt::UserRole] = "SortRole";
44 return roles;
45}
46
47
48int RandomSortModel::rowCount(const QModelIndex& parent) const
49{
50 if (!parent.isValid())
51 return mData.count();
52
53 return 0;
54}
55
56QVariant RandomSortModel::data(const QModelIndex& index, int role) const
57{
58 if (!index.isValid()) {
59 return QVariant();
60 }
61
62 if (index.row() >= mData.count()) {
63 return QVariant();
64 }
65
66 if (role == Qt::DisplayRole) {
67 return QString::fromLatin1(str: "%1 (weight %2)").arg(a: mData[index.row()].first).arg(a: mData[index.row()].second);
68 } else if (role == Qt::UserRole) {
69 return mData[index.row()].second;
70 }
71
72 return QVariant();
73}
74
75void RandomSortModel::randomize()
76{
77 const int row = QRandomGenerator::global()->bounded(highest: mData.count());
78 int random;
79 bool exists = false;
80 // Make sure we won't end up with two items with the same weight, as that
81 // would make unit-testing much harder
82 do {
83 exists = false;
84 random = QRandomGenerator::global()->bounded(highest: mData.count() * 10);
85 QList<QPair<QString, int> >::ConstIterator iter, end;
86 for (iter = mData.constBegin(), end = mData.constEnd(); iter != end; ++iter) {
87 if ((*iter).second == random) {
88 exists = true;
89 break;
90 }
91 }
92 } while (exists);
93 mData[row].second = random;
94 Q_EMIT dataChanged(topLeft: index(row, column: 0), bottomRight: index(row, column: 0));
95}
96

source code of qtdeclarative/tests/auto/quick/qquicklistview/randomsortmodel.cpp