LibrePCB Developers Documentation
attributetype.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_ATTRIBUTETYPE_H
21 #define LIBREPCB_ATTRIBUTETYPE_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 AttributeUnit;
36 
37 /*******************************************************************************
38  * Class AttributeType
39  ******************************************************************************/
40 
45  Q_DECLARE_TR_FUNCTIONS(AttributeType)
46 
47 public:
49  enum class Type_t {
50  String = 0,
51  Resistance,
52  Capacitance,
53  Inductance,
54  Voltage,
55  Current,
56  Power,
57  Frequency,
58  _COUNT
59  };
60 
61  // Constructors / Destructor
62  AttributeType() = delete;
63  AttributeType(const AttributeType& other) = delete;
64  AttributeType(Type_t type, const QString& typeName,
65  const QString& typeNameTr) noexcept;
66  virtual ~AttributeType() noexcept;
67 
68  // Getters
69  Type_t getType() const noexcept { return mType; }
70  const QString& getName() const noexcept { return mTypeName; }
71  const QString& getNameTr() const noexcept { return mTypeNameTr; }
72  const QList<const AttributeUnit*>& getAvailableUnits() const noexcept {
73  return mAvailableUnits;
74  }
75  const AttributeUnit* getUnitFromString(const QString& unit) const;
76  const AttributeUnit* getDefaultUnit() const noexcept { return mDefaultUnit; }
77  bool isUnitAvailable(const AttributeUnit* unit) const noexcept;
78  const AttributeUnit* tryExtractUnitFromValue(QString& value) const noexcept;
79  virtual bool isValueValid(const QString& value) const noexcept = 0;
80  virtual QString valueFromTr(const QString& value) const noexcept = 0;
81  virtual QString printableValueTr(const QString& value,
82  const AttributeUnit* unit = nullptr) const
83  noexcept = 0;
84 
85  // Static Methods
86  static QList<const AttributeType*> getAllTypes() noexcept;
87  static const AttributeType& fromString(const QString& type);
88 
89  // Operator Overloadings
90  AttributeType& operator=(const AttributeType& rhs) = delete;
91 
92 protected:
93  // General Attributes
95  QString mTypeName;
96  QString mTypeNameTr;
97  QList<const AttributeUnit*> mAvailableUnits;
99 };
100 
101 /*******************************************************************************
102  * Non-Member Functions
103  ******************************************************************************/
104 
105 template <>
106 inline SExpression serialize(const AttributeType& obj) {
107  return SExpression::createToken(obj.getName());
108 }
109 
110 template <>
111 inline const AttributeType& deserialize(const SExpression& sexpr,
112  const Version& fileFormat) {
113  Q_UNUSED(fileFormat);
114  return AttributeType::fromString(sexpr.getValue()); // can throw
115 }
116 
117 /*******************************************************************************
118  * End of File
119  ******************************************************************************/
120 
121 } // namespace librepcb
122 
123 #endif // LIBREPCB_ATTRIBUTETYPE_H
static const AttributeType & fromString(const QString &type)
Definition: attributetype.cpp:117
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
const QString & getNameTr() const noexcept
Definition: attributetype.h:71
const QString & getName() const noexcept
Definition: attributetype.h:70
const QList< const AttributeUnit * > & getAvailableUnits() const noexcept
Definition: attributetype.h:72
SExpression serialize(const HAlign &obj)
Definition: alignment.h:76
Definition: airwiresbuilder.cpp:32
const AttributeUnit * getDefaultUnit() const noexcept
Definition: attributetype.h:76
HAlign deserialize(const SExpression &sexpr, const Version &fileFormat)
Definition: alignment.h:90
AttributeType & operator=(const AttributeType &rhs)=delete
static QList< const AttributeType * > getAllTypes() noexcept
Definition: attributetype.cpp:104
QList< const AttributeUnit * > mAvailableUnits
Definition: attributetype.h:97
const AttributeUnit * mDefaultUnit
Definition: attributetype.h:98
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:290
The AttributeUnit class.
Definition: attributeunit.h:42
Type_t getType() const noexcept
Definition: attributetype.h:69
QString mTypeName
Definition: attributetype.h:95
Type_t mType
Definition: attributetype.h:94
virtual bool isValueValid(const QString &value) const noexcept=0
virtual ~AttributeType() noexcept
Definition: attributetype.cpp:54
QString mTypeNameTr
Definition: attributetype.h:96
virtual QString printableValueTr(const QString &value, const AttributeUnit *unit=nullptr) const noexcept=0
virtual QString valueFromTr(const QString &value) const noexcept=0
Type_t
Available Attribute Types.
Definition: attributetype.h:49
const AttributeUnit * getUnitFromString(const QString &unit) const
Definition: attributetype.cpp:64
const QString & getValue() const
Definition: sexpression.cpp:77
const AttributeUnit * tryExtractUnitFromValue(QString &value) const noexcept
Definition: attributetype.cpp:86
The AttributeType class.
Definition: attributetype.h:44
The SExpression class.
Definition: sexpression.h:72
bool isUnitAvailable(const AttributeUnit *unit) const noexcept
Definition: attributetype.cpp:78