Commit 2ae4b4393b53de46d4c240c376a22bbfef8a83b8

Authored by oliverhaag
1 parent 0dd75a14

New SiSpinBox

openhantek/ChangeLog
@@ -176,3 +176,7 @@ @@ -176,3 +176,7 @@
176 176
177 2012-11-05 Oliver Haag <oliver.haag@gmail.com> 177 2012-11-05 Oliver Haag <oliver.haag@gmail.com>
178 * Bugfix: short instead of long int for DSO-2250 trigger offset 178 * Bugfix: short instead of long int for DSO-2250 trigger offset
  179 +
  180 +2012-11-05 Oliver Haag <oliver.haag@gmail.com>
  181 +* Added new SiSpinBox
  182 +* Replaced ComboBox with SiSpinBox for time- and frequencybase
openhantek/OpenHantek.pro
@@ -26,6 +26,7 @@ SOURCES += \ @@ -26,6 +26,7 @@ SOURCES += \
26 src/configpages.cpp \ 26 src/configpages.cpp \
27 src/dataanalyzer.cpp \ 27 src/dataanalyzer.cpp \
28 src/dockwindows.cpp \ 28 src/dockwindows.cpp \
  29 + src/dso.cpp \
29 src/dsocontrol.cpp \ 30 src/dsocontrol.cpp \
30 src/dsowidget.cpp \ 31 src/dsowidget.cpp \
31 src/exporter.cpp \ 32 src/exporter.cpp \
@@ -36,16 +37,17 @@ SOURCES += \ @@ -36,16 +37,17 @@ SOURCES += \
36 src/main.cpp \ 37 src/main.cpp \
37 src/openhantek.cpp \ 38 src/openhantek.cpp \
38 src/settings.cpp \ 39 src/settings.cpp \
  40 + src/sispinbox.cpp \
39 src/hantek/control.cpp \ 41 src/hantek/control.cpp \
40 src/hantek/device.cpp \ 42 src/hantek/device.cpp \
41 - src/hantek/types.cpp \  
42 - src/dso.cpp 43 + src/hantek/types.cpp
43 HEADERS += \ 44 HEADERS += \
44 src/colorbox.h \ 45 src/colorbox.h \
45 src/configdialog.h \ 46 src/configdialog.h \
46 src/configpages.h \ 47 src/configpages.h \
47 src/dataanalyzer.h \ 48 src/dataanalyzer.h \
48 src/dockwindows.h \ 49 src/dockwindows.h \
  50 + src/dso.h \
49 src/dsocontrol.h \ 51 src/dsocontrol.h \
50 src/dsowidget.h \ 52 src/dsowidget.h \
51 src/exporter.h \ 53 src/exporter.h \
@@ -55,10 +57,10 @@ HEADERS += \ @@ -55,10 +57,10 @@ HEADERS += \
55 src/levelslider.h \ 57 src/levelslider.h \
56 src/openhantek.h \ 58 src/openhantek.h \
57 src/settings.h \ 59 src/settings.h \
  60 + src/sispinbox.h \
58 src/hantek/control.h \ 61 src/hantek/control.h \
59 src/hantek/device.h \ 62 src/hantek/device.h \
60 - src/hantek/types.h \  
61 - src/dso.h 63 + src/hantek/types.h
62 64
63 # Ressource files 65 # Ressource files
64 RESOURCES += \ 66 RESOURCES += \
openhantek/src/dockwindows.cpp
@@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
32 #include "dockwindows.h" 32 #include "dockwindows.h"
33 33
34 #include "settings.h" 34 #include "settings.h"
  35 +#include "sispinbox.h"
