1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Dmitrii Kosarev aka Kakadu <kakadu.hafanana@gmail.com> |
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 | #ifndef STRINGMODEL_H |
29 | #define STRINGMODEL_H |
30 | |
31 | #include <QtCore/QObject> |
32 | #include <QtCore/QAbstractItemModel> |
33 | #include <QtCore/QDebug> |
34 | |
35 | class StringModel : public QAbstractItemModel |
36 | { |
37 | Q_OBJECT |
38 | QVector<QString> items; |
39 | QHash<int, QByteArray> roles; |
40 | QString name; |
41 | |
42 | public: |
43 | explicit StringModel(const QString& name) : QAbstractItemModel(), name(name) |
44 | { |
45 | roles.insert(akey: 555, avalue: "text" ); |
46 | } |
47 | |
48 | void drop(int count) |
49 | { |
50 | beginRemoveRows(parent: QModelIndex(), first: 0, last: count-1); |
51 | for (int i=0; i<count; i++) |
52 | items.pop_front(); |
53 | endRemoveRows(); |
54 | } |
55 | |
56 | Q_INVOKABLE void add(QString s) |
57 | { |
58 | beginInsertRows(parent: QModelIndex(), first: 0, last: 0); |
59 | items.push_front(t: s); |
60 | endInsertRows(); |
61 | } |
62 | |
63 | int rowCount(const QModelIndex &) const override |
64 | { |
65 | return items.count(); |
66 | } |
67 | |
68 | QHash<int, QByteArray> roleNames() const override |
69 | { |
70 | return roles; |
71 | } |
72 | |
73 | int columnCount(const QModelIndex &) const override |
74 | { |
75 | return 1; |
76 | } |
77 | |
78 | bool hasChildren(const QModelIndex &) const override |
79 | { |
80 | return rowCount(QModelIndex()) > 0; |
81 | } |
82 | |
83 | QModelIndex index(int row, int column, const QModelIndex &parent) const override |
84 | { |
85 | Q_UNUSED(column); |
86 | if (row>=0 && row<rowCount(parent)) |
87 | return createIndex(arow: row,acolumn: 0); |
88 | else |
89 | return QModelIndex(); |
90 | } |
91 | |
92 | QModelIndex parent(const QModelIndex &) const override |
93 | { |
94 | return QModelIndex(); |
95 | } |
96 | |
97 | QVariant data (const QModelIndex & index, int role) const override |
98 | { |
99 | int row = index.row(); |
100 | if ((row<0) || (row>=items.count())) |
101 | return QVariant::Invalid; |
102 | |
103 | switch (role) { |
104 | case Qt::DisplayRole: |
105 | case 555: |
106 | return QVariant::fromValue(value: items.at(i: row)); |
107 | default: |
108 | return QVariant(); |
109 | } |
110 | } |
111 | }; |
112 | |
113 | #endif // STRINGMODEL_H |
114 | |