Hi,

I am making an graphical application were users can sale and rote objects in a canvas in an arbitrary way. It uses hot-points in the screen (boxes that appear around the object) for scaling and rotation. I don't know much about transformation matrices, thus I don't know what I need to code to make it work. Currently it works if the user for example, Rotate then Scale Then Scale or if the user Scale, then rotate, then rotate. But if the user alternate Rotation and scaling, for example Rotate, Scale, Rotate the result is not the expected. For sure I am missing something because I don't know much about transformation matrices.

Here is the code:
Qt Code:
  1. //This gets the transformation point for scaling and rotation.
  2. void tnkitemcontainer::setTransformationPoint(QPointF point)
  3. {
  4. TransformationPoint = this->mapFromScene(point);
  5. if ((TransformationPoint.x() != 0) || (TransformationPoint.y() != 0))
  6. translationApplied = true;
  7. else
  8. translationApplied = false;
  9. currentTrans = this->transform();
  10. }
  11.  
  12. //This rotate an object based on an angle. Angles are for example -45 or 45 degrees
  13. void tnkitemcontainer::tnkRotate(float angle)
  14. {
  15. rotated = true;
  16. QTransform trans;
  17. currentRotation = formerRotation + angle;
  18. if (translationApplied == false)
  19. {
  20. trans = trans.rotate(currentRotation);
  21. }
  22. else
  23. {
  24. trans.translate(TransformationPoint.x(),TransformationPoint.y());
  25. trans.rotate(currentRotation);
  26. trans.translate(-TransformationPoint.x(),-TransformationPoint.y());
  27. }
  28. trans = trans.scale(formerXFactor,formerYFactor); //Apply the last scale if any
  29. setTransform(trans,false);
  30.  
  31. }
  32.  
  33. //This fuction will update the former rotation. If translation is used in the rotation
  34. // this means that the object will move in the canvas due to transformation. This fuction will also move the object
  35. // to its final localtion just like the transformation did.
  36. void tnkitemcontainer::finishRotation()
  37. {
  38. rotated = false;
  39. formerRotation = currentRotation;
  40. if (translationApplied) //If translation was applied the object would move position in the scene
  41. {
  42. float currentX;
  43. float currentY;
  44. //Gets the current position in the scene;
  45. currentX = scenePos().x();
  46. currentY = scenePos().y();
  47. //Override the transformation with the current values of scale and rotation
  48. QTransform trans;
  49. trans = trans.rotate(formerRotation); //Apply rotation
  50. trans = trans.scale(formerXFactor,formerYFactor); //Apply the last scale if any
  51. setTransform(trans,false);
  52. //Move the object to its new position
  53. setPos(currentX,currentY);
  54. translationApplied = false;
  55. }
  56. }
  57.  
  58. //This scale an object based on X and Y factors. Examples of factors are 1, 1.5 and 2.
  59. bool tnkitemcontainer::tnkScale(float xFactor,float yFactor)
  60. {
  61. scaled = true;
  62. currentXFactor = formerXFactor * xFactor; //This will get the proportion from the former factor.
  63. currentYFactor = formerYFactor * yFactor;
  64. QTransform trans;
  65. if (translationApplied == false)
  66. {
  67. trans = trans.scale(currentXFactor,currentYFactor);
  68. }
  69. else
  70. {
  71. trans = trans.translate(TransformationPoint.x(),TransformationPoint.y());
  72. trans = trans.scale(currentXFactor,currentYFactor);
  73. trans = trans.translate(-TransformationPoint.x(),-TransformationPoint.y());
  74. }
  75. trans = trans.rotate(formerRotation); //Applies the final rotation if any
  76. setTransform(trans,false);
  77. }
  78.  
  79. //This function will update the former rotation factors. If translation was applied, it means that the
  80. // centre of the object will move in the canvas, so we move the object to its last position
  81. void tnkitemcontainer::finishScale()
  82. {
  83. scaled = false;
  84. formerXFactor = currentXFactor;
  85. formerYFactor = currentYFactor;
  86.  
  87. if (translationApplied)
  88. {
  89. float currentX;
  90. float currentY;
  91. //Gets the current position in the scene;
  92. currentX = scenePos().x();
  93. currentY = scenePos().y();
  94. //Override the transformation with the current values of scale and rotation
  95. QTransform trans;
  96. trans = trans.scale(formerXFactor,formerYFactor);
  97. trans = trans.rotate(formerRotation);
  98. setTransform(trans,false);
  99. //Move the object to its new position
  100. setPos(currentX,currentY);
  101. translationApplied = false;
  102. }
  103. }
To copy to clipboard, switch view to plain text mode 

What do I need to take into consideration when user alternate scaling and rotation?

Many thanks,
QLands