Skip to content

Commit 3c47ae9

Browse files
committed
enum property parser default value
1 parent 9071ce7 commit 3c47ae9

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Diff for: property.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,18 @@ quint8 PropertyObject::percentage(double min, double max, double value)
123123
return static_cast <quint8> ((value - min) / (max - min) * 100);
124124
}
125125

126-
QVariant PropertyObject::enumValue(const QString &name, int index)
126+
QVariant PropertyObject::enumValue(const QString &name, int index, const QVariant &defaultValue)
127127
{
128-
QVariant data = option(name).toMap().value("enum");
128+
QVariant data = option(name).toMap().value("enum"), value;
129129

130130
switch (data.type())
131131
{
132-
case QVariant::Map: return data.toMap().value(QString::number(index));
133-
case QVariant::List: return data.toList().value(index);
134-
default: return QVariant();
132+
case QVariant::Map: value = data.toMap().value(QString::number(index)); break;
133+
case QVariant::List: value = data.toList().value(index); break;
134+
default: break;
135135
}
136+
137+
return value.isValid() ? value : defaultValue;
136138
}
137139

138140
void EnumProperty::parseAttribte(quint16, quint16 attributeId, const QByteArray &data)

Diff for: property.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class PropertyObject : public AbstractMetaObject
6666
QQueue <PropertyRequest> m_queue;
6767

6868
quint8 percentage(double min, double max, double value);
69-
QVariant enumValue(const QString &name, int index);
69+
QVariant enumValue(const QString &name, int index, const QVariant &defaultValue = QVariant());
7070

7171
};
7272

0 commit comments

Comments
 (0)