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

Adam Treat treat at kde.org
Tue Sep 18 21:56:35 CEST 2007


SVN commit 714134 by treat:

* Resize child geometries when resizing a parent.


 M  +3 -3      libkstapp.pro  
 M  +30 -20    view.cpp  
 M  +2 -3      view.h  
 M  +87 -0     viewitem.cpp  
 M  +6 -0      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/libkstapp.pro #714133:714134
@@ -47,12 +47,12 @@
     plotitem.cpp \
     plotrenderitem.cpp \
     qgetoptions.cpp \
-    vectorcurverenderitem.cpp \
     sessionmodel.cpp \
     stroketab.cpp \
     svgitem.cpp \
     tabwidget.cpp \
     vectoreditordialog.cpp \
+    vectorcurverenderitem.cpp \
     vectormodel.cpp \
     vectortablemodel.cpp \
     viewcommand.cpp \
@@ -92,13 +92,13 @@
     plotitem.h \
     plotrenderitem.h \
     qgetoptions.h \
-    vectorcurverenderitem.h \
     sessionmodel.h \
     svgitem.h \
     stroketab.h \
     tabwidget.h \
     vectoreditordialog.h \
     vectormodel.h \
+    vectorcurverenderitem.h \
     vectortablemodel.h \
     viewcommand.h \
     view.h \
@@ -110,9 +110,9 @@
 FORMS += \
     aboutdialog.ui \
     datamanager.ui \
+    debugdialog.ui \
     dialog.ui \
     dialogpage.ui \
-    debugdialog.ui \
     exportgraphicsdialog.ui \
     filltab.ui \
     layouttab.ui \
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #714133:714134
@@ -166,12 +166,6 @@
 }
 
 
-void View::setVisible(bool visible) {
-  QGraphicsView::setVisible(visible);
-  QTimer::singleShot(0, this, SLOT(initializeSceneRect()));
-}
-
-
 void View::createLayout() {
   if (_layoutBoxItem && _layoutBoxItem->isVisible() && _layoutBoxItem->layout() )
     return;
@@ -181,26 +175,28 @@
 }
 
 
-void View::initializeSceneRect() {
+void View::resizeEvent(QResizeEvent *event) {
+  QGraphicsView::resizeEvent(event);
 
-  //Maybe this should be the size of the desktop?
-  setSceneRect(QRectF(0, 0, 800, 600));
+  if (size() != sceneRect().size()) {
+    QRectF oldSceneRect = sceneRect();
 
-  fitInView(sceneRect(), Qt::KeepAspectRatioByExpanding);
+    setSceneRect(QRectF(0.0, 0.0, width() - 4.0, height() - 4.0));
 
-  //See what I'm doing
-  QLinearGradient l(0,0,0,600);
-  l.setColorAt(0, Qt::white);
-  l.setColorAt(1, Qt::lightGray);
-  setBackgroundBrush(l);
-}
+    QLinearGradient l(0.0, 0.0, 0.0, height() - 4.0);
+    l.setColorAt(0.0, Qt::white);
+    l.setColorAt(1.0, Qt::lightGray);
+    setBackgroundBrush(l);
 
+    foreach (QGraphicsItem *item, items()) {
+      if (item->parentItem())
+        continue;
 
-void View::resizeEvent(QResizeEvent *event) {
-  QGraphicsView::resizeEvent(event);
+      ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
+      Q_ASSERT(viewItem);
 
-  if (size() != sceneRect().size() && sceneRect().isValid()) {
-    fitInView(sceneRect(), Qt::KeepAspectRatioByExpanding);
+      ViewItem::updateChildGeometry(viewItem, oldSceneRect, sceneRect());
+    }
   }
 }
 
@@ -239,6 +235,20 @@
   painter->restore();
 }
 
+
+void View::updateChildGeometry(const QRectF &oldSceneRect) {
+  foreach (QGraphicsItem *item, items()) {
+    if (item->parentItem())
+      continue;
+
+    ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
+    Q_ASSERT(viewItem);
+
+    ViewItem::updateChildGeometry(viewItem, oldSceneRect, sceneRect());
+  }
 }
 
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #714133:714134
@@ -80,12 +80,11 @@
 
 protected:
   bool eventFilter(QObject *obj, QEvent *event);
-  void setVisible(bool visible);
   void resizeEvent(QResizeEvent *event);
   void drawBackground(QPainter *painter, const QRectF &rect);
 
-private Q_SLOTS:
-  void initializeSceneRect();
+private:
+  void updateChildGeometry(const QRectF &oldSceneRect);
 
 private:
   QUndoStack *_undoStack;
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #714133:714134
@@ -103,8 +103,28 @@
 
 
 void ViewItem::setViewRect(const QRectF &viewRect) {
+  QRectF oldViewRect = rect();
+
+  if (oldViewRect == viewRect)
+    return;
+
   setRect(viewRect);
   emit geometryChanged();
+
+  if (layout())
+    return;
+
+  foreach (QGraphicsItem *item, QGraphicsItem::children()) {
+    if (item->parentItem() != this)
+      continue;
+
+    ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
+
+    if (!viewItem)
+      continue;
+
+    ViewItem::updateChildGeometry(viewItem, oldViewRect, viewRect);
+  }
 }
 
 
@@ -1069,6 +1089,73 @@
 }
 
 
