LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
alignment.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_ALIGNMENT_H
21 #define LIBREPCB_ALIGNMENT_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class HAlign
37  ******************************************************************************/
38 
42 class HAlign final {
43  Q_DECLARE_TR_FUNCTIONS(HAlign)
44 
45 public:
46  HAlign() noexcept : mAlign(Qt::AlignLeft) {}
47  HAlign(const HAlign& other) noexcept : mAlign(other.mAlign) {}
48  Qt::AlignmentFlag toQtAlignFlag() const noexcept { return mAlign; }
49  HAlign& mirror() noexcept;
50  HAlign mirrored() const noexcept { return HAlign(*this).mirror(); }
51  static HAlign left() noexcept { return HAlign(Qt::AlignLeft); }
52  static HAlign center() noexcept { return HAlign(Qt::AlignHCenter); }
53  static HAlign right() noexcept { return HAlign(Qt::AlignRight); }
54  HAlign& operator=(const HAlign& rhs) noexcept {
55  mAlign = rhs.mAlign;
56  return *this;
57  }
58  bool operator==(const HAlign& rhs) const noexcept {
59  return mAlign == rhs.mAlign;
60  }
61  bool operator!=(const HAlign& rhs) const noexcept {
62  return mAlign != rhs.mAlign;
63  }
64 
65 private:
66  explicit HAlign(Qt::AlignmentFlag align) noexcept : mAlign(align) {}
67 
68  Qt::AlignmentFlag mAlign;
69 };
70 
71 /*******************************************************************************
72  * HAlign Non-Member Functions
73  ******************************************************************************/
74 
75 template <>
77  switch (obj.toQtAlignFlag()) {
78  case Qt::AlignLeft:
79  return SExpression::createToken("left");
80  case Qt::AlignHCenter:
81  return SExpression::createToken("center");
82  case Qt::AlignRight:
83  return SExpression::createToken("right");
84  default:
85  throw LogicError(__FILE__, __LINE__);
86  }
87 }
88 
89 template <>
91  bool throwIfEmpty) {
92  QString str = sexpr.getStringOrToken(throwIfEmpty);
93  if (str == "left")
94  return HAlign::left();
95  else if (str == "center")
96  return HAlign::center();
97  else if (str == "right")
98  return HAlign::right();
99  else {
100  throw RuntimeError(
101  __FILE__, __LINE__,
102  QString(HAlign::tr("Invalid horizontal alignment: \"%1\"")).arg(str));
103  }
104 }
105 
106 /*******************************************************************************
107  * Class VAlign
108  ******************************************************************************/
109 
113 class VAlign final {
114  Q_DECLARE_TR_FUNCTIONS(VAlign)
115 
116 public:
117  VAlign() noexcept : mAlign(Qt::AlignTop) {}
118  VAlign(const VAlign& other) noexcept : mAlign(other.mAlign) {}
119  Qt::AlignmentFlag toQtAlignFlag() const noexcept { return mAlign; }
120  VAlign& mirror() noexcept;
121  VAlign mirrored() const noexcept { return VAlign(*this).mirror(); }
122  static VAlign top() noexcept { return VAlign(Qt::AlignTop); }
123  static VAlign center() noexcept { return VAlign(Qt::AlignVCenter); }
124  static VAlign bottom() noexcept { return VAlign(Qt::AlignBottom); }
125  VAlign& operator=(const VAlign& rhs) noexcept {
126  mAlign = rhs.mAlign;
127  return *this;
128  }
129  bool operator==(const VAlign& rhs) const noexcept {
130  return mAlign == rhs.mAlign;
131  }
132  bool operator!=(const VAlign& rhs) const noexcept {
133  return mAlign != rhs.mAlign;
134  }
135 
136 private:
137  explicit VAlign(Qt::AlignmentFlag align) noexcept : mAlign(align) {}
138 
139  Qt::AlignmentFlag mAlign;
140 };
141 
142 /*******************************************************************************
143  * VAlign Non-Member Functions
144  ******************************************************************************/
145 
146 template <>
148  switch (obj.toQtAlignFlag()) {
149  case Qt::AlignTop:
150  return SExpression::createToken("top");
151  case Qt::AlignVCenter:
152  return SExpression::createToken("center");
153  case Qt::AlignBottom:
154  return SExpression::createToken("bottom");
155  default:
156  throw LogicError(__FILE__, __LINE__);
157  }
158 }
159 
160 template <>
162  bool throwIfEmpty) {
163  QString str = sexpr.getStringOrToken(throwIfEmpty);
164  if (str == "top")
165  return VAlign::top();
166  else if (str == "center")
167  return VAlign::center();
168  else if (str == "bottom")
169  return VAlign::bottom();
170  else {
171  throw RuntimeError(
172  __FILE__, __LINE__,
173  QString(VAlign::tr("Invalid vertical alignment: \"%1\"")).arg(str));
174  }
175 }
176 
177 /*******************************************************************************
178  * Class Alignment
179  ******************************************************************************/
180 
184 class Alignment final : public SerializableObject {
185  Q_DECLARE_TR_FUNCTIONS(Alignment)
186 
187 public:
188  Alignment() noexcept : mH(HAlign::left()), mV(VAlign::bottom()) {}
189  Alignment(const Alignment& other) noexcept : mH(other.mH), mV(other.mV) {}
190  explicit Alignment(const HAlign& h, const VAlign& v) noexcept
191  : mH(h), mV(v) {}
192  explicit Alignment(const SExpression& node);
193  const HAlign getH() const noexcept { return mH; }
194  const VAlign getV() const noexcept { return mV; }
195  void setH(const HAlign& h) noexcept { mH = h; }
196  void setV(const VAlign& v) noexcept { mV = v; }
197  Qt::Alignment toQtAlign() const noexcept {
198  return mH.toQtAlignFlag() | mV.toQtAlignFlag();
199  }
200  Alignment& mirror() noexcept;
201  Alignment& mirrorH() noexcept;
202  Alignment& mirrorV() noexcept;
203  Alignment mirrored() const noexcept { return Alignment(*this).mirror(); }
204  Alignment mirroredH() const noexcept { return Alignment(*this).mirrorH(); }
205  Alignment mirroredV() const noexcept { return Alignment(*this).mirrorV(); }
206 
208  void serialize(SExpression& root) const override;
209 
210  Alignment& operator=(const Alignment& rhs) noexcept {
211  mH = rhs.mH;
212  mV = rhs.mV;
213  return *this;
214  }
215  bool operator==(const Alignment& rhs) const noexcept {
216  return mH == rhs.mH && mV == rhs.mV;
217  }
218  bool operator!=(const Alignment& rhs) const noexcept {
219  return mH != rhs.mH || mV != rhs.mV;
220  }
221 
222 private:
225 };
226 
227 /*******************************************************************************
228  * End of File
229  ******************************************************************************/
230 
231 } // namespace librepcb
232 
233 #endif // LIBREPCB_ALIGNMENT_H
Alignment & mirrorV() noexcept
Definition: alignment.cpp:99
VAlign() noexcept
Definition: alignment.h:117
The LogicError class.
Definition: exceptions.h:182
Alignment & mirror() noexcept
Definition: alignment.cpp:88
HAlign mirrored() const noexcept
Definition: alignment.h:50
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: alignment.cpp:104
Alignment mirroredH() const noexcept
Definition: alignment.h:204
const VAlign getV() const noexcept
Definition: alignment.h:194
The Alignment class.
Definition: alignment.h:184
HAlign & operator=(const HAlign &rhs) noexcept
Definition: alignment.h:54
Alignment mirroredV() const noexcept
Definition: alignment.h:205
bool operator!=(const HAlign &rhs) const noexcept
Definition: alignment.h:61
bool operator==(const VAlign &rhs) const noexcept
Definition: alignment.h:129
Qt::AlignmentFlag mAlign
Definition: alignment.h:68
static VAlign bottom() noexcept
Definition: alignment.h:124
void setH(const HAlign &h) noexcept
Definition: alignment.h:195
HAlign() noexcept
Definition: alignment.h:46
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
bool operator==(const HAlign &rhs) const noexcept
Definition: alignment.h:58
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:43
VAlign mirrored() const noexcept
Definition: alignment.h:121
Alignment & operator=(const Alignment &rhs) noexcept
Definition: alignment.h:210
HAlign(Qt::AlignmentFlag align) noexcept
Definition: alignment.h:66
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
static HAlign left() noexcept
Definition: alignment.h:51
const HAlign getH() const noexcept
Definition: alignment.h:193
bool operator!=(const VAlign &rhs) const noexcept
Definition: alignment.h:132
Alignment(const Alignment &other) noexcept
Definition: alignment.h:189
The VAlign class.
Definition: alignment.h:113
The HAlign class.
Definition: alignment.h:42
VAlign(const VAlign &other) noexcept
Definition: alignment.h:118
The RuntimeError class.
Definition: exceptions.h:219
Alignment mirrored() const noexcept
Definition: alignment.h:203
HAlign & mirror() noexcept
Definition: alignment.cpp:36
static HAlign center() noexcept
Definition: alignment.h:52
VAlign & mirror() noexcept
Definition: alignment.cpp:57
bool operator!=(const Alignment &rhs) const noexcept
Definition: alignment.h:218
Qt::AlignmentFlag toQtAlignFlag() const noexcept
Definition: alignment.h:48
HAlign(const HAlign &other) noexcept
Definition: alignment.h:47
static VAlign center() noexcept
Definition: alignment.h:123
VAlign & operator=(const VAlign &rhs) noexcept
Definition: alignment.h:125
Qt::AlignmentFlag mAlign
Definition: alignment.h:139
Qt::AlignmentFlag toQtAlignFlag() const noexcept
Definition: alignment.h:119
static VAlign top() noexcept
Definition: alignment.h:122
void setV(const VAlign &v) noexcept
Definition: alignment.h:196
VAlign(Qt::AlignmentFlag align) noexcept
Definition: alignment.h:137
static HAlign right() noexcept
Definition: alignment.h:53
VAlign mV
Definition: alignment.h:224
bool operator==(const Alignment &rhs) const noexcept
Definition: alignment.h:215
The SExpression class.
Definition: sexpression.h:57
Qt::Alignment toQtAlign() const noexcept
Definition: alignment.h:197
Alignment & mirrorH() noexcept
Definition: alignment.cpp:94
HAlign mH
Definition: alignment.h:223
Alignment() noexcept
Definition: alignment.h:188
Alignment(const HAlign &h, const VAlign &v) noexcept
Definition: alignment.h:190