diff --git a/iio-widgets/include/iio-widgets/guistrategy/rangeguistrategy.h b/iio-widgets/include/iio-widgets/guistrategy/rangeguistrategy.h index 4a0dea0ae6..630b30bafb 100644 --- a/iio-widgets/include/iio-widgets/guistrategy/rangeguistrategy.h +++ b/iio-widgets/include/iio-widgets/guistrategy/rangeguistrategy.h @@ -69,9 +69,10 @@ public Q_SLOTS: * @param number A QString that represents a double or an int. * @param success This will be set to false if the QString parse fails and true if the * number is parsed successfully. + * @param unit Optional pointer to store the unit found in the string * @return The double that was extracted from the QString. */ - double tryParse(QString number, bool *success); + double tryParse(QString number, bool *success, QString *unit = nullptr); QWidget *m_ui; gui::MenuSpinbox *m_spinBox; diff --git a/iio-widgets/src/guistrategy/rangeguistrategy.cpp b/iio-widgets/src/guistrategy/rangeguistrategy.cpp index 081d30212b..22b06c5f85 100644 --- a/iio-widgets/src/guistrategy/rangeguistrategy.cpp +++ b/iio-widgets/src/guistrategy/rangeguistrategy.cpp @@ -84,7 +84,8 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData) << availableAttributeValue << ") and will try to partially initialize."; bool ok; - double value = currentData.toDouble(&ok); + QString unit; + double value = tryParse(currentData, &ok, &unit); if(!ok) { qCritical(CAT_ATTR_GUI_STRATEGY) << "Cannot partially initialize, something is very wrong here. " << currentData @@ -93,11 +94,19 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData) return; } + if(!unit.isEmpty()) { + QString currentTitle = m_spinBox->name(); + if(!currentTitle.contains("(")) { + m_spinBox->setName(currentTitle + " (" + unit + ")"); + } + } + m_spinBox->setValue(value); return; } bool ok = true, finalOk = true; + QString unit; double min = tryParse(optionsList[0], &ok); finalOk &= ok; @@ -108,7 +117,7 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData) double max = tryParse(optionsList[2], &ok); finalOk &= ok; - double currentNum = tryParse(currentData, &ok); + double currentNum = tryParse(currentData, &ok, &unit); finalOk &= ok; if(!finalOk) { @@ -121,13 +130,21 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData) m_spinBox->setMinValue(min); m_spinBox->setMaxValue(max); m_spinBox->incrementStrategy()->setScale(step); + + if(!unit.isEmpty()) { + QString currentTitle = m_spinBox->name(); + if(!currentTitle.contains("(")) { + m_spinBox->setName(currentTitle + " (" + unit + ")"); + } + } + m_spinBox->setValue(currentNum); } Q_EMIT displayedNewData(currentData, optionalData); } -double RangeAttrUi::tryParse(QString number, bool *success) +double RangeAttrUi::tryParse(QString number, bool *success, QString *unit) { // Try to parse as double first bool ok = true; @@ -145,6 +162,42 @@ double RangeAttrUi::tryParse(QString number, bool *success) return result; } + if(unit) { + // Try to parse value with unit suffix (e.g., "100 dB", "50 Hz") + QString trimmed = number.trimmed(); + int unitStartIndex = -1; + for(int i = 0; i < trimmed.length(); ++i) { + QChar ch = trimmed.at(i); + if(ch.isSpace()) { + unitStartIndex = i; + break; + } + if(ch.isLetter() && i > 0) { + unitStartIndex = i; + break; + } + } + + if(unitStartIndex > 0) { + QString numberPart = trimmed.left(unitStartIndex).trimmed(); + QString unitPart = trimmed.mid(unitStartIndex).trimmed(); + *unit = unitPart; + + // Try to parse the number part + double unitResult = numberPart.toDouble(&ok); + if(ok) { + *success = true; + return unitResult; + } + // Try as int + int intVal = numberPart.toInt(&ok); + if(ok) { + *success = true; + return static_cast(intVal); + } + } + } + *success = false; return -1; }