LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 serializeToSExpression(obj.getRatio());
97  }
98 }
99 
100 template <>
102  bool throwIfEmpty) {
103  QString str = sexpr.getStringOrToken(throwIfEmpty);
104  if (str == "auto") {
105  return StrokeTextSpacing();
106  } else {
107  return StrokeTextSpacing(
108  deserializeFromSExpression<Ratio>(sexpr, throwIfEmpty)); // can throw
109  }
110 }
111 
112 /*******************************************************************************
113  * Class StrokeText
114  ******************************************************************************/
115 
119 class StrokeText final : public SerializableObject {
120  Q_DECLARE_TR_FUNCTIONS(StrokeText)
121 
122 public:
123  // Signals
124  enum class Event {
125  UuidChanged,
127  TextChanged,
134  AlignChanged,
137  PathsChanged,
138  };
141 
142  // Constructors / Destructor
143  StrokeText() = delete;
144  StrokeText(const StrokeText& other) noexcept;
145  StrokeText(const Uuid& uuid, const StrokeText& other) noexcept;
146  StrokeText(const Uuid& uuid, const GraphicsLayerName& layerName,
147  const QString& text, const Point& pos, const Angle& rotation,
148  const PositiveLength& height, const UnsignedLength& strokeWidth,
149  const StrokeTextSpacing& letterSpacing,
150  const StrokeTextSpacing& lineSpacing, const Alignment& align,
151  bool mirrored, bool autoRotate) noexcept;
152  explicit StrokeText(const SExpression& node);
153  ~StrokeText() noexcept;
154 
155  // Getters
156  const Uuid& getUuid() const noexcept { return mUuid; }
157  const GraphicsLayerName& getLayerName() const noexcept { return mLayerName; }
158  const Point& getPosition() const noexcept { return mPosition; }
159  const Angle& getRotation() const noexcept { return mRotation; }
160  const PositiveLength& getHeight() const noexcept { return mHeight; }
161  const UnsignedLength& getStrokeWidth() const noexcept { return mStrokeWidth; }
162  const StrokeTextSpacing& getLetterSpacing() const noexcept {
163  return mLetterSpacing;
164  }
165  const StrokeTextSpacing& getLineSpacing() const noexcept {
166  return mLineSpacing;
167  }
168  const Alignment& getAlign() const noexcept { return mAlign; }
169  bool getMirrored() const noexcept { return mMirrored; }
170  bool getAutoRotate() const noexcept { return mAutoRotate; }
171  const QString& getText() const noexcept { return mText; }
172  const QVector<Path>& getPaths() const noexcept;
173  bool needsAutoRotation() const noexcept;
174  Length calcLetterSpacing() const noexcept;
175  Length calcLineSpacing() const noexcept;
176 
177  // Setters
178  bool setLayerName(const GraphicsLayerName& name) noexcept;
179  bool setText(const QString& text) noexcept;
180  bool setPosition(const Point& pos) noexcept;
181  bool setRotation(const Angle& rotation) noexcept;
182  bool setHeight(const PositiveLength& height) noexcept;
183  bool setStrokeWidth(const UnsignedLength& strokeWidth) noexcept;
184  bool setLetterSpacing(const StrokeTextSpacing& spacing) noexcept;
185  bool setLineSpacing(const StrokeTextSpacing& spacing) noexcept;
186  bool setAlign(const Alignment& align) noexcept;
187  bool setMirrored(bool mirrored) noexcept;
188  bool setAutoRotate(bool autoRotate) noexcept;
189 
190  // General Methods
191  void setAttributeProvider(const AttributeProvider* provider) noexcept;
192  void setFont(const StrokeFont* font) noexcept;
193  const StrokeFont* getCurrentFont() const noexcept { return mFont; }
194  void updatePaths() noexcept;
195 
197  void serialize(SExpression& root) const override;
198 
199  // Operator Overloadings
200  bool operator==(const StrokeText& rhs) const noexcept;
201  bool operator!=(const StrokeText& rhs) const noexcept {
202  return !(*this == rhs);
203  }
204  StrokeText& operator=(const StrokeText& rhs) noexcept;
205 
206 private: // Data
209  QString mText;
217  bool mMirrored;
219 
220  // Misc
221  const AttributeProvider*
223  const StrokeFont* mFont;
224  QVector<Path> mPaths;
225  QVector<Path> mPathsRotated;
227 };
228 
229 /*******************************************************************************
230  * Class StrokeTextList
231  ******************************************************************************/
232 
234  static constexpr const char* tagname = "stroke_text";
235 };
236 using StrokeTextList =
239 using CmdStrokeTextInsert =
242 using CmdStrokeTextRemove =
245 using CmdStrokeTextsSwap =
248 
249 /*******************************************************************************
250  * End of File
251  ******************************************************************************/
252 
253 } // namespace librepcb
254 
255 #endif // LIBREPCB_STROKETEXT_H
bool setLineSpacing(const StrokeTextSpacing &spacing) noexcept
Definition: stroketext.cpp:233
const UnsignedLength & getStrokeWidth() const noexcept
Definition: stroketext.h:161
bool setPosition(const Point &pos) noexcept
Definition: stroketext.cpp:176
const QString & getText() const noexcept
Definition: stroketext.h:171
Signal< StrokeText, Event > onEdited
Definition: stroketext.h:139
Angle mRotation
Definition: stroketext.h:211
bool setHeight(const PositiveLength &height) noexcept
Definition: stroketext.cpp:200
Uuid mUuid
Definition: stroketext.h:207
StrokeTextSpacing() noexcept
Definition: stroketext.h:57
The CmdListElementRemove class.
Definition: cmdlistelementremove.h:44
bool setStrokeWidth(const UnsignedLength &strokeWidth) noexcept
Definition: stroketext.cpp:211
bool setText(const QString &text) noexcept
Definition: stroketext.cpp:165
bool setMirrored(bool mirrored) noexcept
Definition: stroketext.cpp:255
The Alignment class.
Definition: alignment.h:184
StrokeTextSpacing mLetterSpacing
Definition: stroketext.h:214
bool setLayerName(const GraphicsLayerName &name) noexcept
Definition: stroketext.cpp:155
const StrokeTextSpacing & getLineSpacing() const noexcept
Definition: stroketext.h:165
bool setAutoRotate(bool autoRotate) noexcept
Definition: stroketext.cpp:269
type_safe::constrained_type< QString, GraphicsLayerNameConstraint, GraphicsLayerNameVerifier > GraphicsLayerName
Definition: graphicslayername.h:78
StrokeTextSpacing mLineSpacing
Definition: stroketext.h:215
Definition: stroketext.h:233
const Alignment & getAlign() const noexcept
Definition: stroketext.h:168
PositiveLength mHeight
Definition: stroketext.h:212
The StrokeText class.
Definition: stroketext.h:119
static constexpr const char * tagname
Definition: stroketext.h:234
GraphicsLayerName mLayerName
Definition: stroketext.h:208
const StrokeTextSpacing & getLetterSpacing() const noexcept
Definition: stroketext.h:162
bool setRotation(const Angle &rotation) noexcept
Definition: stroketext.cpp:186
Length calcLetterSpacing() const noexcept
Definition: stroketext.cpp:125
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
bool getAutoRotate() const noexcept
Definition: stroketext.h:170
~StrokeTextSpacing() noexcept
Definition: stroketext.h:62
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
Event
Definition: stroketext.h:124
void setFont(const StrokeFont *font) noexcept
Definition: stroketext.cpp:294
QVector< Path > mPaths
Definition: stroketext.h:224
Slot< StrokeText, Event > OnEditedSlot
Definition: stroketext.h:140
StrokeTextSpacing & operator=(const StrokeTextSpacing &rhs) noexcept
Definition: stroketext.h:76
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
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
const Point & getPosition() const noexcept
Definition: stroketext.h:158
The CmdListElementsSwap class.
Definition: cmdlistelementsswap.h:44
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
bool mAutoRotate
Definition: stroketext.h:218
StrokeText & operator=(const StrokeText &rhs) noexcept
Definition: stroketext.cpp:360
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:169
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: stroketext.cpp:325
void setAttributeProvider(const AttributeProvider *provider) noexcept
Definition: stroketext.cpp:287
bool setAlign(const Alignment &align) noexcept
Definition: stroketext.cpp:244
const StrokeFont * getCurrentFont() const noexcept
Definition: stroketext.h:193
const AttributeProvider * mAttributeProvider
for substituting placeholders in text
Definition: stroketext.h:222
const Ratio & getRatio() const noexcept
Definition: stroketext.h:66
const Angle & getRotation() const noexcept
Definition: stroketext.h:159
bool mAuto
Definition: stroketext.h:83
QString mText
Definition: stroketext.h:209
StrokeTextSpacing(const Ratio &ratio) noexcept
Definition: stroketext.h:60
bool operator!=(const StrokeTextSpacing &rhs) const noexcept
Definition: stroketext.h:73
~StrokeText() noexcept
Definition: stroketext.cpp:105
Length calcLineSpacing() const noexcept
Definition: stroketext.cpp:138
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:157
const QVector< Path > & getPaths() const noexcept
Definition: stroketext.cpp:112
bool needsAutoRotation() const noexcept
Definition: stroketext.cpp:119
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:780
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:213
Point mPosition
Definition: stroketext.h:210
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
QVector< Path > mPathsRotated
same as mPaths, but rotated by 180°
Definition: stroketext.h:226
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:82
const Uuid & getUuid() const noexcept
Definition: stroketext.h:156
const StrokeFont * mFont
font used for calculating paths
Definition: stroketext.h:223
void updatePaths() noexcept
Definition: stroketext.cpp:300
The StrokeFont class.
Definition: strokefont.h:52
const PositiveLength & getHeight() const noexcept
Definition: stroketext.h:160
bool operator==(const StrokeTextSpacing &rhs) const noexcept
Definition: stroketext.h:69
The SExpression class.
Definition: sexpression.h:57
Ratio mRatio
Definition: stroketext.h:84
bool setLetterSpacing(const StrokeTextSpacing &spacing) noexcept
Definition: stroketext.cpp:222
bool mMirrored
Definition: stroketext.h:217
Alignment mAlign
Definition: stroketext.h:216
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:659