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