+void AnalogSignal::on_conv_threshold_changed(int index)
+{
+ SignalBase::ConversionType conv_type = base_->get_conversion_type();
+
+ // Note: index is set to -1 if the text in the combo box matches none of
+ // the entries in the combo box
+
+ if ((index == -1) && (conv_threshold_cb_->currentText().length() == 0))
+ return;
+
+ // The combo box entry with the custom value has user_data set to -1
+ const int user_data = conv_threshold_cb_->findText(
+ conv_threshold_cb_->currentText());
+
+ const bool use_custom_thr = (index == -1) || (user_data == -1);
+
+ if (conv_type == SignalBase::A2LConversionByThreshold && use_custom_thr) {
+ // Not one of the preset values, try to parse the combo box text
+ // Note: Regex loosely based on
+ // https://txt2re.com/index-c++.php3?s=0.1V&1&-13
+ QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
+ QString re2 = "([a-zA-Z]*)"; // SI unit
+ QRegExp regex(re1 + re2);
+
+ const QString text = conv_threshold_cb_->currentText();
+ if (!regex.exactMatch(text))
+ return; // String doesn't match the regex
+
+ QStringList tokens = regex.capturedTexts();
+
+ // For now, we simply assume that the unit is volt without modifiers
+ const double thr = tokens.at(1).toDouble();
+
+ // Only restart the conversion if the threshold was updated.
+ // We're starting a delayed conversion because the user may still be
+ // typing and the UI would lag if we kept on restarting it immediately
+ if (base_->set_conversion_option("threshold_value", thr))
+ base_->start_conversion(true);
+ }
+
+ if (conv_type == SignalBase::A2LConversionBySchmittTrigger && use_custom_thr) {
+ // Not one of the preset values, try to parse the combo box text
+ // Note: Regex loosely based on
+ // https://txt2re.com/index-c++.php3?s=0.1V/0.2V&2&14&-22&3&15
+ QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
+ QString re2 = "([a-zA-Z]*)"; // SI unit
+ QString re3 = "\\/"; // Forward slash, not captured
+ QString re4 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
+ QString re5 = "([a-zA-Z]*)"; // SI unit
+ QRegExp regex(re1 + re2 + re3 + re4 + re5);
+
+ const QString text = conv_threshold_cb_->currentText();
+ if (!regex.exactMatch(text))
+ return; // String doesn't match the regex
+
+ QStringList tokens = regex.capturedTexts();
+
+ // For now, we simply assume that the unit is volt without modifiers
+ const double low_thr = tokens.at(1).toDouble();
+ const double high_thr = tokens.at(3).toDouble();
+
+ // Only restart the conversion if one of the options was updated.
+ // We're starting a delayed conversion because the user may still be
+ // typing and the UI would lag if we kept on restarting it immediately
+ bool o1 = base_->set_conversion_option("threshold_value_low", low_thr);
+ bool o2 = base_->set_conversion_option("threshold_value_high", high_thr);
+ if (o1 || o2)
+ base_->start_conversion(true); // Start delayed conversion
+ }
+
+ base_->set_conversion_preset((SignalBase::ConversionPreset)index);
+
+ // Immediately start the conversion if we're not using custom values
+ // (i.e. we're using one of the presets)
+ if (!use_custom_thr)
+ base_->start_conversion();
+}
+
+void AnalogSignal::on_delayed_conversion_starter()
+{
+ base_->start_conversion();
+}
+