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

Adam Treat treat at kde.org
Sat May 26 01:30:56 CEST 2007


SVN commit 668321 by treat:

* Visual debug for geometry problems
* Don't scale individual items... scale the scene
instead when view is resized


 M  +23 -29    kstplotitems.cpp  
 M  +7 -5      kstplotitems.h  
 M  +8 -5      kstplotview.cpp  
 M  +1 -1      kstplotview.h  


--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.cpp #668320:668321
@@ -17,7 +17,15 @@
 
 KstPlotItem::KstPlotItem(KstPlotView *parent)
     : QObject(parent) {
-  connect(parent, SIGNAL(resized()), this, SLOT(updateGeometry()));
+
+#ifdef DEBUG_GEOMETRY
+  QColor semiRed(QColor(255, 0, 0, 50));
+  _rectItem = new QGraphicsRectItem;
+  _rectItem->setZValue(0);
+  _rectItem->setPen(semiRed);
+  _rectItem->setBrush(semiRed);
+  parent->scene()->addItem(_rectItem);
+#endif
 }
 
 
@@ -30,38 +38,20 @@
 }
 
 
-void KstPlotItem::updateAspectFromGeometry() {
-  KstPlotView *v = parentView();
-  Q_ASSERT(v);
-  QGraphicsItem *i = graphicsItem();
-  Q_ASSERT(i);
-
-  _aspectPos = QPointF(100.0 * i->scenePos().x() / v->width(),
-                       100.0 * i->scenePos().y() / v->height());
-  _aspectSize = QSizeF(100.0 * i->boundingRect().width() / v->width(),
-                       100.0 * i->boundingRect().height() / v->height());
+#ifdef DEBUG_GEOMETRY
+void KstPlotItem::debugGeometry() {
+  _rectItem->setRect(graphicsItem()->boundingRect());
 }
+#endif
 
 
-void KstPlotItem::updateGeometry() {
-  KstPlotView *v = parentView();
-  Q_ASSERT(v);
-  QGraphicsItem *i = graphicsItem();
-  Q_ASSERT(i);
-
-  i->setPos(_aspectPos.x() * v->width() / 100.0,
-            _aspectPos.y() * v->height() / 100.0);
-  qreal sx = _aspectSize.width() * v->width() / (i->boundingRect().width() * 100.0);
-  qreal sy = _aspectSize.height() * v->height() / (i->boundingRect().height() * 100.0);
-  i->scale(sx, sy);
-
-  updateAspectFromGeometry();
-}
-
-
 LabelItem::LabelItem(const QString &text, KstPlotView *parent)
     : KstPlotItem(parent), QGraphicsSimpleTextItem(text) {
-  updateAspectFromGeometry();
+
+#ifdef DEBUG_GEOMETRY
+  debugGeometry();
+#endif
+
   setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
 }
 
@@ -106,7 +96,11 @@
   if (event == KstPlotView::MouseRelease) {
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(KstPlotView::MouseRelease));
     setLine(QLineF(line().p1(), poly.last())); //start and end
-    updateAspectFromGeometry();
+
+#ifdef DEBUG_GEOMETRY
+    debugGeometry();
+#endif
+
     parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself
     parentView()->disconnect(this, SLOT(creationPolygonChanged(KstPlotView::CreationEvent)));
     parentView()->setMouseMode(KstPlotView::Default);
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotitems.h #668320:668321
@@ -20,6 +20,8 @@
 
 #include "kstplotview.h" //forward declare, but enums??
 
+// #define DEBUG_GEOMETRY 1
+
 class QGraphicsItem;
 
 class KST_EXPORT KstPlotItem : public QObject
@@ -36,13 +38,13 @@
 Q_SIGNALS:
   void creationComplete();
 
-public Q_SLOTS:
-  void updateAspectFromGeometry();
-  void updateGeometry();
+#ifdef DEBUG_GEOMETRY
+protected:
+  void debugGeometry();
 
 private:
-  QPointF _aspectPos;   // In % from 0.0 to 100.0
-  QSizeF _aspectSize;   // In % from 0.0 to 100.0
+  QGraphicsRectItem *_rectItem;
+#endif
 };
 
 class LabelItem : public KstPlotItem, public QGraphicsSimpleTextItem
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotview.cpp #668320:668321
@@ -116,18 +116,21 @@
   }
 }
 
+void KstPlotView::setVisible(bool visible) {
+  QGraphicsView::setVisible(visible);
 
-void KstPlotView::resizeEvent(QResizeEvent *event) {
-  QGraphicsView::resizeEvent(event);
-  setSceneRect(QRectF(0, 0, event->size().width(), event->size().height()));
+  setSceneRect(QRectF(0, 0, width(), height()));
 
   //See what I'm doing
-  QLinearGradient l(0,0,0,event->size().height());
+  QLinearGradient l(0,0,0,height());
   l.setColorAt(0, Qt::white);
   l.setColorAt(1, Qt::lightGray);
   setBackgroundBrush(l);
+}
 
-  emit resized();
+void KstPlotView::resizeEvent(QResizeEvent *event) {
+  QGraphicsView::resizeEvent(event);
+  fitInView(sceneRect());
 }
 
 #include "kstplotview.moc"
--- branches/work/kst/portto4/kst/src/libkstapp/kstplotview.h #668320:668321
@@ -45,10 +45,10 @@
 Q_SIGNALS:
   void mouseModeChanged();
   void creationPolygonChanged(KstPlotView::CreationEvent event);
-  void resized();
 
 protected:
   bool eventFilter(QObject *obj, QEvent *event);
+  void setVisible(bool visible);
   void resizeEvent(QResizeEvent *event);
 
 private:


More information about the Kst mailing list