20 #ifndef LIBREPCB_CORE_LENGTH_H 21 #define LIBREPCB_CORE_LENGTH_H 26 #include "../exceptions.h" 27 #include "../serialization/sexpression.h" 29 #include <type_safe/constrained_type.hpp> 59 #ifdef USE_32BIT_LENGTH_UNITS 84 Q_DECLARE_TR_FUNCTIONS(
Length)
109 constexpr
Length(LengthBase_t nanometers) noexcept
587 static LengthBase_t
mapNmToGrid(LengthBase_t nanometers,
588 const Length& gridInterval) noexcept;
602 static LengthBase_t
mmStringToNm(
const QString& millimeters);
612 static constexpr qreal
sNmPerPixel = (qreal)sNmPerInch / sPixelsPerInch;
613 static constexpr qreal
sPixelsPerNm = (qreal)sPixelsPerInch / sNmPerInch;
627 Q_UNUSED(fileFormat);
632 stream << length.
toMm();
637 stream << QString(
"Length(%1mm)").arg(length.
toMm());
650 template <
typename Value,
typename Predicate>
651 static constexpr
auto verify(Value&& val,
const Predicate& p) ->
652 typename std::decay<Value>::type {
653 return p(val) ? std::forward<Value>(val)
655 Length::tr(
"Value must be >= 0!")),
656 std::forward<Value>(val));
715 return (*lhs) >= rhs;
718 return (*lhs) >= rhs;
721 return (*lhs) <= rhs;
724 return (*lhs) <= rhs;
727 return (*lhs) == rhs;
730 return (*lhs) == rhs;
733 return (*lhs) != rhs;
736 return (*lhs) != rhs;
752 stream << length->toMm();
757 stream << QString(
"UnsignedLength(%1mm)").arg(length->toMm());
770 template <
typename Value,
typename Predicate>
771 static constexpr
auto verify(Value&& val,
const Predicate& p) ->
772 typename std::decay<Value>::type {
773 return p(val) ? std::forward<Value>(val)
775 Length::tr(
"Value must be > 0!")),
776 std::forward<Value>(val));
781 constexpr
bool operator()(
const Length& l)
const noexcept {
return l > 0; }
867 return (*lhs) > (*rhs);
871 return (*lhs) > (*rhs);
881 return (*lhs) >= (*rhs);
885 return (*lhs) >= (*rhs);
888 return (*lhs) >= rhs;
891 return (*lhs) >= rhs;
895 return (*lhs) < (*rhs);
899 return (*lhs) < (*rhs);
902 return (*lhs) <= rhs;
905 return (*lhs) <= rhs;
909 return (*lhs) == (*rhs);
913 return (*lhs) == (*rhs);
916 return (*lhs) == rhs;
919 return (*lhs) == rhs;
923 return (*lhs) != (*rhs);
927 return (*lhs) != (*rhs);
930 return (*lhs) != rhs;
933 return (*lhs) != rhs;
949 stream << length->toMm();
954 stream << QString(
"PositiveLength(%1mm)").arg(length->toMm());
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition: length.h:274
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:110
Length operator-() const
Definition: length.h:508
Length & operator+=(const Length &rhs)
Definition: length.h:481
Length operator%(const Length &rhs) const
Definition: length.h:520
constexpr bool operator>=(LengthBase_t rhs) const
Definition: length.h:534
constexpr Length(const Length &length) noexcept
Copy Constructor.
Definition: length.h:101
Length mappedToGrid(const Length &gridInterval) const noexcept
Get a Length object which is mapped to a specific grid interval.
Definition: length.cpp:65
Length operator+(const Length &rhs) const
Definition: length.h:505
Length & mapToGrid(const Length &gridInterval) noexcept
Map this Length object to a specific grid interval.
Definition: length.cpp:70
Length scaled(qreal factor) const noexcept
Get a Length object which is scaled with a specific factor.
Definition: length.cpp:75
static constexpr qreal sPixelsPerNm
Definition: length.h:613
Definition: airwiresbuilder.cpp:34
static Length fromInch(qreal inches, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition: length.cpp:101
uint qHash(const PositiveLength &key, uint seed=0) noexcept
Definition: length.h:958
QString toNmString() const noexcept
Get the length in nanometers as a QString.
Definition: length.h:222
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:124
static constexpr qreal sNmPerPixel
Definition: length.h:612
Q_DECLARE_METATYPE(QImage)
qint64 LengthBase_t
This type is the ONLY base type to store all lengths (always in nanometers)!
Definition: length.h:62
constexpr bool operator==(LengthBase_t rhs) const
Definition: length.h:546
constexpr bool operator()(const Length &l) const noexcept
Definition: length.h:661
qreal toMm() const noexcept
Get the length in millimeters.
Definition: length.h:231
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition: length.h:214
constexpr bool operator<=(LengthBase_t rhs) const
Definition: length.h:540
constexpr bool operator!=(const Length &rhs) const
Definition: length.h:549
~Length()=default
Destructor.
static Length fromMm(qreal millimeters, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition: length.cpp:89
constexpr bool operator>(const Length &rhs) const
Definition: length.h:523
static Length min() noexcept
Get the smallest possible length value.
Definition: length.cpp:119
constexpr bool operator==(const Length &rhs) const
Definition: length.h:543
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:301
Length operator-(const Length &rhs) const
Definition: length.h:509
Length & operator*=(const Length &rhs)
Definition: length.h:489
Length & makeAbs() noexcept
Make the length absolute (mNanometers >= 0)
Definition: length.cpp:56
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: length.h:651
Length & operator/=(LengthBase_t rhs)
Definition: length.h:501
constexpr bool operator>(LengthBase_t rhs) const
Definition: length.h:526
Length & operator-=(const Length &rhs)
Definition: length.h:485
AttributeKey deserialize(const SExpression &sexpr, const Version &fileFormat)
Definition: attributekey.h:104
void setLengthFromFloat(qreal nanometers)
Set the length from a floating point number in nanometers.
Definition: length.cpp:131
Length operator/(const Length &rhs) const
Definition: length.h:516
static Length fromMil(qreal mils, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition: length.cpp:107
static Length max() noexcept
Get the highest possible length value.
Definition: length.cpp:123
constexpr bool operator>=(const Length &rhs) const
Definition: length.h:531
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:262
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition: length.h:205
Length operator*(const Length &rhs) const
Definition: length.h:512
static constexpr LengthBase_t sNmPerMil
1 inch = 25.4mm
Definition: length.h:609
constexpr bool operator!=(LengthBase_t rhs) const
Definition: length.h:552
Length & operator*=(LengthBase_t rhs)
Definition: length.h:493
The RuntimeError class.
Definition: exceptions.h:216
constexpr bool operator()(const Length &l) const noexcept
Definition: length.h:781
Length & operator=(const Length &rhs)
Definition: length.h:477
LengthBase_t mNanometers
the length in nanometers
Definition: length.h:605
constexpr bool operator<(LengthBase_t rhs) const
Definition: length.h:530
constexpr bool operator<(const Length &rhs) const
Definition: length.h:527
static LengthBase_t mmStringToNm(const QString &millimeters)
Convert a length from a QString (in millimeters) to an integer (in nanometers)
Definition: length.cpp:186
static LengthBase_t mapNmToGrid(LengthBase_t nanometers, const Length &gridInterval) noexcept
Map a length in nanometers to a grid interval in nanometers.
Definition: length.cpp:146
Length operator/(LengthBase_t rhs) const
Definition: length.h:519
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: length.h:771
void setLengthInch(qreal inches)
Set the length in inches.
Definition: length.h:173
Length abs() const noexcept
Get a Length object with absolute value (mNanometers >= 0)
Definition: length.cpp:50
static Length fromPx(qreal pixels, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition: length.cpp:113
Length & operator/=(const Length &rhs)
Definition: length.h:497
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:793
void setLengthMil(qreal mils)
Set the length in mils (1/1000 inch)
Definition: length.h:188
void setLengthMm(const QString &millimeters)
Set the length in millimeters, represented in a QString.
Definition: length.h:156
constexpr bool operator<=(const Length &rhs) const
Definition: length.h:537
static constexpr LengthBase_t sNmPerInch
1 inch = 25.4mm
Definition: length.h:608
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:83
UnsignedLength positiveToUnsigned(const PositiveLength &l) noexcept
Definition: length.h:795
qreal toInch() const noexcept
Get the length in inches.
Definition: length.h:253
void setLengthMm(qreal millimeters)
Set the length in millimeters.
Definition: length.h:141
SExpression serialize(const AttributeKey &obj)
Definition: attributekey.h:99
Length operator*(LengthBase_t rhs) const
Definition: length.h:515
static constexpr LengthBase_t sPixelsPerInch
72 dpi for the QGraphics* objects
Definition: length.h:610
Length & scale(qreal factor) noexcept
Scale this Length object with a specific factor.
Definition: length.cpp:80
QString toMmString() const noexcept
Get the length in millimeters as a QString.
Definition: length.cpp:42
const QString & getValue() const
Definition: sexpression.cpp:68
The SExpression class.
Definition: sexpression.h:72
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:120
constexpr Length() noexcept
Default Constructor.
Definition: length.h:94
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:673
constexpr Length(LengthBase_t nanometers) noexcept
Constructor with length in nanometers.
Definition: length.h:109