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 
64 class LengthUnit final {
65  Q_DECLARE_TR_FUNCTIONS(LengthUnit)
66 
67 private:
68  // Private Types
69 
81  enum class LengthUnit_t {
82  Millimeters = 0,
84  Nanometers,
85  Inches,
86  Mils,
87  _COUNT
88  };
89 
90 public:
91  // Constructors / Destructor
92 
96  LengthUnit() noexcept : mUnit(LengthUnit_t::Millimeters) {}
97 
103  LengthUnit(const LengthUnit& other) noexcept : mUnit(other.mUnit) {}
104 
108  ~LengthUnit() noexcept {}
109 
110  // Getters
111 
128  int getIndex() const noexcept { return static_cast<int>(mUnit); }
129 
135  QString toStr() const noexcept;
136 
145  QString toStringTr() const noexcept;
146 
152  QString toShortStringTr() const noexcept;
153 
154  // General Methods
155 
170  qreal convertToUnit(const Length& length) const noexcept;
171 
187  QPointF convertToUnit(const Point& point) const noexcept;
188 
201  Length convertFromUnit(qreal length) const noexcept;
202 
215  Point convertFromUnit(const QPointF& point) const noexcept;
216 
217  // Static Methods
218 
232  static LengthUnit fromIndex(int index);
233  ;
234 
245  static QList<LengthUnit> getAllUnits() noexcept;
246 
247  // Static Methods to get all available length units
248  static LengthUnit millimeters() noexcept {
250  }
251  static LengthUnit micrometers() noexcept {
253  }
254  static LengthUnit nanometers() noexcept {
256  }
257  static LengthUnit inches() noexcept {
259  }
260  static LengthUnit mils() noexcept { return LengthUnit(LengthUnit_t::Mils); }
261 
262  // Operators
263  LengthUnit& operator=(const LengthUnit& rhs) noexcept {
264  mUnit = rhs.mUnit;
265  return *this;
266  }
267  bool operator==(const LengthUnit& rhs) noexcept { return mUnit == rhs.mUnit; }
268 
269 private:
270  // Private Methods
271 
278  explicit LengthUnit(LengthUnit_t unit) noexcept : mUnit(unit) {}
279 
280  // Attributes
281 
286 };
287 
288 /*******************************************************************************
289  * Non-Member Functions
290  ******************************************************************************/
291 
292 template <>
294  return SExpression::createToken(obj.toStr());
295 }
296 
297 template <>
299  bool throwIfEmpty) {
300  QString str = sexpr.getStringOrToken(throwIfEmpty);
301  if (str == "millimeters")
302  return LengthUnit::millimeters();
303  else if (str == "micrometers")
304  return LengthUnit::micrometers();
305  else if (str == "nanometers")
306  return LengthUnit::nanometers();
307  else if (str == "inches")
308  return LengthUnit::inches();
309  else if (str == "mils")
310  return LengthUnit::mils();
311  else {
312  throw RuntimeError(
313  __FILE__, __LINE__,
314  QString(LengthUnit::tr("Invalid length unit: \"%1\"")).arg(str));
315  }
316 }
317 
318 inline QDataStream& operator<<(QDataStream& stream, const LengthUnit& unit) {
319  stream << unit.toStr();
320  return stream;
321 }
322 
323 inline QDebug operator<<(QDebug stream, const LengthUnit& unit) {
324  stream << QString("LengthUnit(%1)").arg(unit.toStr());
325  return stream;
326 }
327 
328 /*******************************************************************************
329  * End of File
330  ******************************************************************************/
331 
332 } // namespace librepcb
333 
334 #endif // LIBREPCB_LENGTHUNIT_H
~LengthUnit() noexcept
Destructor.
Definition: lengthunit.h:108
LengthUnit(LengthUnit_t unit) noexcept
Private Constructor to create a LengthUnit object with a specific unit.
Definition: lengthunit.h:278
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:114
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:248
LengthUnit & operator=(const LengthUnit &rhs) noexcept
Definition: lengthunit.h:263
static LengthUnit nanometers() noexcept
Definition: lengthunit.h:254
bool operator==(const LengthUnit &rhs) noexcept
Definition: lengthunit.h:267
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
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:257
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
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:81
LengthUnit() noexcept
Default constructor which uses millimeters as unit.
Definition: lengthunit.h:96
static LengthUnit mils() noexcept
Definition: lengthunit.h:260
LengthUnit_t mUnit
Holds the length unit of the object.
Definition: lengthunit.h:285
int getIndex() const noexcept
Get the Index of the length unit of this object.
Definition: lengthunit.h:128
QString toStringTr() const noexcept
Convert the length unit to a localized string.
Definition: lengthunit.cpp:60
The RuntimeError class.
Definition: exceptions.h:219
Length convertFromUnit(qreal length) const noexcept
Convert a floating point number with this unit to a Length object.
Definition: lengthunit.cpp:136
static LengthUnit micrometers() noexcept
Definition: lengthunit.h:251
LengthUnit_t
An enum which contains all available length units.
Definition: lengthunit.h:81
LengthUnit(const LengthUnit &other) noexcept
Copy constructor.
Definition: lengthunit.h:103
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:85
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:64
The SExpression class.
Definition: sexpression.h:60