LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
61 class LengthUnit final {
62  Q_DECLARE_TR_FUNCTIONS(LengthUnit)
63 
64 private:
65  // Private Types
66 
78  enum class LengthUnit_t {
79  Millimeters = 0,
81  Nanometers,
82  Inches,
83  Mils,
84  _COUNT
85  };
86 
87 public:
88  // Constructors / Destructor
89 
93  LengthUnit() noexcept : mUnit(LengthUnit_t::Millimeters) {}
94 
100  LengthUnit(const LengthUnit& other) noexcept : mUnit(other.mUnit) {}
101 
105  ~LengthUnit() noexcept {}
106 
107  // Getters
108 
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 
151  // General Methods
152 
167  qreal convertToUnit(const Length& length) const noexcept;
168 
184  QPointF convertToUnit(const Point& point) const noexcept;
185 
198  Length convertFromUnit(qreal length) const;
199 
212  Point convertFromUnit(const QPointF& point) const;
213 
214  // Static Methods
215 
229  static LengthUnit fromIndex(int index);
230  ;
231 
242  static QList<LengthUnit> getAllUnits() noexcept;
243 
244  // Static Methods to get all available length units
245  static LengthUnit millimeters() noexcept {
247  }
248  static LengthUnit micrometers() noexcept {
250  }
251  static LengthUnit nanometers() noexcept {
253  }
254  static LengthUnit inches() noexcept {
256  }
257  static LengthUnit mils() noexcept { return LengthUnit(LengthUnit_t::Mils); }
258 
259  // Operators
260  LengthUnit& operator=(const LengthUnit& rhs) noexcept {
261  mUnit = rhs.mUnit;
262  return *this;
263  }
264  bool operator==(const LengthUnit& rhs) const noexcept {
265  return mUnit == rhs.mUnit;
266  }
267  bool operator!=(const LengthUnit& rhs) const noexcept {
268  return mUnit != rhs.mUnit;
269  }
270 
271 private:
272  // Private Methods
273 
280  explicit LengthUnit(LengthUnit_t unit) noexcept : mUnit(unit) {}
281 
282  // Attributes
283 
288 };
289 
290 /*******************************************************************************
291  * Non-Member Functions
292  ******************************************************************************/
293 
294 template <>
296  return SExpression::createToken(obj.toStr());
297 }
298 
299 template <>
301  bool throwIfEmpty) {
302  QString str = sexpr.getStringOrToken(throwIfEmpty);
303  if (str == "millimeters")
304  return LengthUnit::millimeters();
305  else if (str == "micrometers")
306  return LengthUnit::micrometers();
307  else if (str == "nanometers")
308  return LengthUnit::nanometers();
309  else if (str == "inches")
310  return LengthUnit::inches();
311  else if (str == "mils")
312  return LengthUnit::mils();
313  else {
314  throw RuntimeError(
315  __FILE__, __LINE__,
316  QString(LengthUnit::tr("Invalid length unit: \"%1\"")).arg(str));
317  }
318 }
319 
320 inline QDataStream& operator<<(QDataStream& stream, const LengthUnit& unit) {
321  stream << unit.toStr();
322  return stream;
323 }
324 
325 inline QDebug operator<<(QDebug stream, const LengthUnit& unit) {
326  stream << QString("LengthUnit(%1)").arg(unit.toStr());
327  return stream;
328 }
329 
330 /*******************************************************************************
331  * End of File
332  ******************************************************************************/
333 
334 } // namespace librepcb
335 
336 #endif // LIBREPCB_LENGTHUNIT_H
~LengthUnit() noexcept
Destructor.
Definition: lengthunit.h:105
LengthUnit(LengthUnit_t unit) noexcept
Private Constructor to create a LengthUnit object with a specific unit.
Definition: lengthunit.h:280
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
static QList< LengthUnit > getAllUnits() noexcept
Get all available length units.
Definition: lengthunit.cpp:183
static LengthUnit fromIndex(int index)
Get the length unit of a specific index (to use with getIndex())
Definition: lengthunit.cpp:176
static LengthUnit millimeters() noexcept
Definition: lengthunit.h:245
LengthUnit & operator=(const LengthUnit &rhs) noexcept
Definition: lengthunit.h:260
static LengthUnit nanometers() noexcept
Definition: lengthunit.h:251
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
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:254
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
qreal convertToUnit(const Length &length) const noexcept
Convert a Length to this length unit.
Definition: lengthunit.cpp:98
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:93
static LengthUnit mils() noexcept
Definition: lengthunit.h:257
LengthUnit_t mUnit
Holds the length unit of the object.
Definition: lengthunit.h:287
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
The RuntimeError class.
Definition: exceptions.h:219
static LengthUnit micrometers() noexcept
Definition: lengthunit.h:248
LengthUnit_t
An enum which contains all available length units.
Definition: lengthunit.h:78
bool operator==(const LengthUnit &rhs) const noexcept
Definition: lengthunit.h:264
LengthUnit(const LengthUnit &other) noexcept
Copy constructor.
Definition: lengthunit.h:100
bool operator!=(const LengthUnit &rhs) const noexcept
Definition: lengthunit.h:267
Length convertFromUnit(qreal length) const
Convert a floating point number with this unit to a Length object.
Definition: lengthunit.cpp:136
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
The LengthUnit class represents a length unit (millimeters, inches,...) and provides some useful meth...
Definition: lengthunit.h:61
The SExpression class.
Definition: sexpression.h:57