Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 56 additions & 3 deletions iio-widgets/src/guistrategy/rangeguistrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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<double>(intVal);
}
}
}

*success = false;
return -1;
}
Expand Down
Loading