src/hbcore/effects/hbeffectscale.cpp
changeset 5 627c4a0fd0e7
parent 1 f7ac710697a9
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    33 #include <QVariantAnimation>
    33 #include <QVariantAnimation>
    34 
    34 
    35 HbEffectScaleAnimation::HbEffectScaleAnimation(
    35 HbEffectScaleAnimation::HbEffectScaleAnimation(
    36     HbEffectGroup *group,
    36     HbEffectGroup *group,
    37     HbEffectScale *effect,
    37     HbEffectScale *effect,
    38     int duration ) :
    38     int duration) :
    39         HbEffectAnimation(group),
    39     HbEffectAnimation(group),
    40         mEffect(effect),
    40     mEffect(effect),
    41         mCenter(0),
    41     mCenter(0),
    42         mCurrentScaling(1.0)
    42     mCurrentScaling(1.0)
    43 {
    43 {
    44     setDuration(duration);
    44     setDuration(duration);
    45 }
    45 }
    46 
    46 
    47 void HbEffectScaleAnimation::setCenter(qreal center)
    47 void HbEffectScaleAnimation::setCenter(qreal center)
    73 
    73 
    74 HbEffectScale::HbEffectScale(
    74 HbEffectScale::HbEffectScale(
    75     const QList<HbEffectFxmlParamData> &data,
    75     const QList<HbEffectFxmlParamData> &data,
    76     QGraphicsItem *item,
    76     QGraphicsItem *item,
    77     HbEffectGroup *group) :
    77     HbEffectGroup *group) :
    78         HbEffectAbstract(0, item, group),
    78     HbEffectAbstract(0, item, group),
    79         mAnimationX(0),
    79     mAnimationX(0),
    80         mAnimationY(0)
    80     mAnimationY(0)
    81 {
    81 {
    82     int durationX = 0;
    82     int durationX = 0;
    83     int durationY = 0;
    83     int durationY = 0;
    84     QEasingCurve curveX = QEasingCurve::Linear;
    84     QEasingCurve curveX = QEasingCurve::Linear;
    85     QEasingCurve curveY = QEasingCurve::Linear;
    85     QEasingCurve curveY = QEasingCurve::Linear;
    90     mStartYValue = 1.0f;
    90     mStartYValue = 1.0f;
    91     mEndYValue = 1.0f;
    91     mEndYValue = 1.0f;
    92     mCenterXValue = 0;
    92     mCenterXValue = 0;
    93     mCenterYValue = 0;
    93     mCenterYValue = 0;
    94 
    94 
    95     const HbEffectFxmlParamData* paramX = 0;
    95     const HbEffectFxmlParamData *paramX = 0;
    96     const HbEffectFxmlParamData* paramY = 0;
    96     const HbEffectFxmlParamData *paramY = 0;
    97 
    97 
    98     Q_FOREACH(const HbEffectFxmlParamData &param, data) {
    98     Q_FOREACH(const HbEffectFxmlParamData & param, data) {
    99 	    if (param.name() == FXML_KEYWORD_SCALE_X) {
    99         if (param.name() == FXML_KEYWORD_SCALE_X) {
   100             mKeyFrameListX = param.keyFrames();
   100             mKeyFrameListX = param.keyFrames();
   101 			// <start> for scale_x
   101             // <start> for scale_x
   102 			mStartWidth =  param.getAttribute(FXML_KEYWORD_START);
   102             mStartWidth =  param.getAttribute(FXML_KEYWORD_START);
   103 			mStartWidthRef = param.startRef();
   103             mStartWidthRef = param.startRef();
   104 			// <end> for scale_x
   104             // <end> for scale_x
   105 			mEndWidth = param.getAttribute(FXML_KEYWORD_END);
   105             mEndWidth = param.getAttribute(FXML_KEYWORD_END);
   106 			mEndWidthRef = param.endRef();
   106             mEndWidthRef = param.endRef();
   107 			// duration and curvepath
   107             // duration and curvepath
   108             HbEffectUtils::resolveFxmlDuration(durationX, param);
   108             HbEffectUtils::resolveFxmlDuration(durationX, param);
   109             HbEffectUtils::resolveFxmlCurveShape(curveX, param);
   109             HbEffectUtils::resolveFxmlCurveShape(curveX, param);
   110 
   110 
   111             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
   111             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
   112             if (mStartWidthRef.isEmpty() && mEndWidthRef.isEmpty()) {
   112             if (mStartWidthRef.isEmpty() && mEndWidthRef.isEmpty()) {
   113                 mParamRefX = param.getAttribute(FXML_PARAM_REF);
   113                 mParamRefX = param.getAttribute(FXML_PARAM_REF);
   114             }
   114             }
   115 
   115 
   116             paramX = &param;
   116             paramX = &param;
   117 	    }
   117         } else if (param.name() == FXML_KEYWORD_SCALE_Y) {
   118 	    else if (param.name() == FXML_KEYWORD_SCALE_Y) {
       
   119             mKeyFrameListY = param.keyFrames();
   118             mKeyFrameListY = param.keyFrames();
   120 			// <start> for scale_y
   119             // <start> for scale_y
   121 			mStartHeight =  param.getAttribute(FXML_KEYWORD_START);
   120             mStartHeight =  param.getAttribute(FXML_KEYWORD_START);
   122 			mStartHeightRef = param.startRef();
   121             mStartHeightRef = param.startRef();
   123 			// <end> for scale_y
   122             // <end> for scale_y
   124 			mEndHeight = param.getAttribute(FXML_KEYWORD_END);
   123             mEndHeight = param.getAttribute(FXML_KEYWORD_END);
   125 			mEndHeightRef = param.endRef();
   124             mEndHeightRef = param.endRef();
   126             HbEffectUtils::resolveFxmlDuration(durationY, param);
   125             HbEffectUtils::resolveFxmlDuration(durationY, param);
   127             HbEffectUtils::resolveFxmlCurveShape(curveY, param);
   126             HbEffectUtils::resolveFxmlCurveShape(curveY, param);
   128 
   127 
   129             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
   128             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
   130             if (mStartHeightRef.isEmpty() && mEndHeightRef.isEmpty()) {
   129             if (mStartHeightRef.isEmpty() && mEndHeightRef.isEmpty()) {
   131                 mParamRefY = param.getAttribute(FXML_PARAM_REF);
   130                 mParamRefY = param.getAttribute(FXML_PARAM_REF);
   132             }
   131             }
   133 
   132 
   134             paramY = &param;
   133             paramY = &param;
   135 	    }
   134         } else if (param.name() == FXML_KEYWORD_SCALE_ORIGIN_X) {
   136 	    else if (param.name() == FXML_KEYWORD_SCALE_ORIGIN_X) {
       
   137             mCenterX = param.getValue();
   135             mCenterX = param.getValue();
   138             mCenterXRef = param.getAttribute(FXML_PARAM_REF);
   136             mCenterXRef = param.getAttribute(FXML_PARAM_REF);
   139         }                        
   137         } else if (param.name() == FXML_KEYWORD_SCALE_ORIGIN_Y) {
   140 	    else if (param.name() == FXML_KEYWORD_SCALE_ORIGIN_Y) {
       
   141             mCenterY = param.getValue();
   138             mCenterY = param.getValue();
   142             mCenterYRef = param.getAttribute(FXML_PARAM_REF);
   139             mCenterYRef = param.getAttribute(FXML_PARAM_REF);
   143 	    }
   140         }
   144     }
   141     }
   145 
   142 
   146     // Validate references. If start and end references are used, at least one of them must be a visual reference.
   143     // Validate references. If start and end references are used, at least one of them must be a visual reference.
   147     if ((mStartWidthRef.isEmpty() || mStartWidthRef.startsWith(FXML_VISUAL) ||
   144     if ((mStartWidthRef.isEmpty() || mStartWidthRef.startsWith(FXML_VISUAL) ||
   148          mEndWidthRef.isEmpty() || mEndWidthRef.startsWith(FXML_VISUAL)) &&
   145             mEndWidthRef.isEmpty() || mEndWidthRef.startsWith(FXML_VISUAL)) &&
   149         (mStartHeightRef.isEmpty() || mStartHeightRef.startsWith(FXML_VISUAL) ||
   146             (mStartHeightRef.isEmpty() || mStartHeightRef.startsWith(FXML_VISUAL) ||
   150          mEndHeightRef.isEmpty() || mEndHeightRef.startsWith(FXML_VISUAL))) {
   147              mEndHeightRef.isEmpty() || mEndHeightRef.startsWith(FXML_VISUAL))) {
   151         // references ok
   148         // references ok
   152     } else {
   149     } else {
   153         // Invalid references - disable effect
   150         // Invalid references - disable effect
   154         durationX = 0;
   151         durationX = 0;
   155         durationY = 0;
   152         durationY = 0;
   185     }
   182     }
   186 }
   183 }
   187 
   184 
   188 
   185 
   189 /* deletes the effect and rewinds the animation so that next animation can star,
   186 /* deletes the effect and rewinds the animation so that next animation can star,
   190     rewind is necessary as the general use case is that the effect is restarted before 
   187     rewind is necessary as the general use case is that the effect is restarted before
   191     being complete, in which case the rewind would transform the item and prepare for
   188     being complete, in which case the rewind would transform the item and prepare for
   192     the next effect.
   189     the next effect.
   193 */
   190 */
   194 HbEffectScale::~HbEffectScale()
   191 HbEffectScale::~HbEffectScale()
   195 {
   192 {
   222     const QRectF &extRect = group()->extRect();
   219     const QRectF &extRect = group()->extRect();
   223 
   220 
   224     // start width
   221     // start width
   225     if (!mStartWidth.isEmpty()) {
   222     if (!mStartWidth.isEmpty()) {
   226         value = HbEffectUtils::resolveFxmlRef(
   223         value = HbEffectUtils::resolveFxmlRef(
   227             mStartWidthRef, mStartWidth, &valueOk, item(), HbEffectUtils::Size, extRect);
   224                     mStartWidthRef, mStartWidth, &valueOk, item(), HbEffectUtils::Size, extRect);
   228         if (valueOk) {
   225         if (valueOk) {
   229             mStartXValue = value;
   226             mStartXValue = value;
   230         }
   227         }
   231     }
   228     }
   232     // start height
   229     // start height
   238     }
   235     }
   239 
   236 
   240     // end width
   237     // end width
   241     if (!mEndWidth.isEmpty()) {
   238     if (!mEndWidth.isEmpty()) {
   242         value = HbEffectUtils::resolveFxmlRef(
   239         value = HbEffectUtils::resolveFxmlRef(
   243             mEndWidthRef, mEndWidth, &valueOk, item(), HbEffectUtils::Size, extRect);
   240                     mEndWidthRef, mEndWidth, &valueOk, item(), HbEffectUtils::Size, extRect);
   244 
   241 
   245         if (valueOk) {
   242         if (valueOk) {
   246             mEndXValue = value;
   243             mEndXValue = value;
   247         }
   244         }
   248     }
   245     }
   249     // end height
   246     // end height
   250     if (!mEndHeight.isEmpty()) {
   247     if (!mEndHeight.isEmpty()) {
   251         value = HbEffectUtils::resolveFxmlRef(
   248         value = HbEffectUtils::resolveFxmlRef(
   252             mEndHeightRef, mEndHeight, &valueOk, item(), HbEffectUtils::Size, extRect);
   249                     mEndHeightRef, mEndHeight, &valueOk, item(), HbEffectUtils::Size, extRect);
   253 
   250 
   254         if (valueOk) {
   251         if (valueOk) {
   255             mEndYValue = value;
   252             mEndYValue = value;
   256         }
   253         }
   257     }
   254     }
   258 
   255 
   259     // CenterX
   256     // CenterX
   260     value = HbEffectUtils::resolveFxmlRef(
   257     value = HbEffectUtils::resolveFxmlRef(
   261         mCenterXRef, mCenterX, &valueOk, item(), HbEffectUtils::CenterMappedToTargetRect, extRect);
   258                 mCenterXRef, mCenterX, &valueOk, item(), HbEffectUtils::CenterMappedToTargetRect, extRect);
   262     
   259 
   263     if (valueOk) {
   260     if (valueOk) {
   264         mCenterXValue = value;
   261         mCenterXValue = value;
   265     }
   262     }
   266     // CenterY
   263     // CenterY
   267     value = HbEffectUtils::resolveFxmlRef(
   264     value = HbEffectUtils::resolveFxmlRef(
   268         mCenterYRef, mCenterY, &valueOk, item(), HbEffectUtils::CenterMappedToTargetRect, extRect);
   265                 mCenterYRef, mCenterY, &valueOk, item(), HbEffectUtils::CenterMappedToTargetRect, extRect);
   269 
   266 
   270     if (valueOk) {
   267     if (valueOk) {
   271         mCenterYValue = value;
   268         mCenterYValue = value;
   272     }
   269     }
   273 
   270 
   293         mAnimationX->setCenter(mCenterXValue);
   290         mAnimationX->setCenter(mCenterXValue);
   294 
   291 
   295         bool startEndRefUsed = !mStartWidthRef.isEmpty() && !mEndWidthRef.isEmpty();
   292         bool startEndRefUsed = !mStartWidthRef.isEmpty() && !mEndWidthRef.isEmpty();
   296 
   293 
   297         qreal paramRefValueX = 0.0;
   294         qreal paramRefValueX = 0.0;
   298         
   295 
   299         // Resolve SCALE_X parameter's "ref" value only if that's needed
   296         // Resolve SCALE_X parameter's "ref" value only if that's needed
   300         if (!startEndRefUsed) {
   297         if (!startEndRefUsed) {
   301             paramRefValueX = HbEffectUtils::resolveFxmlRef(
   298             paramRefValueX = HbEffectUtils::resolveFxmlRef(
   302                 mParamRefX, "1", &valueOk, item(), HbEffectUtils::Size, extRect);
   299                                  mParamRefX, "1", &valueOk, item(), HbEffectUtils::Size, extRect);
   303         }
   300         }
   304 
   301 
   305         // Set keyframes in animation
   302         // Set keyframes in animation
   306         foreach(const HbKeyFrame &kf, mKeyFrameListX) {
   303         foreach(const HbKeyFrame & kf, mKeyFrameListX) {
   307 
   304 
   308             // If start and end references are used,
   305             // If start and end references are used,
   309             // value at given step is (1-c)*startX + c*endX where c is the keyframe coefficient value
   306             // value at given step is (1-c)*startX + c*endX where c is the keyframe coefficient value
   310             if (startEndRefUsed) {
   307             if (startEndRefUsed) {
   311                 value = (1 - kf.val) * mStartXValue + kf.val * mEndXValue;
   308                 value = (1 - kf.val) * mStartXValue + kf.val * mEndXValue;
   340 
   337 
   341         qreal paramRefValueY = 0.0;
   338         qreal paramRefValueY = 0.0;
   342         // Resolve SCALE_Y parameter's "ref" value only if that's needed
   339         // Resolve SCALE_Y parameter's "ref" value only if that's needed
   343         if (!startEndRefUsed) {
   340         if (!startEndRefUsed) {
   344             paramRefValueY = HbEffectUtils::resolveFxmlRef(
   341             paramRefValueY = HbEffectUtils::resolveFxmlRef(
   345                 mParamRefY, "1", &valueOk, item(), HbEffectUtils::Size, extRect);
   342                                  mParamRefY, "1", &valueOk, item(), HbEffectUtils::Size, extRect);
   346         }
   343         }
   347 
   344 
   348         // Set keyframes in animation
   345         // Set keyframes in animation
   349         foreach(const HbKeyFrame &kf, mKeyFrameListY) {
   346         foreach(const HbKeyFrame & kf, mKeyFrameListY) {
   350             // If start and end references are used,
   347             // If start and end references are used,
   351             // value at given step is (1-c)*startY + c*endY where c is the keyframe coefficient value
   348             // value at given step is (1-c)*startY + c*endY where c is the keyframe coefficient value
   352             if (startEndRefUsed) {
   349             if (startEndRefUsed) {
   353                 value = (1 - kf.val) * mStartYValue + kf.val * mEndYValue;
   350                 value = (1 - kf.val) * mStartYValue + kf.val * mEndYValue;
   354             }
   351             }
   423 void HbEffectScale::updateItemTransform(QTransform &transform)
   420 void HbEffectScale::updateItemTransform(QTransform &transform)
   424 {
   421 {
   425     QTransform newTransform;
   422     QTransform newTransform;
   426     // Handle centering for scaling the matrix
   423     // Handle centering for scaling the matrix
   427     newTransform.translate(mCenterXValue, mCenterYValue);
   424     newTransform.translate(mCenterXValue, mCenterYValue);
   428     
   425 
   429     // Get the current scaling factor from animation or use 1.0 if animation does not exist
   426     // Get the current scaling factor from animation or use 1.0 if animation does not exist
   430     qreal currentScalingX = mAnimationX ? mAnimationX->mCurrentScaling : 1.0;
   427     qreal currentScalingX = mAnimationX ? mAnimationX->mCurrentScaling : 1.0;
   431     qreal currentScalingY = mAnimationY ? mAnimationY->mCurrentScaling : 1.0;
   428     qreal currentScalingY = mAnimationY ? mAnimationY->mCurrentScaling : 1.0;
   432 
   429 
   433     // Scale the transformation matrix to reach the new scaling factor.
   430     // Scale the transformation matrix to reach the new scaling factor.