1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef DOMTOOL_H
43 #define DOMTOOL_H
44 
45 #include <QVariant>
46 
47 QT_BEGIN_NAMESPACE
48 
49 class QDomElement;
50 class QDomDocument;
51 class QDomNode;
52 class QDomNodeList;
53 
54 struct Common
55 {
56     int kind;
57 
58     enum {
59         Kind_Unknown = 0,
60         Kind_Color,
61         Kind_Point,
62         Kind_Size,
63         Kind_Rect,
64         Kind_Font,
65         Kind_SizePolicy,
66         Kind_Cursor
67     };
68 
initCommon69     inline void init()
70     { kind = Kind_Unknown; }
71 };
72 
73 struct Color
74 {
75     Common common;
76     int red, green, blue;
77 
initColor78     inline void init(int r, int g, int b)
79     {
80         common.kind = Common::Kind_Color;
81         red = r;
82         green = g;
83         blue = b;
84     }
85 
86     inline bool operator == (const Color &other) const
87     { return red == other.red && green == other.green && blue == other.blue; }
88 };
89 
90 struct Point
91 {
92     Common common;
93     int x, y;
94 
initPoint95     inline void init(int x, int y)
96     {
97         common.kind = Common::Kind_Point;
98         this->x = x;
99         this->y = y;
100     }
101 };
102 
103 struct Size
104 {
105     Common common;
106     int width, height;
107 
isNullSize108     inline bool isNull() const
109     { return this->width == 0 && this->height == 0; }
110 
initSize111     inline void init(int width, int height)
112     {
113         common.kind = Common::Kind_Size;
114         this->width = width;
115         this->height = height;
116     }
117 };
118 
119 struct Rect
120 {
121     Common common;
122     int x, y;
123     int width, height;
124 
initRect125     inline void init(int x, int y, int width, int height)
126     {
127         common.kind = Common::Kind_Rect;
128         this->x = x;
129         this->y = y;
130         this->width = width;
131         this->height = height;
132     }
133 };
134 
135 struct Font
136 {
137     Common common;
138     char *family;
139     int pointsize;
140     bool bold;
141     bool italic;
142     bool underline;
143     bool strikeout;
144 
initFont145     inline void init()
146     {
147         common.kind = Common::Kind_Font;
148         family = 0;
149         pointsize = 0;
150         bold = false;
151         italic = false;
152         underline = false;
153         strikeout = false;
154     }
155 };
156 
157 struct SizePolicy
158 {
159     Common common;
160     int hsizetype;
161     int vsizetype;
162     int horstretch;
163     int verstretch;
164 
initSizePolicy165     inline void init()
166     {
167         common.kind = Common::Kind_SizePolicy;
168         hsizetype = 0;
169         vsizetype = 0;
170         horstretch = 0;
171         verstretch = 0;
172     }
173 };
174 
175 struct Cursor
176 {
177     Common common;
178     int shape;
179 
initCursor180     inline void init(int shape)
181     {
182         common.kind = Common::Kind_Cursor;
183         this->shape = shape;
184     }
185 };
186 
187 union Variant
188 {
189     Common common;
190     Color color;
191     Size size;
192     Point point;
193     Rect rect;
194     Font font;
195     SizePolicy sizePolicy;
196     Cursor cursor;
197 
Variant()198     inline Variant()
199     { common.kind = Common::Kind_Unknown; }
200 
~Variant()201     inline ~Variant()
202     {
203         if (common.kind == Common::Kind_Font) {
204             delete[] font.family;
205             font.family = 0;
206         }
207     }
208 
kind()209     inline int kind() const
210     { return common.kind; }
211 
createColor(int r,int g,int b)212     inline Variant &createColor(int r, int g, int b)
213     { color.init(r, g, b); return *this; }
214 
createPoint(int x,int y)215     inline Variant &createPoint(int x, int y)
216     { point.init(x, y); return *this; }
217 
createSize(int width,int height)218     inline Variant &createSize(int width, int height)
219     { size.init(width, height); return *this; }
220 
createRect(int x,int y,int w,int h)221     inline Variant &createRect(int x, int y, int w, int h)
222     { rect.init(x, y, w, h); return *this; }
223 
createFont()224     inline Variant &createFont()
225     { font.init(); return *this; }
226 
createSizePolicy()227     inline Variant &createSizePolicy()
228     { sizePolicy.init(); return *this; }
229 
createCursor(int shape)230     inline Variant &createCursor(int shape)
231     { cursor.init(shape); return *this; }
232 };
233 
234 class DomTool
235 {
236 public:
237     static QVariant readProperty(const QDomElement& e, const QString& name, const QVariant& defValue);
238     static QVariant readProperty(const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment);
239     static bool hasProperty(const QDomElement& e, const QString& name);
240     static QStringList propertiesOfType(const QDomElement& e, const QString& type);
241     static QVariant elementToVariant(const QDomElement& e, const QVariant& defValue);
242     static QVariant elementToVariant(const QDomElement& e, const QVariant& defValue, QString &comment);
243     static QVariant readAttribute(const QDomElement& e, const QString& name, const QVariant& defValue);
244     static QVariant readAttribute(const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment);
245     static bool hasAttribute(const QDomElement& e, const QString& name);
246     static Color readColor(const QDomElement &e);
247     static void fixDocument(QDomDocument&);
248     static void fixAttributes(QDomNodeList&, double);
249     static void fixAttribute(QDomNode&, double);
250 };
251 
252 QT_END_NAMESPACE
253 
Q_DECLARE_METATYPE(Size)254 Q_DECLARE_METATYPE(Size)
255 Q_DECLARE_METATYPE(Rect)
256 Q_DECLARE_METATYPE(Font)
257 Q_DECLARE_METATYPE(SizePolicy)
258 Q_DECLARE_METATYPE(Cursor)
259 Q_DECLARE_METATYPE(Color)
260 Q_DECLARE_METATYPE(Point)
261 Q_DECLARE_METATYPE(Common)
262 Q_DECLARE_METATYPE(Variant)
263 
264 QT_BEGIN_NAMESPACE
265 
266 inline Variant asVariant(const QVariant &v)
267 {
268     Variant var;
269     var = qvariant_cast<Variant>(v);
270     return var;
271 }
272 
273 QT_END_NAMESPACE
274 
275 #endif // DOMTOOL_H
276