LibrePCB Developers Documentation
stroketext.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_STROKETEXT_H
21 #define LIBREPCB_STROKETEXT_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../alignment.h"
27 #include "../fileio/cmd/cmdlistelementinsert.h"
28 #include "../fileio/cmd/cmdlistelementremove.h"
29 #include "../fileio/cmd/cmdlistelementsswap.h"
30 #include "../fileio/serializableobjectlist.h"
31 #include "../geometry/path.h"
32 #include "../graphics/graphicslayername.h"
33 #include "../units/all_length_units.h"
34 
35 #include <QtCore>
36 
37 /*******************************************************************************
38  * Namespace / Forward Declarations
39  ******************************************************************************/
40 namespace librepcb {
41 
42 class AttributeProvider;
43 class StrokeFont;
44 
45 /*******************************************************************************
46  * Class StrokeTextSpacing
47  ******************************************************************************/
48 
52 class StrokeTextSpacing final {
53  Q_DECLARE_TR_FUNCTIONS(StrokeTextSpacing)
54 
55 public:
56  // Constructors / Destructor
57  StrokeTextSpacing() noexcept : mAuto(true), mRatio(Ratio::percent100()) {}
58  StrokeTextSpacing(const StrokeTextSpacing& other) noexcept
59  : mAuto(other.mAuto), mRatio(other.mRatio) {}
60  explicit StrokeTextSpacing(const Ratio& ratio) noexcept
61  : mAuto(false), mRatio(ratio) {}
62  ~StrokeTextSpacing() noexcept {}
63 
64  // General Methods
65  bool isAuto() const noexcept { return mAuto; }
66  const Ratio& getRatio() const noexcept { return mRatio; }
67 
68  // Operator Overloadings
69  bool operator==(const StrokeTextSpacing& rhs) const noexcept {
70  if (mAuto != rhs.mAuto) return false;
71  return mAuto ? true : (mRatio == rhs.mRatio);
72  }
73  bool operator!=(const StrokeTextSpacing& rhs) const noexcept {
74  return !(*this == rhs);
75  }
77  mAuto = rhs.mAuto;
78  mRatio = rhs.mRatio;
79  return *this;
80  }
81 
82 private: // Data
83  bool mAuto;
85 };
86 
87 /*******************************************************************************
88  * Non-Member Functions
89  ******************************************************************************/
90 
91 template <>
93  if (obj.isAuto()) {
94  return SExpression::createToken("auto");
95  } else {
96  return serialize(obj.getRatio());
97  }
98 }
99 
100 template <>
102  const Version& fileFormat) {
103  if (sexpr.getValue() == "auto") {
104  return StrokeTextSpacing();
105  } else {
106  return StrokeTextSpacing(
107  deserialize<Ratio>(sexpr, fileFormat)); // can throw
108  }
109 }
110 
111 /*******************************************************************************
112  * Class StrokeText
113  ******************************************************************************/
114 
118 class StrokeText final : public SerializableObject {
119  Q_DECLARE_TR_FUNCTIONS(StrokeText)
120 
121 public:
122  // Signals
123  enum class Event {
124  UuidChanged,
125  LayerNameChanged,
126  TextChanged,
127  PositionChanged,
128  RotationChanged,
129  HeightChanged,
130  StrokeWidthChanged,
131  LetterSpacingChanged,
132  LineSpacingChanged,
133  AlignChanged,
134  MirroredChanged,
135  AutoRotateChanged,
136  PathsChanged,
137  };
140 
141  // Constructors / Destructor
142  StrokeText() = delete;
143  StrokeText(const StrokeText& other) noexcept;
144  StrokeText(const Uuid& uuid, const StrokeText& other) noexcept;
145  StrokeText(const Uuid& uuid, const GraphicsLayerName& layerName,
146  const QString& text, const Point& pos, const Angle& rotation,
147  const PositiveLength& height, const UnsignedLength& strokeWidth,
148  const StrokeTextSpacing& letterSpacing,
149  const StrokeTextSpacing& lineSpacing, const Alignment& align,
150  bool mirrored, bool autoRotate) noexcept;
151  StrokeText(const SExpression& node, const Version& fileFormat);
152  ~StrokeText() noexcept;
153 
154  // Getters
155  const Uuid& getUuid() const noexcept { return mUuid; }
156  const GraphicsLayerName& getLayerName() const noexcept { return mLayerName; }
157  const Point& getPosition() const noexcept { return mPosition; }
158  const Angle& getRotation() const noexcept { return mRotation; }
159  const PositiveLength& getHeight() const noexcept { return mHeight; }
160  const UnsignedLength& getStrokeWidth() const noexcept { return mStrokeWidth; }
161  const StrokeTextSpacing& getLetterSpacing() const noexcept {
162  return mLetterSpacing;
163  }
164  const StrokeTextSpacing& getLineSpacing() const noexcept {
165  return mLineSpacing;
166  }
167  const Alignment& getAlign() const noexcept { return mAlign; }
168  bool getMirrored() const noexcept { return mMirrored; }
169  bool getAutoRotate() const noexcept { return mAutoRotate; }
170  const QString& getText() const noexcept { return mText; }
171  const QVector<Path>& getPaths() const noexcept;
172  bool needsAutoRotation() const noexcept;
173  Length calcLetterSpacing() const noexcept;
174  Length calcLineSpacing() const noexcept;
175 
176  // Setters
177  bool setLayerName(const GraphicsLayerName& name) noexcept;
178  bool setText(const QString& text) noexcept;
179  bool setPosition(const Point& pos) noexcept;
180  bool setRotation(const Angle& rotation) noexcept;
181  bool setHeight(const PositiveLength& height) noexcept;
182  bool setStrokeWidth(const UnsignedLength& strokeWidth) noexcept;
183  bool setLetterSpacing(const StrokeTextSpacing& spacing) noexcept;
184  bool setLineSpacing(const StrokeTextSpacing& spacing) noexcept;
185  bool setAlign(const Alignment& align) noexcept;
186  bool setMirrored(bool mirrored) noexcept;
187  bool setAutoRotate(bool autoRotate) noexcept;
188 
189  // General Methods
190  void setAttributeProvider(const AttributeProvider* provider) noexcept;
191  void setFont(const StrokeFont* font) noexcept;
192  const StrokeFont* getCurrentFont() const noexcept { return mFont; }
193  void updatePaths() noexcept;
194 
196  void serialize(SExpression& root) const override;
197 
198  // Operator Overloadings
199  bool operator==(const StrokeText& rhs) const noexcept;
200  bool operator!=(const StrokeText& rhs) const noexcept {
201  return !(*this == rhs);
202  }
203  StrokeText& operator=(const StrokeText& rhs) noexcept;
204 
205 private: // Data
208  QString mText;
216  bool mMirrored;
218 
219  // Misc
220  const AttributeProvider*
222  const StrokeFont* mFont;
223  QVector<Path> mPaths;
224  QVector<Path> mPathsRotated;
226 };
227 
228 /*******************************************************************************
229  * Class StrokeTextList
230  ******************************************************************************/
231 
233  static constexpr const char* tagname = "stroke_text";
234 };
235 using StrokeTextList =
238 using CmdStrokeTextInsert =
239  CmdListElementInsert<StrokeText, StrokeTextListNameProvider,
241 using CmdStrokeTextRemove =
242  CmdListElementRemove<StrokeText, StrokeTextListNameProvider,
244 using CmdStrokeTextsSwap =
245  CmdListElementsSwap<StrokeText, StrokeTextListNameProvider,
247 
248 /*******************************************************************************
249  * End of File
250  ******************************************************************************/
251 
252 } // namespace librepcb
253 
254 #endif // LIBREPCB_STROKETEXT_H
const UnsignedLength & getStrokeWidth() const noexcept
Definition: stroketext.h:160
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
const QString & getText() const noexcept
Definition: stroketext.h:170
Signal< StrokeText, Event > onEdited
Definition: stroketext.h:138
Angle mRotation
Definition: stroketext.h:210
Uuid mUuid
Definition: stroketext.h:206
StrokeTextSpacing() noexcept
Definition: stroketext.h:57
The CmdListElementRemove class.
Definition: cmdlistelementremove.h:44
The Alignment class.
Definition: alignment.h:184
StrokeTextSpacing mLetterSpacing
Definition: stroketext.h:213
const StrokeTextSpacing & getLineSpacing() const noexcept
Definition: stroketext.h:164
SExpression serialize(const HAlign &obj)
Definition: alignment.h:76
Definition: airwiresbuilder.cpp:32
type_safe::constrained_type< QString, GraphicsLayerNameConstraint, GraphicsLayerNameVerifier > GraphicsLayerName
Definition: graphicslayername.h:78
StrokeTextSpacing mLineSpacing
Definition: stroketext.h:214
Definition: stroketext.h:232
const Alignment & getAlign() const noexcept
Definition: stroketext.h:167
PositiveLength mHeight
Definition: stroketext.h:211
The StrokeText class.
Definition: stroketext.h:118
HAlign deserialize(const SExpression &sexpr, const Version &fileFormat)
Definition: alignment.h:90
GraphicsLayerName mLayerName
Definition: stroketext.h:207
const StrokeTextSpacing & getLetterSpacing() const noexcept
Definition: stroketext.h:161
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
bool getAutoRotate() const noexcept
Definition: stroketext.h:169
~StrokeTextSpacing() noexcept
Definition: stroketext.h:62
Event
Definition: stroketext.h:123
QVector< Path > mPaths
Definition: stroketext.h:223
Slot< StrokeText, Event > OnEditedSlot
Definition: stroketext.h:139
StrokeTextSpacing & operator=(const StrokeTextSpacing &rhs) noexcept
Definition: stroketext.h:76
bool operator!=(const StrokeText &rhs) const noexcept
Definition: stroketext.h:200
The StrokeTextSpacing class.
Definition: stroketext.h:52
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:43
const Point & getPosition() const noexcept
Definition: stroketext.h:157
The CmdListElementsSwap class.
Definition: cmdlistelementsswap.h:44
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:290
bool mAutoRotate
Definition: stroketext.h:217
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
bool getMirrored() const noexcept
Definition: stroketext.h:168
const StrokeFont * getCurrentFont() const noexcept
Definition: stroketext.h:192
const AttributeProvider * mAttributeProvider
for substituting placeholders in text
Definition: stroketext.h:221
const Ratio & getRatio() const noexcept
Definition: stroketext.h:66
const Angle & getRotation() const noexcept
Definition: stroketext.h:158
bool mAuto
Definition: stroketext.h:83
QString mText
Definition: stroketext.h:208
StrokeTextSpacing(const Ratio &ratio) noexcept
Definition: stroketext.h:60
bool operator!=(const StrokeTextSpacing &rhs) const noexcept
Definition: stroketext.h:73
The Signal class is used to emit signals on non-QObject derived classes.
Definition: signalslot.h:65
const GraphicsLayerName & getLayerName() const noexcept
Definition: stroketext.h:156
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:792
The CmdListElementInsert class.
Definition: cmdlistelementinsert.h:44
bool isAuto() const noexcept
Definition: stroketext.h:65
The Ratio class is used to represent a ratio number (e.g. 13.37%)
Definition: ratio.h:44
StrokeTextSpacing(const StrokeTextSpacing &other) noexcept
Definition: stroketext.h:58
UnsignedLength mStrokeWidth
Definition: stroketext.h:212
Point mPosition
Definition: stroketext.h:209
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:82
const StrokeFont * mFont
font used for calculating paths
Definition: stroketext.h:222
const QString & getValue() const
Definition: sexpression.cpp:77
The StrokeFont class.
Definition: strokefont.h:52
const PositiveLength & getHeight() const noexcept
Definition: stroketext.h:159
bool operator==(const StrokeTextSpacing &rhs) const noexcept
Definition: stroketext.h:69
The SExpression class.
Definition: sexpression.h:72
Ratio mRatio
Definition: stroketext.h:84
bool mMirrored
Definition: stroketext.h:216
Alignment mAlign
Definition: stroketext.h:215
The AttributeProvider class defines an interface for classes which provides some attributes which can...
Definition: attributeprovider.h:59
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:672