42 const QList<int> &indices,
const QString &limits,
49 elementNames.append(QString(
"%1").arg(
n));
53 defaultValues, display);
57 const QStringList &elementNames,
const QStringList &options,
58 const QList<int> &indices,
const QString &limits,
63 defaultValues, display);
67 FieldType type,
const QStringList &elementNames,
68 const QStringList &options,
const QList<int> &indices,
69 const QString &limits,
const QString &description,
115 this->options = QStringList() << tr(
"0") << tr(
"1");
126 for (
auto i = this->defaultValues.length();
i < this->
numElements;
i++)
127 this->defaultValues << QVariant(0);
130 for (
int i = 0;
i < indices.length();
i++)
142 if (limits.isEmpty())
144 QStringList stringPerElement = limits.split(
",");
146 foreach (
const QString &str, stringPerElement) {
147 QStringList ruleList = str.split(
";");
149 foreach (
const QString &rule, ruleList) {
150 QString _str = rule.trimmed();
153 QStringList valuesPerElement = _str.split(
":");
155 bool startFlag = valuesPerElement.at(0).startsWith(
"%");
157 bool elemNumberSizeFlag = valuesPerElement.at(0).size() == 3;
159 valuesPerElement.at(0).mid(1, 4).toInt(&aux, 16);
160 bool b4 = ((valuesPerElement.at(0).size()) == 7 && aux);
161 if (startFlag && maxIndexFlag && (elemNumberSizeFlag || b4)) {
163 lstruc.
board = valuesPerElement.at(0).mid(1, 4).toInt(&aux, 16);
166 if (valuesPerElement.at(0).right(2) ==
"EQ")
168 else if (valuesPerElement.at(0).right(2) ==
"NE")
170 else if (valuesPerElement.at(0).right(2) ==
"BE")
172 else if (valuesPerElement.at(0).right(2) ==
"BI")
174 else if (valuesPerElement.at(0).right(2) ==
"SM")
177 qDebug() <<
"limits parsing failed (invalid property) on UAVObjectField"
179 valuesPerElement.removeAt(0);
180 foreach (
const QString &_value, valuesPerElement) {
181 QString value = _value.trimmed();
187 lstruc.
values.append((quint32)value.toULong());
192 lstruc.
values.append((qint32)value.toLong());
195 lstruc.
values.append((
float)value.toFloat());
198 lstruc.
values.append((QString)value);
201 lstruc.
values.append((QString)value);
204 lstruc.
values.append(QVariant());
207 limitList.append(lstruc);
209 if (!valuesPerElement.at(0).isEmpty() && !startFlag)
211 <<
"limits parsing failed (property doesn't start with %) on UAVObjectField"
213 else if (!maxIndexFlag)
214 qDebug() <<
"limits parsing failed (index>numelements) on UAVObjectField"
216 else if (!elemNumberSizeFlag || !b4)
217 qDebug() <<
"limits parsing failed limit not starting with %XX or %YYYYXX "
218 "where XX is the limit type and YYYY is the board type on "
234 if ((struc.
board != board) && board != 0 && struc.
board != 0)
236 switch (struc.
type) {
242 foreach (
const QVariant &vars, struc.
values) {
243 if (var.toInt() == vars.toInt())
252 foreach (
const QVariant &vars, struc.
values) {
253 if (var.toUInt() == vars.toUInt())
260 foreach (
const QVariant &vars, struc.
values) {
261 if (var.toString() == vars.toString())
267 foreach (
const QVariant &vars, struc.
values) {
268 if (var.toFloat() == vars.toFloat())
282 foreach (
const QVariant &vars, struc.
values) {
283 if (var.toInt() == vars.toInt())
292 foreach (
const QVariant &vars, struc.
values) {
293 if (var.toUInt() == vars.toUInt())
300 foreach (
const QVariant &vars, struc.
values) {
301 if (var.toString() == vars.toString())
307 foreach (
const QVariant &vars, struc.
values) {
308 if (var.toFloat() == vars.toFloat())
318 if (struc.
values.length() < 2) {
319 qDebug() << __FUNCTION__
320 <<
"between limit with less than 1 pair, aborting; field:" <<
name;
323 if (struc.
values.length() > 2)
324 qDebug() << __FUNCTION__
325 <<
"between limit with more than 1 pair, using first; field" <<
name;
330 if (!(var.toInt() >= struc.
values.at(0).toInt()
331 && var.toInt() <= struc.
values.at(1).toInt()))
339 if (!(var.toUInt() >= struc.
values.at(0).toUInt()
340 && var.toUInt() <= struc.
values.at(1).toUInt()))
348 if (!(
options.indexOf(var.toString())
350 &&
options.indexOf(var.toString())
359 if (!(var.toFloat() >= struc.
values.at(0).toFloat()
360 && var.toFloat() <= struc.
values.at(1).toFloat()))
369 if (struc.
values.length() < 1) {
370 qDebug() << __FUNCTION__
371 <<
"BIGGER limit with less than 1 value, aborting; field:" <<
name;
374 if (struc.
values.length() > 1)
375 qDebug() << __FUNCTION__
376 <<
"BIGGER limit with more than 1 value, using first; field" <<
name;
381 if (!(var.toInt() >= struc.
values.at(0).toInt()))
389 if (!(var.toUInt() >= struc.
values.at(0).toUInt()))
394 if (!(
options.indexOf(var.toString())
403 if (!(var.toFloat() >= struc.
values.at(0).toFloat()))
416 if (!(var.toInt() <= struc.
values.at(0).toInt()))
424 if (!(var.toUInt() <= struc.
values.at(0).toUInt()))
429 if (!(
options.indexOf(var.toString())
438 if (!(var.toFloat() <= struc.
values.at(0).toFloat()))
478 if ((struc.
board != board) && board != 0 && struc.
board != 0)
480 switch (struc.
type) {
488 return struc.
values.at(1);
491 return struc.
values.at(0);
526 if ((struc.
board != board) && board != 0 && struc.
board != 0)
528 switch (struc.
type) {
536 return struc.
values.at(0);
539 return struc.
values.at(0);
551 this->
offset = dataOffset;
598 qWarning() <<
"Invalid element:" << index <<
" max=" <<
elementNames.length();
652 return options.contains(option);
670 sout.append(QString(
"%1: [ ").arg(
name));
675 sout.append(QString(
"%1 ").arg(
getDouble(
n)));
678 sout.append(QString(
"] %1\n").arg(
units));
690 for (
auto index = 0; index <
numElements; ++index) {
693 qToLittleEndian<qint16>(value, &dataOut[
elementSize * index]);
697 for (
auto index = 0; index <
numElements; ++index) {
700 qToLittleEndian<qint32>(value, &dataOut[
elementSize * index]);
704 for (
auto index = 0; index <
numElements; ++index) {
709 for (
auto index = 0; index <
numElements; ++index) {
712 qToLittleEndian<quint16>(value, &dataOut[
elementSize * index]);
716 for (
auto index = 0; index <
numElements; ++index) {
719 qToLittleEndian<quint32>(value, &dataOut[
elementSize * index]);
723 for (
auto index = 0; index <
numElements; ++index) {
726 qToLittleEndian<quint32>(value, &dataOut[
elementSize * index]);
730 for (
auto index = 0; index <
numElements; ++index) {
735 for (
auto index = 0; index < (1 + (numElements - 1) / 8); ++index) {
740 memcpy(dataOut, &
data[offset], numElements);
755 for (
auto index = 0; index <
numElements; ++index) {
757 value = qFromLittleEndian<qint16>(&dataIn[
elementSize * index]);
762 for (
auto index = 0; index <
numElements; ++index) {
764 value = qFromLittleEndian<qint32>(&dataIn[
elementSize * index]);
769 for (
auto index = 0; index <
numElements; ++index) {
774 for (
auto index = 0; index <
numElements; ++index) {
776 value = qFromLittleEndian<quint16>(&dataIn[
elementSize * index]);
781 for (
auto index = 0; index <
numElements; ++index) {
783 value = qFromLittleEndian<quint32>(&dataIn[
elementSize * index]);
788 for (
auto index = 0; index <
numElements; ++index) {
790 value = qFromLittleEndian<quint32>(&dataIn[
elementSize * index]);
795 for (
auto index = 0; index <
numElements; ++index) {
800 for (
auto index = 0; index < (1 + (numElements - 1) / 8); ++index) {
805 memcpy(&
data[offset], dataIn, numElements);
862 return QVariant::fromValue(static_cast<int>(*static_cast<const qint8 *>(d)));
864 return QVariant::fromValue(*static_cast<const qint16 *>(d));
866 return QVariant::fromValue(*static_cast<const qint32 *>(d));
869 return QVariant::fromValue(static_cast<int>(*static_cast<const quint8 *>(d)));
871 return QVariant::fromValue(*static_cast<const quint16 *>(d));
873 return QVariant::fromValue(*static_cast<const quint32 *>(d));
875 return QVariant::fromValue(*static_cast<const float *>(d));
878 auto i =
enumToIndex.at(*static_cast<const quint8 *>(d));
879 return QVariant::fromValue(
options[
i]);
880 }
catch (
const std::out_of_range &
e) {
881 qWarning() <<
"Invalid value" << *
static_cast<const quint8 *
>(d)
882 <<
"for ENUM field" <<
name <<
":" << e.what();
884 return QVariant::fromValue(QStringLiteral(
"Bad Value"));
887 quint8 val = (*
static_cast<const quint8 *
>(d) >> (index % 8)) & 1;
888 return QVariant::fromValue(val > 0 ? QChar(
'1') : QChar(
'0'));
891 return QVariant::fromValue(QString::fromLatin1(static_cast<const char *>(d),
892 static_cast<int>(strnlen(static_cast<const char *>(d),
922 if (static_cast<QMetaType::Type>(value.type()) == QMetaType::QString) {
924 if (idx >= 0 && idx <
indices.length())
926 }
else if (value.canConvert(QMetaType::Int)) {
927 if (
indices.contains(value.toInt()))
951 *
static_cast<qint8 *
>(d) = static_cast<qint8>(value.toInt());
954 *
static_cast<qint16 *
>(d) = static_cast<qint16>(value.toInt());
957 *
static_cast<qint32 *
>(d) = static_cast<qint32>(value.toInt());
960 *
static_cast<quint8 *
>(d) = static_cast<quint8>(value.toUInt());
963 *
static_cast<quint16 *
>(d) = static_cast<quint16>(value.toUInt());
966 *
static_cast<quint32 *
>(d) = static_cast<quint32>(value.toUInt());
969 *
static_cast<float *
>(d) = value.toFloat();
972 if (static_cast<QMetaType::Type>(value.type()) == QMetaType::QString) {
974 if (idx < 0 || idx >=
indices.length()) {
976 qWarning() <<
"Invalid option!" <<
obj->
getName() <<
name << value.toString();
979 *
static_cast<quint8 *
>(d) = static_cast<quint8>(
indices[idx]);
980 }
else if (value.canConvert(QMetaType::Int)) {
981 if (!
indices.contains(value.toInt())) {
983 qWarning() <<
"Invalid option!" <<
obj->
getName() <<
name << value.toInt();
986 *
static_cast<quint8 *
>(d) = static_cast<quint8>(value.toInt());
995 *
static_cast<quint8 *
>(d) &= ~(1 << (index % 8));
996 *
static_cast<quint8 *
>(d) |= ((value.toUInt() != 0 ? 1 : 0) << (index % 8));
999 QByteArray barray = value.toString().toLatin1();
1002 memcpy(d, barray.constData(),
static_cast<size_t>(
numElements));
1037 return val == defVal;
1043 unsigned val =
getValue(index).toUInt();
1045 return val == defVal;
1048 float val =
getValue(index).toFloat();
1050 return qFuzzyCompare(val, defVal);
1054 const QString val =
getValue(index).toString();
1056 return val == defVal;
1082 return QStringLiteral(
"0x");
1084 return QStringLiteral(
"0b");
1086 return QStringLiteral(
"0");
bool checkValue(const QVariant &data, int index=0) const
bool isDefaultValue(int index=0)
Check if the element is set to default value.
QString getTypeAsString() const
void limitsInitialize(const QString &limits)
qint32 unpack(const quint8 *dataIn)
size_t getNumBytes() const
UAVObject * getObject() const
QVariant getDefaultValue(int index=0) const
Get the default value (defined in the UAVO def) for the element.
UAVObjectField(const QString &name, const QString &units, FieldType type, int numElements, const QStringList &options, const QList< int > &indices, const QString &limits=QString(), const QString &description=QString(), const QList< QVariant > defaultValues=QList< QVariant >(), const DisplayType display=DEC)
double getDouble(int index=0) const
QString getDisplayPrefix() const
Get the prefix for the preferred display format.
QVariant getValue(int index=0) const
QMap< int, QList< LimitStruct > > elementLimits
int getNumElements() const
QString getElementName(int index=0) const
int getElementIndex(const QString &name) const
Get the index of an element from it's name.
virtual Metadata getMetadata()=0
QString getDescription() const
QStringList getOptions() const
QList< QVariant > defaultValues
void setValue(const QVariant &data, int index=0)
bool hasOption(const QString &option)
hasOption Check if the given option exists
QVariant getMinLimit(int index, int board=0) const
void initialize(quint8 *data, quint32 dataOffset, UAVObject *obj)
QStringList getElementNames() const
std::map< int, int > enumToIndex
static AccessMode GetFlightAccess(const Metadata &meta)
QVariant getMaxLimit(int index, int board=0) const
void setDouble(double value, int index=0)
qint32 pack(quint8 *dataOut)
bool isWithinLimits(QVariant var, int index, int board=0) const
int getDisplayIntegerBase() const
Get the preferred integer base for this field.
void constructorInitialize(const QString &name, const QString &units, FieldType type, const QStringList &elementNames, const QStringList &options, const QList< int > &indices, const QString &limits, const QString &description, const QList< QVariant > defaultValues, const DisplayType display)
FieldType getType() const
static AccessMode GetGcsAccess(const Metadata &meta)