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

Mike Fenton mike at staikos.net
Mon Jul 14 20:43:49 CEST 2008


SVN commit 832464 by fenton:

Fix for Label rotate / resize bug.
Add relative center position tracking.
Add fixed size flag to prevent resize in updatechildgeometry triggering a position change.


 M  +1 -0      labelitem.cpp  
 M  +30 -22    viewitem.cpp  
 M  +6 -0      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/labelitem.cpp #832463:832464
@@ -30,6 +30,7 @@
   setName("Label");
   setZValue(LABEL_ZVALUE);
 
+  setFixedSize(true);
   setAllowedGripModes(Move /*| Resize*/ | Rotate /*| Scale*/);
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #832463:832464
@@ -43,6 +43,7 @@
   : QObject(parent),
     _gripMode(Move),
     _allowedGripModes(Move | Resize | Rotate /*| Scale*/),
+    _fixedSize(false),
     _lockAspectRatio(false),
     _lockAspectRatioFixed(false),
     _hasStaticGeometry(false),
@@ -740,6 +741,12 @@
     QRectF newRect(rect().x(), rect().y(),
                    poly.last().x() - rect().x(),
                    poly.last().y() - rect().y());
+
+    if (!newRect.isValid()) {
+      // Special case for labels that don't need to have a size for creation to ensure proper parenting.
+      newRect.setSize(QSize(1, 1));
+    }
+
     setViewRect(newRect.normalized());
 
     parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself
@@ -907,8 +914,8 @@
     case NoGrip:
       break;
     }
-
   }
+  updateRelativeSize();
 }
 
 
@@ -1261,7 +1268,8 @@
   qDebug() << "maybeReparent" << this
            << "topLevel:" << (topLevel ? "true" : "false")
            << "origin:" << origin
-
+           << "rect:" << rect()
+           << "collision count:" << collisions.count()
            << endl;
 #endif
 
@@ -1378,7 +1386,6 @@
 
     return true;
   }
-
   return false;
 }
 
@@ -1393,12 +1400,17 @@
   if (parentViewItem()) {
     _parentRelativeHeight = (height() / parentViewItem()->height());
     _parentRelativeWidth = (width() / parentViewItem()->width());
+    _parentRelativeCenter =  mapToParent(rect().center()) - parentViewItem()->rect().topLeft();
+    _parentRelativeCenter =  QPointF(_parentRelativeCenter.x() / parentViewItem()->width(), _parentRelativeCenter.y() / parentViewItem()->height());
   } else if (parentView()) {
     _parentRelativeHeight = (height() / parentView()->height());
     _parentRelativeWidth = (width() / parentView()->width());
+    _parentRelativeCenter =  mapToParent(rect().center()) - parentView()->rect().topLeft();
+    _parentRelativeCenter =  QPointF(_parentRelativeCenter.x() / parentView()->width(), _parentRelativeCenter.y() / parentView()->height());
   } else {
     _parentRelativeHeight = 0;
     _parentRelativeWidth = 0;
+    _parentRelativeCenter = QPointF(0, 0);
   }
 }
 
@@ -1412,28 +1424,25 @@
   //FIXME is the child rotated with respect to the parent is the real question...
   if (transform().isRotating() || lockAspectRatio()) {
 
-    QPointF offset = mapToParent(rect().center()) - oldParentRect.topLeft();
+//     qDebug() << "ViewItem::updateChildGeometry" << mapToParent(rect().center()) << _parentRelativeCenter;
 
-    qreal xCenterRelation = offset.x() / oldParentRect.width();
-    qreal yCenterRelation = offset.y() / oldParentRect.height();
-//     qDebug() << "ViewItem::updateChildGeometry" << offset << xCenterRelation << yCenterRelation << mapToParent(rect().center());
+    if (!_fixedSize) {
+      qreal newHeight = relativeHeight() * newParentRect.height();
+      qreal newWidth = relativeWidth() * newParentRect.width();
 
-    qreal newHeight = relativeHeight() * newParentRect.height();
-    qreal newWidth = relativeWidth() * newParentRect.width();
-
-    qreal aspectRatio = rect().width() / rect().height();
-    if ((newWidth / newHeight) > aspectRatio) {
-      // newWidth is too large.  Use newHeight as key.
-      newWidth = newHeight * aspectRatio;
-    } else {
-      // newHeight is either too large, or perfect.  use newWidth as key.
-      newHeight = newWidth / aspectRatio;
+      qreal aspectRatio = rect().width() / rect().height();
+      if ((newWidth / newHeight) > aspectRatio) {
+        // newWidth is too large.  Use newHeight as key.
+        newWidth = newHeight * aspectRatio;
+      } else {
+        // newHeight is either too large, or perfect.  use newWidth as key.
+        newHeight = newWidth / aspectRatio;
+      }
+      itemRect.setBottom(itemRect.top() + newHeight);
+      itemRect.setRight(itemRect.left() + newWidth);
     }
-    itemRect.setBottom(itemRect.top() + newHeight);
-    itemRect.setRight(itemRect.left() + newWidth);
+    QPointF newCenter = newParentRect.topLeft() + QPointF(newParentRect.width() * _parentRelativeCenter.x(), newParentRect.height() * _parentRelativeCenter.y());
 
-    QPointF newCenter = newParentRect.topLeft() + QPointF(newParentRect.width() * xCenterRelation, newParentRect.height() * yCenterRelation);
-
     QRectF r = itemRect;
     r.moveCenter(mapFromParent(newCenter));
 
@@ -1528,7 +1537,6 @@
 //             << "\nwidth:" << width
 //             << "\nheight:" << height
 //             << endl;
-
   setViewRect(itemRect, true);
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #832463:832464
@@ -67,6 +67,7 @@
     virtual void updateRelativeSize();
     qreal relativeHeight() const { return _parentRelativeHeight; }
     qreal relativeWidth() const { return _parentRelativeWidth; }
+    QPointF relativeCenter() const { return _parentRelativeCenter; }
 
     GripMode gripMode() const;
     void setGripMode(GripMode mode);
@@ -75,6 +76,9 @@
     void setAllowedGripModes(GripModes modes);
     bool isAllowed(GripMode mode) const;
 
+    bool fixedSize() const { return _fixedSize; }
+    void setFixedSize(bool fixedSize) { _fixedSize = fixedSize; }
+
     bool lockAspectRatio() const { return _lockAspectRatio; }
     void setLockAspectRatio(bool lockAspectRatio) { _lockAspectRatio = lockAspectRatio; }
 
@@ -209,6 +213,7 @@
   private:
     GripMode _gripMode;
     GripModes _allowedGripModes;
+    bool _fixedSize;
     bool _lockAspectRatio;
     bool _lockAspectRatioFixed;
     bool _hasStaticGeometry;
@@ -227,6 +232,7 @@
     QTransform _rotationTransform;
     QHash<QString, QAction*> _shortcutMap;
     qreal _parentRelativeHeight, _parentRelativeWidth;
+    QPointF _parentRelativeCenter;
 
     friend class View;
     friend class Scene;


More information about the Kst mailing list