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_LENGTHUNIT_H
21 #define LIBREPCB_LENGTHUNIT_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/sexpression.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 class Length;
36 class Point;
37 
38 /*******************************************************************************
39  * Class LengthUnit
40  ******************************************************************************/
41 
62 class LengthUnit final {
63  Q_DECLARE_TR_FUNCTIONS(LengthUnit)
64 
65 private:
66  // Private Types
67 
79  enum class LengthUnit_t {
80  Millimeters = 0,
82  Nanometers,
83  Inches,
84  Mils,
85  _COUNT
86  };
87 
88 public:
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 
166  int getReasonableNumberOfDecimals() const noexcept;
167 
177  QStringList getUserInputSuffixes() const noexcept;
178 
179  // General Methods
180 
195  qreal convertToUnit(const Length& length) const noexcept;
196 
212  QPointF convertToUnit(const Point& point) const noexcept;
213 
226  Length convertFromUnit(qreal length) const;
227 
240  Point convertFromUnit(const QPointF& point) const;
241 
242  // Static Methods
243 
253  static LengthUnit fromString(const QString& str);
254 
268  static LengthUnit fromIndex(int index);
269 
280  static QList<LengthUnit> getAllUnits() noexcept;
281 
282  // Static Methods to get all available length units
283  static LengthUnit millimeters() noexcept {
285  }
286  static LengthUnit micrometers() noexcept {
288  }
289  static LengthUnit nanometers() noexcept {
291  }
292  static LengthUnit inches() noexcept {
294  }
295  static LengthUnit mils() noexcept { return LengthUnit(LengthUnit_t::Mils); }
296 
297  // Operators
298  LengthUnit& operator=(const LengthUnit& rhs) noexcept {
299  mUnit = rhs.mUnit;
300  return *this;
301  }
302  bool operator==(const LengthUnit& rhs) const noexcept {
303  return mUnit == rhs.mUnit;
304  }
305  bool operator!=(const LengthUnit& rhs) const noexcept {
306  return mUnit != rhs.mUnit;
307  }
308 
309 private:
310  // Private Methods
311 
318  explicit LengthUnit(LengthUnit_t unit) noexcept : mUnit(unit) {}
319 
320  // Attributes
321 
326 };
327 
328 /*******************************************************************************
329  * Non-Member Functions
330  ******************************************************************************/
331 
332 template <>
334  return SExpression::createToken(obj.toStr());
335 }
336 
337 template <>
339  bool throwIfEmpty) {
340  return LengthUnit::fromString(
341  sexpr.getStringOrToken(throwIfEmpty)); // can throw
342 }
343 
344 inline QDataStream& operator<<(QDataStream& stream, const LengthUnit& unit) {
345  stream << unit.toStr();
346  return stream;
347 }
348 
349 inline QDebug operator<<(QDebug stream, const LengthUnit& unit) {
350  stream << QString("LengthUnit(%1)").arg(unit.toStr());
351  return stream;
352 }
353 
354 /*******************************************************************************
355  * End of File
356  ******************************************************************************/
357 
358 } // namespace librepcb
359 
360 #endif // LIBREPCB_LENGTHUNIT_H
~LengthUnit() noexcept
Destructor.
Definition: lengthunit.h:106
LengthUnit(LengthUnit_t unit) noexcept
Private Constructor to create a LengthUnit object with a specific unit.
Definition: lengthunit.h:318
static LengthUnit fromString(const QString &str)
Get the length unit represented by a string.
Definition: lengthunit.cpp:214
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
static QList< LengthUnit > getAllUnits() noexcept
Get all available length units.
Definition: lengthunit.cpp:232
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
static LengthUnit fromIndex(int index)
Get the length unit of a specific index (to use with getIndex())
Definition: lengthunit.cpp:225
static LengthUnit millimeters() noexcept
Definition: lengthunit.h:283
LengthUnit & operator=(const LengthUnit &rhs) noexcept
Definition: lengthunit.h:298
Definition: airwiresbuilder.cpp:32
static LengthUnit nanometers() noexcept
Definition: lengthunit.h:289
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
QString toShortStringTr() const noexcept
Convert the length unit to a localized string (short form)
Definition: lengthunit.cpp:79
static LengthUnit inches() noexcept
Definition: lengthunit.h:292
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:278
qreal convertToUnit(const Length &length) const noexcept
Convert a Length to this length unit.
Definition: lengthunit.cpp:136
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
LengthUnit() noexcept
Default constructor which uses millimeters as unit.
Definition: lengthunit.h:94
static LengthUnit mils() noexcept
Definition: lengthunit.h:295
LengthUnit_t mUnit
Holds the length unit of the object.
Definition: lengthunit.h:325
QStringList getUserInputSuffixes() const noexcept
Get user input suffixes.
Definition: lengthunit.cpp:117
int getIndex() const noexcept
Get the Index of the length unit of this object.
Definition: lengthunit.h:125
QString toStringTr() const noexcept
Convert the length unit to a localized string.
Definition: lengthunit.cpp:60
static LengthUnit micrometers() noexcept
Definition: lengthunit.h:286
LengthUnit_t
An enum which contains all available length units.
Definition: lengthunit.h:79
bool operator==(const LengthUnit &rhs) const noexcept
Definition: lengthunit.h:302
LengthUnit(const LengthUnit &other) noexcept
Copy constructor.
Definition: lengthunit.h:101
bool operator!=(const LengthUnit &rhs) const noexcept
Definition: lengthunit.h:305
QString toStr() const noexcept
Serialize this object into a string.
Definition: lengthunit.cpp:41
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:82
count of units, must be the last entry of the enum
int getReasonableNumberOfDecimals() const noexcept
Get a reasonable number of decimals to be shown.
Definition: lengthunit.cpp:98
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful meth...
Definition: lengthunit.h:62
The SExpression class.
Definition: sexpression.h:57
Length convertFromUnit(qreal length) const
Convert a floating point number with this unit to a Length object.
Definition: lengthunit.cpp:174