ginebra2/ContentViews/ViewportMetaData.cpp
changeset 16 3c88a81ff781
parent 3 0954f5dd2cd0
--- a/ginebra2/ContentViews/ViewportMetaData.cpp	Thu Sep 23 15:32:11 2010 -0400
+++ b/ginebra2/ContentViews/ViewportMetaData.cpp	Fri Oct 15 17:30:59 2010 -0400
@@ -20,20 +20,33 @@
 */
 
 #include "ViewportMetaData.h"
+#include <QApplication>
+#include <QDesktopWidget>
+#include "WebGestureHelper.h"
 
-static const int DefaultViewportWidth = 640;
-static const int DefaultViewportHeight = 640;
+
 static const int MinViewportWidth = 200;
 static const int MaxViewportWidth = 10000;
 static const int MinViewportHeight = 200;
 static const int MaxViewportHeight = 10000;
 static const qreal DefaultMinScale = 0.2;
-static const qreal DefaultMaxScale = 5.;
+static const qreal DefaultMaxScale = 3.;
+static const qreal DefaultPageLayoutScale = 0.667;
 
 namespace GVA {
 
 ViewportMetaData::ViewportMetaData()
 {
+    #ifndef Q_WS_MAEMO_5
+    QRect screenGeom = browserApp->mainWindow()->geometry();
+    m_defaultViewportWidth = screenGeom.width();
+    m_defaultViewportHeight = screenGeom.height();
+    #else
+    m_defaultViewportWidth = 700;
+    m_defaultViewportHeight = 400;
+    #endif
+    m_maxViewportWidth = m_defaultViewportWidth;
+    m_maxViewportHeight = m_defaultViewportHeight;
     initialize();
 }
 
@@ -50,6 +63,9 @@
     m_specifiedData.m_width = other.m_specifiedData.m_width;
     m_specifiedData.m_height = other.m_specifiedData.m_height;
     m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
+#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
+    m_userScalableOverRidden = other.m_userScalableOverRidden;
+#endif
 }
 
 ViewportMetaData& ViewportMetaData::operator=(const ViewportMetaData& other)
@@ -65,6 +81,9 @@
     m_specifiedData.m_width = other.m_specifiedData.m_width;
     m_specifiedData.m_height = other.m_specifiedData.m_height;
     m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
+#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
+    m_userScalableOverRidden = other.m_userScalableOverRidden;
+#endif
 
     return *this;
 }
@@ -72,50 +91,74 @@
 ViewportMetaData::~ViewportMetaData()
 {}
 
-void ViewportMetaData::adjustViewportData(const QRect& clientRect)
+QSize ViewportMetaData::getSpecifiedSize() const
+{
+    QSize ret(m_maxViewportWidth / DefaultPageLayoutScale,
+              m_maxViewportHeight / DefaultPageLayoutScale);
+    if(m_initialScale > 0)
+        ret = QSize(m_maxViewportWidth / m_initialScale,
+                    m_maxViewportHeight / m_initialScale);
+
+    // assign specified data and swap if necessary
+    if(!m_specifiedData.m_width.isEmpty() && m_specifiedData.m_width != "device-width") {
+
+        if(m_specifiedData.m_width == "device-height")
+            ret.setWidth(m_maxViewportHeight);
+        else
+            ret.setWidth(m_specifiedData.m_width.toInt());
+    }
+
+    if(!m_specifiedData.m_height.isEmpty() && m_specifiedData.m_height != "device-height") {
+        if(m_specifiedData.m_height == "device-width")
+            ret.setHeight(m_maxViewportWidth);
+        else
+            ret.setHeight(m_specifiedData.m_height.toInt());
+    }
+
+    return ret;
+}
+
+void ViewportMetaData::adjustViewportData(const QSizeF& clientRect)
 {
     //Data updated from viewport tag
     m_isValid = true;
 
+    m_maxViewportWidth = clientRect.width();
+    m_maxViewportHeight = clientRect.height();
+
+    QSize s = getSpecifiedSize();
+
+//    if((m_maxViewportWidth > m_maxViewportHeight && m_width < m_height) ||
+//       (m_maxViewportWidth < m_maxViewportHeight && m_width > m_height))
+//        qSwap(m_width,m_height);
+
+/*
     //Adjust viewport dimensions
-    m_width = qBound(MinViewportWidth, m_width, MaxViewportWidth);
-    m_height = qBound(MinViewportHeight, m_height, MaxViewportHeight);
+    m_width = qBound(MinViewportWidth, m_width, m_maxViewportWidth);
+    m_height = qBound(MinViewportHeight, m_height, m_maxViewportHeight);
 
     //Aspect ratio
     qreal aspectRation = (qreal)clientRect.width() / clientRect.height();
 
-    if (m_width != DefaultViewportWidth && m_height == DefaultViewportHeight) {
+    if (m_width != m_maxViewportWidth && m_height == m_maxViewportHeight) {
         //Width has been specified. Adjust height, min scale and max scale
         m_height = m_width * (1 / aspectRation);
-    } else if (m_width == DefaultViewportWidth && m_height != DefaultViewportHeight) {
+    } else if (m_width == m_maxViewportWidth && m_height != m_maxViewportHeight) {
         //Height has been specified. Adjust width, min scale and max scale
         m_width = m_height * aspectRation;
     } else {
         //Putting below code under seperate 'else' to make it readable!
         m_height = m_width * (1 / aspectRation);
     }
-
-    //Adjust zoom limits
-    adjustZoomValues(clientRect);
+*/
+    m_width = qBound(MinViewportWidth, s.width(), m_maxViewportWidth);
+    m_height = qBound(MinViewportHeight, s.height(), m_maxViewportHeight);
 }
 
