ginebra2/ContentViews/ViewportMetaData.cpp
author hgs
Tue, 29 Jun 2010 00:46:29 -0400
changeset 3 0954f5dd2cd0
child 16 3c88a81ff781
permissions -rw-r--r--
201026
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
*
hgs
parents:
diff changeset
     5
* This program is free software: you can redistribute it and/or modify
hgs
parents:
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
hgs
parents:
diff changeset
     7
* the Free Software Foundation, version 2.1 of the License.
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* This program is distributed in the hope that it will be useful,
hgs
parents:
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
hgs
parents:
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
hgs
parents:
diff changeset
    12
* GNU Lesser General Public License for more details.
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
hgs
parents:
diff changeset
    15
* along with this program.  If not,
hgs
parents:
diff changeset
    16
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
hgs
parents:
diff changeset
    17
*
hgs
parents:
diff changeset
    18
* Description:
hgs
parents:
diff changeset
    19
*
hgs
parents:
diff changeset
    20
*/
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
#include "ViewportMetaData.h"
hgs
parents:
diff changeset
    23
hgs
parents:
diff changeset
    24
static const int DefaultViewportWidth = 640;
hgs
parents:
diff changeset
    25
static const int DefaultViewportHeight = 640;
hgs
parents:
diff changeset
    26
static const int MinViewportWidth = 200;
hgs
parents:
diff changeset
    27
static const int MaxViewportWidth = 10000;
hgs
parents:
diff changeset
    28
static const int MinViewportHeight = 200;
hgs
parents:
diff changeset
    29
static const int MaxViewportHeight = 10000;
hgs
parents:
diff changeset
    30
static const qreal DefaultMinScale = 0.2;
hgs
parents:
diff changeset
    31
static const qreal DefaultMaxScale = 5.;
hgs
parents:
diff changeset
    32
hgs
parents:
diff changeset
    33
