LibrePCB Developers Documentation
Ratio Class Reference

The Ratio class is used to represent a ratio number (e.g. 13.37%) More...

#include <ratio.h>

+ Collaboration diagram for Ratio:

Public Member Functions

constexpr Ratio () noexcept
 Default Constructor. More...
 
constexpr Ratio (const Ratio &ratio) noexcept
 Copy Constructor. More...
 
constexpr Ratio (qint32 ppm) noexcept
 Constructor with a ratio in PPM. More...
 
 ~Ratio ()=default
 Destructor. More...
 
void setRatioPpm (qint32 ppm) noexcept
 Set the ratio in PPM. More...
 
void setRatioPercent (int percent) noexcept
 Set the ratio in percent. More...
 
void setRatioPercent (qreal percent) noexcept
 Set the ratio in percent. More...
 
void setRatioNormalized (qreal normalized) noexcept
 Set the ratio as a normalized number. More...
 
void setRatioNormalized (const QString &normalized)
 Set the ratio as a normalized number, represented in a QString. More...
 
qint32 toPpm () const noexcept
 Get the ratio in PPM. More...
 
qreal toPercent () const noexcept
 Get the ratio in percent. More...
 
qreal toNormalized () const noexcept
 Get the ratio as a normalized number. More...
 
QString toNormalizedString () const noexcept
 Get the ratio as a normalized QString. More...
 
Ratiooperator= (const Ratio &rhs)
 
Ratiooperator+= (const Ratio &rhs)
 
Ratiooperator-= (const Ratio &rhs)
 
Ratiooperator*= (const Ratio &rhs)
 
Ratiooperator*= (qint32 rhs)
 
Ratiooperator/= (const Ratio &rhs)
 
Ratiooperator/= (qint32 rhs)
 
Ratio operator+ (const Ratio &rhs) const
 
Ratio operator- () const
 
Ratio operator- (const Ratio &rhs) const
 
Ratio operator* (const Ratio &rhs) const
 
Ratio operator* (qint32 rhs) const
 
Ratio operator/ (const Ratio &rhs) const
 
Ratio operator/ (qint32 rhs) const
 
Ratio operator% (const Ratio &rhs) const
 
constexpr bool operator> (const Ratio &rhs) const
 
constexpr bool operator> (qint32 rhs) const
 
constexpr bool operator< (const Ratio &rhs) const
 
constexpr bool operator< (qint32 rhs) const
 
constexpr bool operator>= (const Ratio &rhs) const
 
constexpr bool operator>= (qint32 rhs) const
 
constexpr bool operator<= (const Ratio &rhs) const
 
constexpr bool operator<= (qint32 rhs) const
 
constexpr bool operator== (const Ratio &rhs) const
 
constexpr bool operator== (qint32 rhs) const
 
constexpr bool operator!= (const Ratio &rhs) const
 
constexpr bool operator!= (qint32 rhs) const
 
 operator bool () const
 

Static Public Member Functions

static Ratio fromPercent (int percent) noexcept
 Get a Ratio object with a specific ratio. More...
 
static Ratio fromPercent (qreal percent) noexcept
 Get a Ratio object with a specific ratio. More...
 
static Ratio fromNormalized (qreal normalized) noexcept
 Get a Ratio object with a specific ratio. More...
 
static Ratio fromNormalized (const QString &normalized)
 Get a Ratio object with a specific ratio. More...
 

Static Private Member Functions

static qint32 normalizedStringToPpm (const QString &normalized)
 Convert a normalized ratio from a QString to an integer (in PPM) More...
 

Private Attributes

qint32 mPpm
 the ratio in PPM More...
 

Detailed Description

The Ratio class is used to represent a ratio number (e.g. 13.37%)

Constructor & Destructor Documentation

◆ Ratio() [1/3]

constexpr Ratio ( )
inlineconstexprnoexcept

Default Constructor.

+ Here is the caller graph for this function:

