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

Mike Fenton mike at staikos.net
Fri Mar 7 19:48:05 CET 2008


SVN commit 783301 by fenton:

Convert Static updateChildGeometry to virtual functions.
Add updated line / arrow geometry updates.
Fix for parent level rotated view items not having a proper relative size.


 M  +46 -1     lineitem.cpp  
 M  +10 -0     lineitem.h  
 M  +2 -2      view.cpp  
 M  +44 -43    viewitem.cpp  
 M  +2 -3      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.cpp #783300:783301
@@ -30,7 +30,8 @@
   setAllowedGripModes(Resize);
   QPen p = pen();
   p.setWidthF(1);
-  setPen(p);}
+  setPen(p);
+}
 
 
 LineItem::~LineItem() {
@@ -208,6 +209,49 @@
 }
 
 
+void LineItem::updateRelativeSize() {
+    QPointF topLeft = mapToParent(rect().topLeft());
+    QPointF bottomRight = mapToParent(rect().bottomRight());
+
+  if (parentViewItem()) {
+    QPointF topLeftOffset = topLeft - parentViewItem()->rect().topLeft();
+    QPointF bottomRightOffset = bottomRight - parentViewItem()->rect().topLeft();
+
+    _xTopLeftRelation = topLeftOffset.x() / parentViewItem()->width();
+    _yTopLeftRelation = topLeftOffset.y() / parentViewItem()->height();
+    _xBottomRightRelation = bottomRightOffset.x() / parentViewItem()->width();
+    _yBottomRightRelation = bottomRightOffset.y() / parentViewItem()->height();
+  } else if (parentView()) {
+    QPointF topLeftOffset = topLeft - parentView()->rect().topLeft();
+    QPointF bottomRightOffset = bottomRight - parentView()->rect().topLeft();
+
+    _xTopLeftRelation = topLeftOffset.x() / parentView()->width();
+    _yTopLeftRelation = topLeftOffset.y() / parentView()->height();
+    _xBottomRightRelation = bottomRightOffset.x() / parentView()->width();
+    _yBottomRightRelation = bottomRightOffset.y() / parentView()->height();
+  }
+  ViewItem::updateRelativeSize();
+}
+
+
+void LineItem::updateChildGeometry(const QRectF &oldParentRect, const QRectF &newParentRect) {
+//   qDebug() << "LineItem::updateChildGeometry" << oldParentRect << newParentRect << endl;
+
+  QRectF itemRect = rect();
+
+  QPointF newTopLeft = newParentRect.topLeft() + QPointF(newParentRect.width() * _xTopLeftRelation, newParentRect.height() * _yTopLeftRelation);
+  QPointF newBottomRight = newParentRect.topLeft() + QPointF(newParentRect.width() * _xBottomRightRelation, newParentRect.height() * _yBottomRightRelation);
+
+  QPointF posOffset = newTopLeft - mapToParent(rect().topLeft());
+
+  itemRect.setRight(itemRect.left() + mapFromParent(newBottomRight).x() - mapFromParent(newTopLeft).x());
+  setViewRect(itemRect, true);
+
+  rotateTowards(rightMidGrip().controlPointRect().center(), mapFromParent(pos() + (newBottomRight - newTopLeft)));
+  setPos(pos() + posOffset);
+}
+
+
 void CreateLineCommand::createItem() {
   _item = new LineItem(_view);
   _view->setCursor(Qt::CrossCursor);
@@ -215,6 +259,7 @@
   CreateCommand::createItem();
 }
 
+
 LineItemFactory::LineItemFactory()
 : GraphicsFactory() {
   registerFactory("line", this);
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.h #783300:783301
@@ -34,6 +34,8 @@
 
     virtual QPointF centerOfRotation() const;
 
+    virtual void updateRelativeSize();
+
   protected Q_SLOTS:
     virtual void creationPolygonChanged(View::CreationEvent event);
 
@@ -47,6 +49,14 @@
     virtual QPainterPath leftMidGrip() const;
     virtual QPainterPath rightMidGrip() const;
 
+  private:
+    virtual void updateChildGeometry(const QRectF &oldParentRect, const QRectF &newParentRect);
+
+  private:
+    qreal _xTopLeftRelation;
+    qreal _yTopLeftRelation;
+    qreal _xBottomRightRelation;
+    qreal _yBottomRightRelation;
 };
 
 class KST_EXPORT CreateLineCommand : public CreateCommand
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #783300:783301
@@ -272,7 +272,7 @@
       ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
       Q_ASSERT(viewItem);
 
-      ViewItem::updateChildGeometry(viewItem, oldSceneRect, sceneRect());
+      viewItem->updateChildGeometry(oldSceneRect, sceneRect());
     }
   }
 }
@@ -338,7 +338,7 @@
     ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
     Q_ASSERT(viewItem);
 
