ginebra2/ContentViews/ViewportMetaData.cpp
changeset 6 1c3b8676e58c
child 16 3c88a81ff781
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
       
     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"
       
    23 
       
    24 static const int DefaultViewportWidth = 640;
       
    25 static const int DefaultViewportHeight = 640;
       
    26 static const int MinViewportWidth = 200;
       
    27 static const int MaxViewportWidth = 10000;
       
    28 static const int MinViewportHeight = 200;
       
    29 static const int MaxViewportHeight = 10000;
       
    30 static const qreal DefaultMinScale = 0.2;
       
    31 static const qreal DefaultMaxScale = 5.;
       
    32 
       
    33 namespace GVA {
       
    34 
       
    35 ViewportMetaData::ViewportMetaData()
       
    36 {
       
    37     initialize();
       
    38 }
       
    39 
       
    40 ViewportMetaData::ViewportMetaData(const ViewportMetaData& other)
       
    41 {
       
    42     m_initialScale = other.m_initialScale;
       
    43     m_minimumScale = other.m_minimumScale;
       
    44     m_maximumScale = other.m_maximumScale;
       
    45     m_width = other.m_width;
       
    46     m_height = other.m_height;
       
    47     m_userScalable = other.m_userScalable;
       
    48     m_isValid = other.m_isValid;
       
    49     m_scaleLimits = other.m_scaleLimits;
       
    50     m_specifiedData.m_width = other.m_specifiedData.m_width;
       
    51     m_specifiedData.m_height = other.m_specifiedData.m_height;
       
    52     m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
       
    53 }
       
    54 
       
    55 ViewportMetaData& ViewportMetaData::operator=(const ViewportMetaData& other)
       
    56 {
       
    57     m_initialScale = other.m_initialScale;
       
    58     m_minimumScale = other.m_minimumScale;
       
    59     m_maximumScale = other.m_maximumScale;
       
    60     m_width = other.m_width;
       
    61     m_height = other.m_height;
       
    62     m_userScalable = other.m_userScalable;
       
    63     m_isValid = other.m_isValid;
       
    64     m_scaleLimits = other.m_scaleLimits;
       
    65     m_specifiedData.m_width = other.m_specifiedData.m_width;
       
    66     m_specifiedData.m_height = other.m_specifiedData.m_height;
       
    67     m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
       
    68 
       
    69     return *this;
       
    70 }
       
    71 
       
    72 ViewportMetaData::~ViewportMetaData()
       
    73 {}
       
    74 
       
    75 void ViewportMetaData::adjustViewportData(const QRect& clientRect)
       
    76 {
       
    77     //Data updated from viewport tag
       
    78     m_isValid = true;
       
    79 
       
    80     //Adjust viewport dimensions
       
    81     m_width = qBound(MinViewportWidth, m_width, MaxViewportWidth);
       
    82     m_height = qBound(MinViewportHeight, m_height, MaxViewportHeight);
       
    83 
       
    84     //Aspect ratio
       
    85     qreal aspectRation = (qreal)clientRect.width() / clientRect.height();
       
    86 
       
    87     if (m_width != DefaultViewportWidth && m_height == DefaultViewportHeight) {
       
    88         //Width has been specified. Adjust height, min scale and max scale
       
    89         m_height = m_width * (1 / aspectRation);
       
    90     } else if (m_width == DefaultViewportWidth && m_height != DefaultViewportHeight) {
       
    91         //Height has been specified. Adjust width, min scale and max scale
       
    92         m_width = m_height * aspectRation;
       
    93     } else {
       
    94         //Putting below code under seperate 'else' to make it readable!
       
    95         m_height = m_width * (1 / aspectRation);
       
    96     }
       
    97 
       
    98     //Adjust zoom limits
       
    99     adjustZoomValues(clientRect);
       
   100 }
       
   101 
       
   102 void ViewportMetaData::updateViewportData(const QSize& newContentSize, const QRect& clientRect)
       
   103 {
       
   104     //If still viewport tag has not been parsed
       
   105     //Do not update values.
       
   106     if(!m_isValid)
       
   107         return;
       
   108 
       
   109     //Update with viewport dimensions
       
   110     m_width = qBound(MinViewportWidth, newContentSize.width(), MaxViewportWidth);
       
   111     m_height = qBound(MinViewportHeight, newContentSize.height(), MaxViewportHeight);
       
   112 
       
   113     //Adjust zoom limits
       
   114     adjustZoomValues(clientRect);
       
   115 }
       
   116 
       
   117 void ViewportMetaData::orientationChanged(const QRect& newClientRect)
       
   118 {
       
   119     //If still viewport tag has not been parsed
       
   120     //Do not update values.
       
   121     if(!m_isValid)
       
   122         return;
       
   123 
       
   124     //Aspect ratio
       
   125     qreal aspectRation = (qreal)newClientRect.width() / newClientRect.height();
       
   126 
       
   127     //Update with viewport dimensions
       
   128     if (m_specifiedData.m_width.length()) {
       
   129         if (QString::compare(m_specifiedData.m_width, "device-width", Qt::CaseInsensitive) == 0)
       
   130             m_width = newClientRect.width();
       
   131         else if(QString::compare(m_specifiedData.m_width, "device-height", Qt::CaseInsensitive) == 0)
       
   132             m_width = newClientRect.height();
       
   133 
       
   134         m_height = m_width * (1 / aspectRation);
       
   135     }
       
   136 
       
   137     //Check if width has not bee specified.
       
   138     if (!m_specifiedData.m_width.length()) {
       
   139         if (QString::compare(m_specifiedData.m_height, "device-width", Qt::CaseInsensitive) == 0)
       
   140             m_height = newClientRect.width();
       
   141         else if(QString::compare(m_specifiedData.m_height, "device-height", Qt::CaseInsensitive) == 0)
       
   142             m_height = newClientRect.height();
       
   143 
       
   144         m_width = m_height * aspectRation;
       
   145     }
       
   146 
       
   147     //Update with bounds
       
   148     m_width = qBound(MinViewportWidth, m_width, MaxViewportWidth);
       
   149     m_height = qBound(MinViewportHeight, m_height, MaxViewportHeight);
       
   150 }
       
   151 
       
   152 bool ViewportMetaData::isLayoutNeeded()
       
   153 {
       
   154     if (!isUserSpecifiedWidth() || !isUserSpecifiedHeight())
       
   155         return true;
       
   156     return false;
       
   157 }
       
   158 void ViewportMetaData::initialize()
       
   159 {
       
   160     m_initialScale = ValueUndefined;
       
   161     m_minimumScale = DefaultMinScale;
       
   162     m_maximumScale = DefaultMaxScale;
       
   163     m_width = DefaultViewportWidth;
       
   164     m_height = DefaultViewportHeight;
       
   165     m_userScalable = true;
       
   166     m_isValid = false;
       
   167 
       
   168     //Clear user defined scales
       
   169     setFlag(UserDefinedMinumumScale, false);
       
   170     setFlag(UserDefinedMaximumScale, false);
       
   171     setFlag(UserDefinedInitialScale, false);
       
   172 }
       
   173 
       
   174 void ViewportMetaData::adjustZoomValues(const QRect& clientRect)
       
   175 {
       
   176     qreal fitToWidthZoom = (qreal)clientRect.width() / m_width;
       
   177     fitToWidthZoom = qBound(DefaultMinScale, fitToWidthZoom, DefaultMaxScale);
       
   178 
       
   179     m_maximumScale = qBound(fitToWidthZoom, m_maximumScale, DefaultMaxScale);
       
   180 
       
   181     //Adjust minimum-sclae
       
   182     if (getFlag(UserDefinedMinumumScale)) {
       
   183         m_minimumScale = m_specifiedData.m_minScale;
       
   184         m_minimumScale = qBound(fitToWidthZoom, m_minimumScale, m_maximumScale);
       
   185     }
       
   186     else
       
   187         m_minimumScale = fitToWidthZoom;
       
   188 
       
   189     //Adjust initial-scale
       
   190     if (getFlag(UserDefinedInitialScale))
       
   191         m_initialScale = qBound(m_minimumScale, m_initialScale, m_maximumScale);
       
   192     else
       
   193         m_initialScale = m_minimumScale;
       
   194 
       
   195     //Turn off zooming if min and max zoom are same
       
   196     if (m_minimumScale == m_maximumScale)
       
   197         m_userScalable = false;
       
   198 }
       
   199 
       
   200 
       
   201 //FIX ME : Merge below functions to single. Now in Hurry!!
       
   202 bool ViewportMetaData::isUserSpecifiedWidth()
       
   203 {
       
   204     if (m_specifiedData.m_width.length()) {
       
   205         if (!QString::compare(m_specifiedData.m_width, "device-width", Qt::CaseInsensitive)
       
   206             || !QString::compare(m_specifiedData.m_width, "device-height", Qt::CaseInsensitive))
       
   207             return false;
       
   208     }
       
   209     return true;
       
   210 }
       
   211 
       
   212 bool ViewportMetaData::isUserSpecifiedHeight()
       
   213 {
       
   214     if (m_specifiedData.m_height.length()) {
       
   215         if (!QString::compare(m_specifiedData.m_height, "device-width", Qt::CaseInsensitive)
       
   216             || !QString::compare(m_specifiedData.m_height, "device-height", Qt::CaseInsensitive))
       
   217             return false;
       
   218     }
       
   219     return true;
       
   220 }
       
   221 
       
   222 }//namespace GVA
       
   223