35 #include "helper.h" 36 #include "helper.h"
36 37
37 38
@@ -44,29 +45,20 @@ @@ -44,29 +45,20 @@
44 HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::WindowFlags flags) : QDockWidget(tr("Horizontal"), parent, flags) { 45 HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::WindowFlags flags) : QDockWidget(tr("Horizontal"), parent, flags) {
45 this->settings = settings; 46 this->settings = settings;
46 47
47 - // Initialize lists for comboboxes  
48 - this->timebaseSteps << 1e-8 << 2e-8 << 4e-8 << 1e-7 << 2e-7 << 4e-7  
49 - << 1e-6 << 2e-6 << 4e-6 << 1e-5 << 2e-5 << 4e-5 << 1e-4 << 2e-4 << 4e-4  
50 - << 1e-3 << 2e-3 << 4e-3 << 1e-2 << 2e-2 << 4e-2 << 1e-1 << 2e-1 << 4e-1  
51 - << 1e0 << 2e0 << 4e0 << 1e1 << 2e1 << 4e1 << 6e1 << 12e1 << 24e1  
52 - << 6e2 << 12e2 << 24e2 << 36e2; ///< Timebase steps in seconds/div  
53 - for(QList<double>::iterator timebase = this->timebaseSteps.begin(); timebase != this->timebaseSteps.end(); ++timebase)  
54 - this->timebaseStrings << Helper::valueToString(*timebase, Helper::UNIT_SECONDS, 0);  
55 - this->frequencybaseSteps  
56 - << 1.0 << 2.0 << 5.0 << 1e1 << 2e1 << 5e1 << 1e2 << 2e2 << 5e2  
57 - << 1e3 << 2e3 << 5e3 << 1e4 << 2e4 << 4e4 << 1e5 << 2e5 << 5e5  
58 - << 1e6 << 2e6 << 5e6 << 1e7; ///< Frequencybase steps in Hz/div  
59 - for(QList<double>::iterator frequencybase = this->frequencybaseSteps.begin(); frequencybase != this->frequencybaseSteps.end(); ++frequencybase)  
60 - this->frequencybaseStrings << Helper::valueToString(*frequencybase, Helper::UNIT_HERTZ, 0);  
61 -  
62 // Initialize elements 48 // Initialize elements
63 this->timebaseLabel = new QLabel(tr("Timebase")); 49 this->timebaseLabel = new QLabel(tr("Timebase"));
64 - this->timebaseComboBox = new QComboBox();  
65 - this->timebaseComboBox->addItems(this->timebaseStrings); 50 + this->timebaseSiSpinBox = new SiSpinBox(Helper::UNIT_SECONDS);
  51 + this->timebaseSiSpinBox->setMinimum(1e-9);
  52 + this->timebaseSiSpinBox->setMaximum(3.6e3);
  53 +
  54 + QList<double> frequencybaseSteps;
  55 + frequencybaseSteps << 1.0 << 2.0 << 5.0 << 10.0;
66 56
67 this->frequencybaseLabel = new QLabel(tr("Frequencybase")); 57 this->frequencybaseLabel = new QLabel(tr("Frequencybase"));
68 - this->frequencybaseComboBox = new QComboBox();  
69 - this->frequencybaseComboBox->addItems(this->frequencybaseStrings); 58 + this->frequencybaseSiSpinBox = new SiSpinBox(Helper::UNIT_HERTZ);
  59 + this->frequencybaseSiSpinBox->setSteps(frequencybaseSteps);
  60 + this->frequencybaseSiSpinBox->setMinimum(1.0);
  61 + this->frequencybaseSiSpinBox->setMaximum(100e6);
70 62
71 this->formatLabel = new QLabel(tr("Format")); 63 this->formatLabel = new QLabel(tr("Format"));
72 this->formatComboBox = new QComboBox(); 64 this->formatComboBox = new QComboBox();
@@ -77,9 +69,9 @@ HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::Windo @@ -77,9 +69,9 @@ HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::Windo
77 this->dockLayout->setColumnMinimumWidth(0, 64); 69 this->dockLayout->setColumnMinimumWidth(0, 64);
78 this->dockLayout->setColumnStretch(1, 1); 70 this->dockLayout->setColumnStretch(1, 1);
79 this->dockLayout->addWidget(this->timebaseLabel, 0, 0); 71 this->dockLayout->addWidget(this->timebaseLabel, 0, 0);
80 - this->dockLayout->addWidget(this->timebaseComboBox, 0, 1); 72 + this->dockLayout->addWidget(this->timebaseSiSpinBox, 0, 1);
81 this->dockLayout->addWidget(this->frequencybaseLabel, 1, 0); 73 this->dockLayout->addWidget(this->frequencybaseLabel, 1, 0);
82 - this->dockLayout->addWidget(this->frequencybaseComboBox, 1, 1); 74 + this->dockLayout->addWidget(this->frequencybaseSiSpinBox, 1, 1);
83 this->dockLayout->addWidget(this->formatLabel, 2, 0); 75 this->dockLayout->addWidget(this->formatLabel, 2, 0);
84 this->dockLayout->addWidget(this->formatComboBox, 2, 1); 76 this->dockLayout->addWidget(this->formatComboBox, 2, 1);
85 77
@@ -90,8 +82,8 @@ HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::Windo @@ -90,8 +82,8 @@ HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::Windo
90 this->setWidget(this->dockWidget); 82 this->setWidget(this->dockWidget);
91 83
92 // Connect signals and slots 84 // Connect signals and slots
93 - connect(this->frequencybaseComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(frequencybaseSelected(int)));  
94 - connect(this->timebaseComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(timebaseSelected(int))); 85 + connect(this->frequencybaseSiSpinBox, SIGNAL(valueChanged(double)), this, SLOT(frequencybaseSelected(double)));
  86 + connect(this->timebaseSiSpinBox, SIGNAL(valueChanged(double)), this, SLOT(timebaseSelected(double)));
95 connect(this->formatComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(formatSelected(int))); 87 connect(this->formatComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(formatSelected(int)));
96 88
97 // Set values 89 // Set values
@@ -114,26 +106,14 @@ void HorizontalDock::closeEvent(QCloseEvent *event) { @@ -114,26 +106,14 @@ void HorizontalDock::closeEvent(QCloseEvent *event) {
114 106
115 /// \brief Changes the frequencybase if the new value is supported. 107 /// \brief Changes the frequencybase if the new value is supported.
116 /// \param frequencybase The frequencybase in hertz. 108 /// \param frequencybase The frequencybase in hertz.
117 -/// \return Index of frequencybase-value, -1 on error.  
118 -int HorizontalDock::setFrequencybase(double frequencybase) {  
119 - int index = this->frequencybaseSteps.indexOf(frequencybase);  
120 -  
121 - if(index != -1)  
122 - this->frequencybaseComboBox->setCurrentIndex(index);  
123 -  
124 - return index; 109 +void HorizontalDock::setFrequencybase(double frequencybase) {
  110 + this->frequencybaseSiSpinBox->setValue(frequencybase);
125 } 111 }
126 112
127 /// \brief Changes the timebase if the new value is supported. 113 /// \brief Changes the timebase if the new value is supported.
128 /// \param timebase The timebase in seconds. 114 /// \param timebase The timebase in seconds.
129 -/// \return Index of timebase-value, -1 on error.  
130 -int HorizontalDock::setTimebase(double timebase) {  
131 - int index = this->timebaseSteps.indexOf(timebase);  
132 -  
133 - if(index != -1)  
134 - this->timebaseComboBox->setCurrentIndex(index);  
135 -  
136 - return index; 115 +void HorizontalDock::setTimebase(double timebase) {
  116 + this->timebaseSiSpinBox->setValue(timebase);
137 } 117 }
138 118
139 /// \brief Changes the format if the new value is supported. 119 /// \brief Changes the format if the new value is supported.
@@ -149,17 +129,17 @@ int HorizontalDock::setFormat(Dso::GraphFormat format) { @@ -149,17 +129,17 @@ int HorizontalDock::setFormat(Dso::GraphFormat format) {
149 } 129 }
150 130
151 /// \brief Called when the frequencybase combo box changes it's value. 131 /// \brief Called when the frequencybase combo box changes it's value.
152 -/// \param index The index of the combo box item.  
153 -void HorizontalDock::frequencybaseSelected(int index) {  
154 - this->settings->scope.horizontal.frequencybase = this->frequencybaseSteps.at(index);  
155 - emit frequencybaseChanged(this->settings->scope.horizontal.frequencybase); 132 +/// \param frequencybase The frequencybase in hertz.
  133 +void HorizontalDock::frequencybaseSelected(double frequencybase) {
  134 + this->settings->scope.horizontal.frequencybase = frequencybase;
  135 + emit frequencybaseChanged(frequencybase);
156 } 136 }
157 137
158 /// \brief Called when the timebase combo box changes it's value. 138 /// \brief Called when the timebase combo box changes it's value.
159 -/// \param index The index of the combo box item.  
160 -void HorizontalDock::timebaseSelected(int index) {  
161 - this->settings->scope.horizontal.timebase = this->timebaseSteps.at(index);  
162 - emit timebaseChanged(this->settings->scope.horizontal.timebase); 139 +/// \param timebase The timebase in seconds.
  140 +void HorizontalDock::timebaseSelected(double timebase) {
  141 + this->settings->scope.horizontal.timebase = timebase;
  142 + emit timebaseChanged(timebase);
163 } 143 }
164 144
165 /// \brief Called when the format combo box changes it's value. 145 /// \brief Called when the format combo box changes it's value.
openhantek/src/dockwindows.h
@@ -39,6 +39,8 @@ class QLabel; @@ -39,6 +39,8 @@ class QLabel;
39 class QCheckBox; 39 class QCheckBox;
40 class QComboBox; 40 class QComboBox;
41 41
  42 +class SiSpinBox;
  43 +
42 44
43 //////////////////////////////////////////////////////////////////////////////// 45 ////////////////////////////////////////////////////////////////////////////////
44 /// \class HorizontalDock dockwindows.h 46 /// \class HorizontalDock dockwindows.h
@@ -51,8 +53,8 @@ class HorizontalDock : public QDockWidget { @@ -51,8 +53,8 @@ class HorizontalDock : public QDockWidget {
51 HorizontalDock(DsoSettings *settings, QWidget *parent = 0, Qt::WindowFlags flags = 0); 53 HorizontalDock(DsoSettings *settings, QWidget *parent = 0, Qt::WindowFlags flags = 0);
52 ~HorizontalDock(); 54 ~HorizontalDock();
53 55
54 - int setFrequencybase(double timebase);  
55 - int setTimebase(double timebase); 56 + void setFrequencybase(double timebase);
  57 + void setTimebase(double timebase);
56 int setFormat(Dso::GraphFormat format); 58 int setFormat(Dso::GraphFormat format);
57 59
58 protected: 60 protected:
@@ -63,21 +65,17 @@ class HorizontalDock : public QDockWidget { @@ -63,21 +65,17 @@ class HorizontalDock : public QDockWidget {
63 QLabel *timebaseLabel; ///< The label for the timebase combobox 65 QLabel *timebaseLabel; ///< The label for the timebase combobox
64 QLabel *frequencybaseLabel; ///< The label for the frequencybase combobox 66 QLabel *frequencybaseLabel; ///< The label for the frequencybase combobox
65 QLabel *formatLabel; ///< The label for the format combobox 67 QLabel *formatLabel; ///< The label for the format combobox
66 - QComboBox *timebaseComboBox; ///< Selects the timebase for voltage graphs  
67 - QComboBox *frequencybaseComboBox; ///< Selects the frequencybase for spectrum graphs 68 + SiSpinBox *timebaseSiSpinBox; ///< Selects the timebase for voltage graphs
  69 + SiSpinBox *frequencybaseSiSpinBox; ///< Selects the frequencybase for spectrum graphs
68 QComboBox *formatComboBox; ///< Selects the way the sampled data is interpreted and shown 70 QComboBox *formatComboBox; ///< Selects the way the sampled data is interpreted and shown
69 71
70 DsoSettings *settings; ///< The settings provided by the parent class 72 DsoSettings *settings; ///< The settings provided by the parent class
71 73
72 - QList<double> frequencybaseSteps; ///< The selectable steps for the frequencybase  
73 - QList<double> timebaseSteps; ///< The selectable steps for the timebase  
74 - QStringList frequencybaseStrings; ///< String representations for the frequencybase steps  
75 - QStringList timebaseStrings; ///< String representations for the timebase steps  
76 QStringList formatStrings; ///< Strings for the formats 74 QStringList formatStrings; ///< Strings for the formats
77 75
78 protected slots: 76 protected slots:
79 - void frequencybaseSelected(int index);  
80 - void timebaseSelected(int index); 77 + void frequencybaseSelected(double frequencybase);
  78 + void timebaseSelected(double timebase);
81 void formatSelected(int index); 79 void formatSelected(int index);
82 80
83 signals: 81 signals:
openhantek/src/hantek/types.cpp
@@ -673,13 +673,13 @@ namespace Hantek { @@ -673,13 +673,13 @@ namespace Hantek {
673 } 673 }
674 674
675 /// \brief Get the downsampling state in ESamplerateBits. 675 /// \brief Get the downsampling state in ESamplerateBits.
676 - /// \return The samplerateFast state. 676 + /// \return The downsampling state.
677 bool BulkSetSamplerate2250::getDownsampling() { 677 bool BulkSetSamplerate2250::getDownsampling() {
678 return ((ESamplerateBits *) &(this->array[2]))->downsampling == 1; 678 return ((ESamplerateBits *) &(this->array[2]))->downsampling == 1;
679 } 679 }
680 680
681 - /// \brief Set the samplerateFast in ESamplerateBits to the given value.  
682 - /// \param value The new samplerateFast value. 681 + /// \brief Set the downsampling in ESamplerateBits to the given state.
  682 + /// \param downsampling The new downsampling state.
683 void BulkSetSamplerate2250::setDownsampling(bool downsampling) { 683 void BulkSetSamplerate2250::setDownsampling(bool downsampling) {
684 ((ESamplerateBits *) &(this->array[2]))->downsampling = downsampling ? 1 : 0; 684 ((ESamplerateBits *) &(this->array[2]))->downsampling = downsampling ? 1 : 0;
685 } 685 }
openhantek/src/helper.cpp
@@ -25,6 +25,8 @@ @@ -25,6 +25,8 @@
25 #include <cmath> 25 #include <cmath>
26 26
27 #include <QApplication> 27 #include <QApplication>
  28 +#include <QLocale>
  29 +#include <QStringList>
28 30
29 #if LIBUSB_VERSION == 0 31 #if LIBUSB_VERSION == 0
30 #include <usb.h> 32 #include <usb.h>
@@ -74,7 +76,7 @@ namespace Helper { @@ -74,7 +76,7 @@ namespace Helper {
74 } 76 }
75 } 77 }
76 78
77 - /// \brief Converts double to string containing value and (prefix+)unit. 79 + /// \brief Converts double to string containing value and (prefix+)unit (Counterpart to Helper::stringToValue).
78 /// \param value The value in prefixless units. 80 /// \param value The value in prefixless units.
79 /// \param unit The unit for the value. 81 /// \param unit The unit for the value.
80 /// \param precision Significant digits, 0 for integer, -1 for auto. 82 /// \param precision Significant digits, 0 for integer, -1 for auto.
@@ -87,9 +89,9 @@ namespace Helper { @@ -87,9 +89,9 @@ namespace Helper {
87 // Voltage string representation 89 // Voltage string representation
88 int logarithm = floor(log10(fabs(value))); 90 int logarithm = floor(log10(fabs(value)));
89 if(value < 1e-3) 91 if(value < 1e-3)
90 - return QApplication::tr("%L1 \265V").arg(value * 1e6, 0, format, (precision <= 0) ? precision : qBound(0, precision - 7 - logarithm, precision)); 92 + return QApplication::tr("%L1 \265V").arg(value / 1e-6, 0, format, (precision <= 0) ? precision : qBound(0, precision - 7 - logarithm, precision));
91 else if(value < 1.0) 93 else if(value < 1.0)
92 - return QApplication::tr("%L1 mV").arg(value * 1e3, 0, format, (precision <= 0) ? precision : (precision - 4 - logarithm)); 94 + return QApplication::tr("%L1 mV").arg(value / 1e-3, 0, format, (precision <= 0) ? precision : (precision - 4 - logarithm));
93 else 95 else
94 return QApplication::tr("%L1 V").arg(value, 0, format, (precision <= 0) ? precision : qMax(0, precision - 1 - logarithm)); 96 return QApplication::tr("%L1 V").arg(value, 0, format, (precision <= 0) ? precision : qMax(0, precision - 1 - logarithm));
95 } 97 }
@@ -100,13 +102,13 @@ namespace Helper { @@ -100,13 +102,13 @@ namespace Helper {
100 case UNIT_SECONDS: 102 case UNIT_SECONDS:
101 // Time string representation 103 // Time string representation
102 if(value < 1e-9) 104 if(value < 1e-9)
103 - return QApplication::tr("%L1 ps").arg(value * 1e12, 0, format, (precision <= 0) ? precision : qBound(0, precision - 13 - (int) floor(log10(fabs(value))), precision)); 105 + return QApplication::tr("%L1 ps").arg(value / 1e-12, 0, format, (precision <= 0) ? precision : qBound(0, precision - 13 - (int) floor(log10(fabs(value))), precision));
104 else if(value < 1e-6) 106 else if(value < 1e-6)
105 - return QApplication::tr("%L1 ns").arg(value * 1e9, 0, format, (precision <= 0) ? precision : (precision - 10 - (int) floor(log10(fabs(value))))); 107 + return QApplication::tr("%L1 ns").arg(value / 1e-9, 0, format, (precision <= 0) ? precision : (precision - 10 - (int) floor(log10(fabs(value)))));
106 else if(value < 1e-3) 108 else if(value < 1e-3)
107 - return QApplication::tr("%L1 \265s").arg(value * 1e6, 0, format, (precision <= 0) ? precision : (precision - 7 - (int) floor(log10(fabs(value))))); 109 + return QApplication::tr("%L1 \265s").arg(value / 1e-6, 0, format, (precision <= 0) ? precision : (precision - 7 - (int) floor(log10(fabs(value)))));
108 else if(value < 1.0) 110 else if(value < 1.0)
109 - return QApplication::tr("%L1 ms").arg(value * 1e3, 0, format, (precision <= 0) ? precision : (precision - 4 - (int) floor(log10(fabs(value))))); 111 + return QApplication::tr("%L1 ms").arg(value / 1e-3, 0, format, (precision <= 0) ? precision : (precision - 4 - (int) floor(log10(fabs(value)))));
110 else if(value < 60) 112 else if(value < 60)
111 return QApplication::tr("%L1 s").arg(value, 0, format, (precision <= 0) ? precision : (precision - 1 - (int) floor(log10(fabs(value))))); 113 return QApplication::tr("%L1 s").arg(value, 0, format, (precision <= 0) ? precision : (precision - 1 - (int) floor(log10(fabs(value)))));
112 else if(value < 3600) 114 else if(value < 3600)
@@ -143,6 +145,116 @@ namespace Helper { @@ -143,6 +145,116 @@ namespace Helper {
143 } 145 }
144 } 146 }
145 147
  148 + /// \brief Converts string containing value and (prefix+)unit to double (Counterpart to Helper::valueToString).
  149 + /// \param text The text containing the value and its unit.
  150 + /// \param unit The base unit of the value.
  151 + /// \param ok Pointer to a success-flag, true on success, false on error.
  152 + /// \return Decoded value.
  153 + double stringToValue(const QString &text, Unit unit, bool *ok) {
  154 + // Check if the text is empty
  155 + int totalSize = text.size();
  156 + if(!totalSize){
  157 + if(ok)
  158 + *ok = false;
  159 + return 0.0;
  160 + }
  161 +
  162 + // Split value and unit apart
  163 + int valueSize = 0;
  164 + QLocale locale;
  165 + bool decimalFound = false;
  166 + bool exponentFound = false;
  167 + if(text[valueSize] == locale.negativeSign())
  168 + ++valueSize;
  169 + for(; valueSize < text.size(); ++valueSize) {
  170 + QChar character = text[valueSize];
  171 +
  172 + if(character.isDigit()) {
  173 + }
  174 + else if(character == locale.decimalPoint() && decimalFound == false && exponentFound == false) {
  175 + decimalFound = true;
  176 + }
  177 + else if(character == locale.exponential() && exponentFound == false) {
  178 + exponentFound = true;
  179 + if(text[valueSize + 1] == locale.negativeSign())
  180 + ++valueSize;
  181 + }
  182 + else {
  183 + break;
  184 + }
  185 + }
  186 + QString valueString = text.left(valueSize);
  187 + bool valueOk = false;
  188 + double value = valueString.toDouble(&valueOk);
  189 + if(!valueOk) {
  190 + if(ok)
  191 + *ok = false;
  192 + return value;
  193 + }
  194 + QString unitString = text.right(text.size() - valueSize).trimmed();
  195 +
  196 + if(ok)
  197 + *ok = true;
  198 + switch(unit) {
  199 + case UNIT_VOLTS: {
  200 + // Voltage string decoding
  201 + if(unitString.startsWith('\265'))
  202 + return value * 1e-6;
  203 + else if(unitString.startsWith('m'))
  204 + return value * 1e-3;
  205 + else
  206 + return value;
  207 + }
  208 + case UNIT_DECIBEL:
  209 + // Power level string decoding
  210 + return value;
  211 +
  212 + case UNIT_SECONDS:
  213 + // Time string decoding
  214 + if(unitString.startsWith('p'))
  215 + return value * 1e-12;
  216 + else if(unitString.startsWith('n'))
  217 + return value * 1e-9;
  218 + else if(unitString.startsWith('\265'))
  219 + return value * 1e-6;
  220 + else if(unitString.startsWith("min"))
  221 + return value * 60;
  222 + else if(unitString.startsWith('m'))
  223 + return value * 1e-3;
  224 + else if(unitString.startsWith('h'))
  225 + return value * 3600;
  226 + else
  227 + return value;
  228 +
  229 + case UNIT_HERTZ:
  230 + // Frequency string decoding
  231 + if(unitString.startsWith('k'))
  232 + return value * 1e3;
  233 + else if(unitString.startsWith('M'))
  234 + return value * 1e6;
  235 + else if(unitString.startsWith('G'))
  236 + return value * 1e9;
  237 + else
  238 + return value;
  239 +
  240 + case UNIT_SAMPLES:
  241 + // Sample count string decoding
  242 + if(unitString.startsWith('k'))
  243 + return value * 1e3;
  244 + else if(unitString.startsWith('M'))
  245 + return value * 1e6;
  246 + else if(unitString.startsWith('G'))
  247 + return value * 1e9;
  248 + else
  249 + return value;
  250 +
  251 + default:
  252 + if(ok)
  253 + *ok = false;
  254 + return value;
  255 + }
  256 + }
  257 +
146 #ifdef DEBUG 258 #ifdef DEBUG
147 /// \brief Returns the hex dump for the given data. 259 /// \brief Returns the hex dump for the given data.
148 /// \param data Pointer to the data bytes that should be dumped. 260 /// \param data Pointer to the data bytes that should be dumped.
openhantek/src/helper.h
@@ -64,7 +64,9 @@ namespace Helper { @@ -64,7 +64,9 @@ namespace Helper {
64 }; 64 };
65 65
66 QString libUsbErrorString(int error); 66 QString libUsbErrorString(int error);
  67 +
67 QString valueToString(double value, Unit unit, int precision = -1); 68 QString valueToString(double value, Unit unit, int precision = -1);
  69 + double stringToValue(const QString &text, Unit unit, bool *ok = 0);
68 70
69 #ifdef DEBUG 71 #ifdef DEBUG
70 QString hexDump(unsigned char *data, unsigned int length); 72 QString hexDump(unsigned char *data, unsigned int length);