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 
45 class HAlign final {
46  Q_DECLARE_TR_FUNCTIONS(HAlign)
47 
48 public:
49  HAlign() noexcept : mAlign(Qt::AlignLeft) {}
50  HAlign(const HAlign& other) noexcept : mAlign(other.mAlign) {}
51  Qt::AlignmentFlag toQtAlignFlag() const noexcept { return mAlign; }
52  HAlign& mirror() noexcept;
53  HAlign mirrored() const noexcept { return HAlign(*this).mirror(); }
54  static HAlign left() noexcept { return HAlign(Qt::AlignLeft); }
55  static HAlign center() noexcept { return HAlign(Qt::AlignHCenter); }
56  static HAlign right() noexcept { return HAlign(Qt::AlignRight); }
57  HAlign& operator=(const HAlign& rhs) noexcept {
58  mAlign = rhs.mAlign;
59  return *this;
60  }
61  bool operator==(const HAlign& rhs) const noexcept {
62  return mAlign == rhs.mAlign;
63  }
64  bool operator!=(const HAlign& rhs) const noexcept {
65  return mAlign != rhs.mAlign;
66  }
67 
68 private:
69  explicit HAlign(Qt::AlignmentFlag align) noexcept : mAlign(align) {}
70 
71  Qt::AlignmentFlag mAlign;
72 };
73 
74 /*******************************************************************************
75  * HAlign Non-Member Functions
76  ******************************************************************************/
77 
78 template <>
80  switch (obj.toQtAlignFlag()) {
81  case Qt::AlignLeft:
82  return SExpression::createToken("left");
83  case Qt::AlignHCenter:
84  return SExpression::createToken("center");
85  case Qt::AlignRight:
86  return SExpression::createToken("right");
87  default:
88  throw LogicError(__FILE__, __LINE__);
89  }
90 }
91 
92 template <>
94  bool throwIfEmpty) {
95  QString str = sexpr.getStringOrToken(throwIfEmpty);
96  if (str == "left")
97  return HAlign::left();
98  else if (str == "center")
99  return HAlign::center();
100  else if (str == "right")
101  return HAlign::right();
102  else {
103  throw RuntimeError(
104  __FILE__, __LINE__,
105  QString(HAlign::tr("Invalid horizontal alignment: \"%1\"")).arg(str));
106  }
107 }
108 
109 /*******************************************************************************
110  * Class VAlign
111  ******************************************************************************/
112 
119 class VAlign final {
120  Q_DECLARE_TR_FUNCTIONS(VAlign)
121 
122 public:
123  VAlign() noexcept : mAlign(Qt::AlignTop) {}
124  VAlign(const VAlign& other) noexcept : mAlign(other.mAlign) {}
125  Qt::AlignmentFlag toQtAlignFlag() const noexcept { return mAlign; }
126  VAlign& mirror() noexcept;
127  VAlign mirrored() const noexcept { return VAlign(*this).mirror(); }
128  static VAlign top() noexcept { return VAlign(Qt::AlignTop); }
129  static VAlign center() noexcept { return VAlign(Qt::AlignVCenter); }
130  static VAlign bottom() noexcept { return VAlign(Qt::AlignBottom); }
131  VAlign& operator=(const VAlign& rhs) noexcept {
132  mAlign = rhs.mAlign;
133  return *this;
134  }
135  bool operator==(const VAlign& rhs) const noexcept {
136  return mAlign == rhs.mAlign;
137  }
138  bool operator!=(const VAlign& rhs) const noexcept {
139  return mAlign != rhs.mAlign;
140  }
141 
142 private:
143  explicit VAlign(Qt::AlignmentFlag align) noexcept : mAlign(align) {}
144 
145  Qt::AlignmentFlag mAlign;
146 };
147 
148 /*******************************************************************************
149  * VAlign Non-Member Functions
150  ******************************************************************************/
151 
152 template <>
154  switch (obj.toQtAlignFlag()) {
155  case Qt::AlignTop:
156  return SExpression::createToken("top");
157  case Qt::AlignVCenter:
158  return SExpression::createToken("center");
159  case Qt::AlignBottom:
160  return SExpression::createToken("bottom");
161  default:
162  throw LogicError(__FILE__, __LINE__);
163  }
164 }
165 
166 template <>
168  bool throwIfEmpty) {
169  QString str = sexpr.getStringOrToken(throwIfEmpty);
170  if (str == "top")
171  return VAlign::top();
172  else if (str == "center")
173  return VAlign::center();
174  else if (str == "bottom")
175  return VAlign::bottom();
176  else {
177  throw RuntimeError(
178  __FILE__, __LINE__,
179  QString(VAlign::tr("Invalid vertical alignment: \"%1\"")).arg(str));
180  }
181 }
182 
183 /*******************************************************************************
184  * Class Alignment
185  ******************************************************************************/
186 
193 class Alignment final : public SerializableObject {
194  Q_DECLARE_TR_FUNCTIONS(Alignment)
195 
196 public:
197  Alignment() noexcept : mH(HAlign::left()), mV(VAlign::bottom()) {}
198  Alignment(const Alignment& other) noexcept : mH(other.mH), mV(other.mV) {}
199  explicit Alignment(const HAlign& h, const VAlign& v) noexcept
200  : mH(h), mV(v) {}
201  explicit Alignment(const SExpression& node);
202  const HAlign getH() const noexcept { return mH; }
203  const VAlign getV() const noexcept { return mV; }
204  void setH(const HAlign& h) noexcept { mH = h; }
205  void setV(const VAlign& v) noexcept { mV = v; }
206  Qt::Alignment toQtAlign() const noexcept {
207  return mH.toQtAlignFlag() | mV.toQtAlignFlag();
208  }
209  Alignment& mirror() noexcept;
210  Alignment& mirrorH() noexcept;
211  Alignment& mirrorV() noexcept;
212  Alignment mirrored() const noexcept { return Alignment(*this).mirror(); }
213  Alignment mirroredH() const noexcept { return Alignment(*this).mirrorH(); }
214  Alignment mirroredV() const noexcept { return Alignment(*this).mirrorV(); }
215 
217  void serialize(SExpression& root) const override;
218 
219  Alignment& operator=(const Alignment& rhs) noexcept {
220  mH = rhs.mH;
221  mV = rhs.mV;
222  return *this;
223  }
224  bool operator==(const Alignment& rhs) const noexcept {
225  return mH == rhs.mH && mV == rhs.mV;
226  }
227  bool operator!=(const Alignment& rhs) const noexcept {
228  return mH != rhs.mH || mV != rhs.mV;
229  }
230 
231 private:
234 };
235 
236 /*******************************************************************************
237  * End of File
238  ******************************************************************************/
239 
240 } // namespace librepcb
241 
242 #endif // LIBREPCB_ALIGNMENT_H
Alignment & mirrorV() noexcept
Definition: alignment.cpp:99
VAlign() noexcept
Definition: alignment.h:123
The LogicError class.
Definition: exceptions.h:182
Alignment & mirror() noexcept
Definition: alignment.cpp:88
HAlign mirrored() const noexcept
Definition: alignment.h:53
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:213
const VAlign getV() const noexcept
Definition: alignment.h:203
The Alignment class.
Definition: alignment.h:193
HAlign & operator=(const HAlign &rhs) noexcept
Definition: alignment.h:57
Alignment mirroredV() const noexcept
Definition: alignment.h:214
bool operator!=(const HAlign &rhs) const noexcept
Definition: alignment.h:64
bool operator==(const VAlign &rhs) const noexcept
Definition: alignment.h:135
Qt::AlignmentFlag mAlign
Definition: alignment.h:71
static VAlign bottom() noexcept
Definition: alignment.h:130
void setH(const HAlign &h) noexcept
Definition: alignment.h:204
HAlign() noexcept
Definition: alignment.h:49
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
bool operator==(const HAlign &rhs) const noexcept
Definition: alignment.h:61
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:46
VAlign mirrored() const noexcept
Definition: alignment.h:127
Alignment & operator=(const Alignment &rhs) noexcept
Definition: alignment.h:219
HAlign(Qt::AlignmentFlag align) noexcept
Definition: alignment.h:69
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
static HAlign left() noexcept
Definition: alignment.h:54
const HAlign getH() const noexcept
Definition: alignment.h:202
bool operator!=(const VAlign &rhs) const noexcept
Definition: alignment.h:138
Alignment(const Alignment &other) noexcept
Definition: alignment.h:198
The VAlign class.
Definition: alignment.h:119
The HAlign class.
Definition: alignment.h:45
VAlign(const VAlign &other) noexcept
Definition: alignment.h:124
The RuntimeError class.
Definition: exceptions.h:219
Alignment mirrored() const noexcept
Definition: alignment.h:212
HAlign & mirror() noexcept
Definition: alignment.cpp:36
static HAlign center() noexcept
Definition: alignment.h:55
VAlign & mirror() noexcept
Definition: alignment.cpp:57
bool operator!=(const Alignment &rhs) const noexcept
Definition: alignment.h:227
Qt::AlignmentFlag toQtAlignFlag() const noexcept
Definition: alignment.h:51
HAlign(const HAlign &other) noexcept
Definition: alignment.h:50
static VAlign center() noexcept
Definition: alignment.h:129
VAlign & operator=(const VAlign &rhs) noexcept
Definition: alignment.h:131
Qt::AlignmentFlag mAlign
Definition: alignment.h:145
Qt::AlignmentFlag toQtAlignFlag() const noexcept
Definition: alignment.h:125
static VAlign top() noexcept
Definition: alignment.h:128
void setV(const VAlign &v) noexcept
Definition: alignment.h:205
VAlign(Qt::AlignmentFlag align) noexcept
Definition: alignment.h:143
static HAlign right() noexcept
Definition: alignment.h:56
VAlign mV
Definition: alignment.h:233
bool operator==(const Alignment &rhs) const noexcept
Definition: alignment.h:224
The SExpression class.
Definition: sexpression.h:60
Qt::Alignment toQtAlign() const noexcept
Definition: alignment.h:206
Alignment & mirrorH() noexcept
Definition: alignment.cpp:94
HAlign mH
Definition: alignment.h:232
Alignment() noexcept
Definition: alignment.h:197
Alignment(const HAlign &h, const VAlign &v) noexcept
Definition: alignment.h:199