-void ViewportMetaData::updateViewportData(const QSize& newContentSize, const QRect& clientRect)
+
+void ViewportMetaData::orientationChanged(const QSizeF& newClientRect)
 {
-    //If still viewport tag has not been parsed
-    //Do not update values.
-    if(!m_isValid)
-        return;
-
-    //Update with viewport dimensions
-    m_width = qBound(MinViewportWidth, newContentSize.width(), MaxViewportWidth);
-    m_height = qBound(MinViewportHeight, newContentSize.height(), MaxViewportHeight);
-
-    //Adjust zoom limits
-    adjustZoomValues(clientRect);
-}
-
-void ViewportMetaData::orientationChanged(const QRect& newClientRect)
-{
+    /*
     //If still viewport tag has not been parsed
     //Do not update values.
     if(!m_isValid)
@@ -145,8 +188,14 @@
     }
 
     //Update with bounds
-    m_width = qBound(MinViewportWidth, m_width, MaxViewportWidth);
-    m_height = qBound(MinViewportHeight, m_height, MaxViewportHeight);
+    //m_width = qBound(MinViewportWidth, m_width, m_maxViewportWidth);
+    //m_height = qBound(MinViewportHeight, m_height, m_maxViewportHeight);
+     
+     */
+
+    m_width = newClientRect.height();
+    m_height = newClientRect.width();
+
 }
 
 bool ViewportMetaData::isLayoutNeeded()
@@ -160,25 +209,29 @@
     m_initialScale = ValueUndefined;
     m_minimumScale = DefaultMinScale;
     m_maximumScale = DefaultMaxScale;
-    m_width = DefaultViewportWidth;
-    m_height = DefaultViewportHeight;
+    m_width = m_maxViewportWidth; //m_defaultViewportWidth;
+    m_height = m_maxViewportHeight; //m_defaultViewportHeight;
     m_userScalable = true;
     m_isValid = false;
+#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
+    m_userScalableOverRidden = false;
+#endif
 
     //Clear user defined scales
     setFlag(UserDefinedMinumumScale, false);
     setFlag(UserDefinedMaximumScale, false);
     setFlag(UserDefinedInitialScale, false);
+    m_specifiedData.m_minScale = -1;
 }
 
-void ViewportMetaData::adjustZoomValues(const QRect& clientRect)
+void ViewportMetaData::adjustZoomValues(const QSizeF& contentSize)
 {
-    qreal fitToWidthZoom = (qreal)clientRect.width() / m_width;
+    qreal fitToWidthZoom = m_width / (qreal)contentSize.width();
     fitToWidthZoom = qBound(DefaultMinScale, fitToWidthZoom, DefaultMaxScale);
 
     m_maximumScale = qBound(fitToWidthZoom, m_maximumScale, DefaultMaxScale);
 
-    //Adjust minimum-sclae
+    //Adjust minimum-scale
     if (getFlag(UserDefinedMinumumScale)) {
         m_minimumScale = m_specifiedData.m_minScale;
         m_minimumScale = qBound(fitToWidthZoom, m_minimumScale, m_maximumScale);
@@ -187,14 +240,25 @@
         m_minimumScale = fitToWidthZoom;
 
     //Adjust initial-scale
-    if (getFlag(UserDefinedInitialScale))
-        m_initialScale = qBound(m_minimumScale, m_initialScale, m_maximumScale);
-    else
-        m_initialScale = m_minimumScale;
+//    if (getFlag(UserDefinedInitialScale))
+//        m_initialScale = qBound(m_minimumScale, m_initialScale, m_maximumScale);
+//    else
+//        m_initialScale = m_minimumScale;
 
+#ifdef VIEWPORT_ALWAYS_ALLOW_ZOOMING
+    if (m_minimumScale == m_maximumScale) {
+        m_minimumScale = DefaultMinScale;
+        m_maximumScale = DefaultMaxScale;
+        if(!m_userScalable) {
+            m_userScalable = true;
+            m_userScalableOverRidden = true;
+        }
+    }
+#else
     //Turn off zooming if min and max zoom are same
     if (m_minimumScale == m_maximumScale)
         m_userScalable = false;
+#endif
 }