-
Notifications
You must be signed in to change notification settings - Fork 6
/
nfcrecorditem.cpp
366 lines (331 loc) · 9.58 KB
/
nfcrecorditem.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/****************************************************************************
**
** Copyright (C) 2012-2013 Andreas Jakl.
** All rights reserved.
** Contact: Andreas Jakl ([email protected])
**
** This file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
****************************************************************************/
#include "nfcrecorditem.h"
/*!
\brief Construct a new record item with the supplied information and an optional parent.
*/
NfcRecordItem::NfcRecordItem(const QString& title, NfcTypes::MessageType messageType,
NfcTypes::RecordContent recordContent, const QString& currentText,
bool removeVisible, bool addVisible, int recordId, QObject* parent) :
QObject(parent),
m_title(title),
m_messageType(messageType),
m_recordContent(recordContent),
m_currentText(currentText),
m_removeVisible(removeVisible),
m_addVisible(addVisible),
m_recordId(recordId)
{
}
//NfcRecordItem::NfcRecordItem(const NfcRecordItem &other) :
// QObject(),
// m_title(other.title()),
// m_messageType(other.messageType()),
// m_recordContent(other.recordContent()),
// m_currentText(other.currentText()),
// m_removeVisible(other.removeVisible()),
// m_addVisible(other.addVisible()),
// m_recordId(other.recordId())
//{
// qDebug() << "[NfcRecordItem Copy Constructor]";
//}
/*!
\brief Get all role names supported for this item, to be used by the model
for generic setting and retrieving data.
*/
QHash<int, QByteArray> NfcRecordItem::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TitleRole] = "title";
roles[MessageTypeRole] = "messageType";
roles[RecordContentRole] = "recordContent";
roles[CurrentTextRole] = "currentText";
roles[SelectOptionsRole] = "selectOptions";
roles[SelectedOptionRole] = "selectedOption";
roles[RemoveVisibleRole] = "removeVisible";
roles[AddVisibleRole] = "addVisible";
roles[RecordIdRole] = "recordId";
return roles;
}
/*!
\brief Get the current data of the instance, based on the \a role.
*/
QVariant NfcRecordItem::data(int role) const
{
switch (role) {
case TitleRole:
return title();
break;
case MessageTypeRole:
return messageType();
break;
case RecordContentRole:
return recordContent();
break;
case CurrentTextRole:
return currentText();
break;
case SelectOptionsRole:
return selectOptions();
break;
case SelectedOptionRole:
return selectedOption();
break;
case RemoveVisibleRole:
return removeVisible();
break;
case AddVisibleRole:
return addVisible();
break;
case RecordIdRole:
return recordId();
break;
}
return QVariant();
}
//int NfcRecordItem::getIntForRoleString(const QString& role)
//{
// return roleNames().key(role);
//}
/*!
\brief Set data of the item using the role name as a string.
*/
void NfcRecordItem::setData(const QVariant& value, const QString& role)
{
// Convert QString to enum
int roleInt = roleNames().key(role.toAscii());
setData(value, roleInt);
}
/*!
\brief Set data of the item using the role name as an int (based on
the enum, which is not supported by QML).
*/
void NfcRecordItem::setData(const QVariant& value, const int role)
{
switch (role) {
case TitleRole:
setTitle(value.toString());
break;
case MessageTypeRole:
setMessageType((NfcTypes::MessageType)value.toInt());
break;
case RecordContentRole:
setRecordContent((NfcTypes::RecordContent)value.toInt());
break;
case CurrentTextRole:
setCurrentText(value.toString());
break;
case SelectOptionsRole:
if (value.canConvert<QVariantList>()) {
setSelectOptions(value.value<QVariantList>());
}
break;
case SelectedOptionRole:
setSelectedOption(value.toInt());
break;
case RemoveVisibleRole:
setRemoveVisible(value.toBool());
break;
case AddVisibleRole:
setAddVisible(value.toBool());
break;
case RecordIdRole:
setRecordId(value.toInt());
break;
}
}
/*!
\brief Get the title text / header of the record item.
*/
QString NfcRecordItem::title() const
{
return m_title;
}
/*!
\brief Get the message type this item adds info to.
*/
NfcTypes::MessageType NfcRecordItem::messageType() const
{
return m_messageType;
}
/*!
\brief Get the record content - the detail of the NDEF record that is
stored by this specific instance.
*/
NfcTypes::RecordContent NfcRecordItem::recordContent() const
{
return m_recordContent;
}
/*!
\brief Get the record content - the detail of the NDEF record that is
stored by this specific instance.
The record content is actually stored as an enum, but to communicate with
QML, this is casted to an int.
*/
int NfcRecordItem::recordContentInt() const
{
return (int)m_recordContent;
}
/*!
\brief The current text of the record item.
Used by most record items (exception being for example the selection item)
*/
QString NfcRecordItem::currentText() const
{
return m_currentText;
}
/*!
\brief Whether this record item can be removed again from the message.
*/
bool NfcRecordItem::removeVisible() const
{
return m_removeVisible;
}
/*!
\brief Whether a sub-item can be added after this record item to the
same message type, providing more detail.
This is usually only the case for header items. Could be combined
with the message header type to save this boolean in the future, if
there is no need for a separate addVisible() when more different
item editors are added.
*/
bool NfcRecordItem::addVisible() const
{
return m_addVisible;
}
/*!
\brief Get the record id this record item belongs to.
*/
int NfcRecordItem::recordId() const
{
return m_recordId;
}
/*!
\brief Set the title text / header of the record item.
*/
void NfcRecordItem::setTitle(const QString &title)
{
if (m_title != title) {
m_title = title;
emit dataChanged();
}
}
/*!
\brief Set the message type this item adds info to.
*/
void NfcRecordItem::setMessageType(const NfcTypes::MessageType messageType)
{
if (m_messageType != messageType) {
m_messageType = messageType;
emit dataChanged();
}
}
/*!
\brief Set the record content - the detail of the NDEF record that is
stored by this specific instance.
*/
void NfcRecordItem::setRecordContent(const NfcTypes::RecordContent recordContent)
{
if (m_recordContent != recordContent) {
m_recordContent = recordContent;
emit dataChanged();
}
}
/*!
\brief Set the current text of the record item.
Used by most record items (exception being for example the selection item)
*/
void NfcRecordItem::setCurrentText(const QString ¤tText)
{
if (m_currentText != currentText) {
m_currentText = currentText;
//qDebug() << "NfcRecordItem::setCurrentText: changed to: " << m_currentText;
emit dataChanged();
}
}
/*!
\brief Whether this record item can be removed again from the message.
*/
void NfcRecordItem::setRemoveVisible(const bool removeVisible)
{
if (m_removeVisible != removeVisible) {
m_removeVisible = removeVisible;
emit dataChanged();
}
}
/*!
\brief Whether a sub-item can be added after this record item to the
same message type, providing more detail.
This is usually only the case for header items. Could be combined
with the message header type to save this boolean in the future, if
there is no need for a separate addVisible() when more different
item editors are added.
*/
void NfcRecordItem::setAddVisible(const bool addVisible)
{
if (m_addVisible != addVisible) {
m_addVisible = addVisible;
emit dataChanged();
}
}
/*!
\brief Set the record id this record item belongs to.
*/
void NfcRecordItem::setRecordId(const int recordId)
{
if (m_recordId != recordId) {
m_recordId = recordId;
emit dataChanged();
}
}
/*!
\brief Get the list of option items / radio buttons for this record item,
in case it is of a type that uses a selection instead of a text field.
*/
QVariantList NfcRecordItem::selectOptions() const
{
return m_selectOptions;
}
/*!
\brief Set the list of option items / radio buttons for this record item,
in case it is of a type that uses a selection instead of a text field.
*/
void NfcRecordItem::setSelectOptions(const QVariantList selectOptions)
{
if (m_selectOptions != selectOptions) {
m_selectOptions = selectOptions;
emit dataChanged();
}
}
/*!
\brief Get the currently selected option / radio button for this record item,
in case it is of a type that uses a selection instead of a text field.
*/
int NfcRecordItem::selectedOption() const
{
return m_selectedOption;
}
/*!
\brief Set the currently selected option / radio button for this record item,
in case it is of a type that uses a selection instead of a text field.
*/
void NfcRecordItem::setSelectedOption(const int selectedOption)
{
if (m_selectedOption != selectedOption) {
m_selectedOption = selectedOption;
emit dataChanged();
}
//qDebug() << "Nfc Record Item - New selected option: " << m_selectedOption;
}