+void ViewItem::updateChildGeometry(ViewItem *child, const QRectF &oldParentRect,
+                                                    const QRectF &newParentRect) {
+//   qDebug() << "ViewItem::updateChildGeometry" << oldParentRect << newParentRect << endl;
+
+  qreal dx = oldParentRect.width() ? newParentRect.width() / oldParentRect.width() : 0.0;
+  qreal dy = oldParentRect.height() ? newParentRect.height() / oldParentRect.height() : 0.0;
+
+  bool topChanged = oldParentRect.top() != newParentRect.top();
+  bool leftChanged = oldParentRect.left() != newParentRect.left();
+  bool bottomChanged = oldParentRect.bottom() != newParentRect.bottom();
+  bool rightChanged = oldParentRect.right() != newParentRect.right();
+
+#if 0
+  //Lock aspect ratio for rotating objects...
+  if (viewItem->transform().isRotating()) {
+  }
+#endif
+
+  QRectF rect = child->rect();
+
+  qreal width = rect.width() * dx;
+  qreal height = rect.height() * dy;
+
+  if (topChanged) {
+    rect.setTop(rect.top() + rect.height() - height);
+  } else if (bottomChanged) {
+    rect.setBottom(rect.top() + height);
+  }
+
+  if (leftChanged) {
+    rect.setLeft(rect.left() + rect.width() - width);
+  } else if (rightChanged) {
+    rect.setRight(rect.left() + width);
+  }
+
+  if (topChanged || leftChanged) {
+    QPointF offset = oldParentRect.bottomRight() - child->mapToParent(child->rect().bottomRight());
+
+    qreal xOff = offset.x() * dx;
+    qreal yOff = offset.y() * dy;
+
+    QPointF newBottomRight = oldParentRect.bottomRight() - QPointF(xOff, yOff);
+
+    rect.moveBottomRight(child->mapFromParent(newBottomRight));
+  } else {
+
+    QPointF offset = child->mapToParent(child->rect().topLeft()) - oldParentRect.topLeft();
+
+    qreal xOff = offset.x() * dx;
+    qreal yOff = offset.y() * dy;
+
+    QPointF newTopLeft = oldParentRect.topLeft() + QPointF(xOff, yOff);
+
+    rect.moveTopLeft(child->mapFromParent(newTopLeft));
+  }
+
+//   qDebug() << "resize"
+//             << "\nbefore:" << child->rect()
+//             << "\nafter:" << rect
+//             << "\nwidth:" << width
+//             << "\nheight:" << height
+//             << endl;
+
+  child->setViewRect(rect);
+}
+
+
 void ViewItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
   if (parentView()->viewMode() == View::Data) {
     event->ignore();
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #714133:714134
@@ -146,6 +146,10 @@
   void viewMouseModeChanged(View::MouseMode oldMode);
 
 private:
+  static void updateChildGeometry(ViewItem *child, const QRectF &oldParentRect,
+                                                   const QRectF &newParentRect);
+
+private:
   MouseMode _mouseMode;
   bool _hovering;
   bool _lockAspectRatio;
@@ -157,6 +161,8 @@
   QLineF _rotationLine;
   ActiveGrip _activeGrip;
   QTransform _rotationTransform;
+
+  friend class View;
 };
 
 #ifndef QT_NO_DEBUG_STREAM


More information about the Kst mailing list