namespace GVA {
hgs
parents:
diff changeset
    34
hgs
parents:
diff changeset
    35
ViewportMetaData::ViewportMetaData()
hgs
parents:
diff changeset
    36
{
hgs
parents:
diff changeset
    37
    initialize();
hgs
parents:
diff changeset
    38
}
hgs
parents:
diff changeset
    39
hgs
parents:
diff changeset
    40
ViewportMetaData::ViewportMetaData(const ViewportMetaData& other)
hgs
parents:
diff changeset
    41
{
hgs
parents:
diff changeset
    42
    m_initialScale = other.m_initialScale;
hgs
parents:
diff changeset
    43
    m_minimumScale = other.m_minimumScale;
hgs
parents:
diff changeset
    44
    m_maximumScale = other.m_maximumScale;
hgs
parents:
diff changeset
    45
    m_width = other.m_width;
hgs
parents:
diff changeset
    46
    m_height = other.m_height;
hgs
parents:
diff changeset
    47
    m_userScalable = other.m_userScalable;
hgs
parents:
diff changeset
    48
    m_isValid = other.m_isValid;
hgs
parents:
diff changeset
    49
    m_scaleLimits = other.m_scaleLimits;
hgs
parents:
diff changeset
    50
    m_specifiedData.m_width = other.m_specifiedData.m_width;
hgs
parents:
diff changeset
    51
    m_specifiedData.m_height = other.m_specifiedData.m_height;
hgs
parents:
diff changeset
    52
    m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
hgs
parents:
diff changeset
    53
}
hgs
parents:
diff changeset
    54
hgs
parents:
diff changeset
    55
ViewportMetaData& ViewportMetaData::operator=(const ViewportMetaData& other)
hgs
parents:
diff changeset
    56
{
hgs
parents:
diff changeset
    57
    m_initialScale = other.m_initialScale;
hgs
parents:
diff changeset
    58
    m_minimumScale = other.m_minimumScale;
hgs
parents:
diff changeset
    59
    m_maximumScale = other.m_maximumScale;
hgs
parents:
diff changeset
    60
    m_width = other.m_width;
hgs
parents:
diff changeset
    61
    m_height = other.m_height;
hgs
parents:
diff changeset
    62
    m_userScalable = other.m_userScalable;
hgs
parents:
diff changeset
    63
    m_isValid = other.m_isValid;
hgs
parents:
diff changeset
    64
    m_scaleLimits = other.m_scaleLimits;
hgs
parents:
diff changeset
    65
    m_specifiedData.m_width = other.m_specifiedData.m_width;
hgs
parents:
diff changeset
    66
    m_specifiedData.m_height = other.m_specifiedData.m_height;
hgs
parents:
diff changeset
    67
    m_specifiedData.m_minScale = other.m_specifiedData.m_minScale;
hgs
parents:
diff changeset
    68
hgs
parents:
diff changeset
    69
    return *this;
hgs
parents:
diff changeset
    70
}
hgs
parents:
diff changeset
    71
hgs
parents:
diff changeset
    72
ViewportMetaData::~ViewportMetaData()
hgs
parents:
diff changeset
    73
{}
hgs
parents:
diff changeset
    74
hgs
parents:
diff changeset
    75
void ViewportMetaData::adjustViewportData(const QRect& clientRect)
hgs
parents:
diff changeset
    76
{
hgs
parents:
diff changeset
    77
    //Data updated from viewport tag
hgs
parents:
diff changeset
    78
    m_isValid = true;
hgs
parents:
diff changeset
    79
hgs
parents:
diff changeset
    80
    //Adjust viewport dimensions
hgs
parents:
diff changeset
    81
    m_width = qBound(MinViewportWidth, m_width, MaxViewportWidth);
hgs
parents:
diff changeset
    82
    m_height = qBound(MinViewportHeight, m_height, MaxViewportHeight);
hgs
parents:
diff changeset
    83
hgs
parents:
diff changeset
    84
    //Aspect ratio
hgs
parents:
diff changeset
    85
    qreal aspectRation = (qreal)clientRect.width() / clientRect.height();
hgs
parents:
diff changeset
    86
hgs
parents:
diff changeset
    87
    if (m_width != DefaultViewportWidth && m_height == DefaultViewportHeight) {
hgs
parents:
diff changeset
    88
        //Width has been specified. Adjust height, min scale and max scale
hgs
parents:
diff changeset
    89
        m_height = m_width * (1 / aspectRation);
hgs
parents:
diff changeset
    90
    } else if (m_width == DefaultViewportWidth && m_height != DefaultViewportHeight) {
hgs
parents:
diff changeset
    91
        //Height has been specified. Adjust width, min scale and max scale
hgs
parents:
diff changeset
    92
        m_width = m_height * aspectRation;
hgs
parents:
diff changeset
    93
    } else {
hgs
parents:
diff changeset
    94
        //Putting below code under seperate 'else' to make it readable!
hgs
parents:
diff changeset
    95
        m_height = m_width * (1 / aspectRation);
hgs
parents:
diff changeset
    96
    }
hgs
parents:
diff changeset
    97
hgs
parents:
diff changeset
    98
    //Adjust zoom limits
hgs
parents:
diff changeset
    99
    adjustZoomValues(clientRect);
hgs
parents:
diff changeset
   100
}
hgs
parents:
diff changeset
   101
hgs
parents:
diff changeset
   102
void ViewportMetaData::updateViewportData(const QSize& newContentSize, const QRect& clientRect)
hgs
parents:
diff changeset
   103
{
hgs
parents:
diff changeset
   104
    //If still viewport tag has not been parsed
hgs
parents:
diff changeset
   105
    //Do not update values.
hgs
parents:
diff changeset
   106
    if(!m_isValid)
hgs
parents:
diff changeset
   107
        return;
hgs
parents:
diff changeset
   108
hgs
parents:
diff changeset
   109
    //Update with viewport dimensions
hgs
parents:
diff changeset
   110
    m_width = qBound(MinViewportWidth, newContentSize.width(), MaxViewportWidth);
hgs
parents:
diff changeset
   111
    m_height = qBound(MinViewportHeight, newContentSize.height(), MaxViewportHeight);
hgs
parents:
diff changeset
   112
hgs
parents:
diff changeset
   113
    //Adjust zoom limits
hgs
parents:
diff changeset
   114
    adjustZoomValues(clientRect);
hgs
parents:
diff changeset
   115
}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
void ViewportMetaData::orientationChanged(const QRect& newClientRect)
hgs
parents:
diff changeset
   118
{
hgs
parents:
diff changeset
   119
    //If still viewport tag has not been parsed
hgs
parents:
diff changeset
   120
    //Do not update values.
hgs
parents:
diff changeset
   121
    if(!m_isValid)
hgs
parents:
diff changeset
   122
        return;
hgs
parents:
diff changeset
   123
hgs
parents:
diff changeset
   124
    //Aspect ratio
hgs
parents:
diff changeset
   125
    qreal aspectRation = (qreal)newClientRect.width() / newClientRect.height();
hgs
parents:
diff changeset
   126
hgs
parents:
diff changeset
   127
    //Update with viewport dimensions
hgs
parents:
diff changeset
   128
    if (m_specifiedData.m_width.length()) {
hgs
parents:
diff changeset
   129
        if (QString::compare(m_specifiedData.m_width, "device-width", Qt::CaseInsensitive) == 0)
hgs
parents:
diff changeset
   130
            m_width = newClientRect.width();
hgs
parents:
diff changeset
   131
        else if(QString::compare(m_specifiedData.m_width, "device-height", Qt::CaseInsensitive) == 0)
hgs
parents:
diff changeset
   132
            m_width = newClientRect.height();
hgs
parents:
diff changeset
   133
hgs
parents:
diff changeset
   134
        m_height = m_width * (1 / aspectRation);
hgs
parents:
diff changeset
   135
    }
hgs
parents:
diff changeset
   136
hgs
parents:
diff changeset
   137
    //Check if width has not bee specified.
hgs
parents:
diff changeset
   138
    if (!m_specifiedData.m_width.length()) {
hgs
parents:
diff changeset
   139
        if (QString::compare(m_specifiedData.m_height, "device-width", Qt::CaseInsensitive) == 0)
hgs
parents:
diff changeset
   140
            m_height = newClientRect.width();
hgs
parents:
diff changeset
   141
        else if(QString::compare(m_specifiedData.m_height, "device-height", Qt::CaseInsensitive) == 0)
hgs
parents:
diff changeset
   142
            m_height = newClientRect.height();
hgs
parents:
diff changeset
   143
hgs
parents:
diff changeset
   144
        m_width = m_height * aspectRation;
hgs
parents:
diff changeset
   145
    }
hgs
parents:
diff changeset
   146
hgs
parents:
diff changeset
   147
    //Update with bounds
hgs
parents:
diff changeset
   148
    m_width = qBound(MinViewportWidth, m_width, MaxViewportWidth);
hgs
parents:
diff changeset
   149
    m_height = qBound(MinViewportHeight, m_height, MaxViewportHeight);
hgs
parents:
diff changeset
   150
}
hgs
parents:
diff changeset
   151
hgs
parents:
diff changeset
   152
bool ViewportMetaData::isLayoutNeeded()
hgs
parents:
diff changeset
   153
{
hgs
parents:
diff changeset
   154
    if (!isUserSpecifiedWidth() || !isUserSpecifiedHeight())
hgs
parents:
diff changeset
   155
        return true;
hgs
parents:
diff changeset
   156
    return false;
hgs
parents:
diff changeset
   157
}
hgs
parents:
diff changeset
   158
void ViewportMetaData::initialize()
hgs
parents:
diff changeset
   159
{
hgs
parents:
diff changeset
   160
    m_initialScale = ValueUndefined;
hgs
parents:
diff changeset
   161
    m_minimumScale = DefaultMinScale;
hgs
parents:
diff changeset
   162
    m_maximumScale = DefaultMaxScale;
hgs
parents:
diff changeset
   163
    m_width = DefaultViewportWidth;
hgs
parents:
diff changeset
   164
    m_height = DefaultViewportHeight;
hgs
parents:
diff changeset
   165
    m_userScalable = true;
hgs
parents:
diff changeset
   166
    m_isValid = false;
hgs
parents:
diff changeset
   167
hgs
parents:
diff changeset
   168
    //Clear user defined scales
hgs
parents:
diff changeset
   169
    setFlag(UserDefinedMinumumScale, false);
hgs
parents:
diff changeset
   170
    setFlag(UserDefinedMaximumScale, false);
hgs
parents:
diff changeset
   171
    setFlag(UserDefinedInitialScale, false);
hgs
parents:
diff changeset
   172
}
hgs
parents:
diff changeset
   173
hgs
parents:
diff changeset
   174
void ViewportMetaData::adjustZoomValues(const QRect& clientRect)
hgs
parents:
diff changeset
   175
{
hgs
parents:
diff changeset
   176
    qreal fitToWidthZoom = (qreal)clientRect.width() / m_width;
hgs
parents:
diff changeset
   177
    fitToWidthZoom = qBound(DefaultMinScale, fitToWidthZoom, DefaultMaxScale);
hgs
parents:
diff changeset
   178
hgs
parents:
diff changeset
   179
    m_maximumScale = qBound(fitToWidthZoom, m_maximumScale, DefaultMaxScale);
hgs
parents:
diff changeset
   180
hgs
parents:
diff changeset
   181
    //Adjust minimum-sclae
hgs
parents:
diff changeset
   182
    if (getFlag(UserDefinedMinumumScale)) {
hgs
parents:
diff changeset
   183
        m_minimumScale = m_specifiedData.m_minScale;
hgs
parents:
diff changeset
   184
        m_minimumScale = qBound(fitToWidthZoom, m_minimumScale, m_maximumScale);
hgs
parents:
diff changeset
   185
    }
hgs
parents:
diff changeset
   186
    else
hgs
parents:
diff changeset
   187
        m_minimumScale = fitToWidthZoom;
hgs
parents:
diff changeset
   188
hgs
parents:
diff changeset
   189
    //Adjust initial-scale
hgs
parents:
diff changeset
   190
    if (getFlag(UserDefinedInitialScale))
hgs
parents:
diff changeset
   191
        m_initialScale = qBound(m_minimumScale, m_initialScale, m_maximumScale);
hgs
parents:
diff changeset
   192
    else
hgs
parents:
diff changeset
   193
        m_initialScale = m_minimumScale;
hgs
parents:
diff changeset
   194
hgs
parents:
diff changeset
   195
    //Turn off zooming if min and max zoom are same
hgs
parents:
diff changeset
   196
    if (m_minimumScale == m_maximumScale)
hgs
parents:
diff changeset
   197
        m_userScalable = false;
hgs
parents:
diff changeset
   198
}
hgs
parents:
diff changeset
   199
hgs
parents:
diff changeset
   200
hgs
parents:
diff changeset
   201
//FIX ME : Merge below functions to single. Now in Hurry!!
hgs
parents:
diff changeset
   202
bool ViewportMetaData::isUserSpecifiedWidth()
hgs
parents:
diff changeset
   203
{
hgs
parents:
diff changeset
   204
    if (m_specifiedData.m_width.length()) {
hgs
parents:
diff changeset
   205
        if (!QString::compare(m_specifiedData.m_width, "device-width", Qt::CaseInsensitive)
hgs
parents:
diff changeset
   206
            || !QString::compare(m_specifiedData.m_width, "device-height", Qt::CaseInsensitive))
hgs
parents:
diff changeset
   207
            return false;
hgs
parents:
diff changeset
   208
    }
hgs
parents:
diff changeset
   209
    return true;
hgs
parents:
diff changeset
   210
}
hgs
parents:
diff changeset
   211
hgs
parents:
diff changeset
   212
bool ViewportMetaData::isUserSpecifiedHeight()
hgs
parents:
diff changeset
   213
{
hgs
parents:
diff changeset
   214
    if (m_specifiedData.m_height.length()) {
hgs
parents:
diff changeset
   215
        if (!QString::compare(m_specifiedData.m_height, "device-width", Qt::CaseInsensitive)
hgs
parents:
diff changeset
   216
            || !QString::compare(m_specifiedData.m_height, "device-height", Qt::CaseInsensitive))
hgs
parents:
diff changeset
   217
            return false;
hgs
parents:
diff changeset
   218
    }
hgs
parents:
diff changeset
   219
    return true;
hgs
parents:
diff changeset
   220
}
hgs
parents:
diff changeset
   221
hgs
parents:
diff changeset
   222
}//namespace GVA
hgs
parents:
diff changeset
   223