Skip to content

Commit

Permalink
Gui: [skip ci] reduce confusion m vs. mm in units calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Dec 20, 2019
1 parent 43e5933 commit 702ae02
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Gui/DlgUnitsCalculatorImp.cpp
Expand Up @@ -187,8 +187,15 @@ void DlgUnitsCalculator::returnPressed(void)

void DlgUnitsCalculator::on_unitsBox_activated(int index)
{
ui->quantitySpinBox->setValue(1.0);
ui->quantitySpinBox->setUnit(units[index]);
// SI units use [m], not [mm] for lengths
//
Base::Quantity q = ui->quantitySpinBox->value();
int32_t old = q.getUnit().getSignature().Length;
double value = q.getValue();

Base::Unit unit = units[index];
int32_t len = unit.getSignature().Length;
ui->quantitySpinBox->setValue(Base::Quantity(value * std::pow(10.0, 3*(len-old)), unit));
}

void DlgUnitsCalculator::on_comboBoxScheme_activated(int index)
Expand Down

3 comments on commit 702ae02

@berniev
Copy link
Contributor

@berniev berniev commented on 702ae02 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wwmayer I know this is old, so forgive me! Am working through Unit.
Is this PR a workaround for what is really a problem somewhere else?

@wwmayer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is specific to this dialog. With the old implementation if the value of the quantity was "2.4 m" and you changed the unit then the quantity was always reset to 1.0 + the chosen which wasn't very helpful.
Now it keeps the old value and replaces the unit. So, it the quantity is e.g. 2.4 m and you change the unit from length to area then it should become 2.4 m^2. But because internally FreeCAD uses mm instead of m for lengths the value must be multiplied with a factor.

@berniev
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.

Having said that, I spent some time trying to figure out Unit and its mates. It did my head in. I came away thinking "there must be a better way". The amount of repetiteos (Sp?) code makes me think a more structured solution is needed. One for another day.

Please sign in to comment.