diff --git a/sdrgui/CMakeLists.txt b/sdrgui/CMakeLists.txt index f401299f2..6a5470142 100644 --- a/sdrgui/CMakeLists.txt +++ b/sdrgui/CMakeLists.txt @@ -61,6 +61,7 @@ set(sdrgui_SOURCES soapygui/dynamicitemsettinggui.cpp soapygui/intervalslidergui.cpp soapygui/complexfactorgui.cpp + soapygui/arginfogui.cpp webapi/webapiadaptergui.cpp ) @@ -126,6 +127,7 @@ set(sdrgui_HEADERS soapygui/dynamicitemsettinggui.h soapygui/intervalslidergui.h soapygui/complexfactorgui.h + soapygui/arginfogui.h webapi/webapiadaptergui.h ) @@ -159,6 +161,7 @@ set(sdrgui_FORMS soapygui/intervalrangegui.ui soapygui/intervalslidergui.ui soapygui/complexfactorgui.ui + soapygui/arginfogui.ui ) set(sdrgui_RESOURCES diff --git a/sdrgui/soapygui/arginfogui.cpp b/sdrgui/soapygui/arginfogui.cpp new file mode 100644 index 000000000..802fa0ce5 --- /dev/null +++ b/sdrgui/soapygui/arginfogui.cpp @@ -0,0 +1,374 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +#include "ui_arginfogui.h" +#include "arginfogui.h" + +ArgInfoGUI::ArgInfoGUI(ArgInfoType type, ArgInfoValueType valueType, QWidget *parent) : + QWidget(parent), + ui(new Ui::ArgInfoGUI), + m_type(type), + m_valueType(valueType), + m_boolValue(false), + m_intValue(0), + m_floatValue(0.0), + m_hasRange(false), + m_minValue(0.0), + m_maxValue(0.0) +{ + ui->setupUi(this); + QHBoxLayout *layout = ui->argLayout; + + if (m_type != ArgInfoBinary) { + layout->removeWidget(ui->argCheck); + } + if (m_type != ArgInfoContinuous) { + layout->removeWidget(ui->argEdit); + } + if (m_type != ArgInfoDiscrete) { + layout->removeWidget(ui->argCombo); + } +} + +ArgInfoGUI::~ArgInfoGUI() +{ + delete ui; +} + +void ArgInfoGUI::setRange(double min, double max) +{ + m_hasRange = true; + m_minValue = min; + m_maxValue = max; +} + +void ArgInfoGUI::setLabel(const QString& text) +{ + ui->argLabel->setText(text); +} + +void ArgInfoGUI::setUnits(const QString& units) +{ + ui->argUnits->setText(units); +} + +void ArgInfoGUI::setBoolValue(bool value) +{ + if (m_valueType == ArgInfoValueBool) + { + m_boolValue = value; + updateUIFromBool(); + } + else if (m_valueType == ArgInfoValueInt) + { + m_intValue = setIntegerValue(value ? 1 : 0); + updateUIFromInt(); + } + else if (m_valueType == ArgInfoValueFloat) + { + m_floatValue = setDoubleValue(value ? 1.0 : 0.0); + updateUIFromFloat(); + } + else if (m_valueType == ArgInfoValueString) + { + m_stringValue = QString(value ? "true" : "false"); + updateUIFromString(); + } +} + +void ArgInfoGUI::addBoolValue(const QString& text, bool value) +{ + if (m_type == ArgInfoDiscrete) { + ui->argCombo->addItem(text, QVariant(value)); + } +} + +void ArgInfoGUI::setIntValue(int value) +{ + if (m_valueType == ArgInfoValueBool) + { + m_boolValue = (value != 0); + updateUIFromBool(); + } + else if (m_valueType == ArgInfoValueInt) + { + m_intValue = setIntegerValue(value); + updateUIFromInt(); + } + else if (m_valueType == ArgInfoValueFloat) + { + m_floatValue = setDoubleValue(value); + updateUIFromFloat(); + } + else if (m_valueType == ArgInfoValueString) + { + m_stringValue = QString("%1").arg(value); + updateUIFromString(); + } +} + +void ArgInfoGUI::addIntValue(const QString& text, int value) +{ + if (m_type == ArgInfoDiscrete) { + ui->argCombo->addItem(text, QVariant(setIntegerValue(value))); + } +} + +void ArgInfoGUI::setFloatValue(double value) +{ + if (m_valueType == ArgInfoValueBool) + { + m_boolValue = (value != 0.0); + updateUIFromBool(); + } + else if (m_valueType == ArgInfoValueInt) + { + m_intValue = setIntegerValue(value); + updateUIFromInt(); + } + else if (m_valueType == ArgInfoValueFloat) + { + m_floatValue = setDoubleValue(value); + updateUIFromFloat(); + } + else if (m_valueType == ArgInfoValueString) + { + m_stringValue = QString("%1").arg(value); + updateUIFromString(); + } +} + +void ArgInfoGUI::addFloatValue(const QString& text, double value) +{ + if (m_type == ArgInfoDiscrete) { + ui->argCombo->addItem(text, QVariant(setDoubleValue(value))); + } +} + +void ArgInfoGUI::setStringValue(const QString& value) +{ + if (m_valueType == ArgInfoValueBool) + { + m_boolValue = (value == "true"); + updateUIFromBool(); + } + else if (m_valueType == ArgInfoValueInt) + { + int intValue = atoi(value.toStdString().c_str()); + m_intValue = setIntegerValue(intValue); + updateUIFromInt(); + } + else if (m_valueType == ArgInfoValueFloat) + { + double doubleValue = atof(value.toStdString().c_str()); + m_floatValue = setDoubleValue(doubleValue); + updateUIFromFloat(); + } + else if (m_valueType == ArgInfoValueString) + { + m_stringValue = value; + updateUIFromString(); + } +} + +void ArgInfoGUI::addStringValue(const QString& text, const QString& value) +{ + if (m_type == ArgInfoDiscrete) { + ui->argCombo->addItem(text, QVariant(value)); + } +} + +int ArgInfoGUI::setIntegerValue(int value) +{ + if (m_hasRange) { + return value < round(m_minValue) ? round(m_minValue) : value > round(m_maxValue) ? round(m_maxValue) : value; + } else { + return value; + } +} + +double ArgInfoGUI::setDoubleValue(double value) +{ + if (m_hasRange) { + return value < m_minValue ? m_minValue : value > m_maxValue ? m_maxValue : value; + } else { + return value; + } +} + +void ArgInfoGUI::updateUIFromBool() +{ + if (m_type == ArgInfoBinary) + { + ui->argCheck->blockSignals(true); + ui->argCheck->setChecked(m_boolValue); + ui->argCheck->blockSignals(false); + } + else if (m_type == ArgInfoContinuous) + { + ui->argEdit->blockSignals(true); + ui->argEdit->setText(QString(m_boolValue ? "true" : "false")); + ui->argEdit->blockSignals(false); + } + else if (m_type == ArgInfoDiscrete) + { + if (ui->argCombo->count() > 1) + { + ui->argCombo->blockSignals(true); + ui->argCombo->setCurrentIndex(m_boolValue ? 0 : 1); + ui->argCombo->blockSignals(false); + } + } +} + +void ArgInfoGUI::updateUIFromInt() +{ + if (m_type == ArgInfoBinary) + { + ui->argCheck->blockSignals(true); + ui->argCheck->setChecked(m_intValue == 0 ? false : true); + ui->argCheck->blockSignals(false); + } + else if (m_type == ArgInfoContinuous) + { + ui->argEdit->blockSignals(true); + ui->argEdit->setText(QString("%1").arg(m_intValue)); + ui->argEdit->blockSignals(false); + } + else if (m_type == ArgInfoDiscrete) + { + for (int i = 0; i < ui->argCombo->count(); i++) + { + if (ui->argCombo->itemData(i).type() == QVariant::Int) + { + bool ok = false; + QVariant v = ui->argCombo->itemData(i); + + if (m_intValue >= v.toInt(&ok) && ok) + { + ui->argCombo->blockSignals(true); + ui->argCombo->setCurrentIndex(i); + ui->argCombo->blockSignals(false); + break; + } + } + } + } +} + +void ArgInfoGUI::updateUIFromFloat() +{ + if (m_type == ArgInfoBinary) + { + ui->argCheck->blockSignals(true); + ui->argCheck->setChecked(m_floatValue == 0.0 ? false : true); + ui->argCheck->blockSignals(false); + } + else if (m_type == ArgInfoContinuous) + { + ui->argEdit->blockSignals(true); + ui->argEdit->setText(QString("%1").arg(m_floatValue)); + ui->argEdit->blockSignals(false); + } + else if (m_type == ArgInfoDiscrete) + { + for (int i = 0; i < ui->argCombo->count(); i++) + { + if (ui->argCombo->itemData(i).type() == QVariant::Double) + { + bool ok = false; + QVariant v = ui->argCombo->itemData(i); + + if (m_floatValue >= v.toDouble(&ok) && ok) + { + ui->argCombo->blockSignals(true); + ui->argCombo->setCurrentIndex(i); + ui->argCombo->blockSignals(false); + break; + } + } + } + } +} + +void ArgInfoGUI::updateUIFromString() +{ + if (m_type == ArgInfoBinary) + { + ui->argCheck->blockSignals(true); + ui->argCheck->setChecked(m_stringValue == "true" ? true : false); + ui->argCheck->blockSignals(false); + } + else if (m_type == ArgInfoContinuous) + { + ui->argEdit->blockSignals(true); + ui->argEdit->setText(m_stringValue); + ui->argEdit->blockSignals(false); + } +} + +void ArgInfoGUI::on_argCheck_toggled(bool checked) +{ + setBoolValue(checked); + emit valueChanged(); +} + +void ArgInfoGUI::on_argEdit_editingFinished() +{ + setStringValue(ui->argEdit->text()); + emit valueChanged(); +} + +#ifdef _MSC_VER +void ArgInfoGUI::on_argCombo_currentIndexChanged(int index) +#else +void ArgInfoGUI::on_argCombo_currentIndexChanged(int index __attribute__((unused))) +#endif +{ + QVariant v = ui->argCombo->currentData(); + bool ok = false; + + if (v.type() == QVariant::Bool) + { + setBoolValue(v.toBool()); + emit valueChanged(); + } + else if (v.type() == QVariant::Int) + { + setIntValue(v.toInt(&ok)); + + if (ok) { + emit valueChanged(); + } + } + else if (v.type() == QVariant::Double) + { + setFloatValue(v.toDouble(&ok)); + + if (ok) { + emit valueChanged(); + } + } + else if (v.type() ==QVariant::String) + { + setStringValue(v.toString()); + emit valueChanged(); + } +} + diff --git a/sdrgui/soapygui/arginfogui.h b/sdrgui/soapygui/arginfogui.h new file mode 100644 index 000000000..b93777863 --- /dev/null +++ b/sdrgui/soapygui/arginfogui.h @@ -0,0 +1,101 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRGUI_SOAPYGUI_ARGINFOGUI_H_ +#define SDRGUI_SOAPYGUI_ARGINFOGUI_H_ + +#include +#include + +#include "export.h" + +namespace Ui { + class ArgInfoGUI; +} + +class SDRGUI_API ArgInfoGUI : public QWidget +{ + Q_OBJECT +public: + enum ArgInfoType + { + ArgInfoBinary, + ArgInfoContinuous, + ArgInfoDiscrete + }; + + enum ArgInfoValueType + { + ArgInfoValueBool, + ArgInfoValueInt, + ArgInfoValueFloat, + ArgInfoValueString + }; + + explicit ArgInfoGUI(ArgInfoType type, ArgInfoValueType valueType, QWidget *parent = 0); + ~ArgInfoGUI(); + + void setLabel(const QString& text); + void setUnits(const QString& units); + + ArgInfoType getType() const { return m_type; } + void setRange(double min, double max); + + bool getBoolValue() const { return m_boolValue; } + void setBoolValue(bool value); + void addBoolValue(const QString& text, bool value); + + int getIntValue() const { return m_intValue; } + void setIntValue(int value); + void addIntValue(const QString& text, int value); + + double getFloatValue() const { return m_floatValue; } + void setFloatValue(double value); + void addFloatValue(const QString& text, double value); + + const QString& getStringValue() const { return m_stringValue; } + void setStringValue(const QString& value); + void addStringValue(const QString& text, const QString& value); + +signals: + void valueChanged(); + +private slots: + void on_argCheck_toggled(bool checked); + void on_argEdit_editingFinished(); + void on_argCombo_currentIndexChanged(int index); + +private: + int setIntegerValue(int value); + double setDoubleValue(double value); + void updateUIFromBool(); + void updateUIFromInt(); + void updateUIFromFloat(); + void updateUIFromString(); + + Ui::ArgInfoGUI* ui; + ArgInfoType m_type; + ArgInfoValueType m_valueType; + bool m_boolValue; + int m_intValue; + double m_floatValue; + QString m_stringValue; + bool m_hasRange; + double m_minValue; + double m_maxValue; +}; + +#endif /* SDRGUI_SOAPYGUI_ARGINFOGUI_H_ */ diff --git a/sdrgui/soapygui/arginfogui.ui b/sdrgui/soapygui/arginfogui.ui new file mode 100644 index 000000000..bac0a3f0f --- /dev/null +++ b/sdrgui/soapygui/arginfogui.ui @@ -0,0 +1,76 @@ + + + ArgInfoGUI + + + + 0 + 0 + 384 + 30 + + + + + 0 + 0 + + + + + 0 + 30 + + + + Form + + + + + 0 + 0 + 379 + 29 + + + + + + + + 80 + 0 + + + + Label + + + + + + + + + + + + + + + + + + + + Unit + + + + + + + + +