-    ViewItem::updateChildGeometry(viewItem, oldSceneRect, sceneRect());
+    viewItem->updateChildGeometry(oldSceneRect, sceneRect());
   }
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #783300:783301
@@ -364,7 +364,7 @@
     if (viewItem->hasStaticGeometry())
       continue;
 
-    ViewItem::updateChildGeometry(viewItem, oldViewRect, viewRect);
+    viewItem->updateChildGeometry(oldViewRect, viewRect);
   }
 }
 
@@ -1386,6 +1386,9 @@
   if (parentViewItem()) {
     _parentRelativeHeight = (height() / parentViewItem()->height());
     _parentRelativeWidth = (width() / parentViewItem()->width());
+  } else if (parentView()) {
+    _parentRelativeHeight = (height() / parentView()->height());
+    _parentRelativeWidth = (width() / parentView()->width());
   } else {
     _parentRelativeHeight = 0;
     _parentRelativeWidth = 0;
@@ -1393,27 +1396,25 @@
 }
 
 
-void ViewItem::updateChildGeometry(ViewItem *child, const QRectF &oldParentRect,
-                                                    const QRectF &newParentRect) {
+void ViewItem::updateChildGeometry(const QRectF &oldParentRect, const QRectF &newParentRect) {
 //   qDebug() << "ViewItem::updateChildGeometry" << oldParentRect << newParentRect << endl;
 
-  QRectF rect = child->rect();
+  QRectF itemRect = rect();
 
   //Lock aspect ratio for rotating objects or children with a lockedAspectRatio
   //FIXME is the child rotated with respect to the parent is the real question...
-  if (child->transform().isRotating() || child->lockAspectRatio()) {
+  if (transform().isRotating() || lockAspectRatio()) {
 
-    QPointF offset = child->mapToParent(child->rect().center()) - oldParentRect.topLeft();
-    QPointF oldCenter = child->mapToParent(child->rect().center());
+    QPointF offset = mapToParent(rect().center()) - oldParentRect.topLeft();
 
     qreal xCenterRelation = offset.x() / oldParentRect.width();
     qreal yCenterRelation = offset.y() / oldParentRect.height();
-//     qDebug() << "ViewItem::updateChildGeometry" << offset << xCenterRelation << yCenterRelation << child->mapToParent(child->rect().center());
+//     qDebug() << "ViewItem::updateChildGeometry" << offset << xCenterRelation << yCenterRelation << mapToParent(rect().center());
 
-    qreal newHeight = child->relativeHeight() * newParentRect.height();
-    qreal newWidth = child->relativeWidth() * newParentRect.width();
+    qreal newHeight = relativeHeight() * newParentRect.height();
+    qreal newWidth = relativeWidth() * newParentRect.width();
 
-    qreal aspectRatio = child->rect().width() / child->rect().height();
+    qreal aspectRatio = rect().width() / rect().height();
     if ((newWidth / newHeight) > aspectRatio) {
       // newWidth is too large.  Use newHeight as key.
       newWidth = newHeight * aspectRatio;
@@ -1421,16 +1422,16 @@
       // newHeight is either too large, or perfect.  use newWidth as key.
       newHeight = newWidth / aspectRatio;
     }
-    rect.setBottom(rect.top() + newHeight);
-    rect.setRight(rect.left() + newWidth);
+    itemRect.setBottom(itemRect.top() + newHeight);
+    itemRect.setRight(itemRect.left() + newWidth);
 
     QPointF newCenter = newParentRect.topLeft() + QPointF(newParentRect.width() * xCenterRelation, newParentRect.height() * yCenterRelation);
 
-    QRectF r = rect;
-    r.moveCenter(child->mapFromParent(newCenter));
+    QRectF r = itemRect;
+    r.moveCenter(mapFromParent(newCenter));
 
-    QPointF centerOffset = child->mapToParent(r.topLeft()) - child->mapToParent(rect.topLeft());
-    child->setPos(child->pos() + centerOffset);
+    QPointF centerOffset = mapToParent(r.topLeft()) - mapToParent(itemRect.topLeft());
+    setPos(pos() + centerOffset);
   } else {
 
     qreal dx = oldParentRect.width() ? newParentRect.width() / oldParentRect.width() : 0.0;
@@ -1441,87 +1442,87 @@
     bool bottomChanged = oldParentRect.bottom() != newParentRect.bottom();
     bool rightChanged = oldParentRect.right() != newParentRect.right();
 
-    qreal width = rect.width() * dx;
-    qreal height = rect.height() * dy;
+    qreal width = itemRect.width() * dx;
+    qreal height = itemRect.height() * dy;
 
-    rect.setBottom(rect.top() + height);
-    rect.setRight(rect.left() + width);
+    itemRect.setBottom(itemRect.top() + height);
+    itemRect.setRight(itemRect.left() + width);
 
     if (topChanged) {
-      QPointF offset = oldParentRect.bottomRight() - child->mapToParent(child->rect().bottomRight());
+      QPointF offset = oldParentRect.bottomRight() - mapToParent(rect().bottomRight());
 
       qreal xOff = offset.x() * dx;
       qreal yOff = offset.y() * dy;
 
       QPointF newBottomRight = oldParentRect.bottomRight() - QPointF(xOff, yOff);
 
-      QPointF o = child->pos() - child->mapToParent(rect.topLeft());
+      QPointF o = pos() - mapToParent(itemRect.topLeft());
 
-      QRectF r = rect;
-      r.moveBottom(child->mapFromParent(newBottomRight).y());
+      QRectF r = itemRect;
+      r.moveBottom(mapFromParent(newBottomRight).y());
 
-      child->setPos(child->mapToParent(r.topLeft()) + o);
+      setPos(mapToParent(r.topLeft()) + o);
     }
 
     if (leftChanged) {
-      QPointF offset = oldParentRect.bottomRight() - child->mapToParent(child->rect().bottomRight());
+      QPointF offset = oldParentRect.bottomRight() - mapToParent(rect().bottomRight());
 
       qreal xOff = offset.x() * dx;
       qreal yOff = offset.y() * dy;
 
       QPointF newBottomRight = oldParentRect.bottomRight() - QPointF(xOff, yOff);
 
-      QPointF o = child->pos() - child->mapToParent(rect.topLeft());
+      QPointF o = pos() - mapToParent(itemRect.topLeft());
 
-      QRectF r = rect;
-      r.moveRight(child->mapFromParent(newBottomRight).x());
+      QRectF r = itemRect;
+      r.moveRight(mapFromParent(newBottomRight).x());
 
-      child->setPos(child->mapToParent(r.topLeft()) + o);
+      setPos(mapToParent(r.topLeft()) + o);
     }
 
     if (bottomChanged) {
 
-      QPointF offset = child->mapToParent(child->rect().topLeft()) - oldParentRect.topLeft();
+      QPointF offset = mapToParent(rect().topLeft()) - oldParentRect.topLeft();
 
       qreal xOff = offset.x() * dx;
       qreal yOff = offset.y() * dy;
 
       QPointF newTopLeft = oldParentRect.topLeft() + QPointF(xOff, yOff);
 
-      QPointF o = child->pos() - child->mapToParent(rect.topLeft());
+      QPointF o = pos() - mapToParent(itemRect.topLeft());
 
-      QRectF r = rect;
-      r.moveTop(child->mapFromParent(newTopLeft).y());
+      QRectF r = itemRect;
+      r.moveTop(mapFromParent(newTopLeft).y());
 
-      child->setPos(child->mapToParent(r.topLeft()) + o);
+      setPos(mapToParent(r.topLeft()) + o);
     }
 
     if (rightChanged) {
 
-      QPointF offset = child->mapToParent(child->rect().topLeft()) - oldParentRect.topLeft();
+      QPointF offset = mapToParent(rect().topLeft()) - oldParentRect.topLeft();
 
       qreal xOff = offset.x() * dx;
       qreal yOff = offset.y() * dy;
 
       QPointF newTopLeft = oldParentRect.topLeft() + QPointF(xOff, yOff);
 
-      QPointF o = child->pos() - child->mapToParent(rect.topLeft());
+      QPointF o = pos() - mapToParent(itemRect.topLeft());
 
-      QRectF r = rect;
-      r.moveLeft(child->mapFromParent(newTopLeft).x());
+      QRectF r = itemRect;
+      r.moveLeft(mapFromParent(newTopLeft).x());
 
-      child->setPos(child->mapToParent(r.topLeft()) + o);
+      setPos(mapToParent(r.topLeft()) + o);
     }
   }
 
 //   qDebug() << "resize"
-//             << "\nbefore:" << child->rect()
+//             << "\nbefore:" << rect()
 //             << "\nafter:" << rect
 //             << "\nwidth:" << width
 //             << "\nheight:" << height
 //             << endl;
 
-  child->setViewRect(rect, true);
+  setViewRect(itemRect, true);
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #783300:783301
@@ -64,7 +64,7 @@
     ViewItem *parentViewItem() const;
     void setParent(ViewItem *parent);
     bool itemInLayout() const;
-    void updateRelativeSize();
+    virtual void updateRelativeSize();
     qreal relativeHeight() const { return _parentRelativeHeight; }
     qreal relativeWidth() const { return _parentRelativeWidth; }
 
@@ -199,8 +199,7 @@
 
   private:
     bool tryShortcut(const QString &keySequence);
-    static void updateChildGeometry(ViewItem *child, const QRectF &oldParentRect,
-                                                    const QRectF &newParentRect);
+    virtual void updateChildGeometry(const QRectF &oldParentRect, const QRectF &newParentRect);
 
   private:
     GripMode _gripMode;


More information about the Kst mailing list