Brewtarget / brewtarget

Main brewtarget source code repository.
GNU General Public License v3.0
312 stars 135 forks source link

Refactor Unit, UnitSystem etc code - groundwork for BeerJSON #631

Closed matty0ung closed 2 years ago

matty0ung commented 2 years ago

As mentioned in https://github.com/Brewtarget/brewtarget/discussions/621, this is a chunk of work to make Unit, UnitSystem and so on more consistent and extendable, which will help a lot in implementing support for BeerJSON (which requires us to support more different units in more different circumstances than we do right now).

I did the work first in Brewken (where you can see a bit more of the JSON groundwork) and in merging it across to Brewtarget, I have also brought along a fix for https://github.com/Brewtarget/brewtarget/issues/613 and a proper merge of https://github.com/Brewtarget/brewtarget/pull/435. I have also brought across the enhanced version of the Alcohol Tool (which allows you to do temperature correction and alcohol calculation in one step, saving time and removing rounding errors from the previous approach).

In doing the refactoring, I have made the pop-up menus for selecting per-field systems of measurement a bit more visible (you get a mouseover indication in a lot more cases and can now often left-click as well as right-click to get the menu). I have also fixed a bug where the scale sub pop-up menu would not be updated after you've changed a field's system of measurement.

I have removed one feature, which seemed unlikely to be used, but which could, if needed, be added in a future fix/enhancement. Although you can still select date format globally (from the Options dialog), you can no longer set the date format on a per-field basis. (This is because date format is not really a system of measurement!) It seemed unlikely to me that someone would want different date formats in different places, but if I am wrong about this, it would be small-ish work to reimplement per-field date format selection.

I have tried to do a fair bit of testing, and have added some initial unit tests for converting between measurements, but, as ever, please shout if you spot anything I have missed.

Although a lot of files are touched, the main changes are in the measurement directory & Measurement namespace. Unit and UnitSystem remain somewhat as before, but SystemOfMeasurement is now pulled out from Unit to be freestanding, and PhysicalQuantity is created. See comment in measurement/PhysicalQuantity.h for explanation of the structure. Amount is a new small wrapper class that makes it easy to pass quantity + units around (eg {5.0, Measurement::Units::pounds} is 5.0lb). This was helpful to me in debugging and will I think be useful in the future.

I've also pulled out a lot of measurement/formatting/localization/config stuff from brewtarget.h/brewtarget.cpp to put them in more appropriate places.