24
#include "PreCompiled.h"
26
# include <QApplication>
31
#include "DlgUnitsCalculatorImp.h"
32
#include "ui_DlgUnitsCalculator.h"
33
#include <Base/UnitsApi.h>
35
using namespace Gui::Dialog;
46
DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WindowFlags fl )
47
: QDialog(parent, fl), ui(new Ui_DlgUnitCalculator)
51
this->setAttribute(Qt::WA_DeleteOnClose);
53
ui->comboBoxScheme->addItem(QString::fromLatin1("Preference system"), static_cast<int>(-1));
54
int num = static_cast<int>(Base::UnitSystem::NumUnitSystemTypes);
55
for (int i=0; i<num; i++) {
56
QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(i));
57
ui->comboBoxScheme->addItem(item, i);
60
connect(ui->unitsBox, qOverload<int>(&QComboBox::activated),
61
this, &DlgUnitsCalculator::onUnitsBoxActivated);
62
connect(ui->comboBoxScheme, qOverload<int>(&QComboBox::activated),
63
this, &DlgUnitsCalculator::onComboBoxSchemeActivated);
64
connect(ui->spinBoxDecimals, qOverload<int>(&QSpinBox::valueChanged),
65
this, &DlgUnitsCalculator::onSpinBoxDecimalsValueChanged);
66
connect(ui->ValueInput, qOverload<const Base::Quantity&>(&InputField::valueChanged),
67
this, &DlgUnitsCalculator::valueChanged);
68
connect(ui->ValueInput, &InputField::returnPressed,
69
this, &DlgUnitsCalculator::returnPressed);
70
connect(ui->ValueInput, &InputField::parseError, this, &DlgUnitsCalculator::parseError);
71
connect(ui->UnitInput, &QLineEdit::textChanged, this, &DlgUnitsCalculator::textChanged);
72
connect(ui->UnitInput, &QLineEdit::returnPressed, this, &DlgUnitsCalculator::returnPressed);
73
connect(ui->pushButton_Close, &QPushButton::clicked, this, &DlgUnitsCalculator::accept);
74
connect(ui->pushButton_Copy, &QPushButton::clicked, this, &DlgUnitsCalculator::copy);
76
ui->ValueInput->setParamGrpPath(QByteArray("User parameter:BaseApp/History/UnitsCalculator"));
78
ui->ValueInput->setText(QString::fromLatin1("1 cm"));
79
ui->UnitInput->setText(QString::fromLatin1("in"));
81
units << Base::Unit::Acceleration
82
<< Base::Unit::AmountOfSubstance
85
<< Base::Unit::Density
86
<< Base::Unit::CurrentDensity
87
<< Base::Unit::DissipationRate
88
<< Base::Unit::DynamicViscosity
89
<< Base::Unit::ElectricalCapacitance
90
<< Base::Unit::ElectricalInductance
91
<< Base::Unit::ElectricalConductance
92
<< Base::Unit::ElectricalResistance
93
<< Base::Unit::ElectricalConductivity
94
<< Base::Unit::ElectricCharge
95
<< Base::Unit::ElectricCurrent
96
<< Base::Unit::ElectricPotential
98
<< Base::Unit::Frequency
99
<< Base::Unit::HeatFlux
100
<< Base::Unit::InverseArea
101
<< Base::Unit::InverseLength
102
<< Base::Unit::InverseVolume
103
<< Base::Unit::KinematicViscosity
104
<< Base::Unit::Length
105
<< Base::Unit::LuminousIntensity
107
<< Base::Unit::MagneticFieldStrength
108
<< Base::Unit::MagneticFlux
109
<< Base::Unit::MagneticFluxDensity
110
<< Base::Unit::Magnetization
112
<< Base::Unit::Pressure
113
<< Base::Unit::SpecificEnergy
114
<< Base::Unit::SpecificHeat
115
<< Base::Unit::Stiffness
116
<< Base::Unit::Temperature
117
<< Base::Unit::ThermalConductivity
118
<< Base::Unit::ThermalExpansionCoefficient
119
<< Base::Unit::ThermalTransferCoefficient
120
<< Base::Unit::TimeSpan
121
<< Base::Unit::VacuumPermittivity
122
<< Base::Unit::Velocity
123
<< Base::Unit::Volume
124
<< Base::Unit::VolumeFlowRate
125
<< Base::Unit::VolumetricThermalExpansionCoefficient
127
for (const Base::Unit& it : units) {
128
ui->unitsBox->addItem(it.getTypeString());
131
ui->quantitySpinBox->setValue(1.0);
132
ui->quantitySpinBox->setUnit(units.front());
133
ui->spinBoxDecimals->setValue(Base::UnitsApi::getDecimals());
137
DlgUnitsCalculator::~DlgUnitsCalculator() = default;
139
void DlgUnitsCalculator::accept()
144
void DlgUnitsCalculator::reject()
149
void DlgUnitsCalculator::textChanged(QString unit)
152
valueChanged(actValue);
155
void DlgUnitsCalculator::valueChanged(const Base::Quantity& quant)
160
if ( (ui->UnitInput->text().mid(0, 2) == QString::fromLatin1("ee")) ||
161
Base::Unit(ui->UnitInput->text()).getTypeString().isEmpty()) {
162
ui->ValueOutput->setText(QString::fromLatin1("%1 %2").arg(tr("unknown unit:"), ui->UnitInput->text()));
163
ui->pushButton_Copy->setEnabled(false);
166
if (Base::Unit(ui->UnitInput->text()).getTypeString() != quant.getUnit().getTypeString()) {
167
ui->ValueOutput->setText(tr("unit mismatch"));
168
ui->pushButton_Copy->setEnabled(false);
170
double convertValue = Base::Quantity::parse(QString::fromLatin1("1") + ui->UnitInput->text()).getValue();
174
double value = quant.getValue() / convertValue;
179
QString val = QLocale().toString(value, 'g');
180
if (!val.contains(QChar::fromLatin1('e')) && (value > 0.005))
181
val = QLocale().toString(value, 'f', Base::UnitsApi::getDecimals());
183
QString out = QString::fromLatin1("%1 %2").arg(val, ui->UnitInput->text());
184
ui->ValueOutput->setText(out);
185
ui->pushButton_Copy->setEnabled(true);
192
void DlgUnitsCalculator::parseError(const QString& errorText)
194
ui->pushButton_Copy->setEnabled(false);
195
ui->ValueOutput->setText(errorText);
198
void DlgUnitsCalculator::copy()
200
QClipboard *cb = QApplication::clipboard();
201
cb->setText(ui->ValueOutput->text());
204
void DlgUnitsCalculator::returnPressed()
206
if (ui->pushButton_Copy->isEnabled()) {
207
ui->textEdit->append(ui->ValueInput->text() + QString::fromLatin1(" = ") + ui->ValueOutput->text());
208
ui->ValueInput->pushToHistory();
212
void DlgUnitsCalculator::onUnitsBoxActivated(int index)
216
Base::Quantity q = ui->quantitySpinBox->value();
217
int32_t old = q.getUnit().getSignature().Length;
218
double value = q.getValue();
220
Base::Unit unit = units[index];
221
int32_t len = unit.getSignature().Length;
222
ui->quantitySpinBox->setValue(Base::Quantity(value * std::pow(10.0, 3*(len-old)), unit));
225
void DlgUnitsCalculator::onComboBoxSchemeActivated(int index)
227
int item = ui->comboBoxScheme->itemData(index).toInt();
229
ui->quantitySpinBox->setSchema(static_cast<Base::UnitSystem>(item));
231
ui->quantitySpinBox->clearSchema();
234
void DlgUnitsCalculator::onSpinBoxDecimalsValueChanged(int value)
236
ui->quantitySpinBox->setDecimals(value);
239
#include "moc_DlgUnitsCalculatorImp.cpp"