|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "GWebContentViewWidget.h" |
|
19 #include "GWebContentView.h" |
|
20 #include "browserpagefactory.h" |
|
21 #include "webpagecontroller.h" |
|
22 |
|
23 #include <QGraphicsSceneResizeEvent> |
|
24 #include <QGraphicsSceneContextMenuEvent> |
|
25 #include <QWebFrame> |
|
26 #include <QWebHitTestResult> |
|
27 #include <QPoint> |
|
28 #include <QStyleOptionGraphicsItem> |
|
29 #include <QDebug> |
|
30 #include <QGraphicsView> |
|
31 |
|
32 #ifdef ENABLE_PERF_TRACE |
|
33 #include "wrtperftracer.h" |
|
34 // Global reference to WrtPerfTracer |
|
35 extern WrtPerfTracer* g_wrtPerfTracing; |
|
36 #endif |
|
37 |
|
38 namespace GVA { |
|
39 |
|
40 const QString KViewPortWidthTag("width"); |
|
41 const QString KViewPortHeightTag("height"); |
|
42 const QString KViewPortInitialScaleTag("initial-scale"); |
|
43 const QString KViewPortMinScaleTag("minimum-scale"); |
|
44 const QString KViewPortMaxScaleTag("maximum-scale"); |
|
45 const QString KViewPortUserScalableTag("user-scalable"); |
|
46 const QString KViewPortDeviceWidthTag("device-width"); |
|
47 const QString KViewPortDeviceHeightTag("device-height"); |
|
48 |
|
49 |
|
50 const int KDefaultViewportWidth = 980; |
|
51 const int KDefaultPortraitScaleWidth = 540; |
|
52 const int KMinViewportWidth = 200; |
|
53 const int KMaxViewportWidth = 10000; |
|
54 const int KMinViewportHeight = 200; |
|
55 const int KMaxViewportHeight = 10000; |
|
56 const int KMaxPageZoom = 10; |
|
57 const qreal KDefaultMinScale = 0.25; |
|
58 const qreal KDefaultMaxScale = 10.00; |
|
59 const QPoint KFocussPoint(5, 50); |
|
60 const int checkerSize = 16; |
|
61 const unsigned checkerColor1 = 0xff555555; |
|
62 const unsigned checkerColor2 = 0xffaaaaaa; |
|
63 |
|
64 GWebContentViewWidget::GWebContentViewWidget(QObject* parent, GWebContentView* view,QWebPage* pg) : QGraphicsWebView(0) |
|
65 , m_webContentView(view) |
|
66 , m_dirtyZoomFactor(1) |
|
67 , m_frozenPixmap(0) |
|
68 , m_freezeCount(0) |
|
69 , m_wrtPage(0) |
|
70 , m_bitmapZoom(false) |
|
71 , m_pagePixmap(0) |
|
72 , m_isResize(false) |
|
73 , m_currentinitialScale(0) |
|
74 , m_previousViewPortwidth(size().toSize().width()) |
|
75 , m_bitmapImage(NULL) |
|
76 , m_checkeredBoxPixmap(NULL) |
|
77 { |
|
78 qDebug() << "GWebContentViewWidget::GWebContentViewWidget: page=" << pg; |
|
79 setParent(parent); |
|
80 if( pg ) { |
|
81 setPage(pg); |
|
82 } |
|
83 #ifdef ENABLE_PERF_TRACE |
|
84 g_wrtPerfTracing->initPage(pg); |
|
85 #endif //ENABLE_PERF_TRACE |
|
86 |
|
87 m_currentinitialScale = zoomFactor(); |
|
88 //connect(this->page()->mainFrame(), SIGNAL(initialLayoutCompleted()), this, SLOT(onInitLayout())); |
|
89 #ifndef NO_QSTM_GESTURE |
|
90 m_touchNavigation = new WebTouchNavigation(this); |
|
91 #endif |
|
92 } |
|
93 |
|
94 GWebContentViewWidget::~GWebContentViewWidget() |
|
95 { |
|
96 if (m_bitmapImage) |
|
97 delete m_bitmapImage; |
|
98 if (m_checkeredBoxPixmap) |
|
99 delete m_checkeredBoxPixmap; |
|
100 } |
|
101 |
|
102 void GWebContentViewWidget::updateViewport() |
|
103 { |
|
104 if (page() && size() != page()->viewportSize()) { |
|
105 page()->setViewportSize(size().toSize()); |
|
106 } |
|
107 setViewportSize(); |
|
108 } |
|
109 |
|
110 void GWebContentViewWidget::setBlockElement(QWebElement pt) |
|
111 { |
|
112 m_BlockElement = pt; |
|
113 } |
|
114 |
|
115 void GWebContentViewWidget::setCheckeredPixmap() |
|
116 { |
|
117 delete m_checkeredBoxPixmap; |
|
118 m_checkeredBoxPixmap = NULL; |
|
119 int checkerPixmapSizeX = size().toSize().width(); |
|
120 int checkerPixmapSizeY = size().toSize().height() + 50; |
|
121 m_checkeredBoxPixmap = new QPixmap(size().width(), size().height() + 50); |
|
122 QPainter painter(m_checkeredBoxPixmap); |
|
123 |
|
124 for (int y = 0; y < checkerPixmapSizeY; y += checkerSize / 2) { |
|
125 bool alternate = y % checkerSize; |
|
126 for (int x = 0; x < checkerPixmapSizeX; x += checkerSize / 2) { |
|
127 QColor color(alternate ? checkerColor1 : checkerColor2); |
|
128 painter.fillRect(x, y, checkerSize / 2, checkerSize / 2, color); |
|
129 alternate = !alternate; |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 void GWebContentViewWidget::createPageSnapShot() |
|
135 { |
|
136 bitmapZoomCleanup(); |
|
137 QRegion clipRegion; |
|
138 QWebFrame *frame = page()->mainFrame(); |
|
139 m_bitmapImage = new QImage(size().width() ,size().height(),QImage::Format_RGB32); |
|
140 clipRegion = QRect(QPoint(0,0),size().toSize()); |
|
141 QPainter painterImage(m_bitmapImage); |
|
142 painterImage.fillRect(0, 0, size().width(), size().height(), QColor(255, 255, 255)); |
|
143 frame->render(&painterImage,clipRegion); |
|
144 } |
|
145 |
|
146 void GWebContentViewWidget::bitmapZoomCleanup() |
|
147 { |
|
148 m_bitmapZoom = false; |
|
149 if (m_bitmapImage) { |
|
150 delete m_bitmapImage; |
|
151 m_bitmapImage = NULL; |
|
152 } |
|
153 } |
|
154 |
|
155 QImage GWebContentViewWidget::getPageSnapshot() |
|
156 { |
|
157 QImage img(size().toSize(), QImage::Format_RGB32); |
|
158 |
|
159 if(!page()) return QImage(); |
|
160 |
|
161 QPainter painter(&img); |
|
162 QWebFrame *frame = page()->mainFrame(); |
|
163 |
|
164 painter.fillRect(QRectF(0, 0, size().width(), size().height()), QColor(255, 255, 255)); |
|
165 // QTransform transform; |
|
166 // transform.scale(d->m_pageZoomFactor, d->m_pageZoomFactor); |
|
167 // painter.translate(-transform.map(frame->scrollPosition())); |
|
168 |
|
169 QRegion clipRegion(QRect(QPoint(0,0),size().toSize())); |
|
170 // QTransform invert = transform.inverted(); |
|
171 // clipRegion = invert.map(clipRegion); |
|
172 // clipRegion.translate(frame->scrollPosition()); |
|
173 |
|
174 // painter.scale(d->m_pageZoomFactor, d->m_pageZoomFactor); |
|
175 // d->m_webPage->mainFrame()->renderContents(&painter, clipRegion); |
|
176 frame->render(&painter, clipRegion); |
|
177 |
|
178 return img; |
|
179 } |
|
180 |
|
181 void GWebContentViewWidget::updateViewportSize(::QGraphicsSceneResizeEvent* e) |
|
182 { |
|
183 //if there is change in mode (like landscape, potraite relayout the content) |
|
184 if (e->newSize().width() == e->oldSize().width()) |
|
185 return; |
|
186 m_isResize = true; |
|
187 setViewportSize(); |
|
188 m_isResize = false; |
|
189 } |
|
190 |
|
191 bool GWebContentViewWidget::event(QEvent * e) { |
|
192 |
|
193 if (e->type() == QEvent::Gesture) { |
|
194 #ifndef NO_QSTM_GESTURE |
|
195 QStm_Gesture* gesture = getQStmGesture(e, WebGestureHelper::getAssignedGestureType()); |
|
196 if (gesture) { |
|
197 m_touchNavigation->handleQStmGesture(gesture); |
|
198 return true; |
|
199 } |
|
200 #endif |
|
201 } |
|
202 |
|
203 else if(e->type() == WebPageControllerUpdateViewPortEvent::staticType()) { |
|
204 updateViewport(); |
|
205 } |
|
206 return QGraphicsWebView::event(e); |
|
207 } |
|
208 |
|
209 void GWebContentViewWidget::resizeEvent(QGraphicsSceneResizeEvent* e) |
|
210 { |
|
211 // set the fixed text layout size for text wrapping |
|
212 #if defined CWRTINTERNALWEBKIT |
|
213 if (page()) { |
|
214 p->m_webPage->settings()->setMaximumTextColumnWidth(e->newSize().width() - 6); |
|
215 } |
|
216 #endif |
|
217 |
|
218 m_previousViewPortwidth = page()->viewportSize().width(); |
|
219 |
|
220 const QSize &s = e->newSize().toSize(); |
|
221 if (page() && s != page()->viewportSize()) { |
|
222 if(m_BlockElement.isNull()) { |
|
223 QPoint pos = QPoint(0,0); |
|
224 QWebFrame* frame = page()->frameAt(pos); |
|
225 frame = (frame) ? frame : page()->currentFrame(); |
|
226 QWebHitTestResult htr = frame->hitTestContent(pos); |
|
227 m_BlockInFocus = htr.element(); |
|
228 |
|
229 if(m_BlockInFocus.tagName() != "IMG") |
|
230 m_BlockInFocus = htr.enclosingBlockElement(); |
|
231 |
|
232 QPoint position = m_BlockInFocus.geometry().topLeft() - page()->currentFrame()->scrollPosition(); |
|
233 m_Ratiox = (qreal) position.x() / m_BlockInFocus.geometry().width(); |
|
234 m_Ratioy = (qreal) position.y() / m_BlockInFocus.geometry().height(); |
|
235 } |
|
236 page()->setViewportSize(s); |
|
237 } |
|
238 |
|
239 updateViewportSize(e); |
|
240 } |
|
241 |
|
242 void GWebContentViewWidget::contextMenuEvent(::QGraphicsSceneContextMenuEvent *event) { |
|
243 qDebug() << "GWebContentViewWidget::contextMenuEvent: " << event; |
|
244 QPoint p = mapFromGlobal(event->scenePos()).toPoint(); |
|
245 QWebHitTestResult hitTest = page()->currentFrame()->hitTestContent(p); |
|
246 |
|
247 WebViewEventContext *context = |
|
248 new WebViewEventContext(view()->type(), hitTest); |
|
249 |
|
250 emit contextEvent(context); |
|
251 event->accept(); |
|
252 } |
|
253 |
|
254 void GWebContentViewWidget::setZoomFactor(qreal zoom) |
|
255 { |
|
256 this->setFocus(); |
|
257 if (!m_userScalable) |
|
258 return; |
|
259 |
|
260 setPageZoomFactor(zoom); |
|
261 } |
|
262 |
|
263 void GWebContentViewWidget::setPageZoomFactor(qreal zoom) |
|
264 { |
|
265 if(!page()) return; |
|
266 |
|
267 //qDebug() << __func__ << "Zoom " << zoom << "Max : " << m_maximumScale << "Min: " << m_minimumScale; |
|
268 |
|
269 if (zoom < m_minimumScale) |
|
270 zoom = m_minimumScale; |
|
271 else if (zoom > m_maximumScale) |
|
272 zoom = m_maximumScale; |
|
273 |
|
274 QPoint pos = QPoint(0,0); |
|
275 |
|
276 if(!m_isResize) { |
|
277 QWebFrame* frame = page()->frameAt(pos); |
|
278 frame = (frame) ? frame : page()->currentFrame(); |
|
279 QWebHitTestResult htr = frame->hitTestContent(pos); |
|
280 m_BlockInFocus = htr.element(); |
|
281 |
|
282 if(m_BlockInFocus.tagName() != "IMG") |
|
283 m_BlockInFocus = htr.enclosingBlockElement(); |
|
284 |
|
285 QPoint position = m_BlockInFocus.geometry().topLeft() - page()->currentFrame()->scrollPosition(); |
|
286 m_Ratiox = (qreal) position.x() / m_BlockInFocus.geometry().width(); |
|
287 m_Ratioy = (qreal) position.y() / m_BlockInFocus.geometry().height(); |
|
288 } |
|
289 |
|
290 if( m_dirtyZoomFactor != zoom ) { |
|
291 m_dirtyZoomFactor = zoom; |
|
292 } |
|
293 |
|
294 QGraphicsWebView::setZoomFactor( zoom ); |
|
295 |
|
296 if(!m_BlockElement.isNull() && m_isResize) { |
|
297 QPoint imageFocusPoint; |
|
298 QPoint m_focusedBlockPt = QPoint(m_BlockElement.geometry().topLeft()) - page()->mainFrame()->scrollPosition(); |
|
299 if(m_BlockElement.tagName() != "IMG" && (m_BlockElement.styleProperty(QString("background-image"),QWebElement::InlineStyle) == "")) |
|
300 page()->mainFrame()->scroll(m_focusedBlockPt.x() - KFocussPoint.x() , m_focusedBlockPt.y() - KFocussPoint.y()); |
|
301 else { |
|
302 if((page()->viewportSize().width() - m_BlockElement.geometry().width()) > 0) |
|
303 imageFocusPoint.setX((page()->viewportSize().width() - m_BlockElement.geometry().width())/2); |
|
304 else |
|
305 imageFocusPoint.setX(0); |
|
306 |
|
307 if((page()->viewportSize().height() - m_BlockElement.geometry().height()) > 0) |
|
308 imageFocusPoint.setY((page()->viewportSize().height() - m_BlockElement.geometry().height())/2); |
|
309 else |
|
310 imageFocusPoint.setY(0); |
|
311 |
|
312 page()->mainFrame()->scroll(m_focusedBlockPt.x() - imageFocusPoint.x() , |
|
313 m_focusedBlockPt.y() - imageFocusPoint.y()); |
|
314 } |
|
315 m_focusedBlockPt = QPoint(m_BlockElement.geometry().topLeft()) - page()->mainFrame()->scrollPosition(); |
|
316 emit BlockFocusChanged(m_focusedBlockPt); |
|
317 } else { |
|
318 QPoint m_focusedBlockPt = QPoint(m_BlockInFocus.geometry().topLeft()) - page()->mainFrame()->scrollPosition(); |
|
319 page()->currentFrame()->scroll(m_focusedBlockPt.x() - (m_Ratiox * m_BlockInFocus.geometry().width()), |
|
320 m_focusedBlockPt.y() - (m_Ratioy * m_BlockInFocus.geometry().height())); |
|
321 m_BlockElement = QWebElement(); |
|
322 } |
|
323 |
|
324 m_webContentView->changeZoomAction(zoom); |
|
325 |
|
326 } |
|
327 |
|
328 void GWebContentViewWidget::setDirtyZoomFactor(qreal zoom) |
|
329 { |
|
330 if( m_dirtyZoomFactor == zoom ) |
|
331 return; |
|
332 |
|
333 m_dirtyZoomFactor = zoom; |
|
334 |
|
335 update(); |
|
336 } |
|
337 |
|
338 |
|
339 void GWebContentViewWidget::onLoadStarted() |
|
340 { |
|
341 m_inLoading = true; |
|
342 m_loadingTime.start(); |
|
343 } |
|
344 |
|
345 void GWebContentViewWidget::onLoadFinished() |
|
346 { |
|
347 m_inLoading = false; |
|
348 update(); |
|
349 } |
|
350 |
|
351 #ifdef NO_RESIZE_ON_LOAD |
|
352 void GWebContentViewWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* options, QWidget* widget) |
|
353 { |
|
354 QRect clipRect; |
|
355 if(options && !options->exposedRect.isEmpty()) |
|
356 clipRect = options->exposedRect.toRect(); |
|
357 else |
|
358 { |
|
359 clipRect = geometry().toRect(); |
|
360 clipRect.moveTo(0,0); |
|
361 |
|
362 } |
|
363 |
|
364 painter->fillRect(clipRect, QColor(255, 255, 255)); |
|
365 if (!m_inLoading || !(m_loadingTime.elapsed() < 750)) { |
|
366 QGraphicsWebView::paint(painter, options, widget); |
|
367 } |
|
368 } |
|
369 |
|
370 #else //NO_RESIZE_ON_LOAD |
|
371 void GWebContentViewWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) |
|
372 { |
|
373 |
|
374 // if(!m_active) return; |
|
375 |
|
376 if(m_freezeCount > 0) { |
|
377 // Frozen, paint the snapshot. |
|
378 painter->drawPixmap(0, 0, *m_frozenPixmap); |
|
379 } |
|
380 else { |
|
381 if (m_bitmapZoom) { |
|
382 qreal czf = 1; |
|
383 qreal zoomF = zoomFactor(); |
|
384 |
|
385 if ( m_bitmapZoomFactor != zoomF ) |
|
386 czf = m_bitmapZoomFactor/zoomF; |
|
387 |
|
388 painter->save(); |
|
389 |
|
390 if (czf < 1) |
|
391 painter->drawPixmap(QPoint(0,0), *m_checkeredBoxPixmap); |
|
392 |
|
393 painter->drawImage(QRectF(0,0,size().width() * czf,size().height() * czf), *m_bitmapImage); |
|
394 painter->restore(); |
|
395 } |
|
396 |
|
397 // Not frozen, do normal paint. |
|
398 else if( zoomFactor() == m_dirtyZoomFactor ) |
|
399 { |
|
400 // Cannot use normal QGraphicsWebView paint because have to fill background with white for the phone build |
|
401 // QGraphicsWebView::paintEvent( event); |
|
402 |
|
403 // Commented the following line out to try to improve scrolling performance. hab - 3/25/10 |
|
404 //painter->fillRect(0, 0, size().width(), size().height(), QColor(255, 255, 255)); |
|
405 |
|
406 //painter->fillRect(clipRect, QColor(255, 255, 255)); |
|
407 QGraphicsWebView::paint(painter, option, widget); |
|
408 } else { |
|
409 qreal czf = m_dirtyZoomFactor / zoomFactor(); |
|
410 |
|
411 QWebFrame* frame = page()->mainFrame(); |
|
412 |
|
413 painter->save(); |
|
414 |
|
415 // Commented the following line out to try to improve scrolling performance. hab - 3/25/10 |
|
416 //painter->fillRect(0, 0, size().width(), size().height(), QColor(255, 255, 255)); |
|
417 |
|
418 QTransform transform; |
|
419 transform.scale(czf, czf); |
|
420 // painter.translate(-transform.map(frame->scrollPosition())); |
|
421 |
|
422 QRegion clipRegion = QRect(QPoint(0,0), size().toSize()); |
|
423 if(option && !option->exposedRect.isEmpty()) |
|
424 { |
|
425 clipRegion.intersect( option->exposedRect.toRect()); |
|
426 } |
|
427 QTransform invert = transform.inverted(); |
|
428 clipRegion = invert.map(clipRegion); |
|
429 // clipRegion.translate(frame->scrollPosition()); |
|
430 |
|
431 painter->scale(czf, czf); |
|
432 // p->m_webPage->mainFrame()->renderContents(&painter, clipRegion); |
|
433 frame->render(painter, clipRegion); |
|
434 painter->restore(); |
|
435 } |
|
436 |
|
437 } |
|
438 } |
|
439 #endif //NO_RESIZE_ON_LOAD |
|
440 |
|
441 void GWebContentViewWidget::setPage(QWebPage* pg) |
|
442 { |
|
443 if(m_wrtPage == pg) return; |
|
444 |
|
445 qDebug() << "GWebContentViewWidget::setPage: " << pg; |
|
446 if(m_wrtPage) { |
|
447 disconnect(page()->mainFrame(), 0, this, 0); |
|
448 m_wrtPage->setView(0); |
|
449 } |
|
450 /* Reset the webview page as well - for its internal clean up */ |
|
451 QGraphicsWebView::setPage(pg); |
|
452 |
|
453 m_wrtPage = pg; |
|
454 if(page()) { |
|
455 #ifndef NO_RESIZE_ON_LOAD |
|
456 connect(page()->mainFrame(), SIGNAL(initialLayoutCompleted()), this, SLOT(setViewportSize())); |
|
457 #endif |
|
458 |
|
459 } |
|
460 |
|
461 // setPage() above doesn't seem to trigger an update, do it explicitly. |
|
462 update(); |
|
463 |
|
464 emit titleChanged(title()); |
|
465 emit urlChanged(url().toString()); |
|
466 } |
|
467 |
|
468 void GWebContentViewWidget::showNormalPage() |
|
469 { |
|
470 WRT::WrtBrowserContainer *wbc = WebPageController::getSingleton()->currentPage(); |
|
471 setPage((QWebPage *)wbc); // static_cast here gives compiler error |
|
472 } |
|
473 |
|
474 void GWebContentViewWidget::createPagePixmap() |
|
475 { |
|
476 if (m_pagePixmap) |
|
477 delete m_pagePixmap; |
|
478 |
|
479 m_pagePixmap = new QPixmap(size().toSize()); |
|
480 QStyleOptionGraphicsItem op; |
|
481 QPainter p(m_pagePixmap); |
|
482 paint(&p,&op,0); |
|
483 p.end(); |
|
484 } |
|
485 |
|
486 void GWebContentViewWidget::setBitmapZoom(qreal zoom) |
|
487 { |
|
488 if (!m_userScalable || zoom == zoomFactor()) |
|
489 return; |
|
490 |
|
491 if (zoom < m_minimumScale) |
|
492 zoom = m_minimumScale; |
|
493 else if (zoom > m_maximumScale) |
|
494 zoom = m_maximumScale; |
|
495 |
|
496 m_bitmapZoom = true; |
|
497 m_bitmapZoomFactor = zoom; |
|
498 update(); |
|
499 } |
|
500 |
|
501 void GWebContentViewWidget::deletePagePixmap() |
|
502 { |
|
503 if (m_pagePixmap) { |
|
504 delete m_pagePixmap; |
|
505 m_pagePixmap = 0; |
|
506 } |
|
507 m_bitmapZoom = false; |
|
508 } |
|
509 |
|
510 void GWebContentViewWidget::setPageCenterZoomFactor(qreal zoom) |
|
511 { |
|
512 //calculating the center of the widget |
|
513 QPoint widgetCenter = rect().center().toPoint(); |
|
514 //find the content size before applying zoom |
|
515 QSize docSizeBeforeZoom = page()->mainFrame()->contentsSize(); |
|
516 |
|
517 qDebug()<<"setPageCenterZoomFactor() : "<<zoom; |
|
518 setZoomFactor(zoom); |
|
519 //after applying zoom calculate the document size and document center point |
|
520 QSize docSizeAfterZoom = page()->mainFrame()->contentsSize(); |
|
521 QPoint docPoint = widgetCenter + page()->mainFrame()->scrollPosition(); |
|
522 |
|
523 //calculate the shift in center point after applying zoom |
|
524 int dx = docSizeAfterZoom.width() * docPoint.x() / docSizeBeforeZoom.width(); |
|
525 int dy = docSizeAfterZoom.height() * docPoint.y() / docSizeBeforeZoom.height(); |
|
526 |
|
527 //move back the shifted center |
|
528 page()->mainFrame()->scroll(dx-docPoint.x(), dy-docPoint.y()); |
|
529 } |
|
530 |
|
531 void GWebContentViewWidget::initializeViewportParams() |
|
532 { |
|
533 m_maximumScale = KDefaultMaxScale; |
|
534 m_userScalable = true; |
|
535 m_inferWidthHeight = true; |
|
536 |
|
537 m_aspectRation = size().width() / size().height(); |
|
538 |
|
539 #ifdef NO_RESIZE_ON_LOAD |
|
540 QSize sz = size().toSize(); |
|
541 m_viewportWidth = sz.width(); |
|
542 m_viewportHeight = sz.height(); |
|
543 m_initialScale = 1.0; |
|
544 #else |
|
545 m_viewportWidth = KDefaultViewportWidth; |
|
546 m_viewportHeight = (int)size().height(); |
|
547 |
|
548 if( size().width() < size().height()) //if Portrait |
|
549 m_initialScale = size().width() / KDefaultPortraitScaleWidth; |
|
550 else |
|
551 m_initialScale = size().width() / KDefaultViewportWidth; |
|
552 #endif //NO_RESIZE_ON_LOAD |
|
553 |
|
554 m_minimumScale = m_initialScale; |
|
555 } |
|
556 |
|
557 /*! |
|
558 * Provides the default values - used when opening a new blank window |
|
559 */ |
|
560 ZoomMetaData GWebContentViewWidget::defaultZoomData() |
|
561 { |
|
562 ZoomMetaData data; |
|
563 |
|
564 data.maxScale = KDefaultMaxScale; |
|
565 data.minScale = KDefaultMinScale; |
|
566 data.userScalable = false; |
|
567 |
|
568 return data; |
|
569 } |
|
570 |
|
571 /*! |
|
572 * Set the viewport Size |
|
573 */ |
|
574 void GWebContentViewWidget::setViewportSize() |
|
575 { |
|
576 if(!page()) return; |
|
577 |
|
578 QWebFrame* frame = page()->mainFrame(); |
|
579 |
|
580 initializeViewportParams(); |
|
581 |
|
582 // TODO: INVESTIGATE: In the case of multiple windows loading pages simultaneously, it is possible |
|
583 // to be calling this slot on a signal from a frame that is not |
|
584 // the frame of the page saved here. It might be better to use 'sender' instead of |
|
585 // page->mainFrame() to get the metaData so that we use the meta data of the corresponding |
|
586 // frame |
|
587 QMap<QString, QString> metaData = frame->metaData(); |
|
588 QString viewportTag = metaData.value("viewport"); |
|
589 |
|
590 if (!viewportTag.isEmpty()) { |
|
591 QStringList paramList; |
|
592 |
|
593 if (viewportTag.contains(';')) { |
|
594 paramList = viewportTag.split(";", QString::SkipEmptyParts); |
|
595 } else { |
|
596 paramList = viewportTag.split(",", QString::SkipEmptyParts); |
|
597 } |
|
598 |
|
599 int paramCount = 0; |
|
600 while (paramCount < paramList.count()) { |
|
601 QStringList subParamList = paramList[paramCount].split ('=', QString::SkipEmptyParts); |
|
602 paramCount++; |
|
603 QString viewportProperty = subParamList.front(); |
|
604 QString propertyValue = subParamList.back(); |
|
605 parseViewPortParam(viewportProperty.trimmed(), propertyValue.trimmed()); |
|
606 } |
|
607 } |
|
608 |
|
609 m_initialScale = qBound(m_minimumScale, m_initialScale, m_maximumScale); |
|
610 |
|
611 #if QT_VERSION < 0x040600 |
|
612 page()->setFixedContentsSize(QSize(m_viewportWidth, m_viewportHeight)); |
|
613 #else |
|
614 page()->setPreferredContentsSize(QSize((int)m_viewportWidth, (int)m_viewportHeight)); |
|
615 #endif |
|
616 |
|
617 #ifndef NO_RESIZE_ON_LOAD |
|
618 qreal zoomF = 0.0; |
|
619 QString str; |
|
620 if(m_isResize && (m_currentinitialScale != zoomFactor())) { |
|
621 zoomF = ((qreal)(page()->viewportSize().width()-10) * zoomFactor())/(m_previousViewPortwidth-10); |
|
622 str.setNum(zoomF,'f',2); |
|
623 zoomF = str.toDouble(); |
|
624 setPageZoomFactor(zoomF); |
|
625 } |
|
626 else { |
|
627 setPageZoomFactor(m_initialScale); |
|
628 } |
|
629 m_BlockInFocus = QWebElement(); |
|
630 m_currentinitialScale = m_initialScale; |
|
631 #endif //NO_RESIZE_ON_LOAD |
|
632 |
|
633 setCheckeredPixmap(); |
|
634 // Let the page save the data. Even though it is part of the frame, it is easier to |
|
635 // save the info in the page to avoid parsing the meta data again. |
|
636 emit pageZoomMetaDataChange(frame, pageZoomMetaData()); |
|
637 } |
|
638 |
|
639 qreal GWebContentViewWidget::initialScale() |
|
640 { |
|
641 return m_initialScale; |
|
642 } |
|
643 |
|
644 void GWebContentViewWidget::parseViewPortParam(const QString &propertyName, const QString &propertyValue) |
|
645 { |
|
646 if (propertyName == KViewPortWidthTag) { |
|
647 if (propertyValue == KViewPortDeviceWidthTag) { |
|
648 m_viewportWidth = size().width(); |
|
649 m_viewportHeight = m_viewportWidth * m_aspectRation; |
|
650 } |
|
651 else if(propertyValue == KViewPortDeviceHeightTag) { |
|
652 m_viewportWidth = size().height(); |
|
653 m_viewportHeight = m_viewportWidth * m_aspectRation; |
|
654 } |
|
655 else { |
|
656 m_viewportWidth = propertyValue.toInt(); |
|
657 |
|
658 if (m_viewportWidth < KMinViewportWidth) |
|
659 m_viewportWidth = KMinViewportWidth; |
|
660 else if (m_viewportWidth > KMaxViewportWidth) |
|
661 m_viewportWidth = KMaxViewportWidth; |
|
662 |
|
663 m_viewportHeight = m_viewportWidth * m_aspectRation; |
|
664 } |
|
665 m_initialScale = size().width() / m_viewportWidth; |
|
666 if (m_initialScale < KDefaultMinScale || m_initialScale > KDefaultMaxScale) |
|
667 m_initialScale = KDefaultMinScale; |
|
668 m_minimumScale = m_initialScale; |
|
669 m_inferWidthHeight = false; |
|
670 } |
|
671 else if (propertyName == KViewPortHeightTag) { |
|
672 if (propertyValue == KViewPortDeviceWidthTag) { |
|
673 m_viewportHeight = (int)size().width(); |
|
674 m_viewportWidth = m_viewportHeight * m_aspectRation; |
|
675 } |
|
676 else if (propertyValue == KViewPortDeviceHeightTag) { |
|
677 m_viewportHeight = (int)size().height(); |
|
678 m_viewportWidth = m_viewportHeight * m_aspectRation; |
|
679 } |
|
680 else { |
|
681 m_viewportHeight = propertyValue.toInt(); |
|
682 |
|
683 if (m_viewportHeight < KMinViewportHeight) |
|
684 m_viewportHeight = KMinViewportHeight; |
|
685 else if (m_viewportHeight > KMaxViewportHeight) |
|
686 m_viewportHeight = KMaxViewportHeight; |
|
687 |
|
688 m_viewportWidth = m_viewportHeight * m_aspectRation; |
|
689 } |
|
690 m_initialScale = size().height() / m_viewportHeight; |
|
691 if (m_initialScale < KDefaultMinScale || m_initialScale > KDefaultMaxScale) |
|
692 m_initialScale = KDefaultMinScale; |
|
693 m_minimumScale = m_initialScale; |
|
694 m_inferWidthHeight = false; |
|
695 } |
|
696 else if (propertyName == KViewPortInitialScaleTag) { |
|
697 m_initialScale = propertyValue.toDouble(); |
|
698 if (m_inferWidthHeight) { |
|
699 m_viewportWidth = (int)size().width(); |
|
700 m_viewportHeight = m_viewportWidth * m_aspectRation; |
|
701 } |
|
702 } |
|
703 else if (propertyName == KViewPortMinScaleTag) { |
|
704 m_minimumScale = propertyValue.toDouble(); |
|
705 if (m_minimumScale < 0 |
|
706 || m_minimumScale > KMaxPageZoom |
|
707 || m_minimumScale > m_maximumScale) |
|
708 m_minimumScale = KDefaultMinScale; |
|
709 } |
|
710 else if (propertyName == KViewPortMaxScaleTag) { |
|
711 m_maximumScale = propertyValue.toDouble(); |
|
712 if (m_maximumScale < 0 |
|
713 || m_maximumScale > KMaxPageZoom |
|
714 || m_maximumScale < m_minimumScale) |
|
715 |
|
716 m_maximumScale = KDefaultMaxScale; |
|
717 } |
|
718 else if (propertyName == KViewPortUserScalableTag) { |
|
719 if (propertyValue =="no" || propertyValue =="0") |
|
720 { |
|
721 m_userScalable = false; |
|
722 view()->deactivateZoomActions(); |
|
723 } |
|
724 else |
|
725 m_userScalable = true; |
|
726 } |
|
727 } |
|
728 |
|
729 |
|
730 bool GWebContentViewWidget::isUserScalable() |
|
731 { |
|
732 return m_userScalable; |
|
733 } |
|
734 |
|
735 qreal GWebContentViewWidget::minimumScale() |
|
736 { |
|
737 return m_minimumScale; |
|
738 } |
|
739 |
|
740 qreal GWebContentViewWidget::maximumScale() |
|
741 { |
|
742 return m_maximumScale; |
|
743 } |
|
744 |
|
745 ZoomMetaData GWebContentViewWidget::pageZoomMetaData() { |
|
746 |
|
747 ZoomMetaData data; |
|
748 |
|
749 data.minScale = m_minimumScale; |
|
750 data.maxScale = m_maximumScale; |
|
751 data.userScalable = m_userScalable; |
|
752 |
|
753 return data; |
|
754 } |
|
755 |
|
756 void GWebContentViewWidget::setPageZoomMetaData(ZoomMetaData data) { |
|
757 |
|
758 m_minimumScale = data.minScale ; |
|
759 m_maximumScale = data.maxScale ; |
|
760 m_userScalable = data.userScalable; |
|
761 } |
|
762 |
|
763 QWebPage* GWebContentViewWidget::page() const |
|
764 { |
|
765 if (!m_wrtPage) { |
|
766 GWebContentViewWidget* that = const_cast<GWebContentViewWidget*>(this); |
|
767 that->setPage(BrowserPageFactory::openBrowserPage()); |
|
768 } |
|
769 return m_wrtPage; |
|
770 } |
|
771 |
|
772 QPointF GWebContentViewWidget::mapToGlobal(const QPointF& p) |
|
773 { |
|
774 QList<QGraphicsView*> gvList = scene()->views(); |
|
775 QList<QGraphicsView*>::iterator it; |
|
776 for(it = gvList.begin(); it != gvList.end(); it++) |
|
777 { |
|
778 if (static_cast<QGraphicsView*>(*it)->hasFocus()) |
|
779 { |
|
780 QWidget* viewport = static_cast<QGraphicsView*>(*it)->viewport(); |
|
781 return viewport->mapToGlobal(mapToScene(p).toPoint()); |
|
782 } |
|
783 } |
|
784 |
|
785 return QPoint(0, 0); |
|
786 } |
|
787 |
|
788 QPointF GWebContentViewWidget::mapFromGlobal(const QPointF& p) |
|
789 { |
|
790 QList<QGraphicsView*> gvList = scene()->views(); |
|
791 QList<QGraphicsView*>::iterator it; |
|
792 for(it = gvList.begin(); it != gvList.end(); it++) |
|
793 { |
|
794 if (static_cast<QGraphicsView*>(*it)->hasFocus()) |
|
795 { |
|
796 QWidget* viewport = static_cast<QGraphicsView*>(*it)->viewport(); |
|
797 return mapFromScene(viewport->mapFromGlobal(p.toPoint())); |
|
798 } |
|
799 } |
|
800 |
|
801 return QPoint(0, 0); |
|
802 } |
|
803 |
|
804 void GWebContentViewWidget::setTextSizeMultiplier(qreal factor) |
|
805 { |
|
806 page()->mainFrame()->setTextSizeMultiplier(factor); |
|
807 } |
|
808 |
|
809 |
|
810 void GWebContentViewWidget::onInitLayout() |
|
811 { |
|
812 update(); |
|
813 } |
|
814 |
|
815 } |