[Kst] branches/work/kst/portto4/kst/src/libkstapp

Adam Treat treat at kde.org
Thu Jun 14 17:45:31 CEST 2007


SVN commit 675646 by treat:

* Finally, all transformations are properly anchored
no matter the current rotation or translation of the
actual rect object.



 M  +59 -23    viewitem.cpp  
 M  +3 -1      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #675645:675646
@@ -413,12 +413,14 @@
 void ViewItem::setTopLeft(const QPointF &point) {
 //   qDebug() << "setTopLeft" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().bottomRight());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setTopLeft(point);
-  from.moveBottomRight(transform().map(rect().bottomRight()));
-  to.moveBottomRight(transform().map(rect().bottomRight()));
+  from.moveBottomRight(anchor);
+  to.moveBottomRight(anchor);
   transformToRect(from, to);
 }
 
@@ -426,12 +428,14 @@
 void ViewItem::setTopRight(const QPointF &point) {
 //   qDebug() << "setTopRight" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().bottomLeft());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setTopRight(point);
-  from.moveBottomLeft(transform().map(rect().bottomLeft()));
-  to.moveBottomLeft(transform().map(rect().bottomLeft()));
+  from.moveBottomLeft(anchor);
+  to.moveBottomLeft(anchor);
   transformToRect(from, to);
 }
 
@@ -439,12 +443,14 @@
 void ViewItem::setBottomLeft(const QPointF &point) {
 //   qDebug() << "setBottomLeft" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().topRight());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setBottomLeft(point);
-  from.moveTopRight(transform().map(rect().topRight()));
-  to.moveTopRight(transform().map(rect().topRight()));
+  from.moveTopRight(anchor);
+  to.moveTopRight(anchor);
   transformToRect(from, to);
 }
 
@@ -452,12 +458,14 @@
 void ViewItem::setBottomRight(const QPointF &point) {
 //   qDebug() << "setBottomRight" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().topLeft());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setBottomRight(point);
-  from.moveTopLeft(transform().map(rect().topLeft()));
-  to.moveTopLeft(transform().map(rect().topLeft()));
+  from.moveTopLeft(anchor);
+  to.moveTopLeft(anchor);
   transformToRect(from, to);
 }
 
@@ -465,12 +473,14 @@
 void ViewItem::setTop(const QPointF &point) {
 //   qDebug() << "setTop" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().bottomLeft());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setTop(point.y());
-  from.moveBottomLeft(transform().map(rect().bottomLeft()));
-  to.moveBottomLeft(transform().map(rect().bottomLeft()));
+  from.moveBottomLeft(anchor);
+  to.moveBottomLeft(anchor);
   transformToRect(from, to);
 }
 
@@ -478,12 +488,14 @@
 void ViewItem::setBottom(const QPointF &point) {
 //   qDebug() << "setBottom" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().topLeft());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setBottom(point.y());
-  from.moveTopLeft(transform().map(rect().topLeft()));
-  to.moveTopLeft(transform().map(rect().topLeft()));
+  from.moveTopLeft(anchor);
+  to.moveTopLeft(anchor);
   transformToRect(from, to);
 }
 
@@ -491,12 +503,14 @@
 void ViewItem::setLeft(const QPointF &point) {
 //   qDebug() << "setLeft" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().topRight());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
 
   to.setLeft(point.x());
-  from.moveTopRight(transform().map(rect().topRight()));
-  to.moveTopRight(transform().map(rect().topRight()));
+  from.moveTopRight(anchor);
+  to.moveTopRight(anchor);
   transformToRect(from, to);
 }
 
@@ -504,28 +518,48 @@
 void ViewItem::setRight(const QPointF &point) {
 //   qDebug() << "setRight" << point << endl;
 
+  QPointF anchor = selectTransform().map(rect().topLeft());
+
   QRectF from = selectBoundingRect();
   QRectF to = from;
+
   to.setRight(point.x());
-
-  from.moveBottomLeft(transform().map(rect().bottomLeft()));
-  to.moveBottomLeft(transform().map(rect().bottomLeft()));
+  from.moveTopLeft(anchor);
+  to.moveTopLeft(anchor);
   transformToRect(from, to);
 }
 
 
+QTransform ViewItem::selectTransform() const {
+
+  /* Converts a point on the rect() to a point on the selectBoundingRect()
+     or the inverse by using selectTransform().inverted()...
+  */
+
+  QRectF from = rect();
+  QRectF to = selectBoundingRect();
+  QTransform rt = _rotationTransform.inverted(); //inverse rotation so far
+
+  QPolygonF from_ = QPolygonF(rt.map(from));
+  from_.pop_back(); //get rid of last closed point
+
+  QPolygonF to_ = QPolygonF(mapFromScene(to));
+  to_.pop_back(); //get rid of last closed point
+
+  QTransform select;
+  QTransform::quadToQuad(from_, to_, select);
+
+  return _rotationTransform.inverted() * select * transform();
+}
+
+
 bool ViewItem::transformToRect(const QRectF &from, const QRectF &to) {
   //Not sure how to handle yet
   if (!to.isValid()) {
     return false;
   }
 
-  qDebug() << "Mapping from " << from << "to" << to << endl;
-  qDebug() << "Pos is " << pos() << endl;
-  qDebug() << "ScenePos is " << scenePos() << endl;
-  qDebug() << "ItemShape is " << itemShape().controlPointRect() << endl;
-  qDebug() << "BoundingRect is " << QGraphicsRectItem::boundingRect() << endl;
-  qDebug() << "Rect is " << rect() << endl;
+//   qDebug() << "Mapping from " << from << "to" << to << endl;
 
   QPolygonF from_(from);
   from_.pop_back(); //get rid of last closed point
@@ -567,6 +601,8 @@
   t.rotate(angle);
   t.translate(-origin.x(), -origin.y());
 
+  _rotationTransform = t * _rotationTransform;
+
   setTransform(t, true);
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #675645:675646
@@ -19,7 +19,7 @@
 #include "viewcommand.h"
 #include "view.h" //forward declare, but enums??
 
-// #define DEBUG_GEOMETRY
+#define DEBUG_GEOMETRY
 
 namespace Kst {
 
@@ -79,6 +79,7 @@
   void setBottom(const QPointF &point);
   void setLeft(const QPointF &point);
   void setRight(const QPointF &point);
+  QTransform selectTransform() const;
   bool transformToRect(const QRectF &from, const QRectF &to);
   bool transformToRect(const QPolygonF &from, const QPolygonF &to);
   void rotateTowards(const QPointF &corner, const QPointF &point);
@@ -107,6 +108,7 @@
   QLineF _normalLine;
   QLineF _rotationLine;
   ActiveGrip _activeGrip;
+  QTransform _rotationTransform;
 };
 
 class KST_EXPORT ViewItemCommand : public QUndoCommand


More information about the Kst mailing list