| 1 | /*************************************************************************** |
| 2 | hydrometertool.h |
| 3 | ------------------- |
| 4 | A hydrometer correction utility for QBrew |
| 5 | ------------------- |
| 6 | Copyright 2004-2008, David Johnson <[email protected]> |
| 7 | Based on code Copyright 2001, Abe Kabakoff <[email protected]> |
| 8 | Please see the header file for copyright and license information |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | #include <QLabel> |
| 12 | |
| 13 | #include "resource.h" |
| 14 | #include "data.h" |
| 15 | #include "hydrometertool.h" |
| 16 | |
| 17 | using Resource::TITLE; |
| 18 | |
| 19 | ////////////////////////////////////////////////////////////////////////////// |
| 20 | // HydrometerTool() |
| 21 | // ---------------- |
| 22 | // Constructor |
| 23 | |
| 24 | HydrometerTool:: HydrometerTool(QWidget* parent) |
| 25 | : QDialog(parent) |
| 26 | { |
| 27 | ui.setupUi(this); |
| 28 | |
| 29 | QString SUFFIX = QString(" %1%2") |
| 30 | .arg(Resource::DEGREE) |
| 31 | .arg(Data::instance()->defaultTempUnit().symbol()); |
| 32 | |
| 33 | setWindowTitle(TITLE + tr(" - Hydrometer Tool")); |
| 34 | |
| 35 | // additional setup |
| 36 | QFont fnt(font()); |
| 37 | fnt.setBold(true); |
| 38 | ui.corrected->setFont(fnt); |
| 39 | |
| 40 | ui.calibrated->setSuffix(SUFFIX); |
| 41 | ui.sample->setSuffix(SUFFIX); |
| 42 | |
| 43 | // convert UI properties of celsius |
| 44 | if (Data::instance()->defaultTempUnit() == Temperature::celsius) { |
| 45 | ui.calibrated->setMinimum(0.0); |
| 46 | ui.calibrated->setMaximum(100.0); |
| 47 | ui.calibrated->setValue(15.0); |
| 48 | ui.sample->setMinimum(0.0); |
| 49 | ui.sample->setMaximum(100.0); |
| 50 | ui.sample->setValue(15.0); |
| 51 | } |
| 52 | |
| 53 | // connections |
| 54 | connect(ui.sample, SIGNAL(valueChanged(double)), this, SLOT(recalc())); |
| 55 | connect(ui.calibrated, SIGNAL(valueChanged(double)), this, SLOT(recalc())); |
| 56 | connect(ui.reading, SIGNAL(valueChanged(double)), this, SLOT(recalc())); |
| 57 | |
| 58 | recalc(); |
| 59 | } |
| 60 | |
| 61 | ////////////////////////////////////////////////////////////////////////////// |
| 62 | // recalc() |
| 63 | // -------- |
| 64 | // the signal to calculate the actual SG |
| 65 | |
| 66 | void HydrometerTool::recalc() |
| 67 | { |
| 68 | // see http://www.hbd.org/brewery/library/HydromCorr0992.html |
| 69 | |
| 70 | const double COEFF1 = 1.313454; |
| 71 | const double COEFF2 = 0.132674; |
| 72 | const double COEFF3 = 2.057793E-3; |
| 73 | const double COEFF4 = 2.627634E-6; |
| 74 | |
| 75 | double calterm; // E is the correction factor for the calibration temp |
| 76 | double corr; // the corrected reading |
| 77 | |
| 78 | double samp = ui.sample->value(); |
| 79 | double cal = ui.calibrated->value(); |
| 80 | double read = ui.reading->value(); |
| 81 | |
| 82 | // calc needs to be in fahrenheit |
| 83 | if (Data::instance()->defaultTempUnit() == Temperature::celsius) { |
| 84 | samp = celsius2fahrenheit(samp); |
| 85 | cal = celsius2fahrenheit(cal); |
| 86 | } |
| 87 | |
| 88 | // Correction(@59F) = 1.313454 - 0.132674*T + 2.057793e-3*T**2 - 2.627634e-6*T**3 |
| 89 | // the equation is set up for 59F (15C) |
| 90 | calterm = (COEFF1 |
| 91 | - (COEFF2 * cal) |
| 92 | + (COEFF3 * (cal*cal)) |
| 93 | - (COEFF4 * (cal*cal*cal))); |
| 94 | corr = (COEFF1 |
| 95 | - (COEFF2 * samp) |
| 96 | + (COEFF3 * (samp*samp)) |
| 97 | - (COEFF4 * (samp*samp*samp))) - calterm; |
| 98 | |
| 99 | corr /= 1000.0; |
| 100 | corr = 1.0 - corr; |
| 101 | corr = read / corr; |
| 102 | |
| 103 | // now that we have the value change the reading |
| 104 | ui.corrected->setText(QString::number(corr, 'f', 3)); |
| 105 | } |