Skip to content

Commit 4e74222

Browse files
committed
plugins: debugger: fix uneditable range iio widgets with unit
Signed-off-by: Andrei Popa <andrei.popa@analog.com>
1 parent 8dedd7e commit 4e74222

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

iio-widgets/include/iio-widgets/guistrategy/rangeguistrategy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public Q_SLOTS:
6969
* @param number A QString that represents a double or an int.
7070
* @param success This will be set to false if the QString parse fails and true if the
7171
* number is parsed successfully.
72+
* @param unit Optional pointer to store the unit found in the string
7273
* @return The double that was extracted from the QString.
7374
*/
74-
double tryParse(QString number, bool *success);
75+
double tryParse(QString number, bool *success, QString *unit = nullptr);
7576

7677
QWidget *m_ui;
7778
gui::MenuSpinbox *m_spinBox;

iio-widgets/src/guistrategy/rangeguistrategy.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData)
8484
<< availableAttributeValue << ") and will try to partially initialize.";
8585

8686
bool ok;
87-
double value = currentData.toDouble(&ok);
87+
QString unit;
88+
double value = tryParse(currentData, &ok, &unit);
8889
if(!ok) {
8990
qCritical(CAT_ATTR_GUI_STRATEGY)
9091
<< "Cannot partially initialize, something is very wrong here. " << currentData
@@ -93,11 +94,19 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData)
9394
return;
9495
}
9596

97+
if(!unit.isEmpty()) {
98+
QString currentTitle = m_spinBox->name();
99+
if(!currentTitle.contains("(")) {
100+
m_spinBox->setName(currentTitle + " (" + unit + ")");
101+
}
102+
}
103+
96104
m_spinBox->setValue(value);
97105
return;
98106
}
99107

100108
bool ok = true, finalOk = true;
109+
QString unit;
101110

102111
double min = tryParse(optionsList[0], &ok);
103112
finalOk &= ok;
@@ -108,7 +117,7 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData)
108117
double max = tryParse(optionsList[2], &ok);
109118
finalOk &= ok;
110119

111-
double currentNum = tryParse(currentData, &ok);
120+
double currentNum = tryParse(currentData, &ok, &unit);
112121
finalOk &= ok;
113122

114123
if(!finalOk) {
@@ -121,13 +130,21 @@ void RangeAttrUi::receiveData(QString currentData, QString optionalData)
121130
m_spinBox->setMinValue(min);
122131
m_spinBox->setMaxValue(max);
123132
m_spinBox->incrementStrategy()->setScale(step);
133+
134+
if(!unit.isEmpty()) {
135+
QString currentTitle = m_spinBox->name();
136+
if(!currentTitle.contains("(")) {
137+
m_spinBox->setName(currentTitle + " (" + unit + ")");
138+
}
139+
}
140+
124141
m_spinBox->setValue(currentNum);
125142
}
126143

127144
Q_EMIT displayedNewData(currentData, optionalData);
128145
}
129146

130-
double RangeAttrUi::tryParse(QString number, bool *success)
147+
double RangeAttrUi::tryParse(QString number, bool *success, QString *unit)
131148
{
132149
// Try to parse as double first
133150
bool ok = true;
@@ -145,6 +162,40 @@ double RangeAttrUi::tryParse(QString number, bool *success)
145162
return result;
146163
}
147164

165+
// Try to parse value with unit suffix (e.g., "100 dB", "50 Hz")
166+
QString trimmed = number.trimmed();
167+
int unitStartIndex = -1;
168+
for(int i = 0; i < trimmed.length(); ++i) {
169+
QChar ch = trimmed.at(i);
170+
if(ch.isSpace()) {
171+
unitStartIndex = i;
172+
break;
173+
}
174+
if(ch.isLetter() && i > 0) {
175+
unitStartIndex = i;
176+
break;
177+
}
178+
}
179+
180+
if(unitStartIndex > 0) {
181+
QString numberPart = trimmed.left(unitStartIndex).trimmed();
182+
QString unitPart = trimmed.mid(unitStartIndex).trimmed();
183+
*unit = unitPart;
184+
185+
// Try to parse the number part
186+
double unitResult = numberPart.toDouble(&ok);
187+
if(ok) {
188+
*success = true;
189+
return unitResult;
190+
}
191+
// Try as int
192+
int intVal = numberPart.toInt(&ok);
193+
if(ok) {
194+
*success = true;
195+
return static_cast<double>(intVal);
196+
}
197+
}
198+
148199
*success = false;
149200
return -1;
150201
}

0 commit comments

Comments
 (0)