LibrePCB Developers Documentation
lengthunit.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_CORE_LENGTHUNIT_H
21#define LIBREPCB_CORE_LENGTHUNIT_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include <QtCore>
27
28/*******************************************************************************
29 * Namespace / Forward Declarations
30 ******************************************************************************/
31namespace librepcb {
32
33class Length;
34class Point;
35
36/*******************************************************************************
37 * Class LengthUnit
38 ******************************************************************************/
39
60class LengthUnit final {
61 Q_DECLARE_TR_FUNCTIONS(LengthUnit)
62
63private:
64 // Private Types
65
77 enum class LengthUnit_t {
78 Millimeters = 0,
79 Micrometers,
80 Nanometers,
81 Inches,
82 Mils,
83 _COUNT
84 };
85
86public:
87 // Constructors / Destructor
88
92 LengthUnit() noexcept : mUnit(LengthUnit_t::Millimeters) {}
93
99 LengthUnit(const LengthUnit& other) noexcept : mUnit(other.mUnit) {}
100
104 ~LengthUnit() noexcept {}
105
106 // Getters
107
123 int getIndex() const noexcept { return static_cast<int>(mUnit); }
124
130 QString toStr() const noexcept;
131
140 QString toStringTr() const noexcept;
141
147 QString toShortStringTr() const noexcept;
148
161 QString format(const Length& value, const QLocale& locale,
162 const QString& separator = " ") const noexcept;
163
179 int getReasonableNumberOfDecimals() const noexcept;
180
190 QStringList getUserInputSuffixes() const noexcept;
191
192 // General Methods
193
208 qreal convertToUnit(const Length& length) const noexcept;
209
225 QPointF convertToUnit(const Point& point) const noexcept;
226
239 Length convertFromUnit(qreal length) const;
240
253 Point convertFromUnit(const QPointF& point) const;
254
255 // Static Methods
256
266 static LengthUnit fromString(const QString& str);
267
281 static LengthUnit fromIndex(int index);
282
293 static QList<LengthUnit> getAllUnits() noexcept;
294
295 // Static Methods to get all available length units
296 static LengthUnit millimeters() noexcept {
298 }
299 static LengthUnit micrometers() noexcept {
301 }
302 static LengthUnit nanometers() noexcept {
304 }
305 static LengthUnit inches() noexcept {
307 }
308 static LengthUnit mils() noexcept { return LengthUnit(LengthUnit_t::Mils); }
309
310 // Operators
311 LengthUnit& operator=(const LengthUnit& rhs) noexcept {
312 mUnit = rhs.mUnit;
313 return *this;
314 }
315 bool operator==(const LengthUnit& rhs) const noexcept {
316 return mUnit == rhs.mUnit;
317 }
318 bool operator!=(const LengthUnit& rhs) const noexcept {
319 return mUnit != rhs.mUnit;
320 }
321
322private:
323 // Private Methods
324
331 explicit LengthUnit(LengthUnit_t unit) noexcept : mUnit(unit) {}
332
333 // Attributes
334
339};
340
341/*******************************************************************************
342 * Non-Member Functions
343 ******************************************************************************/
344
345inline QDataStream& operator<<(QDataStream& stream, const LengthUnit& unit) {
346 stream << unit.toStr();
347 return stream;
348}
349
350inline QDebug operator<<(QDebug stream, const LengthUnit& unit) {
351 stream << QString("LengthUnit(%1)").arg(unit.toStr());
352 return stream;
353}
354
355/*******************************************************************************
356 * End of File
357 ******************************************************************************/
358
359} // namespace librepcb
360
361#endif
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:83
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful meth...
Definition: lengthunit.h:60
QString toStringTr() const noexcept
Convert the length unit to a localized string.
Definition: lengthunit.cpp:63
QString format(const Length &value, const QLocale &locale, const QString &separator=" ") const noexcept
Get a pretty formatted length value with this unit.
Definition: lengthunit.cpp:103
LengthUnit_t mUnit
Holds the length unit of the object.
Definition: lengthunit.h:338
QString toStr() const noexcept
Serialize this object into a string.
Definition: lengthunit.cpp:43
int getIndex() const noexcept
Get the Index of the length unit of this object.
Definition: lengthunit.h:123
QString toShortStringTr() const noexcept
Convert the length unit to a localized string (short form)
Definition: lengthunit.cpp:83
LengthUnit(LengthUnit_t unit) noexcept
Private Constructor to create a LengthUnit object with a specific unit.
Definition: lengthunit.h:331
qreal convertToUnit(const Length &length) const noexcept
Convert a Length to this length unit.
Definition: lengthunit.cpp:152
bool operator==(const LengthUnit &rhs) const noexcept
Definition: lengthunit.h:315
static LengthUnit fromString(const QString &str)
Get the length unit represented by a string.
Definition: lengthunit.cpp:234
static LengthUnit nanometers() noexcept
Definition: lengthunit.h:302
static LengthUnit micrometers() noexcept
Definition: lengthunit.h:299
static QList< LengthUnit > getAllUnits() noexcept
Get all available length units.
Definition: lengthunit.cpp:252
static LengthUnit fromIndex(int index)
Get the length unit of a specific index (to use with getIndex())
Definition: lengthunit.cpp:245
static LengthUnit mils() noexcept
Definition: lengthunit.h:308
LengthUnit_t
An enum which contains all available length units.
Definition: lengthunit.h:77
LengthUnit & operator=(const LengthUnit &rhs) noexcept
Definition: lengthunit.h:311
static LengthUnit millimeters() noexcept
Definition: lengthunit.h:296
Length convertFromUnit(qreal length) const
Convert a floating point number with this unit to a Length object.
Definition: lengthunit.cpp:192
bool operator!=(const LengthUnit &rhs) const noexcept
Definition: lengthunit.h:318
LengthUnit(const LengthUnit &other) noexcept
Copy constructor.
Definition: lengthunit.h:99
LengthUnit() noexcept
Default constructor which uses millimeters as unit.
Definition: lengthunit.h:92
int getReasonableNumberOfDecimals() const noexcept
Get a reasonable number of decimals to be shown.
Definition: lengthunit.cpp:110
~LengthUnit() noexcept
Destructor.
Definition: lengthunit.h:104
static LengthUnit inches() noexcept
Definition: lengthunit.h:305
QStringList getUserInputSuffixes() const noexcept
Get user input suffixes.
Definition: lengthunit.cpp:131
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition: point.h:79
Definition: occmodel.cpp:77
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109