The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful methods to make the life easier.
More...
#include <lengthunit.h>
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful methods to make the life easier.
With this class, lengths (librepcb::Length) and points (librepcb::Point) can be converted to other units.
- Note
- Please note that the classes librepcb::Length and librepcb::Point do not need a length unit as they represent the values always in nanometers! The class LengthUnit is only needed to show these values in the unit which the user wants, and provides some useful methods to do this.
- Warning
- It's possible to convert lengths and points between all available units. But as the converting methods convertFromUnit() and convertToUnit() work always with floating point numbers, there is a little risk that the conversion is not lossless! Example: If you begin with 1mm and convert via other units back to millimeters, you may get 0,999mm or 1,001mm as result. So be careful on converting lengths and points between different units!
◆ LengthUnit_t
An enum which contains all available length units.
The enum items should be sorted (not alphabetical but by meaning) because the enum order will also define the order of these units in comboboxes and other lists/widgets.
- Warning
- The enum must begin with value 0 and end with _COUNT. Between these values the enum must not contain unused indexes! This is necessary for getIndex() and fromIndex().
Enumerator |
---|
Millimeters | |
Micrometers | |
Nanometers | |
Inches | |
Mils | |
_COUNT | count of units, must be the last entry of the enum
|
◆ LengthUnit() [1/3]
Default constructor which uses millimeters as unit.
◆ LengthUnit() [2/3]
Copy constructor.
- Parameters
-
◆ ~LengthUnit()
◆ LengthUnit() [3/3]
|
inlineexplicitprivatenoexcept |
Private Constructor to create a LengthUnit object with a specific unit.
- Parameters
-
unit | The length unit of the new object |
◆ getIndex()
Get the Index of the length unit of this object.
This method is useful in combination with getAllUnits() to create lists of all available length units (QListWidget, QComboBox, ...). With this method you are able to get the index of this unit in the QList returned by getAllUnits().
- Warning
- The index of an unit can change between different application versions! So you must never save/load such an index to/from files.
- Returns
- The index
- See also
- fromIndex(), getAllUnits()
◆ toStr()
Serialize this object into a string.
- Returns
- This object as a string
◆ toStringTr()
QString toStringTr |
( |
| ) |
const |
|
noexcept |
Convert the length unit to a localized string.
This method uses the application's locale settings to translate the name of the length unit to the user's language.
- Returns
- The unit as a localized string (like "Millimeters" or "Millimeter")
◆ toShortStringTr()
QString toShortStringTr |
( |
| ) |
const |
|
noexcept |
Convert the length unit to a localized string (short form)
- Returns
- The unit as a localized short string (like "mm", "μm" or "″")
◆ format()
QString format |
( |
const Length & |
value, |
|
|
const QLocale & |
locale, |
|
|
const QString & |
separator = " " |
|
) |
| const |
|
noexcept |
Get a pretty formatted length value with this unit.
Uses up to getReasonableNumberOfDecimals() decimals, but without trailing zeros.
- Parameters
-
value | The value to format. |
locale | The locale to use for the number format. |
separator | Separator between value and unit (defaults to one space). |
- Returns
- Formatted string like "-0.5 mm".
◆ getReasonableNumberOfDecimals()
int getReasonableNumberOfDecimals |
( |
| ) |
const |
|
noexcept |
Get a reasonable number of decimals to be shown.
When displaying length values to the user, often it makes sense to limit the displayed number of decimal places. But since this number depends on the unit, this helper method is provided.
- Note
- The returned number of decimals will NOT be enough to represent all possiblle librepcb::Length values without losing precision! So a value with truncated number of decimal places may not be converted back to a librepcb::Length object since this might lead to a different value!
- Returns
- Reasonable number of decimals.
◆ getUserInputSuffixes()
QStringList getUserInputSuffixes |
( |
| ) |
const |
|
noexcept |
Get user input suffixes.
Returns a list of suffixes the user might use to represent this unit. For example "um" is a typical user input to mean Micrometers since "μm" is more difficult to write.
- Returns
- A list of user input suffixes
◆ convertToUnit() [1/2]
qreal convertToUnit |
( |
const Length & |
length | ) |
const |
|
noexcept |
Convert a Length to this length unit.
This method calls the method Length::to*() (* = the unit of this object)
- Parameters
-
length | The length to convert (the Length object will not be modified) |
- Returns
- The specified length in the unit of this object
- Warning
- As this method always returns a floating point number, there is a little risk that the conversion is not lossless. So be careful with it.
◆ convertToUnit() [2/2]
QPointF convertToUnit |
( |
const Point & |
point | ) |
const |
|
noexcept |
Convert a Point to this length unit.
This method calls the method Point::to*QPointF() (* = the unit of this object)
- Parameters
-
point | The point to convert (the Point object will not be modified) |
- Returns
- The specified point in the unit of this object
- Warning
- As this method always returns floating point numbers, there is a little risk that the conversion is not lossless. So be careful with it.
◆ convertFromUnit() [1/2]
Length convertFromUnit |
( |
qreal |
length | ) |
const |
Convert a floating point number with this unit to a Length object.
This method calls the method Length::from*() (* = the unit of this object)
- Parameters
-
length | A length in the unit of this object |
- Returns
- A Length object with the converted length
- Warning
- As this method always uses floating point numbers, there is a little risk that the conversion is not lossless. So be careful with it.
◆ convertFromUnit() [2/2]
Point convertFromUnit |
( |
const QPointF & |
point | ) |
const |
Convert floating point numbers with this unit to a Point object.
This method calls the method Point::from*() (* = the unit of this object)
- Parameters
-
point | A point in the unit of this object |
- Returns
- A Point object with the converted point
- Warning
- As this method always uses floating point numbers, there is a little risk that the conversion is not lossless. So be careful with it.
◆ fromString()
Get the length unit represented by a string.
- Parameters
-
str | The toStr() representation of the unit. |
- Returns
- The LengthUnit of the string.
- Exceptions
-
Exception | If the string did not contain a valid unit. |
◆ fromIndex()
◆ getAllUnits()
Get all available length units.
This method returns a list of all available length units. The index of the objects in the list equals to the value from getIndex() of them.
- Returns
- A list of all available length units
- See also
- getIndex(), fromIndex()
◆ millimeters()
◆ micrometers()
◆ nanometers()
◆ inches()
◆ mils()
◆ operator=()
◆ operator==()
◆ operator!=()
◆ mUnit
Holds the length unit of the object.
The documentation for this class was generated from the following files: