1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickscalegrid_p_p.h" |
5 | |
6 | #include <QtQml/qqml.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | /*! |
11 | \internal |
12 | \class QQuickScaleGrid |
13 | \brief The QQuickScaleGrid class allows you to specify a 3x3 grid to use in scaling an image. |
14 | */ |
15 | |
16 | QQuickScaleGrid::QQuickScaleGrid(QObject *parent) : QObject(parent), _left(0), _top(0), _right(0), _bottom(0) |
17 | { |
18 | } |
19 | |
20 | bool QQuickScaleGrid::isNull() const |
21 | { |
22 | return !_left && !_top && !_right && !_bottom; |
23 | } |
24 | |
25 | void QQuickScaleGrid::setLeft(int pos) |
26 | { |
27 | if (_left != pos) { |
28 | _left = pos; |
29 | emit leftBorderChanged(); |
30 | emit borderChanged(); |
31 | } |
32 | } |
33 | |
34 | void QQuickScaleGrid::setTop(int pos) |
35 | { |
36 | if (_top != pos) { |
37 | _top = pos; |
38 | emit topBorderChanged(); |
39 | emit borderChanged(); |
40 | } |
41 | } |
42 | |
43 | void QQuickScaleGrid::setRight(int pos) |
44 | { |
45 | if (_right != pos) { |
46 | _right = pos; |
47 | emit rightBorderChanged(); |
48 | emit borderChanged(); |
49 | } |
50 | } |
51 | |
52 | void QQuickScaleGrid::setBottom(int pos) |
53 | { |
54 | if (_bottom != pos) { |
55 | _bottom = pos; |
56 | emit bottomBorderChanged(); |
57 | emit borderChanged(); |
58 | } |
59 | } |
60 | |
61 | QQuickGridScaledImage::QQuickGridScaledImage() |
62 | : _l(-1), _r(-1), _t(-1), _b(-1), |
63 | _h(QQuickBorderImage::Stretch), _v(QQuickBorderImage::Stretch) |
64 | { |
65 | } |
66 | |
67 | QQuickGridScaledImage::QQuickGridScaledImage(const QQuickGridScaledImage &o) |
68 | : _l(o._l), _r(o._r), _t(o._t), _b(o._b), _h(o._h), _v(o._v), _pix(o._pix) |
69 | { |
70 | } |
71 | |
72 | QQuickGridScaledImage &QQuickGridScaledImage::operator=(const QQuickGridScaledImage &o) |
73 | { |
74 | _l = o._l; |
75 | _r = o._r; |
76 | _t = o._t; |
77 | _b = o._b; |
78 | _h = o._h; |
79 | _v = o._v; |
80 | _pix = o._pix; |
81 | return *this; |
82 | } |
83 | |
84 | QQuickGridScaledImage::QQuickGridScaledImage(QIODevice *data) |
85 | : _l(-1), _r(-1), _t(-1), _b(-1), _h(QQuickBorderImage::Stretch), _v(QQuickBorderImage::Stretch) |
86 | { |
87 | int l = -1; |
88 | int r = -1; |
89 | int t = -1; |
90 | int b = -1; |
91 | QString imgFile; |
92 | |
93 | QByteArray raw; |
94 | while (raw = data->readLine(), !raw.isEmpty()) { |
95 | QString line = QString::fromUtf8(ba: raw.trimmed()); |
96 | if (line.isEmpty() || line.startsWith(c: QLatin1Char('#'))) |
97 | continue; |
98 | |
99 | int colonId = line.indexOf(c: QLatin1Char(':')); |
100 | if (colonId <= 0) |
101 | return; |
102 | |
103 | const QStringView property = QStringView{line}.left(n: colonId).trimmed(); |
104 | QStringView value = QStringView{line}.mid(pos: colonId + 1).trimmed(); |
105 | |
106 | if (property == QLatin1String("border.left" )) { |
107 | l = value.toInt(); |
108 | } else if (property == QLatin1String("border.right" )) { |
109 | r = value.toInt(); |
110 | } else if (property == QLatin1String("border.top" )) { |
111 | t = value.toInt(); |
112 | } else if (property == QLatin1String("border.bottom" )) { |
113 | b = value.toInt(); |
114 | } else if (property == QLatin1String("source" )) { |
115 | if (value.startsWith(c: QLatin1Char('"')) && value.endsWith(c: QLatin1Char('"'))) |
116 | value = value.mid(pos: 1, n: value.size() - 2); // remove leading/trailing quotes. |
117 | imgFile = value.toString(); |
118 | } else if (property == QLatin1String("horizontalTileRule" ) || property == QLatin1String("horizontalTileMode" )) { |
119 | _h = stringToRule(value); |
120 | } else if (property == QLatin1String("verticalTileRule" ) || property == QLatin1String("verticalTileMode" )) { |
121 | _v = stringToRule(value); |
122 | } |
123 | } |
124 | |
125 | if (l < 0 || r < 0 || t < 0 || b < 0 || imgFile.isEmpty()) |
126 | return; |
127 | |
128 | _l = l; _r = r; _t = t; _b = b; |
129 | _pix = imgFile; |
130 | } |
131 | |
132 | QQuickBorderImage::TileMode QQuickGridScaledImage::stringToRule(QStringView s) |
133 | { |
134 | QStringView string = s; |
135 | if (string.startsWith(c: QLatin1Char('"')) && string.endsWith(c: QLatin1Char('"'))) |
136 | string = string.mid(pos: 1, n: string.size() - 2); // remove leading/trailing quotes. |
137 | |
138 | if (string == QLatin1String("Stretch" ) || string == QLatin1String("BorderImage.Stretch" )) |
139 | return QQuickBorderImage::Stretch; |
140 | if (string == QLatin1String("Repeat" ) || string == QLatin1String("BorderImage.Repeat" )) |
141 | return QQuickBorderImage::Repeat; |
142 | if (string == QLatin1String("Round" ) || string == QLatin1String("BorderImage.Round" )) |
143 | return QQuickBorderImage::Round; |
144 | |
145 | qWarning(msg: "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch." ); |
146 | return QQuickBorderImage::Stretch; |
147 | } |
148 | |
149 | bool QQuickGridScaledImage::isValid() const |
150 | { |
151 | return _l >= 0; |
152 | } |
153 | |
154 | int QQuickGridScaledImage::gridLeft() const |
155 | { |
156 | return _l; |
157 | } |
158 | |
159 | int QQuickGridScaledImage::gridRight() const |
160 | { |
161 | return _r; |
162 | } |
163 | |
164 | int QQuickGridScaledImage::gridTop() const |
165 | { |
166 | return _t; |
167 | } |
168 | |
169 | int QQuickGridScaledImage::gridBottom() const |
170 | { |
171 | return _b; |
172 | } |
173 | |
174 | QString QQuickGridScaledImage::pixmapUrl() const |
175 | { |
176 | return _pix; |
177 | } |
178 | |
179 | QT_END_NAMESPACE |
180 | |
181 | #include "moc_qquickscalegrid_p_p.cpp" |
182 | |