src/hbcore/effects/hbeffectutils.cpp
changeset 0 16d8024aca5e
child 5 627c4a0fd0e7
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbeffectutils_p.h"
       
    27 #include "hbeffectdef_p.h"
       
    28 #include "hbeffectfxmldata_p.h"
       
    29 #include "hbdeviceprofile.h"
       
    30 #include <QGraphicsItem>
       
    31 #include <QEasingCurve>
       
    32 
       
    33 // ===========================================================================
       
    34 
       
    35 /*
       
    36     \class HbEffectUtils
       
    37 
       
    38     \brief HbEffectUtils is an internal helper class used by effect framework.
       
    39 ....HbEffectUtils is an internal class used by effect framework to get common functionalities.
       
    40 
       
    41     \warning This class is a part of internal library implementation and may
       
    42     be removed or modified!
       
    43 
       
    44     \internal
       
    45 */
       
    46 
       
    47 qreal HbEffectUtils::resolveFxmlRef(
       
    48     const QString &ref,
       
    49     const QString &value,
       
    50     bool* ok,
       
    51     const QGraphicsItem *item,
       
    52     HbEffectUtils::valueType type,
       
    53     const QRectF &extRect)
       
    54 {
       
    55     bool valueOk = false;
       
    56     bool refOk = false;
       
    57 
       
    58     // Resolve value parameter
       
    59     qreal ret = value.toFloat(&valueOk);
       
    60     qreal refValue = 0;
       
    61 
       
    62     bool mapToItemCoordinates = false;
       
    63     bool addToPos = false;
       
    64     QRectF srcRect;
       
    65     bool x = false;
       
    66     bool y = false;
       
    67     QRectF rect = item->boundingRect();
       
    68 
       
    69     if (!ref.isEmpty()) {
       
    70         refOk = true; // changed to false if no match
       
    71         // Visual values
       
    72         if (ref == FXML_KEYWORD_VISUAL_LEFT) {
       
    73             refValue = 0;
       
    74             addToPos = true;
       
    75             x = true;
       
    76         }
       
    77         else if (ref == FXML_KEYWORD_VISUAL_RIGHT) {
       
    78             refValue = rect.width();
       
    79             addToPos = true;
       
    80             x = true;
       
    81         }
       
    82         else if (ref == FXML_KEYWORD_VISUAL_TOP) {
       
    83             refValue = 0;
       
    84             addToPos = true;
       
    85             y = true;
       
    86         }
       
    87         else if (ref == FXML_KEYWORD_VISUAL_BOTTOM) {
       
    88             refValue = rect.height();
       
    89             addToPos = true;
       
    90             y = true;
       
    91         }
       
    92         else if (ref == FXML_KEYWORD_VISUAL_WIDTH) {
       
    93             refValue = rect.width();
       
    94             addToPos = true;
       
    95             x = true;
       
    96         }
       
    97         else if (ref == FXML_KEYWORD_VISUAL_HEIGHT) {
       
    98             refValue = rect.height();
       
    99             addToPos = true;
       
   100             y = true;
       
   101         }
       
   102         else {
       
   103             // Reference is something else than visual, so need to map to item's coordinates
       
   104             mapToItemCoordinates = true;
       
   105             srcRect = extRect;
       
   106             
       
   107             // Extrect values
       
   108             if (ref == FXML_KEYWORD_EXTRECT_LEFT) {
       
   109                 refValue = extRect.left();
       
   110                 x = true;
       
   111             }
       
   112             else if (ref == FXML_KEYWORD_EXTRECT_RIGHT) {
       
   113                 refValue = extRect.right();
       
   114                 x = true;
       
   115             }
       
   116             else if (ref == FXML_KEYWORD_EXTRECT_TOP) {
       
   117                 refValue = extRect.top();
       
   118                 y = true;
       
   119             }
       
   120             else if (ref == FXML_KEYWORD_EXTRECT_BOTTOM) {
       
   121                 refValue = extRect.bottom();
       
   122                 y = true;
       
   123             }
       
   124             else if (ref == FXML_KEYWORD_EXTRECT_WIDTH) {
       
   125                 refValue = extRect.width();
       
   126                 x = true;
       
   127             }
       
   128             else if (ref == FXML_KEYWORD_EXTRECT_HEIGHT) {
       
   129                 refValue = extRect.height();
       
   130                 y = true;
       
   131             }
       
   132             else {
       
   133 			    // Screen values
       
   134 			    QSize screenSize = HbDeviceProfile::profile(item).logicalSize();
       
   135                 srcRect = QRectF(QPointF(0,0), screenSize);
       
   136 
       
   137                 if (ref == FXML_KEYWORD_SCREEN_LEFT) {
       
   138                     refValue = 0;
       
   139                     x = true;
       
   140                 }
       
   141                 else if (ref == FXML_KEYWORD_SCREEN_RIGHT) {
       
   142                     refValue = screenSize.width();
       
   143                     x = true;
       
   144                 }
       
   145                 else if (ref == FXML_KEYWORD_SCREEN_TOP) {
       
   146                     refValue = 0;
       
   147                     y = true;
       
   148                 }
       
   149                 else if (ref == FXML_KEYWORD_SCREEN_BOTTOM) {
       
   150                     refValue = screenSize.height();
       
   151                     y = true;
       
   152                 }
       
   153                 else if (ref == FXML_KEYWORD_SCREEN_WIDTH) {
       
   154                     refValue = screenSize.width();
       
   155                     x = true;
       
   156                 }
       
   157                 else if (ref == FXML_KEYWORD_SCREEN_HEIGHT) {
       
   158                     refValue = screenSize.height();
       
   159                     y = true;
       
   160                 }
       
   161                 // If nothing matched, set ok to false
       
   162                 else {
       
   163                     refOk = false;
       
   164                 }
       
   165             }
       
   166         }
       
   167     }
       
   168 
       
   169     if (refOk) {
       
   170         // If value is ok, multiply reference value with it (e.g. 0.5 x screen.height)
       
   171         if (valueOk) {
       
   172             refValue *= ret;
       
   173         }
       
   174 
       
   175         // Add calculated value to item position if the reference was visual's width or height
       
   176         if (addToPos && type == HbEffectUtils::Position) {
       
   177             if (x) {
       
   178                 refValue += item->pos().x();
       
   179             }
       
   180             else {
       
   181                 refValue += item->pos().y();
       
   182             }
       
   183         }
       
   184 
       
   185         if (type == HbEffectUtils::Position || type == HbEffectUtils::Center) {
       
   186             // Map to parent coordinates if needed
       
   187             if (mapToItemCoordinates && item->parentItem()) {
       
   188                 if (x) {
       
   189                     refValue = item->mapToParent(item->mapFromScene(refValue, 0)).x();
       
   190                 } else if (y) {
       
   191                     refValue = item->mapToParent(item->mapFromScene(0, refValue)).y();
       
   192                 }
       
   193             }
       
   194             // Calculate distance from item origin if needed
       
   195             if (type == HbEffectUtils::Center && mapToItemCoordinates) {
       
   196                 if (x) {
       
   197                     refValue -= item->pos().x();
       
   198                 } else if (y) {
       
   199                     refValue -= item->pos().y();
       
   200                 }
       
   201             }
       
   202         }
       
   203         
       
   204         // Convert to item size units if needed
       
   205         if (type == HbEffectUtils::Size) {
       
   206             if (x) {
       
   207                 if (!fuzzyIsNull(rect.width()))
       
   208                     refValue /= rect.width();
       
   209                  else
       
   210                     refValue = 0;
       
   211             } else if (y) {
       
   212                 if (!fuzzyIsNull(rect.height()))
       
   213                     refValue /= rect.height();
       
   214                 else
       
   215                     refValue = 0;
       
   216             }            
       
   217         }
       
   218 
       
   219         // This operation is needed e.g. when scale is done from some other rect to target rect
       
   220         if (type == HbEffectUtils::CenterMappedToTargetRect && mapToItemCoordinates) {
       
   221             QPointF scenePos = item->scenePos();
       
   222             // If the item is already translated, have to substract that to get correct result
       
   223             QTransform trans = item->transform();
       
   224             qreal dx = trans.dx();
       
   225             qreal dy = trans.dy();
       
   226             scenePos -= QPointF(dx, dy);
       
   227 
       
   228             if (x) {
       
   229                 qreal divider = srcRect.width() - rect.width();
       
   230                 qreal mapped = !fuzzyIsNull(divider) ? (scenePos.x() - srcRect.left()) / divider : 0;
       
   231                 mapped *= rect.width();
       
   232                 // TODO: how should refvalue affect here?
       
   233                 refValue = mapped;
       
   234             } else {
       
   235                 qreal divider = srcRect.height() - rect.height();
       
   236                 qreal mapped = !fuzzyIsNull(divider) ? (scenePos.y() - srcRect.top()) / divider : 0;
       
   237                 mapped *= rect.height();
       
   238                 // TODO: how should refvalue affect here?
       
   239                 refValue = mapped;
       
   240             }
       
   241         }
       
   242 
       
   243         ret = refValue;
       
   244     }
       
   245 
       
   246     *ok = valueOk || refOk;
       
   247     return ret;
       
   248 }
       
   249 
       
   250 
       
   251 qreal HbEffectUtils::resolveFxmlRef(
       
   252     const HbEffectFxmlParamData &data, bool* ok, const QGraphicsItem *item, HbEffectUtils::valueType type, const QRectF &extRect)
       
   253 {
       
   254     return HbEffectUtils::resolveFxmlRef(data.getAttribute(FXML_PARAM_REF), data.getValue(), ok, item, type, extRect);
       
   255 }
       
   256 
       
   257 void HbEffectUtils::resolveFxmlCurveShape(QEasingCurve &curve, const HbEffectFxmlParamData &data)
       
   258 {
       
   259     QString style = data.getAttribute(FXML_STYLE);
       
   260 
       
   261     if (!style.isEmpty()) {
       
   262         if (style == FXML_KEYWORD_STYLE_LINEAR) {
       
   263             curve = QEasingCurve::Linear;
       
   264         }
       
   265         else if (style == FXML_KEYWORD_STYLE_INQUAD) {
       
   266             curve = QEasingCurve::InQuad;
       
   267         }
       
   268         else if (style == FXML_KEYWORD_STYLE_OUTQUAD) {
       
   269             curve = QEasingCurve::OutQuad;
       
   270         }
       
   271         else if (style == FXML_KEYWORD_STYLE_INOUTQUAD) {
       
   272             curve = QEasingCurve::InOutQuad;
       
   273         }
       
   274         else if (style == FXML_KEYWORD_STYLE_OUTINQUAD) {
       
   275             curve = QEasingCurve::OutInQuad;
       
   276         }
       
   277         else if (style == FXML_KEYWORD_STYLE_INBACK) {
       
   278             curve = QEasingCurve::InBack;
       
   279         }
       
   280         else if (style == FXML_KEYWORD_STYLE_OUTBACK) {
       
   281             curve = QEasingCurve::OutBack;
       
   282         }
       
   283         else if (style == FXML_KEYWORD_STYLE_INOUTBACK) {
       
   284             curve = QEasingCurve::InOutBack;
       
   285         }
       
   286         else if (style == FXML_KEYWORD_STYLE_OUTINBACK) {
       
   287             curve = QEasingCurve::OutInBack;
       
   288         }
       
   289    }
       
   290 }
       
   291 
       
   292 void HbEffectUtils::resolveFxmlDuration(int &duration, const HbEffectFxmlParamData &data)
       
   293 {
       
   294     QString string = data.duration();
       
   295 
       
   296     if (!string.isEmpty()) {
       
   297         bool ok = false;
       
   298         qreal value = string.toFloat(&ok);
       
   299         if (ok) {
       
   300             duration = (int)(value * 1000); // convert to milliseconds
       
   301         }
       
   302     }
       
   303 }