◆ Ratio() [2/3]

constexpr Ratio ( const Ratio ratio)
inlineconstexprnoexcept

Copy Constructor.

Parameters
ratioAnother Ratio object

◆ Ratio() [3/3]

constexpr Ratio ( qint32  ppm)
inlineexplicitconstexprnoexcept

Constructor with a ratio in PPM.

Parameters
ppmThe ratio in PPM

◆ ~Ratio()

~Ratio ( )
default

Destructor.

Member Function Documentation

◆ setRatioPpm()

void setRatioPpm ( qint32  ppm)
inlinenoexcept

Set the ratio in PPM.

Parameters
ppmThe ratio in PPM

◆ setRatioPercent() [1/2]

void setRatioPercent ( int  percent)
inlinenoexcept

Set the ratio in percent.

Parameters
percentThe ratio in percent
+ Here is the caller graph for this function:

◆ setRatioPercent() [2/2]

void setRatioPercent ( qreal  percent)
inlinenoexcept

Set the ratio in percent.

Parameters
percentThe ratio in percent
Warning
To set an exact value, please use the overload setRatioPercent(int) instead.

◆ setRatioNormalized() [1/2]

void setRatioNormalized ( qreal  normalized)
inlinenoexcept

Set the ratio as a normalized number.

Parameters
normalizedThe normalized ratio
Warning
If you want to set the ratio exactly to common values like 0%, 50% or 100%, you should not use this method. Please use setRatioPercent(int) or setRatioPpm() instead because it is more accurate (no use of floating point numbers). Or you can also use the static method setRatioPercent(int).
+ Here is the caller graph for this function:

◆ setRatioNormalized() [2/2]

void setRatioNormalized ( const QString &  normalized)
inline

Set the ratio as a normalized number, represented in a QString.

This method is useful to read ratios from files (deserialization).

Parameters
normalizedSee fromNormalized(const QString&)
Exceptions
ExceptionIf the argument is invalid, an Exception will be thrown
+ Here is the call graph for this function:

◆ toPpm()

qint32 toPpm ( ) const
inlinenoexcept

Get the ratio in PPM.

Returns
The ratio in PPM
+ Here is the caller graph for this function:

◆ toPercent()

qreal toPercent ( ) const
inlinenoexcept

Get the ratio in percent.

Returns
The ratio in percent
+ Here is the caller graph for this function:

◆ toNormalized()

qreal toNormalized ( ) const
inlinenoexcept

Get the ratio as a normalized number.

Returns
The normalized ratio

◆ toNormalizedString()

QString toNormalizedString ( ) const
noexcept

Get the ratio as a normalized QString.

Returns
The normalized ratio as a QString
Note
This method is useful to store ratios in files (serialization).
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fromPercent() [1/2]

Ratio fromPercent ( int  percent)
staticnoexcept

Get a Ratio object with a specific ratio.

Parameters
percentSee setRatioPercent()
Returns
A new Ratio object with the specified ratio
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fromPercent() [2/2]

Ratio fromPercent ( qreal  percent)
staticnoexcept

Get a Ratio object with a specific ratio.

Parameters
percentSee setRatioPercent()
Returns
A new Ratio object with the specified ratio
+ Here is the call graph for this function:

◆ fromNormalized() [1/2]

Ratio fromNormalized ( qreal  normalized)
staticnoexcept

Get a Ratio object with a specific ratio.

Parameters
normalizedSee setRatioNormalized(qreal)
Returns
A new Ratio object with the specified ratio
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fromNormalized() [2/2]

Ratio fromNormalized ( const QString &  normalized)
static

Get a Ratio object with a specific ratio.

This method can be used to create a Ratio object from a QString which contains a normalized floating point number, like QString("0.1234") for 12.34 percent. The string must not depend on the locale settings (see QLocale), it have always to represent a number in the "C" locale. The maximum count of decimals after the decimal point is 6, because the 6th decimal represents one ppm.

