20 #ifndef LIBREPCB_CORE_LENGTH_H 21 #define LIBREPCB_CORE_LENGTH_H 26 #include "../exceptions.h" 28 #include <type_safe/constrained_type.hpp> 58 #ifdef USE_32BIT_LENGTH_UNITS 83 Q_DECLARE_TR_FUNCTIONS(
Length)
108 constexpr
Length(LengthBase_t nanometers) noexcept
595 static LengthBase_t
mapNmToGrid(LengthBase_t nanometers,
596 const Length& gridInterval) noexcept;
610 static LengthBase_t
mmStringToNm(
const QString& millimeters);
620 static constexpr qreal
sNmPerPixel = (qreal)sNmPerInch / sPixelsPerInch;
621 static constexpr qreal
sPixelsPerNm = (qreal)sPixelsPerInch / sNmPerInch;
629 stream << length.
toMm();
634 stream << QString(
"Length(%1mm)").arg(length.
toMm());
647 template <
typename Value,
typename Predicate>
648 static constexpr
auto verify(Value&& val,
const Predicate& p) ->
649 typename std::decay<Value>::type {
650 return p(val) ? std::forward<Value>(val)
652 Length::tr(
"Value must be >= 0!")),
653 std::forward<Value>(val));
712 return (*lhs) >= rhs;
715 return (*lhs) >= rhs;
724 return (*lhs) <= rhs;
727 return (*lhs) <= rhs;
730 return (*lhs) == rhs;
733 return (*lhs) == rhs;
736 return (*lhs) != rhs;
739 return (*lhs) != rhs;
744 stream << length->toMm();
749 stream << QString(
"UnsignedLength(%1mm)").arg(length->toMm());
762 template <
typename Value,
typename Predicate>
763 static constexpr
auto verify(Value&& val,
const Predicate& p) ->
764 typename std::decay<Value>::type {
765 return p(val) ? std::forward<Value>(val)
767 Length::tr(
"Value must be > 0!")),
768 std::forward<Value>(val));
773 constexpr
bool operator()(
const Length& l)
const noexcept {
return l > 0; }
859 return (*lhs) > (*rhs);
863 return (*lhs) > (*rhs);
873 return (*lhs) >= (*rhs);
877 return (*lhs) >= (*rhs);
880 return (*lhs) >= rhs;
883 return (*lhs) >= rhs;
887 return (*lhs) < (*rhs);
891 return (*lhs) < (*rhs);
901 return (*lhs) <= (*rhs);
905 return (*lhs) <= (*rhs);
908 return (*lhs) <= rhs;
911 return (*lhs) <= rhs;
915 return (*lhs) == (*rhs);
919 return (*lhs) == (*rhs);
922 return (*lhs) == rhs;
925 return (*lhs) == rhs;
929 return (*lhs) != (*rhs);
933 return (*lhs) != (*rhs);
936 return (*lhs) != rhs;
939 return (*lhs) != rhs;
944 stream << length->toMm();
949 stream << QString(
"PositiveLength(%1mm)").arg(length->toMm());
961 using Point3D = std::tuple<Length, Length, Length>;
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition: length.h:282
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:108
Length operator-() const
Definition: length.h:516
Length & operator+=(const Length &rhs)
Definition: length.h:489
Length operator%(const Length &rhs) const
Definition: length.h:528
constexpr bool operator>=(LengthBase_t rhs) const
Definition: length.h:542
constexpr Length(const Length &length) noexcept
Copy Constructor.
Definition: length.h:100
Length mappedToGrid(const Length &gridInterval) const noexcept
Get a Length object which is mapped to a specific grid interval.
Definition: length.cpp:70
Length operator+(const Length &rhs) const
Definition: length.h:513
Length & mapToGrid(const Length &gridInterval) noexcept
Map this Length object to a specific grid interval.
Definition: length.cpp:75
Length scaled(qreal factor) const noexcept
Get a Length object which is scaled with a specific factor.
Definition: length.cpp:80
static constexpr qreal sPixelsPerNm
Definition: length.h:621
Definition: occmodel.cpp:76
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:106
uint qHash(const PositiveLength &key, uint seed=0) noexcept
Definition: length.h:953
QString toNmString() const noexcept
Get the length in nanometers as a QString.
Definition: length.h:221
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:123
static constexpr qreal sNmPerPixel
Definition: length.h:620
qint64 LengthBase_t
This type is the ONLY base type to store all lengths (always in nanometers)!
Definition: length.h:61
constexpr bool operator==(LengthBase_t rhs) const
Definition: length.h:554
constexpr bool operator()(const Length &l) const noexcept
Definition: length.h:658
qreal toMm() const noexcept
Get the length in millimeters.
Definition: length.h:239
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition: length.h:213
constexpr bool operator<=(LengthBase_t rhs) const
Definition: length.h:548
constexpr bool operator!=(const Length &rhs) const
Definition: length.h:557
~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:94
constexpr bool operator>(const Length &rhs) const
Definition: length.h:531
static Length min() noexcept
Get the smallest possible length value.
Definition: length.cpp:124
constexpr bool operator==(const Length &rhs) const
Definition: length.h:551
Length operator-(const Length &rhs) const
Definition: length.h:517
Length & operator*=(const Length &rhs)
Definition: length.h:497
Length & makeAbs() noexcept
Make the length absolute (mNanometers >= 0)
Definition: length.cpp:61
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: length.h:648
Length & operator/=(LengthBase_t rhs)
Definition: length.h:509
constexpr bool operator>(LengthBase_t rhs) const
Definition: length.h:534
Length & operator-=(const Length &rhs)
Definition: length.h:493
void setLengthFromFloat(qreal nanometers)
Set the length from a floating point number in nanometers.
Definition: length.cpp:136
Length operator/(const Length &rhs) const
Definition: length.h:524
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:112
static Length max() noexcept
Get the highest possible length value.
Definition: length.cpp:128
constexpr bool operator>=(const Length &rhs) const
Definition: length.h:539
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:270
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition: length.h:204
Length operator*(const Length &rhs) const
Definition: length.h:520
static constexpr LengthBase_t sNmPerMil
1 inch = 25.4mm
Definition: length.h:617
constexpr bool operator!=(LengthBase_t rhs) const
Definition: length.h:560
Length & operator*=(LengthBase_t rhs)
Definition: length.h:501
The RuntimeError class.
Definition: exceptions.h:216
constexpr bool operator()(const Length &l) const noexcept
Definition: length.h:773
Length & operator=(const Length &rhs)
Definition: length.h:485
LengthBase_t mNanometers
the length in nanometers
Definition: length.h:613
constexpr bool operator<(LengthBase_t rhs) const
Definition: length.h:538
constexpr bool operator<(const Length &rhs) const
Definition: length.h:535
static LengthBase_t mmStringToNm(const QString &millimeters)
Convert a length from a QString (in millimeters) to an integer (in nanometers)
Definition: length.cpp:191
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:151
Length operator/(LengthBase_t rhs) const
Definition: length.h:527
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: length.h:763
void setLengthInch(qreal inches)
Set the length in inches.
Definition: length.h:172
Length abs() const noexcept
Get a Length object with absolute value (mNanometers >= 0)
Definition: length.cpp:55
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:118
Length & operator/=(const Length &rhs)
Definition: length.h:505
qreal toMicrometers() const noexcept
Get the length in micrometers.
Definition: length.h:230
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:785
void setLengthMil(qreal mils)
Set the length in mils (1/1000 inch)
Definition: length.h:187
void setLengthMm(const QString &millimeters)
Set the length in millimeters, represented in a QString.
Definition: length.h:155
constexpr bool operator<=(const Length &rhs) const
Definition: length.h:545
std::tuple< Length, Length, Length > Point3D
Definition: length.h:961
static constexpr LengthBase_t sNmPerInch
1 inch = 25.4mm
Definition: length.h:616
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:82
UnsignedLength positiveToUnsigned(const PositiveLength &l) noexcept
Definition: length.h:787
qreal toInch() const noexcept
Get the length in inches.
Definition: length.h:261
void setLengthMm(qreal millimeters)
Set the length in millimeters.
Definition: length.h:140
Length operator*(LengthBase_t rhs) const
Definition: length.h:523
static constexpr LengthBase_t sPixelsPerInch
72 dpi for the QGraphics* objects
Definition: length.h:618
Length & scale(qreal factor) noexcept
Scale this Length object with a specific factor.
Definition: length.cpp:85
QString toMmString() const noexcept
Get the length in millimeters as a QString.
Definition: length.cpp:47
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:118
constexpr Length() noexcept
Default Constructor.
Definition: length.h:93
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:670
constexpr Length(LengthBase_t nanometers) noexcept
Constructor with length in nanometers.
Definition: length.h:108