DlgUnitsCalculator: tweaks requested by vocx-fc and luzpaz

This commit is contained in:
donovaly
2019-12-02 23:19:14 +01:00
committed by Yorik van Havre
parent a82ba07886
commit a0f5206a2e
2 changed files with 36 additions and 36 deletions

View File

@@ -25,7 +25,7 @@
</size>
</property>
<property name="toolTip">
<string>input the source value and unit</string>
<string>Input the source value and unit</string>
</property>
</widget>
</item>
@@ -45,7 +45,7 @@
</size>
</property>
<property name="toolTip">
<string>specify here the result unit</string>
<string>Input here the unit for the result</string>
</property>
<property name="text">
<string/>
@@ -68,7 +68,7 @@
</size>
</property>
<property name="toolTip">
<string>result</string>
<string>Result</string>
</property>
<property name="readOnly">
<bool>true</bool>
@@ -144,7 +144,7 @@ To add a calculation press Return in the value input field</string>
<item>
<widget class="QPushButton" name="pushButton_Copy">
<property name="toolTip">
<string>copies result into the clipboard</string>
<string>Copy the result into the clipboard</string>
</property>
<property name="text">
<string>Copy</string>

View File

@@ -62,9 +62,9 @@ DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WindowFlags fl )
connect(ui->UnitInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString)));
ui->ValueInput->setParamGrpPath(QByteArray("User parameter:BaseApp/History/UnitsCalculator"));
// set a default that also illustrates how the dialog works
ui->ValueInput->setText(QString::fromLatin1("1 cm"));
ui->UnitInput->setText(QString::fromLatin1("in"));
// set a default that also illustrates how the dialog works
ui->ValueInput->setText(QString::fromLatin1("1 cm"));
ui->UnitInput->setText(QString::fromLatin1("in"));
units << Base::Unit::Length << Base::Unit::Mass << Base::Unit::Angle << Base::Unit::Density
<< Base::Unit::Area << Base::Unit::Volume << Base::Unit::TimeSpan << Base::Unit::Frequency
@@ -98,40 +98,40 @@ void DlgUnitsCalculator::reject()
void DlgUnitsCalculator::textChanged(QString unit)
{
valueChanged(actValue);
valueChanged(actValue);
}
void DlgUnitsCalculator::valueChanged(const Base::Quantity& quant)
{
// first check the unit, if it is invalid, getTypeString() outputs an empty string
// first check the unit, if it is invalid, getTypeString() outputs an empty string
if (Base::Unit(ui->UnitInput->text()).getTypeString().isEmpty()) {
ui->ValueOutput->setText(tr("unknown unit: ") + ui->UnitInput->text());
ui->pushButton_Copy->setEnabled(false);
} else { // the unit is valid
// we can only convert units of the same type, thus check
if (Base::Unit(ui->UnitInput->text()).getTypeString() != quant.getUnit().getTypeString()) {
ui->ValueOutput->setText(tr("unit mismatch"));
ui->pushButton_Copy->setEnabled(false);
} else { // the unit is valid and has the same type
double convertValue = Base::Quantity::parse(QString::fromLatin1("1") + ui->UnitInput->text()).getValue();
// we got now e.g. for "1 in" the value '25.4' because 1 in = 25.4 mm
// the result is now just quant / convertValue because the input is always in a base unit
// (an input of "1 cm" will immediately be converted to "10 mm" by Gui::InputField of the dialog)
double value = quant.getValue() / convertValue;
// determine how many decimals we will need to avoid an output like "0.00"
// at first use scientific notation, if there is no "e", we can round it to the user-defined decimals,
// but the user-defined decimals might be too low for cases like "10 um" in "in",
// thus only if value > 0.005 because FC's default are 2 decimals
QString val = QLocale::system().toString(value, 'g');
if (!val.contains(QChar::fromLatin1('e')) && (value > 0.005))
val = QLocale::system().toString(value, 'f', Base::UnitsApi::getDecimals());
// create the output string
QString out = QString::fromLatin1("%1 %2").arg(val, ui->UnitInput->text());
ui->ValueOutput->setText(out);
ui->pushButton_Copy->setEnabled(true);
}
}
// store the input value
ui->ValueOutput->setText(tr("unknown unit: ") + ui->UnitInput->text());
ui->pushButton_Copy->setEnabled(false);
} else { // the unit is valid
// we can only convert units of the same type, thus check
if (Base::Unit(ui->UnitInput->text()).getTypeString() != quant.getUnit().getTypeString()) {
ui->ValueOutput->setText(tr("unit mismatch"));
ui->pushButton_Copy->setEnabled(false);
} else { // the unit is valid and has the same type
double convertValue = Base::Quantity::parse(QString::fromLatin1("1") + ui->UnitInput->text()).getValue();
// we got now e.g. for "1 in" the value '25.4' because 1 in = 25.4 mm
// the result is now just quant / convertValue because the input is always in a base unit
// (an input of "1 cm" will immediately be converted to "10 mm" by Gui::InputField of the dialog)
double value = quant.getValue() / convertValue;
// determine how many decimals we will need to avoid an output like "0.00"
// at first use scientific notation, if there is no "e", we can round it to the user-defined decimals,
// but the user-defined decimals might be too low for cases like "10 um" in "in",
// thus only if value > 0.005 because FC's default are 2 decimals
QString val = QLocale::system().toString(value, 'g');
if (!val.contains(QChar::fromLatin1('e')) && (value > 0.005))
val = QLocale::system().toString(value, 'f', Base::UnitsApi::getDecimals());
// create the output string
QString out = QString::fromLatin1("%1 %2").arg(val, ui->UnitInput->text());
ui->ValueOutput->setText(out);
ui->pushButton_Copy->setEnabled(true);
}
}
// store the input value
actValue = quant;
}