LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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#include <optional>
29
30/*******************************************************************************
31 * Namespace / Forward Declarations
32 ******************************************************************************/
33namespace librepcb {
34
35class Length;
36class Point;
37
38/*******************************************************************************
39 * Class LengthUnit
40 ******************************************************************************/
41
62class LengthUnit final {
63 Q_DECLARE_TR_FUNCTIONS(LengthUnit)
64
65private:
66 // Private Types
67
79 enum class LengthUnit_t {
80 Millimeters = 0,
83 Inches,
84 Mils,
85 _COUNT
86 };
87
88public:
89 // Constructors / Destructor
90
95
101 LengthUnit(const LengthUnit& other) noexcept : mUnit(other.mUnit) {}
102
106 ~LengthUnit() noexcept {}
107
108 // Getters
109
125 int getIndex() const noexcept { return static_cast<int>(mUnit); }
126
132 QString toStr() const noexcept;
133
142 QString toStringTr() const noexcept;
143
149 QString toShortStringTr() const noexcept;
150
163 QString format(const Length& value, const QLocale& locale,
164 const QString& separator = " ") const noexcept;
165
181 int getReasonableNumberOfDecimals() const noexcept;
182
192 QStringList getUserInputSuffixes() const noexcept;
193
194 // General Methods
195
210 qreal convertToUnit(const Length& length) const noexcept;
211
227 QPointF convertToUnit(const Point& point) const noexcept;
228
241 Length convertFromUnit(qreal length) const;
242
255 Point convertFromUnit(const QPointF& point) const;
256
257 // Static Methods
258
268 static LengthUnit fromString(const QString& str);
269
283 static LengthUnit fromIndex(int index);
284
295 static QList<LengthUnit> getAllUnits() noexcept;
296
297 // Static Methods to get all available length units
298 static LengthUnit millimeters() noexcept {
300 }
301 static LengthUnit micrometers() noexcept {
303 }
304 static LengthUnit nanometers() noexcept {
306 }
307 static LengthUnit inches() noexcept {
309 }
310 static LengthUnit mils() noexcept { return LengthUnit(LengthUnit_t::Mils); }
311
323 static std::optional<LengthUnit> extractFromExpression(
324 QString& expression) noexcept;
325
326 // Operators
327 LengthUnit& operator=(const LengthUnit& rhs) noexcept {
328 mUnit = rhs.mUnit;
329 return *this;
330 }
331 bool operator==(const LengthUnit& rhs) const noexcept {
332 return mUnit == rhs.mUnit;
333 }
334 bool operator!=(const LengthUnit& rhs) const noexcept {
335 return mUnit != rhs.mUnit;
336 }
337
338private:
339 // Private Methods
340
347 explicit LengthUnit(LengthUnit_t unit) noexcept : mUnit(unit) {}
348
349 // Attributes
350
355};
356
357/*******************************************************************************
358 * Non-Member Functions
359 ******************************************************************************/
360
361inline QDataStream& operator<<(QDataStream& stream, const LengthUnit& unit) {
362 stream << unit.toStr();
363 return stream;
364}
365
366inline QDebug operator<<(QDebug stream, const LengthUnit& unit) {
367 stream << QString("LengthUnit(%1)").arg(unit.toStr());
368 return stream;
369}
370
371/*******************************************************************************
372 * End of File
373 ******************************************************************************/
374
375} // namespace librepcb
376
377#endif
The Length class is used to represent a length (for example 12.75 millimeters)
Definition length.h:82
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful meth...
Definition lengthunit.h:62
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
static std::optional< LengthUnit > extractFromExpression(QString &expression) noexcept
Try to extract the unit from a user input string.
Definition lengthunit.cpp:259
LengthUnit_t mUnit
Holds the length unit of the object.
Definition lengthunit.h:354
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:125
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:347
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:331
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:304
static LengthUnit micrometers() noexcept
Definition lengthunit.h:301
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:310
LengthUnit_t
An enum which contains all available length units.
Definition lengthunit.h:79
@ _COUNT
count of units, must be the last entry of the enum
LengthUnit & operator=(const LengthUnit &rhs) noexcept
Definition lengthunit.h:327
static LengthUnit millimeters() noexcept
Definition lengthunit.h:298
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:334
LengthUnit(const LengthUnit &other) noexcept
Copy constructor.
Definition lengthunit.h:101
LengthUnit() noexcept
Default constructor which uses millimeters as unit.
Definition lengthunit.h:94
int getReasonableNumberOfDecimals() const noexcept
Get a reasonable number of decimals to be shown.
Definition lengthunit.cpp:110
~LengthUnit() noexcept
Destructor.
Definition lengthunit.h:106
static LengthUnit inches() noexcept
Definition lengthunit.h:307
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:78
Definition occmodel.cpp:77
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108