Debian Codegraph @

src/Gui/Dialogs/DlgUnitsCalculatorImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2013 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QLocale>
26
27 #include "Dialogs/DlgUnitsCalculatorImp.h"
28 #include "ui_DlgUnitsCalculator.h"
29 #include <Base/Quantity.h>
30 #include <Base/UnitsApi.h>
31
32 using namespace Gui::Dialog;
33 using Base::Quantity;
34 using Base::Unit;
35 using Base::UnitsApi;
36
37 /* TRANSLATOR Gui::Dialog::DlgUnitsCalculator */
38
39 /**
40 * Constructs a DlgUnitsCalculator which is a child of 'parent', with the
41 * name 'name' and widget flags set to 'f'
42 *
43 * The dialog will by default be modeless, unless you set 'modal' to
44 * true to construct a modal dialog.
45 */
46 DlgUnitsCalculator::DlgUnitsCalculator(QWidget* parent, Qt::WindowFlags fl)
47 : QDialog(parent, fl)
48 , ui(new Ui_DlgUnitCalculator)
49 {
50 // create widgets
51 ui->setupUi(this);
52 this->setAttribute(Qt::WA_DeleteOnClose);
53
54 ui->comboBoxScheme->addItem(QStringLiteral("Preference system"), -1);
55 auto addItem = [&, index {0}](const auto& item) mutable {
56 ui->comboBoxScheme->addItem(QString::fromStdString(item), index++);
57 };
58 auto descriptions = UnitsApi::getDescriptions();
59 std::for_each(descriptions.begin(), descriptions.end(), addItem);
60
61 // clang-format off
62 connect(ui->unitsBox, qOverload<int>(&QComboBox::activated),
63 this, &DlgUnitsCalculator::onUnitsBoxActivated);
64 connect(ui->comboBoxScheme, qOverload<int>(&QComboBox::activated),
65 this, &DlgUnitsCalculator::onComboBoxSchemeActivated);
66 connect(ui->spinBoxDecimals, qOverload<int>(&QSpinBox::valueChanged),
67 this, &DlgUnitsCalculator::onSpinBoxDecimalsValueChanged);
68 connect(ui->ValueInput, qOverload<const Quantity&>(&InputField::valueChanged),
69 this, &DlgUnitsCalculator::valueChanged);
70 connect(ui->ValueInput, &InputField::returnPressed,
71 this, &DlgUnitsCalculator::returnPressed);
72 connect(ui->ValueInput, &InputField::parseError,
73 this, &DlgUnitsCalculator::parseError);
74 connect(ui->UnitInput, &QLineEdit::textChanged,
75 this, &DlgUnitsCalculator::textChanged);
76 connect(ui->UnitInput, &QLineEdit::returnPressed,
77 this, &DlgUnitsCalculator::returnPressed);
78 connect(ui->pushButton_Close, &QPushButton::clicked,
79 this, &DlgUnitsCalculator::accept);
80 connect(ui->pushButton_Copy, &QPushButton::clicked,
81 this, &DlgUnitsCalculator::copy);
82 // clang-format on
83
84 ui->ValueInput->setParamGrpPath(QByteArray("User parameter:BaseApp/History/UnitsCalculator"));
85 // set a default that also illustrates how the dialog works
86 ui->ValueInput->setText(QStringLiteral("1 cm"));
87 ui->UnitInput->setText(QStringLiteral("in"));
88
89 units << Unit::Acceleration << Unit::AmountOfSubstance << Unit::Angle << Unit::Area
90 << Unit::Density << Unit::CurrentDensity << Unit::DissipationRate << Unit::DynamicViscosity
91 << Unit::ElectricalCapacitance << Unit::ElectricalInductance << Unit::ElectricalConductance
92 << Unit::ElectricalResistance << Unit::ElectricalConductivity << Unit::ElectricCharge
93 << Unit::ElectricCurrent << Unit::ElectricPotential << Unit::Force << Unit::Frequency
94 << Unit::HeatFlux << Unit::InverseArea << Unit::InverseLength << Unit::InverseVolume
95 << Unit::KinematicViscosity << Unit::Length << Unit::LuminousIntensity << Unit::Mass
96 << Unit::MagneticFieldStrength << Unit::MagneticFlux << Unit::MagneticFluxDensity
97 << Unit::Magnetization << Unit::Power << Unit::Pressure << Unit::SpecificEnergy
98 << Unit::SpecificHeat << Unit::Stiffness << Unit::Temperature << Unit::ThermalConductivity
99 << Unit::ThermalExpansionCoefficient << Unit::ThermalTransferCoefficient << Unit::TimeSpan
100 << Unit::VacuumPermittivity << Unit::Velocity << Unit::Volume << Unit::VolumeFlowRate
101 << Unit::VolumetricThermalExpansionCoefficient << Unit::Work;
102 for (const Unit& it : units) {
103 ui->unitsBox->addItem(QString::fromStdString(it.getTypeString()));
104 }
105
106 ui->quantitySpinBox->setValue(1.0);
107 ui->quantitySpinBox->setUnit(units.front());
108 ui->spinBoxDecimals->setValue(UnitsApi::getDecimals());
109 }
110
111 /** Destroys the object and frees any allocated resources */
112 DlgUnitsCalculator::~DlgUnitsCalculator() = default;
113
114 void DlgUnitsCalculator::accept()
115 {
116 QDialog::accept();
117 }
118
119 void DlgUnitsCalculator::reject()
120 {
121 QDialog::reject();
122 }
123
124 void DlgUnitsCalculator::textChanged(QString unit)
125 {
126 Q_UNUSED(unit)
127 valueChanged(actValue);
128 }
129
130 void DlgUnitsCalculator::valueChanged(const Quantity& quant)
131 {
132 std::string unitTypeStr;
133 try {
134 unitTypeStr = Quantity::parse(ui->UnitInput->text().toStdString()).getUnit().getTypeString();
135 }
136 catch (const Base::ParserError&) {
137 }
138 // first check the unit, if it is invalid, getTypeString() outputs an empty string
139 // explicitly check for "ee" like in "eeV" because this would trigger an exception in Unit
140 // since it expects then a scientific notation number like "1e3"
141 if ((ui->UnitInput->text().mid(0, 2) == QStringLiteral("ee")) || unitTypeStr.empty()) {
142 ui->ValueOutput->setText(
143 QStringLiteral("%1 %2").arg(tr("unknown unit:"), ui->UnitInput->text())
144 );
145 ui->pushButton_Copy->setEnabled(false);
146 }
147 else { // the unit is valid
148 // we can only convert units of the same type, thus check
149 if (unitTypeStr != quant.getUnit().getTypeString()) {
150 ui->ValueOutput->setText(tr("unit mismatch"));
151 ui->pushButton_Copy->setEnabled(false);
152 }
153 else { // the unit is valid and has the same type
154 double convertValue = Quantity::parse("1" + ui->UnitInput->text().toStdString()).getValue();
155 // we got now e.g. for "1 in" the value '25.4' because 1 in = 25.4 mm
156 // the result is now just quant / convertValue because the input is always in a base
157 // unit (an input of "1 cm" will immediately be converted to "10 mm" by Gui::InputField
158 // of the dialog)
159 double value = quant.getValue() / convertValue;
160 // determine how many decimals we will need to avoid an output like "0.00"
161 // at first use scientific notation, if there is no "e", we can round it to the
162 // user-defined decimals, but the user-defined decimals might be too low for cases like
163 // "10 um" in "in", thus only if value > 0.005 because FC's default are 2 decimals
164 QString val = QLocale().toString(value, 'g');
165 if (!val.contains(QChar::fromLatin1('e')) && (value > 0.005)) {
166 val = QLocale().toString(value, 'f', UnitsApi::getDecimals());
167 }
168 // create the output string
169 QString out = QStringLiteral("%1 %2").arg(val, ui->UnitInput->text());
170 ui->ValueOutput->setText(out);
171 ui->pushButton_Copy->setEnabled(true);
172 }
173 }
174 // store the input value
175 actValue = quant;
176 }
177
178 void DlgUnitsCalculator::parseError(const QString& errorText)
179 {
180 ui->pushButton_Copy->setEnabled(false);
181 ui->ValueOutput->setText(errorText);
182 }
183
184 void DlgUnitsCalculator::copy()
185 {
186 QClipboard* cb = QApplication::clipboard();
187 cb->setText(ui->ValueOutput->text());
188 }
189
190 void DlgUnitsCalculator::returnPressed()
191 {
192 if (ui->pushButton_Copy->isEnabled()) {
193 ui->textEdit->append(ui->ValueInput->text() + QStringLiteral(" = ") + ui->ValueOutput->text());
194 ui->ValueInput->pushToHistory();
195 }
196 }
197
198 void DlgUnitsCalculator::onUnitsBoxActivated(int index)
199 {
200 // SI units use [m], not [mm] for lengths
201 //
202 Quantity q = ui->quantitySpinBox->value();
203 int32_t old = q.getUnit().length();
204 double value = q.getValue();
205
206 Unit unit = units[index];
207 int32_t len = unit.length();
208 ui->quantitySpinBox->setValue(Quantity(value * std::pow(10.0, 3 * (len - old)), unit));
209 }
210
211 void DlgUnitsCalculator::onComboBoxSchemeActivated(int index)
212 {
213 int item = ui->comboBoxScheme->itemData(index).toInt();
214 if (item > 0) {
215 ui->quantitySpinBox->setSchema(item);
216 }
217 else {
218 ui->quantitySpinBox->clearSchema();
219 }
220 }
221
222 void DlgUnitsCalculator::onSpinBoxDecimalsValueChanged(int value)
223 {
224 ui->quantitySpinBox->setDecimals(value);
225 }
226
227 #include "moc_DlgUnitsCalculatorImp.cpp"