c52756240bc7e25d2ad4b3c0d86ee5b41ab27ea7
[profile/ivi/qtdeclarative.git] / src / qtquick1 / graphicsitems / qdeclarativescalegrid.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 "QtQuick1/private/qdeclarativescalegrid_p_p.h"
43
44 #include <QtDeclarative/qdeclarative.h>
45
46 #include <QBuffer>
47 #include <QDebug>
48
49 QT_BEGIN_NAMESPACE
50
51
52 /*!
53     \internal
54     \class QDeclarative1ScaleGrid
55     \brief The QDeclarative1ScaleGrid class allows you to specify a 3x3 grid to use in scaling an image.
56 */
57
58 QDeclarative1ScaleGrid::QDeclarative1ScaleGrid(QObject *parent) : QObject(parent), _left(0), _top(0), _right(0), _bottom(0)
59 {
60 }
61
62 QDeclarative1ScaleGrid::~QDeclarative1ScaleGrid()
63 {
64 }
65
66 bool QDeclarative1ScaleGrid::isNull() const
67 {
68     return !_left && !_top && !_right && !_bottom;
69 }
70
71 void QDeclarative1ScaleGrid::setLeft(int pos)
72 {
73     if (_left != pos) {
74         _left = pos;
75         emit borderChanged();
76     }
77 }
78
79 void QDeclarative1ScaleGrid::setTop(int pos)
80 {
81     if (_top != pos) {
82         _top = pos;
83         emit borderChanged();
84     }
85 }
86
87 void QDeclarative1ScaleGrid::setRight(int pos)
88 {
89     if (_right != pos) {
90         _right = pos;
91         emit borderChanged();
92     }
93 }
94
95 void QDeclarative1ScaleGrid::setBottom(int pos)
96 {
97     if (_bottom != pos) {
98         _bottom = pos;
99         emit borderChanged();
100     }
101 }
102
103 QDeclarative1GridScaledImage::QDeclarative1GridScaledImage()
104 : _l(-1), _r(-1), _t(-1), _b(-1),
105   _h(QDeclarative1BorderImage::Stretch), _v(QDeclarative1BorderImage::Stretch)
106 {
107 }
108
109 QDeclarative1GridScaledImage::QDeclarative1GridScaledImage(const QDeclarative1GridScaledImage &o)
110 : _l(o._l), _r(o._r), _t(o._t), _b(o._b), _h(o._h), _v(o._v), _pix(o._pix)
111 {
112 }
113
114 QDeclarative1GridScaledImage &QDeclarative1GridScaledImage::operator=(const QDeclarative1GridScaledImage &o)
115 {
116     _l = o._l;
117     _r = o._r;
118     _t = o._t;
119     _b = o._b;
120     _h = o._h;
121     _v = o._v;
122     _pix = o._pix;
123     return *this;
124 }
125
126 QDeclarative1GridScaledImage::QDeclarative1GridScaledImage(QIODevice *data)
127 : _l(-1), _r(-1), _t(-1), _b(-1), _h(QDeclarative1BorderImage::Stretch), _v(QDeclarative1BorderImage::Stretch)
128 {
129     int l = -1;
130     int r = -1;
131     int t = -1;
132     int b = -1;
133     QString imgFile;
134
135     QByteArray raw;
136     while(raw = data->readLine(), !raw.isEmpty()) {
137         QString line = QString::fromUtf8(raw.trimmed());
138         if (line.isEmpty() || line.startsWith(QLatin1Char('#')))
139             continue;
140
141         int colonId = line.indexOf(QLatin1Char(':'));
142         if (colonId <= 0)
143             return;
144         QStringList list;
145         list.append(line.left(colonId).trimmed());
146         list.append(line.mid(colonId+1).trimmed());
147
148         if (list[0] == QLatin1String("border.left"))
149             l = list[1].toInt();
150         else if (list[0] == QLatin1String("border.right"))
151             r = list[1].toInt();
152         else if (list[0] == QLatin1String("border.top"))
153             t = list[1].toInt();
154         else if (list[0] == QLatin1String("border.bottom"))
155             b = list[1].toInt();
156         else if (list[0] == QLatin1String("source"))
157             imgFile = list[1];
158         else if (list[0] == QLatin1String("horizontalTileRule"))
159             _h = stringToRule(list[1]);
160         else if (list[0] == QLatin1String("verticalTileRule"))
161             _v = stringToRule(list[1]);
162     }
163
164     if (l < 0 || r < 0 || t < 0 || b < 0 || imgFile.isEmpty())
165         return;
166
167     _l = l; _r = r; _t = t; _b = b;
168
169     _pix = imgFile;
170     if (_pix.startsWith(QLatin1Char('"')) && _pix.endsWith(QLatin1Char('"')))
171         _pix = _pix.mid(1, _pix.size() - 2); // remove leading/trailing quotes.
172 }
173
174 QDeclarative1BorderImage::TileMode QDeclarative1GridScaledImage::stringToRule(const QString &s)
175 {
176     if (s == QLatin1String("Stretch"))
177         return QDeclarative1BorderImage::Stretch;
178     if (s == QLatin1String("Repeat"))
179         return QDeclarative1BorderImage::Repeat;
180     if (s == QLatin1String("Round"))
181         return QDeclarative1BorderImage::Round;
182
183     qWarning("QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch.");
184     return QDeclarative1BorderImage::Stretch;
185 }
186
187 bool QDeclarative1GridScaledImage::isValid() const
188 {
189     return _l >= 0;
190 }
191
192 int QDeclarative1GridScaledImage::gridLeft() const
193 {
194     return _l;
195 }
196
197 int QDeclarative1GridScaledImage::gridRight() const
198 {
199     return _r;
200 }
201
202 int QDeclarative1GridScaledImage::gridTop() const
203 {
204     return _t;
205 }
206
207 int QDeclarative1GridScaledImage::gridBottom() const
208 {
209     return _b;
210 }
211
212 QString QDeclarative1GridScaledImage::pixmapUrl() const
213 {
214     return _pix;
215 }
216
217
218
219 QT_END_NAMESPACE