From a0f5206a2e44590484768ac8e55e889ac532a8fe Mon Sep 17 00:00:00 2001 From: donovaly Date: Mon, 2 Dec 2019 23:19:14 +0100 Subject: [PATCH] DlgUnitsCalculator: tweaks requested by vocx-fc and luzpaz --- src/Gui/DlgUnitsCalculator.ui | 8 ++-- src/Gui/DlgUnitsCalculatorImp.cpp | 64 +++++++++++++++---------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/Gui/DlgUnitsCalculator.ui b/src/Gui/DlgUnitsCalculator.ui index b7f3758f5a..2d74c12024 100644 --- a/src/Gui/DlgUnitsCalculator.ui +++ b/src/Gui/DlgUnitsCalculator.ui @@ -25,7 +25,7 @@ - input the source value and unit + Input the source value and unit @@ -45,7 +45,7 @@ - specify here the result unit + Input here the unit for the result @@ -68,7 +68,7 @@ - result + Result true @@ -144,7 +144,7 @@ To add a calculation press Return in the value input field - copies result into the clipboard + Copy the result into the clipboard Copy diff --git a/src/Gui/DlgUnitsCalculatorImp.cpp b/src/Gui/DlgUnitsCalculatorImp.cpp index c365fc321c..c836723f90 100644 --- a/src/Gui/DlgUnitsCalculatorImp.cpp +++ b/src/Gui/DlgUnitsCalculatorImp.cpp @@ -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; }