Skip to content

Commit

Permalink
Fix the broken UnitsCalculator dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
donovaly authored and yorikvanhavre committed Dec 6, 2019
1 parent 0e96abb commit fafbe53
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
30 changes: 21 additions & 9 deletions src/Gui/DlgUnitsCalculator.ui
Expand Up @@ -24,6 +24,9 @@
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>input the source value and unit</string>
</property>
</widget>
</item>
<item>
Expand All @@ -34,13 +37,19 @@
</widget>
</item>
<item>
<widget class="Gui::InputField" name="UnitInput">
<widget class="QLineEdit" name="UnitInput">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>specify here the result unit</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
Expand All @@ -58,6 +67,9 @@
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>result</string>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
Expand All @@ -67,6 +79,10 @@
</item>
<item row="1" column="0">
<widget class="QTextEdit" name="textEdit">
<property name="toolTip">
<string>List of last used calculations
To add a calculation press Return in the value input field</string>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
Expand All @@ -82,7 +98,7 @@
</widget>
</item>
<item>
<widget class="Gui::QuantitySpinBox" name="quantitySpinBox">
<widget class="Gui::QuantitySpinBox" name="quantitySpinBox" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
Expand Down Expand Up @@ -112,13 +128,6 @@
</item>
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton_Help">
<property name="text">
<string>Help</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand All @@ -134,6 +143,9 @@
</item>
<item>
<widget class="QPushButton" name="pushButton_Copy">
<property name="toolTip">
<string>copies result into the clipboard</string>
</property>
<property name="text">
<string>Copy</string>
</property>
Expand Down
68 changes: 38 additions & 30 deletions src/Gui/DlgUnitsCalculatorImp.cpp
Expand Up @@ -51,19 +51,20 @@ DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WindowFlags fl )
this->setAttribute(Qt::WA_DeleteOnClose);

connect(ui->ValueInput, SIGNAL(valueChanged(Base::Quantity)), this, SLOT(valueChanged(Base::Quantity)));
connect(ui->ValueInput, SIGNAL(returnPressed () ), this, SLOT(returnPressed()));
connect(ui->UnitInput, SIGNAL(valueChanged(Base::Quantity)), this, SLOT(unitValueChanged(Base::Quantity)));
connect(ui->ValueInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(ui->UnitInput, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
connect(ui->UnitInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));

connect(ui->pushButton_Help, SIGNAL(clicked()), this, SLOT(help()));
connect(ui->pushButton_Close, SIGNAL(clicked()), this, SLOT(accept()));
connect(ui->pushButton_Copy, SIGNAL(clicked()), this, SLOT(copy()));

connect(ui->ValueInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString)));
connect(ui->UnitInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString)));

ui->ValueInput->setParamGrpPath(QByteArray("User parameter:BaseApp/History/UnitsCalculator"));
actUnit.setInvalid();
// 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
Expand All @@ -72,7 +73,7 @@ DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WindowFlags fl )
<< Base::Unit::AmountOfSubstance << Base::Unit::LuminousIntensity << Base::Unit::Stress
<< Base::Unit::Pressure << Base::Unit::Force << Base::Unit::Work << Base::Unit::Power
<< Base::Unit::ThermalConductivity << Base::Unit::ThermalExpansionCoefficient
<< Base::Unit::SpecificHeat << Base::Unit::ThermalTransferCoefficient <<Base::Unit::HeatFlux;
<< Base::Unit::SpecificHeat << Base::Unit::ThermalTransferCoefficient << Base::Unit::HeatFlux;
for (QList<Base::Unit>::iterator it = units.begin(); it != units.end(); ++it) {
ui->unitsBox->addItem(it->getTypeString());
}
Expand All @@ -95,31 +96,43 @@ void DlgUnitsCalculator::reject()
QDialog::reject();
}

void DlgUnitsCalculator::unitValueChanged(const Base::Quantity& unit)
void DlgUnitsCalculator::textChanged(QString unit)
{
actUnit = unit;
valueChanged(actValue);
valueChanged(actValue);
}

void DlgUnitsCalculator::valueChanged(const Base::Quantity& quant)
{
if (actUnit.isValid()) {
if (actUnit.getUnit() != quant.getUnit()) {
ui->ValueOutput->setText(tr("Unit mismatch"));
ui->pushButton_Copy->setEnabled(false);
} else {
double value = quant.getValue()/actUnit.getValue();
QString val = QLocale::system().toString(value, 'f', Base::UnitsApi::getDecimals());
QString out = QString::fromLatin1("%1 %2").arg(val, ui->UnitInput->text());
ui->ValueOutput->setText(out);
ui->pushButton_Copy->setEnabled(true);
}
} else {
//ui->ValueOutput->setValue(quant);
ui->ValueOutput->setText(quant.getUserString());
ui->pushButton_Copy->setEnabled(true);
}

// 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
Base::Quantity inputt = Base::Quantity(1.0, ui->UnitInput->text());
// this gives us e.g. for "1 in" the value '25.4' because 1 in = 25.4 mm
double convertValue = Base::Quantity::parse(QString::fromLatin1("1") + ui->UnitInput->text()).getValue();
// the result is now just input / convertValue because the imput is always in a base unit
// (an input of "1 cm" will immediately be converted to "10 mm" by Gui::InputField from 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
// 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;
}

Expand All @@ -135,11 +148,6 @@ void DlgUnitsCalculator::copy(void)
cb->setText(ui->ValueOutput->text());
}

void DlgUnitsCalculator::help(void)
{
//TODO: call help page Std_UnitsCalculator
}

void DlgUnitsCalculator::returnPressed(void)
{
if (ui->pushButton_Copy->isEnabled()) {
Expand Down
4 changes: 1 addition & 3 deletions src/Gui/DlgUnitsCalculatorImp.h
Expand Up @@ -50,19 +50,17 @@ class DlgUnitsCalculator : public QDialog
void reject();

protected Q_SLOTS:
void unitValueChanged(const Base::Quantity&);
void textChanged(const QString);
void valueChanged(const Base::Quantity&);
void on_unitsBox_activated(int);

void copy(void);
void help(void);
void returnPressed(void);

void parseError(const QString& errorText);

private:
Base::Quantity actValue;
Base::Quantity actUnit;
std::unique_ptr<Ui_DlgUnitCalculator> ui;
QList<Base::Unit> units;
};
Expand Down

0 comments on commit fafbe53

Please sign in to comment.