| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQuick module 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qquickscalegrid_p_p.h" |
| 41 | |
| 42 | #include <QtQml/qqml.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | /*! |
| 47 | \internal |
| 48 | \class QQuickScaleGrid |
| 49 | \brief The QQuickScaleGrid class allows you to specify a 3x3 grid to use in scaling an image. |
| 50 | */ |
| 51 | |
| 52 | QQuickScaleGrid::QQuickScaleGrid(QObject *parent) : QObject(parent), _left(0), _top(0), _right(0), _bottom(0) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | QQuickScaleGrid::~QQuickScaleGrid() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | bool QQuickScaleGrid::isNull() const |
| 61 | { |
| 62 | return !_left && !_top && !_right && !_bottom; |
| 63 | } |
| 64 | |
| 65 | void QQuickScaleGrid::setLeft(int pos) |
| 66 | { |
| 67 | if (_left != pos) { |
| 68 | _left = pos; |
| 69 | emit leftBorderChanged(); |
| 70 | emit borderChanged(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void QQuickScaleGrid::setTop(int pos) |
| 75 | { |
| 76 | if (_top != pos) { |
| 77 | _top = pos; |
| 78 | emit topBorderChanged(); |
| 79 | emit borderChanged(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void QQuickScaleGrid::setRight(int pos) |
| 84 | { |
| 85 | if (_right != pos) { |
| 86 | _right = pos; |
| 87 | emit rightBorderChanged(); |
| 88 | emit borderChanged(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void QQuickScaleGrid::setBottom(int pos) |
| 93 | { |
| 94 | if (_bottom != pos) { |
| 95 | _bottom = pos; |
| 96 | emit bottomBorderChanged(); |
| 97 | emit borderChanged(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | QQuickGridScaledImage::QQuickGridScaledImage() |
| 102 | : _l(-1), _r(-1), _t(-1), _b(-1), |
| 103 | _h(QQuickBorderImage::Stretch), _v(QQuickBorderImage::Stretch) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | QQuickGridScaledImage::QQuickGridScaledImage(const QQuickGridScaledImage &o) |
| 108 | : _l(o._l), _r(o._r), _t(o._t), _b(o._b), _h(o._h), _v(o._v), _pix(o._pix) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | QQuickGridScaledImage &QQuickGridScaledImage::operator=(const QQuickGridScaledImage &o) |
| 113 | { |
| 114 | _l = o._l; |
| 115 | _r = o._r; |
| 116 | _t = o._t; |
| 117 | _b = o._b; |
| 118 | _h = o._h; |
| 119 | _v = o._v; |
| 120 | _pix = o._pix; |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | QQuickGridScaledImage::QQuickGridScaledImage(QIODevice *data) |
| 125 | : _l(-1), _r(-1), _t(-1), _b(-1), _h(QQuickBorderImage::Stretch), _v(QQuickBorderImage::Stretch) |
| 126 | { |
| 127 | int l = -1; |
| 128 | int r = -1; |
| 129 | int t = -1; |
| 130 | int b = -1; |
| 131 | QString imgFile; |
| 132 | |
| 133 | QByteArray raw; |
| 134 | while (raw = data->readLine(), !raw.isEmpty()) { |
| 135 | QString line = QString::fromUtf8(str: raw.trimmed()); |
| 136 | if (line.isEmpty() || line.startsWith(c: QLatin1Char('#'))) |
| 137 | continue; |
| 138 | |
| 139 | int colonId = line.indexOf(c: QLatin1Char(':')); |
| 140 | if (colonId <= 0) |
| 141 | return; |
| 142 | |
| 143 | const QStringRef property = line.leftRef(n: colonId).trimmed(); |
| 144 | QStringRef value = line.midRef(position: colonId + 1).trimmed(); |
| 145 | |
| 146 | if (property == QLatin1String("border.left" )) { |
| 147 | l = value.toInt(); |
| 148 | } else if (property == QLatin1String("border.right" )) { |
| 149 | r = value.toInt(); |
| 150 | } else if (property == QLatin1String("border.top" )) { |
| 151 | t = value.toInt(); |
| 152 | } else if (property == QLatin1String("border.bottom" )) { |
| 153 | b = value.toInt(); |
| 154 | } else if (property == QLatin1String("source" )) { |
| 155 | if (value.startsWith(c: QLatin1Char('"')) && value.endsWith(c: QLatin1Char('"'))) |
| 156 | value = value.mid(pos: 1, n: value.size() - 2); // remove leading/trailing quotes. |
| 157 | imgFile = value.toString(); |
| 158 | } else if (property == QLatin1String("horizontalTileRule" ) || property == QLatin1String("horizontalTileMode" )) { |
| 159 | _h = stringToRule(value); |
| 160 | } else if (property == QLatin1String("verticalTileRule" ) || property == QLatin1String("verticalTileMode" )) { |
| 161 | _v = stringToRule(value); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (l < 0 || r < 0 || t < 0 || b < 0 || imgFile.isEmpty()) |
| 166 | return; |
| 167 | |
| 168 | _l = l; _r = r; _t = t; _b = b; |
| 169 | _pix = imgFile; |
| 170 | } |
| 171 | |
| 172 | QQuickBorderImage::TileMode QQuickGridScaledImage::stringToRule(const QStringRef &s) |
| 173 | { |
| 174 | QStringRef string = s; |
| 175 | if (string.startsWith(c: QLatin1Char('"')) && string.endsWith(c: QLatin1Char('"'))) |
| 176 | string = string.mid(pos: 1, n: string.size() - 2); // remove leading/trailing quotes. |
| 177 | |
| 178 | if (string == QLatin1String("Stretch" ) || string == QLatin1String("BorderImage.Stretch" )) |
| 179 | return QQuickBorderImage::Stretch; |
| 180 | if (string == QLatin1String("Repeat" ) || string == QLatin1String("BorderImage.Repeat" )) |
| 181 | return QQuickBorderImage::Repeat; |
| 182 | if (string == QLatin1String("Round" ) || string == QLatin1String("BorderImage.Round" )) |
| 183 | return QQuickBorderImage::Round; |
| 184 | |
| 185 | qWarning(msg: "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch." ); |
| 186 | return QQuickBorderImage::Stretch; |
| 187 | } |
| 188 | |
| 189 | bool QQuickGridScaledImage::isValid() const |
| 190 | { |
| 191 | return _l >= 0; |
| 192 | } |
| 193 | |
| 194 | int QQuickGridScaledImage::gridLeft() const |
| 195 | { |
| 196 | return _l; |
| 197 | } |
| 198 | |
| 199 | int QQuickGridScaledImage::gridRight() const |
| 200 | { |
| 201 | return _r; |
| 202 | } |
| 203 | |
| 204 | int QQuickGridScaledImage::gridTop() const |
| 205 | { |
| 206 | return _t; |
| 207 | } |
| 208 | |
| 209 | int QQuickGridScaledImage::gridBottom() const |
| 210 | { |
| 211 | return _b; |
| 212 | } |
| 213 | |
| 214 | QString QQuickGridScaledImage::pixmapUrl() const |
| 215 | { |
| 216 | return _pix; |
| 217 | } |
| 218 | |
| 219 | QT_END_NAMESPACE |
| 220 | |