Parameters
normalizedSee setRatioNormalized(const QString&)
Returns
A new Ratio object with the specified ratio
Exceptions
ExceptionIf the argument is invalid, an Exception will be thrown
+ Here is the call graph for this function:

◆ operator=()

Ratio & operator= ( const Ratio rhs)
inline

◆ operator+=()

Ratio & operator+= ( const Ratio rhs)
inline

◆ operator-=()

Ratio & operator-= ( const Ratio rhs)
inline

◆ operator*=() [1/2]

Ratio & operator*= ( const Ratio rhs)
inline

◆ operator*=() [2/2]

Ratio & operator*= ( qint32  rhs)
inline

◆ operator/=() [1/2]

Ratio & operator/= ( const Ratio rhs)
inline

◆ operator/=() [2/2]

Ratio & operator/= ( qint32  rhs)
inline

◆ operator+()

Ratio operator+ ( const Ratio rhs) const
inline
+ Here is the call graph for this function:

◆ operator-() [1/2]

Ratio operator- ( ) const
inline
+ Here is the call graph for this function:

◆ operator-() [2/2]

Ratio operator- ( const Ratio rhs) const
inline
+ Here is the call graph for this function:

◆ operator*() [1/2]

Ratio operator* ( const Ratio rhs) const
inline
+ Here is the call graph for this function:

◆ operator*() [2/2]

Ratio operator* ( qint32  rhs) const
inline
+ Here is the call graph for this function:

◆ operator/() [1/2]

Ratio operator/ ( const Ratio rhs) const
inline
+ Here is the call graph for this function:

◆ operator/() [2/2]

Ratio operator/ ( qint32  rhs) const
inline
+ Here is the call graph for this function:

◆ operator%()

Ratio operator% ( const Ratio rhs) const
inline
+ Here is the call graph for this function:

◆ operator>() [1/2]

constexpr bool operator> ( const Ratio rhs) const
inlineconstexpr

◆ operator>() [2/2]

constexpr bool operator> ( qint32  rhs) const
inlineconstexpr

◆ operator<() [1/2]

constexpr bool operator< ( const Ratio rhs) const
inlineconstexpr

◆ operator<() [2/2]

constexpr bool operator< ( qint32  rhs) const
inlineconstexpr

◆ operator>=() [1/2]

constexpr bool operator>= ( const Ratio rhs) const
inlineconstexpr

◆ operator>=() [2/2]

constexpr bool operator>= ( qint32  rhs) const
inlineconstexpr

◆ operator<=() [1/2]

constexpr bool operator<= ( const Ratio rhs) const
inlineconstexpr

◆ operator<=() [2/2]

constexpr bool operator<= ( qint32  rhs) const
inlineconstexpr

◆ operator==() [1/2]

constexpr bool operator== ( const Ratio rhs) const
inlineconstexpr

◆ operator==() [2/2]

constexpr bool operator== ( qint32  rhs) const
inlineconstexpr

◆ operator!=() [1/2]

constexpr bool operator!= ( const Ratio rhs) const
inlineconstexpr

◆ operator!=() [2/2]

constexpr bool operator!= ( qint32  rhs) const
inlineconstexpr

◆ operator bool()

operator bool ( ) const
inlineexplicit

◆ normalizedStringToPpm()

qint32 normalizedStringToPpm ( const QString &  normalized)
staticprivate

Convert a normalized ratio from a QString to an integer (in PPM)

This is a helper function for Ratio(const QString&) and fromNormalized(const QString&));.

Parameters
normalizedA QString which contains a floating point number with maximum six decimals after the decimal point. The locale of the string have to be "C"! Example: QString("-0.1234") for -12.34 percent.
Returns
The ratio in PPM
+ Here is the caller graph for this function:

Member Data Documentation

◆ mPpm

qint32 mPpm
private

the ratio in PPM


The documentation for this class was generated from the following files: