3366d878f8043818963bfdaca0299ba6bb3777a5
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickscalegrid.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickscalegrid_p_p.h"
43
44 #include <QtDeclarative/qdeclarative.h>
45
46 QT_BEGIN_NAMESPACE
47
48 /*!
49     \internal
50     \class QQuickScaleGrid
51     \brief The QQuickScaleGrid class allows you to specify a 3x3 grid to use in scaling an image.
52 */
53
54 QQuickScaleGrid::QQuickScaleGrid(QObject *parent) : QObject(parent), _left(0), _top(0), _right(0), _bottom(0)
55 {
56 }
57
58 QQuickScaleGrid::~QQuickScaleGrid()
59 {
60 }
61
62 bool QQuickScaleGrid::isNull() const
63 {
64     return !_left && !_top && !_right && !_bottom;
65 }
66
67 void QQuickScaleGrid::setLeft(int pos)
68 {
69     if (_left != pos) {
70         _left = pos;
71         emit borderChanged();
72     }
73 }
74
75 void QQuickScaleGrid::setTop(int pos)
76 {
77     if (_top != pos) {
78         _top = pos;
79         emit borderChanged();
80     }
81 }
82
83 void QQuickScaleGrid::setRight(int pos)
84 {
85     if (_right != pos) {
86         _right = pos;
87         emit borderChanged();
88     }
89 }
90
91 void QQuickScaleGrid::setBottom(int pos)
92 {
93     if (_bottom != pos) {
94         _bottom = pos;
95         emit borderChanged();
96     }
97 }
98
99 QQuickGridScaledImage::QQuickGridScaledImage()
100 : _l(-1), _r(-1), _t(-1), _b(-1),
101   _h(QQuickBorderImage::Stretch), _v(QQuickBorderImage::Stretch)
102 {
103 }
104
105 QQuickGridScaledImage::QQuickGridScaledImage(const QQuickGridScaledImage &o)
106 : _l(o._l), _r(o._r), _t(o._t), _b(o._b), _h(o._h), _v(o._v), _pix(o._pix)
107 {
108 }
109
110 QQuickGridScaledImage &QQuickGridScaledImage::operator=(const QQuickGridScaledImage &o)
111 {
112     _l = o._l;
113     _r = o._r;
114     _t = o._t;
115     _b = o._b;
116     _h = o._h;
117     _v = o._v;
118     _pix = o._pix;
119     return *this;
120 }
121
122 QQuickGridScaledImage::QQuickGridScaledImage(QIODevice *data)
123 : _l(-1), _r(-1), _t(-1), _b(-1), _h(QQuickBorderImage::Stretch), _v(QQuickBorderImage::Stretch)
124 {
125     int l = -1;
126     int r = -1;
127     int t = -1;
128     int b = -1;
129     QString imgFile;
130
131     QByteArray raw;
132     while (raw = data->readLine(), !raw.isEmpty()) {
133         QString line = QString::fromUtf8(raw.trimmed());
134         if (line.isEmpty() || line.startsWith(QLatin1Char('#')))
135             continue;
136
137         int colonId = line.indexOf(QLatin1Char(':'));
138         if (colonId <= 0)
139             return;
140
141         QStringList list;
142         list.append(line.left(colonId).trimmed());
143         list.append(line.mid(colonId+1).trimmed());
144
145         if (list[0] == QLatin1String("border.left"))
146             l = list[1].toInt();
147         else if (list[0] == QLatin1String("border.right"))
148             r = list[1].toInt();
149         else if (list[0] == QLatin1String("border.top"))
150             t = list[1].toInt();
151         else if (list[0] == QLatin1String("border.bottom"))
152             b = list[1].toInt();
153         else if (list[0] == QLatin1String("source"))
154             imgFile = list[1];
155         else if (list[0] == QLatin1String("horizontalTileRule"))
156             _h = stringToRule(list[1]);
157         else if (list[0] == QLatin1String("verticalTileRule"))
158             _v = stringToRule(list[1]);
159     }
160
161     if (l < 0 || r < 0 || t < 0 || b < 0 || imgFile.isEmpty())
162         return;
163
164     _l = l; _r = r; _t = t; _b = b;
165
166     _pix = imgFile;
167     if (_pix.startsWith(QLatin1Char('"')) && _pix.endsWith(QLatin1Char('"')))
168         _pix = _pix.mid(1, _pix.size() - 2); // remove leading/trailing quotes.
169 }
170
171 QQuickBorderImage::TileMode QQuickGridScaledImage::stringToRule(const QString &s)
172 {
173     if (s == QLatin1String("Stretch"))
174         return QQuickBorderImage::Stretch;
175     if (s == QLatin1String("Repeat"))
176         return QQuickBorderImage::Repeat;
177     if (s == QLatin1String("Round"))
178         return QQuickBorderImage::Round;
179
180     qWarning("QQuickGridScaledImage: Invalid tile rule specified. Using Stretch.");
181     return QQuickBorderImage::Stretch;
182 }
183
184 bool QQuickGridScaledImage::isValid() const
185 {
186     return _l >= 0;
187 }
188
189 int QQuickGridScaledImage::gridLeft() const
190 {
191     return _l;
192 }
193
194 int QQuickGridScaledImage::gridRight() const
195 {
196     return _r;
197 }
198
199 int QQuickGridScaledImage::gridTop() const
200 {
201     return _t;
202 }
203
204 int QQuickGridScaledImage::gridBottom() const
205 {
206     return _b;
207 }
208
209 QString QQuickGridScaledImage::pixmapUrl() const
210 {
211     return _pix;
212 }
213
214 QT_END_NAMESPACE