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  * Interface IF_StrokeTextObserver
114  ******************************************************************************/
115 
120 public:
121  virtual void strokeTextLayerNameChanged(
122  const GraphicsLayerName& newLayerName) noexcept = 0;
123  virtual void strokeTextTextChanged(const QString& newText) noexcept = 0;
124  virtual void strokeTextPositionChanged(const Point& newPos) noexcept = 0;
125  virtual void strokeTextRotationChanged(const Angle& newRot) noexcept = 0;
126  virtual void strokeTextHeightChanged(
127  const PositiveLength& newHeight) noexcept = 0;
128  virtual void strokeTextStrokeWidthChanged(
129  const UnsignedLength& newWidth) noexcept = 0;
130  virtual void strokeTextLetterSpacingChanged(
131  const StrokeTextSpacing& spacing) noexcept = 0;
132  virtual void strokeTextLineSpacingChanged(
133  const StrokeTextSpacing& spacing) noexcept = 0;
134  virtual void strokeTextAlignChanged(const Alignment& newAlign) noexcept = 0;
135  virtual void strokeTextMirroredChanged(bool newMirrored) noexcept = 0;
136  virtual void strokeTextAutoRotateChanged(bool newAutoRotate) noexcept = 0;
137  virtual void strokeTextPathsChanged(const QVector<Path>& paths) noexcept = 0;
138 
139 protected:
140  IF_StrokeTextObserver() noexcept {}
141  explicit IF_StrokeTextObserver(const IF_StrokeTextObserver& other) = delete;
142  virtual ~IF_StrokeTextObserver() noexcept {}
144 };
145 
146 /*******************************************************************************
147  * Class StrokeText
148  ******************************************************************************/
149 
153 class StrokeText final : public SerializableObject {
154  Q_DECLARE_TR_FUNCTIONS(StrokeText)
155 
156 public:
157  // Constructors / Destructor
158  StrokeText() = delete;
159  StrokeText(const StrokeText& other) noexcept;
160  StrokeText(const Uuid& uuid, const StrokeText& other) noexcept;
161  StrokeText(const Uuid& uuid, const GraphicsLayerName& layerName,
162  const QString& text, const Point& pos, const Angle& rotation,
163  const PositiveLength& height, const UnsignedLength& strokeWidth,
164  const StrokeTextSpacing& letterSpacing,
165  const StrokeTextSpacing& lineSpacing, const Alignment& align,
166  bool mirrored, bool autoRotate) noexcept;
167  explicit StrokeText(const SExpression& node);
168  ~StrokeText() noexcept;
169 
170  // Getters
171  const Uuid& getUuid() const noexcept { return mUuid; }
172  const GraphicsLayerName& getLayerName() const noexcept { return mLayerName; }
173  const Point& getPosition() const noexcept { return mPosition; }
174  const Angle& getRotation() const noexcept { return mRotation; }
175  const PositiveLength& getHeight() const noexcept { return mHeight; }
176  const UnsignedLength& getStrokeWidth() const noexcept { return mStrokeWidth; }
177  const StrokeTextSpacing& getLetterSpacing() const noexcept {
178  return mLetterSpacing;
179  }
180  const StrokeTextSpacing& getLineSpacing() const noexcept {
181  return mLineSpacing;
182  }
183  const Alignment& getAlign() const noexcept { return mAlign; }
184  bool getMirrored() const noexcept { return mMirrored; }
185  bool getAutoRotate() const noexcept { return mAutoRotate; }
186  const QString& getText() const noexcept { return mText; }
187  const QVector<Path>& getPaths() const noexcept;
188  bool needsAutoRotation() const noexcept;
189  Length calcLetterSpacing() const noexcept;
190  Length calcLineSpacing() const noexcept;
191 
192  // Setters
193  void setLayerName(const GraphicsLayerName& name) noexcept;
194  void setText(const QString& text) noexcept;
195  void setPosition(const Point& pos) noexcept;
196  void setRotation(const Angle& rotation) noexcept;
197  void setHeight(const PositiveLength& height) noexcept;
198  void setStrokeWidth(const UnsignedLength& strokeWidth) noexcept;
199  void setLetterSpacing(const StrokeTextSpacing& spacing) noexcept;
200  void setLineSpacing(const StrokeTextSpacing& spacing) noexcept;
201  void setAlign(const Alignment& align) noexcept;
202  void setMirrored(bool mirrored) noexcept;
203  void setAutoRotate(bool autoRotate) noexcept;
204 
205  // General Methods
206  void setAttributeProvider(const AttributeProvider* provider) noexcept;
207  void setFont(const StrokeFont* font) noexcept;
208  const StrokeFont* getCurrentFont() const noexcept { return mFont; }
209  void updatePaths() noexcept;
210  void registerObserver(IF_StrokeTextObserver& object) const noexcept;
211  void unregisterObserver(IF_StrokeTextObserver& object) const noexcept;
212 
214  void serialize(SExpression& root) const override;
215 
216  // Operator Overloadings
217  bool operator==(const StrokeText& rhs) const noexcept;
218  bool operator!=(const StrokeText& rhs) const noexcept {
219  return !(*this == rhs);
220  }
221  StrokeText& operator=(const StrokeText& rhs) noexcept;
222 
223 private: // Data
226  QString mText;
234  bool mMirrored;
236 
237  // Misc
238  mutable QSet<IF_StrokeTextObserver*>
240  const AttributeProvider*
242  const StrokeFont* mFont;
243  QVector<Path> mPaths;
244  QVector<Path> mPathsRotated;
246 };
247 
248 /*******************************************************************************
249  * Class StrokeTextList
250  ******************************************************************************/
251 
253  static constexpr const char* tagname = "stroke_text";
254 };
255 using StrokeTextList =
257 using CmdStrokeTextInsert =
259 using CmdStrokeTextRemove =
261 using CmdStrokeTextsSwap =
263 
264 /*******************************************************************************
265  * End of File
266  ******************************************************************************/
267 
268 } // namespace librepcb
269 
270 #endif // LIBREPCB_STROKETEXT_H
void setPosition(const Point &pos) noexcept
Definition: stroketext.cpp:214
const UnsignedLength & getStrokeWidth() const noexcept
Definition: stroketext.h:176
const QString & getText() const noexcept
Definition: stroketext.h:186
Angle mRotation
Definition: stroketext.h:228
virtual void strokeTextRotationChanged(const Angle &newRot) noexcept=0
Uuid mUuid
Definition: stroketext.h:224
StrokeTextSpacing() noexcept
Definition: stroketext.h:57
virtual void strokeTextAutoRotateChanged(bool newAutoRotate) noexcept=0
virtual void strokeTextLayerNameChanged(const GraphicsLayerName &newLayerName) noexcept=0
The CmdListElementRemove class.
Definition: cmdlistelementremove.h:44
void setHeight(const PositiveLength &height) noexcept
Definition: stroketext.cpp:236
The Alignment class.
Definition: alignment.h:193
void setMirrored(bool mirrored) noexcept
Definition: stroketext.cpp:281
StrokeTextSpacing mLetterSpacing
Definition: stroketext.h:231
const StrokeTextSpacing & getLineSpacing() const noexcept
Definition: stroketext.h:180
virtual void strokeTextPositionChanged(const Point &newPos) noexcept=0
type_safe::constrained_type< QString, GraphicsLayerNameConstraint, GraphicsLayerNameVerifier > GraphicsLayerName
Definition: graphicslayername.h:78
StrokeTextSpacing mLineSpacing
Definition: stroketext.h:232
Definition: stroketext.h:252
const Alignment & getAlign() const noexcept
Definition: stroketext.h:183
PositiveLength mHeight
Definition: stroketext.h:229
The StrokeText class.
Definition: stroketext.h:153
static constexpr const char * tagname
Definition: stroketext.h:253
void registerObserver(IF_StrokeTextObserver &object) const noexcept
Definition: stroketext.cpp:353
void setLetterSpacing(const StrokeTextSpacing &spacing) noexcept
Definition: stroketext.cpp:254
virtual void strokeTextAlignChanged(const Alignment &newAlign) noexcept=0
virtual ~IF_StrokeTextObserver() noexcept
Definition: stroketext.h:142
GraphicsLayerName mLayerName
Definition: stroketext.h:225
const StrokeTextSpacing & getLetterSpacing() const noexcept
Definition: stroketext.h:177
Length calcLetterSpacing() const noexcept
Definition: stroketext.cpp:167
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:81
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
bool getAutoRotate() const noexcept
Definition: stroketext.h:185
~StrokeTextSpacing() noexcept
Definition: stroketext.h:62
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
void setFont(const StrokeFont *font) noexcept
Definition: stroketext.cpp:320
void setText(const QString &text) noexcept
Definition: stroketext.cpp:205
QVector< Path > mPaths
Definition: stroketext.h:243
virtual void strokeTextHeightChanged(const PositiveLength &newHeight) noexcept=0
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:46
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
const Point & getPosition() const noexcept
Definition: stroketext.h:173
The CmdListElementsSwap class.
Definition: cmdlistelementsswap.h:44
The IF_StrokeTextObserver class.
Definition: stroketext.h:119
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
bool mAutoRotate
Definition: stroketext.h:235
void setRotation(const Angle &rotation) noexcept
Definition: stroketext.cpp:222
StrokeText & operator=(const StrokeText &rhs) noexcept
Definition: stroketext.cpp:398
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:81
virtual void strokeTextMirroredChanged(bool newMirrored) noexcept=0
bool getMirrored() const noexcept
Definition: stroketext.h:184
IF_StrokeTextObserver() noexcept
Definition: stroketext.h:140
virtual void strokeTextStrokeWidthChanged(const UnsignedLength &newWidth) noexcept=0
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: stroketext.cpp:363
void setAttributeProvider(const AttributeProvider *provider) noexcept
Definition: stroketext.cpp:313
const StrokeFont * getCurrentFont() const noexcept
Definition: stroketext.h:208
virtual void strokeTextPathsChanged(const QVector< Path > &paths) noexcept=0
const AttributeProvider * mAttributeProvider
for substituting placeholders in text
Definition: stroketext.h:241
const Ratio & getRatio() const noexcept
Definition: stroketext.h:66
const Angle & getRotation() const noexcept
Definition: stroketext.h:174
IF_StrokeTextObserver & operator=(const IF_StrokeTextObserver &rhs)=delete
void setLineSpacing(const StrokeTextSpacing &spacing) noexcept
Definition: stroketext.cpp:263
bool mAuto
Definition: stroketext.h:83
void setLayerName(const GraphicsLayerName &name) noexcept
Definition: stroketext.cpp:197
QString mText
Definition: stroketext.h:226
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:150
Length calcLineSpacing() const noexcept
Definition: stroketext.cpp:180
const GraphicsLayerName & getLayerName() const noexcept
Definition: stroketext.h:172
const QVector< Path > & getPaths() const noexcept
Definition: stroketext.cpp:157
bool needsAutoRotation() const noexcept
Definition: stroketext.cpp:161
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:783
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:47
StrokeTextSpacing(const StrokeTextSpacing &other) noexcept
Definition: stroketext.h:58
virtual void strokeTextLetterSpacingChanged(const StrokeTextSpacing &spacing) noexcept=0
QSet< IF_StrokeTextObserver * > mObservers
A list of all observer objects.
Definition: stroketext.h:239
UnsignedLength mStrokeWidth
Definition: stroketext.h:230
Point mPosition
Definition: stroketext.h:227
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:245
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:85
const Uuid & getUuid() const noexcept
Definition: stroketext.h:171
virtual void strokeTextLineSpacingChanged(const StrokeTextSpacing &spacing) noexcept=0
const StrokeFont * mFont
font used for calculating paths
Definition: stroketext.h:242
void setStrokeWidth(const UnsignedLength &strokeWidth) noexcept
Definition: stroketext.cpp:245
void setAlign(const Alignment &align) noexcept
Definition: stroketext.cpp:272
void updatePaths() noexcept
Definition: stroketext.cpp:326
The StrokeFont class.
Definition: strokefont.h:52
const PositiveLength & getHeight() const noexcept
Definition: stroketext.h:175
bool operator==(const StrokeTextSpacing &rhs) const noexcept
Definition: stroketext.h:69
The SExpression class.
Definition: sexpression.h:60
Ratio mRatio
Definition: stroketext.h:84
void setAutoRotate(bool autoRotate) noexcept
Definition: stroketext.cpp:295
bool mMirrored
Definition: stroketext.h:234
virtual void strokeTextTextChanged(const QString &newText) noexcept=0
Alignment mAlign
Definition: stroketext.h:233
The AttributeProvider class defines an interface for classes which provides some attributes which can...
Definition: attributeprovider.h:62
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:662
void unregisterObserver(IF_StrokeTextObserver &object) const noexcept
Definition: stroketext.cpp:358