3
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
*
|
|
5 |
* This program is free software: you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU Lesser General Public License as published by
|
|
7 |
* the Free Software Foundation, version 2.1 of the License.
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU Lesser General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU Lesser General Public License
|
|
15 |
* along with this program. If not,
|
|
16 |
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
|
|
17 |
*
|
|
18 |
* Description:
|
|
19 |
*
|
|
20 |
*/
|
|
21 |
|
|
22 |
#include "ViewportMetaData.h"
|
16
|
23 |
#include <QApplication>
|
|
24 |
#include <QDesktopWidget>
|
|
25 |
#include "WebGestureHelper.h"
|
3
|
26 |
|
16
|
27 |
|
3
|
28 |
static const int MinViewportWidth = 200;
|
|
29 |
static const int MaxViewportWidth = 10000;
|
|
30 |
static const int MinViewportHeight = 200;
|
|
31 |
static const int MaxViewportHeight = 10000;
|
|
32 |
static const qreal DefaultMinScale = 0.2;
|
16
|
33 |
static const qreal DefaultMaxScale = 3.;
|
|
34 |
static const qreal DefaultPageLayoutScale = 0.667;
|
3
|
35 |
|
|
36 |
namespace GVA {
|
|
37 |
|
|
38 |
ViewportMetaData::ViewportMetaData()
|
|
39 |
{
|
16
|
40 |
#ifndef Q_WS_MAEMO_5
|
|
41 |
QRect screenGeom = browserApp->mainWindow()->geometry();
|
|
42 |
m_defaultViewportWidth = screenGeom.width();
|
|
43 |
m_defaultViewportHeight = screenGeom.height();
|
|
44 |
#else
|
|
45 |
m_defaultViewportWidth = 700;
|
|
46 |
m_defaultViewportHeight = 400;
|
|
47 |
#endif
|
|
48 |
m_maxViewportWidth = m_defaultViewportWidth;
|
|
49 |
m_maxViewportHeight = m_defaultViewportHeight;
|
3
|
50 |
initialize();
|
|
51 |
}
|
|
52 |
|
|
53 |
ViewportMetaData::ViewportMetaData(const ViewportMetaData& other)
|
|
54 |
{
|
|
55 |
m_initialScale = other.m_initialScale;
|
|
56 |
m_minimumScale = other.m_minimumScale;
|
|
57 |
m_maximumScale = other.m_maximumScale;
|
|
58 |
m_width = other.m_width;
|
|
59 |
m_height = other.m_height;
|
|
60 |
m_userScalable = other.m_userScalable;
|
|
61 |
m_isValid = other.m_isValid;
|
|
62 |
m_scaleLimits = other.m_scaleLimits;
|
|
63 |
m_specifiedData.m_width = other.m_specifiedData.m_width;
|
|
64 |
m_specifiedData.m_height = other.m_specifiedData.m_height;
|
|
65 |
m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
|
16
|
66 |
#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
|
|
67 |
m_userScalableOverRidden = other.m_userScalableOverRidden;
|
|
68 |
#endif
|
3
|
69 |
}
|
|
70 |
|
|
71 |
ViewportMetaData& ViewportMetaData::operator=(const ViewportMetaData& other)
|
|
72 |
{
|
|
73 |
m_initialScale = other.m_initialScale;
|
|
74 |
m_minimumScale = other.m_minimumScale;
|
|
75 |
m_maximumScale = other.m_maximumScale;
|
|
76 |
m_width = other.m_width;
|
|
77 |
m_height = other.m_height;
|
|
78 |
m_userScalable = other.m_userScalable;
|
|
79 |
m_isValid = other.m_isValid;
|
|
80 |
m_scaleLimits = other.m_scaleLimits;
|
|
81 |
m_specifiedData.m_width = other.m_specifiedData.m_width;
|
|
82 |
m_specifiedData.m_height = other.m_specifiedData.m_height;
|
|
83 |
m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
|
16
|
84 |
#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
|
|
85 |
m_userScalableOverRidden = other.m_userScalableOverRidden;
|
|
86 |
#endif
|
3
|
87 |
|
|
88 |
return *this;
|
|
89 |
}
|
|
90 |
|
|
91 |
ViewportMetaData::~ViewportMetaData()
|
|
92 |
{}
|
|
93 |
|
16
|
94 |
QSize ViewportMetaData::getSpecifiedSize() const
|
|
95 |
{
|
|
96 |
QSize ret(m_maxViewportWidth / DefaultPageLayoutScale,
|
|
97 |
m_maxViewportHeight / DefaultPageLayoutScale);
|
|
98 |
if(m_initialScale > 0)
|
|
99 |
ret = QSize(m_maxViewportWidth / m_initialScale,
|
|
100 |
m_maxViewportHeight / m_initialScale);
|
|
101 |
|
|
102 |
// assign specified data and swap if necessary
|
|
103 |
if(!m_specifiedData.m_width.isEmpty() && m_specifiedData.m_width != "device-width") {
|
|
104 |
|
|
105 |
if(m_specifiedData.m_width == "device-height")
|
|
106 |
ret.setWidth(m_maxViewportHeight);
|
|
107 |
else
|
|
108 |
ret.setWidth(m_specifiedData.m_width.toInt());
|
|
109 |
}
|
|
110 |
|
|
111 |
if(!m_specifiedData.m_height.isEmpty() && m_specifiedData.m_height != "device-height") {
|
|
112 |
if(m_specifiedData.m_height == "device-width")
|
|
113 |
ret.setHeight(m_maxViewportWidth);
|
|
114 |
else
|
|
115 |
ret.setHeight(m_specifiedData.m_height.toInt());
|
|
116 |
}
|
|
117 |
|
|
118 |
return ret;
|
|
119 |
}
|
|
120 |
|
|
121 |
void ViewportMetaData::adjustViewportData(const QSizeF& clientRect)
|
3
|
122 |
{
|
|
123 |
//Data updated from viewport tag
|
|
124 |
m_isValid = true;
|
|
125 |
|
16
|
126 |
m_maxViewportWidth = clientRect.width();
|
|
127 |
m_maxViewportHeight = clientRect.height();
|
|
128 |
|
|
129 |
QSize s = getSpecifiedSize();
|
|
130 |
|
|
131 |
// if((m_maxViewportWidth > m_maxViewportHeight && m_width < m_height) ||
|
|
132 |
// (m_maxViewportWidth < m_maxViewportHeight && m_width > m_height))
|
|
133 |
// qSwap(m_width,m_height);
|
|
134 |
|
|
135 |
/*
|
3
|
136 |
//Adjust viewport dimensions
|
16
|
137 |
m_width = qBound(MinViewportWidth, m_width, m_maxViewportWidth);
|
|
138 |
m_height = qBound(MinViewportHeight, m_height, m_maxViewportHeight);
|
3
|
139 |
|
|
140 |
//Aspect ratio
|
|
141 |
qreal aspectRation = (qreal)clientRect.width() / clientRect.height();
|
|
142 |
|
16
|
143 |
if (m_width != m_maxViewportWidth && m_height == m_maxViewportHeight) {
|
3
|
144 |
//Width has been specified. Adjust height, min scale and max scale
|
|
145 |
m_height = m_width * (1 / aspectRation);
|
16
|
146 |
} else if (m_width == m_maxViewportWidth && m_height != m_maxViewportHeight) {
|
3
|
147 |
//Height has been specified. Adjust width, min scale and max scale
|
|
148 |
m_width = m_height * aspectRation;
|
|
149 |
} else {
|
|
150 |
//Putting below code under seperate 'else' to make it readable!
|
|
151 |
m_height = m_width * (1 / aspectRation);
|
|
152 |
}
|
16
|
153 |
*/
|
|
154 |
m_width = qBound(MinViewportWidth, s.width(), m_maxViewportWidth);
|
|
155 |
m_height = qBound(MinViewportHeight, s.height(), m_maxViewportHeight);
|
3
|
156 |
}
|
|
157 |
|
16
|
158 |
|
|
159 |
void ViewportMetaData::orientationChanged(const QSizeF& newClientRect)
|
3
|
160 |
{
|
16
|
161 |
/*
|
3
|
162 |
//If still viewport tag has not been parsed
|
|
163 |
//Do not update values.
|
|
164 |
if(!m_isValid)
|
|
165 |
return;
|
|
166 |
|
|
167 |
//Aspect ratio
|
|
168 |
qreal aspectRation = (qreal)newClientRect.width() / newClientRect.height();
|
|
169 |
|
|
170 |
//Update with viewport dimensions
|
|
171 |
if (m_specifiedData.m_width.length()) {
|
|
172 |
if (QString::compare(m_specifiedData.m_width, "device-width", Qt::CaseInsensitive) == 0)
|
|
173 |
m_width = newClientRect.width();
|
|
174 |
else if(QString::compare(m_specifiedData.m_width, "device-height", Qt::CaseInsensitive) == 0)
|
|
175 |
m_width = newClientRect.height();
|
|
176 |
|
|
177 |
m_height = m_width * (1 / aspectRation);
|
|
178 |
}
|
|
179 |
|
|
180 |
//Check if width has not bee specified.
|
|
181 |
if (!m_specifiedData.m_width.length()) {
|
|
182 |
if (QString::compare(m_specifiedData.m_height, "device-width", Qt::CaseInsensitive) == 0)
|
|
183 |
m_height = newClientRect.width();
|
|
184 |
else if(QString::compare(m_specifiedData.m_height, "device-height", Qt::CaseInsensitive) == 0)
|
|
185 |
m_height = newClientRect.height();
|
|
186 |
|
|
187 |
m_width = m_height * aspectRation;
|
|
188 |
}
|
|
189 |
|
|
190 |
//Update with bounds
|
16
|
191 |
//m_width = qBound(MinViewportWidth, m_width, m_maxViewportWidth);
|
|
192 |
//m_height = qBound(MinViewportHeight, m_height, m_maxViewportHeight);
|
|
193 |
|
|
194 |
*/
|
|
195 |
|
|
196 |
m_width = newClientRect.height();
|
|
197 |
m_height = newClientRect.width();
|
|
198 |
|
3
|
199 |
}
|
|
200 |
|
|
201 |
bool ViewportMetaData::isLayoutNeeded()
|
|
202 |
{
|
|
203 |
if (!isUserSpecifiedWidth() || !isUserSpecifiedHeight())
|
|
204 |
return true;
|
|
205 |
return false;
|
|
206 |
}
|
|
207 |
void ViewportMetaData::initialize()
|
|
208 |
{
|
|
209 |
m_initialScale = ValueUndefined;
|
|
210 |
m_minimumScale = DefaultMinScale;
|
|
211 |
m_maximumScale = DefaultMaxScale;
|
16
|
212 |
m_width = m_maxViewportWidth; //m_defaultViewportWidth;
|
|
213 |
m_height = m_maxViewportHeight; //m_defaultViewportHeight;
|
3
|
214 |
m_userScalable = true;
|
|
215 |
m_isValid = false;
|
16
|
216 |
#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
|
|
217 |
m_userScalableOverRidden = false;
|
|
218 |
#endif
|
3
|
219 |
|
|
220 |
//Clear user defined scales
|
|
221 |
setFlag(UserDefinedMinumumScale, false);
|
|
222 |
setFlag(UserDefinedMaximumScale, false);
|
|
223 |
setFlag(UserDefinedInitialScale, false);
|
16
|
224 |
m_specifiedData.m_minScale = -1;
|
3
|
225 |
}
|
|
226 |
|
16
|
227 |
void ViewportMetaData::adjustZoomValues(const QSizeF& contentSize)
|
3
|
228 |
{
|
16
|
229 |
qreal fitToWidthZoom = m_width / (qreal)contentSize.width();
|
3
|
230 |
fitToWidthZoom = qBound(DefaultMinScale, fitToWidthZoom, DefaultMaxScale);
|
|
231 |
|
|
232 |
m_maximumScale = qBound(fitToWidthZoom, m_maximumScale, DefaultMaxScale);
|
|
233 |
|
16
|
234 |
//Adjust minimum-scale
|
3
|
235 |
if (getFlag(UserDefinedMinumumScale)) {
|
|
236 |
m_minimumScale = m_specifiedData.m_minScale;
|
|
237 |
m_minimumScale = qBound(fitToWidthZoom, m_minimumScale, m_maximumScale);
|
|
238 |
}
|
|
239 |
else
|
|
240 |
m_minimumScale = fitToWidthZoom;
|
|
241 |
|
|
242 |
//Adjust initial-scale
|
16
|
243 |
// if (getFlag(UserDefinedInitialScale))
|
|
244 |
// m_initialScale = qBound(m_minimumScale, m_initialScale, m_maximumScale);
|
|
245 |
// else
|
|
246 |
// m_initialScale = m_minimumScale;
|
3
|
247 |
|
16
|
248 |
#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
|
|
249 |
if (m_minimumScale == m_maximumScale) {
|
|
250 |
m_minimumScale = DefaultMinScale;
|
|
251 |
m_maximumScale = DefaultMaxScale;
|
|
252 |
if(!m_userScalable) {
|
|
253 |
m_userScalable = true;
|
|
254 |
m_userScalableOverRidden = true;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
#else
|
3
|
258 |
//Turn off zooming if min and max zoom are same
|
|
259 |
if (m_minimumScale == m_maximumScale)
|
|
260 |
m_userScalable = false;
|
16
|
261 |
#endif
|
3
|
262 |
}
|
|
263 |
|
|
264 |
|
|
265 |
//FIX ME : Merge below functions to single. Now in Hurry!!
|
|
266 |
bool ViewportMetaData::isUserSpecifiedWidth()
|
|
267 |
{
|
|
268 |
if (m_specifiedData.m_width.length()) {
|
|
269 |
if (!QString::compare(m_specifiedData.m_width, "device-width", Qt::CaseInsensitive)
|
|
270 |
|| !QString::compare(m_specifiedData.m_width, "device-height", Qt::CaseInsensitive))
|
|
271 |
return false;
|
|
272 |
}
|
|
273 |
return true;
|
|
274 |
}
|
|
275 |
|
|
276 |
bool ViewportMetaData::isUserSpecifiedHeight()
|
|
277 |
{
|
|
278 |
if (m_specifiedData.m_height.length()) {
|
|
279 |
if (!QString::compare(m_specifiedData.m_height, "device-width", Qt::CaseInsensitive)
|
|
280 |
|| !QString::compare(m_specifiedData.m_height, "device-height", Qt::CaseInsensitive))
|
|
281 |
return false;
|
|
282 |
}
|
|
283 |
return true;
|
|
284 |
}
|
|
285 |
|
|
286 |
}//namespace GVA
|
|
287 |
|