src/hbcore/effects/hbeffectrotate.cpp
changeset 5 627c4a0fd0e7
parent 1 f7ac710697a9
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    35 class HbEffectRotateAnimation : public HbEffectAnimation
    35 class HbEffectRotateAnimation : public HbEffectAnimation
    36 {
    36 {
    37 public:
    37 public:
    38     HbEffectRotateAnimation(HbEffectGroup *group,
    38     HbEffectRotateAnimation(HbEffectGroup *group,
    39                             Qt::Axis axis,
    39                             Qt::Axis axis,
    40                             int duration );
    40                             int duration);
    41 
    41 
    42     void setCenter(const QPointF &p);
    42     void setCenter(const QPointF &p);
    43 
    43 
    44 private:
    44 private:
    45     void handleAnimationUpdate(const QVariant &value);
    45     void handleAnimationUpdate(const QVariant &value);
    54 };
    54 };
    55 
    55 
    56 HbEffectRotateAnimation::HbEffectRotateAnimation(
    56 HbEffectRotateAnimation::HbEffectRotateAnimation(
    57     HbEffectGroup *group,
    57     HbEffectGroup *group,
    58     Qt::Axis axis,
    58     Qt::Axis axis,
    59     int duration ) :
    59     int duration) :
    60         HbEffectAnimation(group),
    60     HbEffectAnimation(group),
    61         mAxis(axis),
    61     mAxis(axis),
    62         mCenterX(0),
    62     mCenterX(0),
    63         mCenterY(0),
    63     mCenterY(0),
    64         mCurrentRotation(0),
    64     mCurrentRotation(0),
    65         mTargetRotation(0)
    65     mTargetRotation(0)
    66 {
    66 {
    67     setDuration(duration);
    67     setDuration(duration);
    68 }
    68 }
    69 
    69 
    70 void HbEffectRotateAnimation::setCenter(const QPointF &p)
    70 void HbEffectRotateAnimation::setCenter(const QPointF &p)
    92     \brief HbEffectRotate is an internal class used by effect framework.
    92     \brief HbEffectRotate is an internal class used by effect framework.
    93     HbEffectRotate is an internal class used by effect framework to get rotation effect.
    93     HbEffectRotate is an internal class used by effect framework to get rotation effect.
    94 
    94 
    95     Example of how to use HbEffectRotate
    95     Example of how to use HbEffectRotate
    96     \code
    96     \code
    97     // animData here is pointer to HbEffectAnimation data, gruop is pointer to HbEffectGroup, and 
    97     // animData here is pointer to HbEffectAnimation data, gruop is pointer to HbEffectGroup, and
    98     // item is the QGraphicsItem on which effect will be applied.
    98     // item is the QGraphicsItem on which effect will be applied.
    99     HbEffectRotate *effect= new HbEffectScale(animData, item, group);
    99     HbEffectRotate *effect= new HbEffectScale(animData, item, group);
   100     effect->start();
   100     effect->start();
   101     
   101 
   102     \endcode
   102     \endcode
   103 
   103 
   104     \warning This class is a part of internal library implementation and may
   104     \warning This class is a part of internal library implementation and may
   105     be removed or modified!
   105     be removed or modified!
   106 
   106 
   109 
   109 
   110 HbEffectRotate::HbEffectRotate(
   110 HbEffectRotate::HbEffectRotate(
   111     const QList<HbEffectFxmlParamData> &data,
   111     const QList<HbEffectFxmlParamData> &data,
   112     QGraphicsItem *item,
   112     QGraphicsItem *item,
   113     HbEffectGroup *group) :
   113     HbEffectGroup *group) :
   114         HbEffectAbstract(0, item, group),
   114     HbEffectAbstract(0, item, group),
   115         mAnimation(0),
   115     mAnimation(0),
   116         mCenterX(0.0),
   116     mCenterX(0.0),
   117         mCenterY(0.0),
   117     mCenterY(0.0),
   118         mPosChanged(false)
   118     mPosChanged(false)
   119 {
   119 {
   120     // Default values of rotation effect if something is not passed in FXML
   120     // Default values of rotation effect if something is not passed in FXML
   121     Qt::Axis axis = Qt::ZAxis;
   121     Qt::Axis axis = Qt::ZAxis;
   122     qreal startAngle = 0.0;
   122     qreal startAngle = 0.0;
   123     qreal endAngle = 0.0;
   123     qreal endAngle = 0.0;
   128     QList<HbKeyFrame> keyFrameList;
   128     QList<HbKeyFrame> keyFrameList;
   129 
   129 
   130     const HbEffectFxmlParamData *angleParam = 0;
   130     const HbEffectFxmlParamData *angleParam = 0;
   131 
   131 
   132     // Handle FXML parameters
   132     // Handle FXML parameters
   133     Q_FOREACH(const HbEffectFxmlParamData &param, data) {
   133     Q_FOREACH(const HbEffectFxmlParamData & param, data) {
   134 	    if (param.name() == FXML_KEYWORD_ROTATION_ANGLE) {
   134         if (param.name() == FXML_KEYWORD_ROTATION_ANGLE) {
   135             keyFrameList = param.keyFrames();
   135             keyFrameList = param.keyFrames();
   136             HbEffectUtils::resolveFxmlDuration(duration, param);
   136             HbEffectUtils::resolveFxmlDuration(duration, param);
   137             HbEffectUtils::resolveFxmlCurveShape(curve, param);
   137             HbEffectUtils::resolveFxmlCurveShape(curve, param);
   138             angleParam = &param;
   138             angleParam = &param;
   139 	    }
   139         } else if (param.name() == FXML_KEYWORD_ROTATION_ORIGIN_X) {
   140 	    else if (param.name() == FXML_KEYWORD_ROTATION_ORIGIN_X) {
       
   141             originXData = param;
   140             originXData = param;
   142 	    }
   141         } else if (param.name() == FXML_KEYWORD_ROTATION_ORIGIN_Y) {
   143 	    else if (param.name() == FXML_KEYWORD_ROTATION_ORIGIN_Y) {
       
   144             originYData = param;
   142             originYData = param;
   145 	    }
   143         } else if (param.name() == FXML_KEYWORD_ROTATION_AXIS_X) {
   146 	    else if (param.name() == FXML_KEYWORD_ROTATION_AXIS_X) {
       
   147             bool valueOk = false;
   144             bool valueOk = false;
   148             int value = param.getValue().toInt(&valueOk);
   145             int value = param.getValue().toInt(&valueOk);
   149             if (valueOk && (bool)value) {
   146             if (valueOk && (bool)value) {
   150                 axis = Qt::XAxis;
   147                 axis = Qt::XAxis;
   151             }
   148             }
   152         }
   149         } else if (param.name() == FXML_KEYWORD_ROTATION_AXIS_Y) {
   153 	    else if (param.name() == FXML_KEYWORD_ROTATION_AXIS_Y) {
       
   154             bool valueOk = false;
   150             bool valueOk = false;
   155             int value = param.getValue().toInt(&valueOk);
   151             int value = param.getValue().toInt(&valueOk);
   156             if (valueOk && (bool)value) {
   152             if (valueOk && (bool)value) {
   157                 axis = Qt::YAxis;
   153                 axis = Qt::YAxis;
   158             }
   154             }
   164     HbEffectRotateAnimation *anim = new HbEffectRotateAnimation(
   160     HbEffectRotateAnimation *anim = new HbEffectRotateAnimation(
   165         group, axis, duration);
   161         group, axis, duration);
   166 
   162 
   167     anim->addLooping(angleParam);
   163     anim->addLooping(angleParam);
   168 
   164 
   169     foreach( const HbKeyFrame &kf, keyFrameList ) {
   165     foreach(const HbKeyFrame & kf, keyFrameList) {
   170 	    if (HbEffectUtils::fuzzyIsNull(kf.pos)) {
   166         if (HbEffectUtils::fuzzyIsNull(kf.pos)) {
   171 		    startAngle = kf.val;
   167             startAngle = kf.val;
   172 	    }
   168         } else if (HbEffectUtils::fuzzyIsOneOrGreater(kf.pos)) {
   173 	    else if (HbEffectUtils::fuzzyIsOneOrGreater(kf.pos)) {
   169             endAngle = kf.val;
   174 		    endAngle = kf.val;
   170         }
   175 	    }
       
   176         // Set keyframe in animation
   171         // Set keyframe in animation
   177         else {
   172         else {
   178             anim->setKeyValueAt(kf.pos, QVariant(kf.val));
   173             anim->setKeyValueAt(kf.pos, QVariant(kf.val));
   179         }
   174         }
   180     }
   175     }
   226 
   221 
   227 /* Starts the rotate effect animation. If the effect is already started,
   222 /* Starts the rotate effect animation. If the effect is already started,
   228 the result is undefined.
   223 the result is undefined.
   229 */
   224 */
   230 void HbEffectRotate::start()
   225 void HbEffectRotate::start()
   231 {  
   226 {
   232 	mAnimation->setCenter(QPointF(mCenterX, mCenterY));
   227     mAnimation->setCenter(QPointF(mCenterX, mCenterY));
   233     mAnimation->setCurrentTime(0);
   228     mAnimation->setCurrentTime(0);
   234 	mAnimation->start();
   229     mAnimation->start();
   235 }
   230 }
   236 
   231 
   237 /* Cancels the effect animation and sets the animation end state immediately.
   232 /* Cancels the effect animation and sets the animation end state immediately.
   238 */
   233 */
   239 void HbEffectRotate::cancel(QTransform &transform, bool itemIsValid)
   234 void HbEffectRotate::cancel(QTransform &transform, bool itemIsValid)
   240 {
   235 {
   241     Q_UNUSED(itemIsValid)
   236     Q_UNUSED(itemIsValid)
   242 
   237 
   243     mAnimation->stop();
   238     mAnimation->stop();
   244     
   239 
   245     //This will set the endstate for the object correctly when cancelling effect
   240     //This will set the endstate for the object correctly when cancelling effect
   246     QTransform newTransform;
   241     QTransform newTransform;
   247     newTransform.translate(mAnimation->mCenterX, mAnimation->mCenterY);
   242     newTransform.translate(mAnimation->mCenterX, mAnimation->mCenterY);
   248     newTransform.rotate(mAnimation->mTargetRotation, mAnimation->mAxis);
   243     newTransform.rotate(mAnimation->mTargetRotation, mAnimation->mAxis);
   249     newTransform.translate(-mAnimation->mCenterX, -mAnimation->mCenterY);
   244     newTransform.translate(-mAnimation->mCenterX, -mAnimation->mCenterY);
   252 }
   247 }
   253 
   248 
   254 void HbEffectRotate::updateItemTransform(QTransform &transform)
   249 void HbEffectRotate::updateItemTransform(QTransform &transform)
   255 {
   250 {
   256     if (mAnimation) {
   251     if (mAnimation) {
   257         // Rotate the transformation matrix to reach the new rotation angle.        
   252         // Rotate the transformation matrix to reach the new rotation angle.
   258         QTransform newTransform;
   253         QTransform newTransform;
   259         newTransform.translate(mAnimation->mCenterX, mAnimation->mCenterY);
   254         newTransform.translate(mAnimation->mCenterX, mAnimation->mCenterY);
   260         newTransform.rotate(mAnimation->mCurrentRotation, mAnimation->mAxis);
   255         newTransform.rotate(mAnimation->mCurrentRotation, mAnimation->mAxis);
   261         newTransform.translate(-mAnimation->mCenterX, -mAnimation->mCenterY);
   256         newTransform.translate(-mAnimation->mCenterX, -mAnimation->mCenterY);
   262         // Combine the new transform with the existing one
   257         // Combine the new transform with the existing one