uniformly check if user entered value is valid
This commit is contained in:
parent
e889e36ccd
commit
a0cb4c18a4
|
@ -138,42 +138,23 @@ bool UniverseModel::setData(const QModelIndex &index, const QVariant &value,
|
|||
return false;
|
||||
|
||||
uint16_t slot = (index.row() * 10) + (index.column() + 1);
|
||||
uint8_t data;
|
||||
|
||||
uint data;
|
||||
bool ok;
|
||||
switch (data_mode_)
|
||||
{
|
||||
case Decimal:
|
||||
{
|
||||
if (!value.canConvert<uint>())
|
||||
return false;
|
||||
unsigned int d = value.toUInt();
|
||||
if (d > 255)
|
||||
return false;
|
||||
data = d;
|
||||
}
|
||||
data = value.toUInt(&ok);
|
||||
break;
|
||||
case Hex:
|
||||
{
|
||||
if (!value.canConvert<QString>())
|
||||
return false;
|
||||
bool ok;
|
||||
unsigned int d = value.toString().toInt(&ok, 16);
|
||||
if (!ok || d > 255)
|
||||
return false;
|
||||
data = d;
|
||||
}
|
||||
data = value.toString().toUInt(&ok, 16);
|
||||
break;
|
||||
case Percent:
|
||||
{
|
||||
if (!value.canConvert<double>())
|
||||
return false;
|
||||
int d = 255 * (value.toDouble() / 100);
|
||||
if (d < 0 || d > 255)
|
||||
return false;
|
||||
data = d;
|
||||
}
|
||||
data = 255 * (value.toDouble(&ok) / 100);
|
||||
break;
|
||||
}
|
||||
if (!ok || data > 255)
|
||||
return false;
|
||||
|
||||
universe_->setValue(slot, data);
|
||||
emit dataChanged(index, index, QVector<int>() << role);
|
||||
|
|
Loading…
Reference in New Issue