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

Adam Treat treat at kde.org
Fri Aug 24 19:04:07 CEST 2007


SVN commit 704333 by treat:

Labels are drawing and resizing nicely...


 M  +92 -85    plotitem.cpp  
 M  +8 -4      plotitem.h  
 M  +1 -1      plotrenderitem.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #704332:704333
@@ -34,12 +34,12 @@
 
   // FIXME fake data for testing rendering
   KstVectorPtr xTest = new KstSVector(0.0, 100.0, 10000, KstObjectTag::fromString("X vector"));
-  xTest->setLabel("a nice bottom label");
+  xTest->setLabel("a nice x label");
   KstVectorPtr yTest = new KstSVector(0.0, 100.0, 10000, KstObjectTag::fromString("Y vector"));
-  yTest->setLabel("a nice left label");
+  yTest->setLabel("a nice y label");
 
   KstVectorPtr yTest2 = new KstSVector(-100.0, 100.0, 10000, KstObjectTag::fromString("Y vector 2"));
-  yTest2->setLabel("another nice left label");
+  yTest2->setLabel("another nice y label");
 
   KstVectorPtr errorX = new KstSVector(0.0, 0.0, 0, KstObjectTag::fromString("X error"));
   KstVectorPtr errorY = new KstSVector(0.0, 0.0, 0, KstObjectTag::fromString("y error"));
@@ -84,49 +84,28 @@
   painter->translate(QPointF(rect().x(), rect().y()));
 
   //Calculate and adjust the margins based on the bounds...
-  calculateLeftLabelBound(painter);
-//   calculateBottomLabelBound(painter);
-//   calculateRightLabelBound(painter);
-//   calculateTopLabelBound(painter);
+  QSizeF margins;
+  margins = margins.expandedTo(calculateLeftLabelBound(painter));
+  margins = margins.expandedTo(calculateBottomLabelBound(painter));
+  margins = margins.expandedTo(calculateRightLabelBound(painter));
+  margins = margins.expandedTo(calculateTopLabelBound(painter));
 
-//   qDebug() << "=============> leftLabel:" << leftLabel() << endl;
-//   qDebug() << "=============> bottomLabel:" << bottomLabel() << endl;
-//   qDebug() << "=============> rightLabel:" << rightLabel() << endl;
-//   qDebug() << "=============> topLabel:" << topLabel() << endl;
+//  qDebug() << "setting margin width" << margins.width() << endl;
+  setMarginWidth(margins.width());
 
-  //paint the left label
-  {
-  painter->save();
-  QTransform t;
-  t.rotate(90.0);
-  painter->rotate(-90.0);
+//  qDebug() << "setting margin height" << margins.height() << endl;
+  setMarginHeight(margins.height());
 
-  QRectF leftLabelRect = verticalLabelRect();
-  leftLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
+//  qDebug() << "=============> leftLabel:" << leftLabel() << endl;
+  paintLeftLabel(painter);
+//  qDebug() << "=============> bottomLabel:" << bottomLabel() << endl;
+  paintBottomLabel(painter);
+//  qDebug() << "=============> rightLabel:" << rightLabel() << endl;
+  paintRightLabel(painter);
+//  qDebug() << "=============> topLabel:" << topLabel() << endl;
+  paintTopLabel(painter);
 
-  qDebug() << "leftLabelRect" << leftLabelRect << endl;
-
-  painter->drawText(t.mapRect(leftLabelRect), Qt::TextWordWrap | Qt::AlignCenter, leftLabel());
   painter->restore();
-  }
-
-  //paint the right label
-  {
-  painter->save();
-  painter->translate(width() - marginWidth(), 0.0);
-  QTransform t;
-  t.rotate(-90.0);
-  painter->rotate(90.0);
-
-  //same as left but painter is translated
-  QRectF rightLabelRect = verticalLabelRect();
-  rightLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
-
-  painter->drawText(t.mapRect(rightLabelRect), Qt::TextWordWrap | Qt::AlignCenter, rightLabel());
-  painter->restore();
-  }
-
-  painter->restore();
 }
 
 
@@ -142,8 +121,10 @@
 
 
 void PlotItem::setMarginWidth(qreal marginWidth) {
+  qreal before = this->marginWidth();
   _marginWidth = marginWidth;
-  emit geometryChanged();
+  if (before != this->marginWidth())
+    emit geometryChanged();
 }
 
 
@@ -159,8 +140,10 @@
 
 
 void PlotItem::setMarginHeight(qreal marginHeight) {
+  qreal before = this->marginHeight();
   _marginHeight = marginHeight;
-  emit geometryChanged();
+  if (before != this->marginHeight())
+    emit geometryChanged();
 }
 
 
@@ -210,78 +193,102 @@
 }
 
 
-void PlotItem::calculateLeftLabelBound(QPainter *painter) {
+void PlotItem::paintLeftLabel(QPainter *painter) {
+  painter->save();
+  QTransform t;
+  t.rotate(90.0);
+  painter->rotate(-90.0);
 
-//   painter->save();
-//   QTransform t;
-//   t.rotate(90.0);
-//   painter->rotate(-90.0);
-// 
-//   QRectF leftLabelRect = verticalLabelRect();
-//   leftLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
-// 
-//   painter->drawText(t.mapRect(leftLabelRect), Qt::AlignCenter, leftLabel());
-//   painter->restore();
+  QRectF leftLabelRect = verticalLabelRect();
+  leftLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
+  painter->drawText(t.mapRect(leftLabelRect), Qt::TextWordWrap | Qt::AlignCenter, leftLabel());
+  painter->restore();
+}
 
 
+QSizeF PlotItem::calculateLeftLabelBound(QPainter *painter) {
   painter->save();
   QTransform t;
   t.rotate(90.0);
   painter->rotate(-90.0);
 
-  QRectF leftLabelRect = verticalLabelRect();
-  leftLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
-
-  QRectF leftLabelBound = painter->boundingRect(t.mapRect(leftLabelRect),
+  QRectF leftLabelBound = painter->boundingRect(t.mapRect(verticalLabelRect()),
                                                 Qt::TextWordWrap | Qt::AlignCenter, leftLabel());
   painter->restore();
 
-  qDebug() << leftLabelBound << endl;
+  QSizeF margins;
+  margins.setWidth(leftLabelBound.height());
+  return margins;
+}
 
-  if (leftLabelBound.height() > marginWidth()) {
-    qDebug() << "here1" << endl;
-    setMarginWidth(leftLabelBound.width());
-  }
-  if (leftLabelBound.width() > height() - 2 * marginHeight() && height() > 2 * marginHeight()) {
-    qDebug() << "here2" << leftLabelBound.width() << height() << (2 * marginHeight()) << endl;
-    setMarginHeight(height() - leftLabelBound.height() / 2);
-  }
+
+void PlotItem::paintBottomLabel(QPainter *painter) {
+  painter->save();
+  QRectF bottomLabelRect = horizontalLabelRect();
+  bottomLabelRect.moveTopLeft(QPointF(marginWidth(), height() - marginHeight()));
+  painter->drawText(bottomLabelRect, Qt::TextWordWrap | Qt::AlignCenter, bottomLabel());
+  painter->restore();
 }
 
 
-void PlotItem::calculateBottomLabelBound(QPainter *painter) {
-  QRectF bottomLabelBound = painter->boundingRect(horizontalLabelRect(), Qt::AlignCenter, bottomLabel());
-  if (bottomLabelBound.height() > marginHeight())
-    setMarginHeight(bottomLabelBound.height());
-  if (bottomLabelBound.width() > width() - 2 * marginWidth())
-    setMarginWidth(width() - bottomLabelBound.width() / 2);
+QSizeF PlotItem::calculateBottomLabelBound(QPainter *painter) {
+  QRectF bottomLabelBound = painter->boundingRect(horizontalLabelRect(),
+                                                  Qt::TextWordWrap | Qt::AlignCenter, bottomLabel());
+
+  QSizeF margins;
+  margins.setHeight(bottomLabelBound.height());
+  return margins;
 }
 
 
-void PlotItem::calculateRightLabelBound(QPainter *painter) {
+void PlotItem::paintRightLabel(QPainter *painter) {
   painter->save();
+  painter->translate(width() - marginWidth(), 0.0);
   QTransform t;
   t.rotate(-90.0);
   painter->rotate(90.0);
+
+  //same as left but painter is translated
+  QRectF rightLabelRect = verticalLabelRect();
+  rightLabelRect.moveTopLeft(QPointF(0.0, marginHeight()));
+  painter->drawText(t.mapRect(rightLabelRect), Qt::TextWordWrap | Qt::AlignCenter, rightLabel());
+  painter->restore();
+}
+
+
+QSizeF PlotItem::calculateRightLabelBound(QPainter *painter) {
+  painter->save();
+  QTransform t;
+  t.rotate(-90.0);
+  painter->rotate(90.0);
   QRectF rightLabelBound = painter->boundingRect(t.mapRect(verticalLabelRect()),
-                                                 Qt::AlignCenter, rightLabel());
+                                                 Qt::TextWordWrap | Qt::AlignCenter, rightLabel());
   painter->restore();
 
-  if (rightLabelBound.width() > marginWidth())
-    setMarginWidth(rightLabelBound.width());
-  if (rightLabelBound.height() > height() - 2 * marginHeight())
-    setMarginHeight(height() - rightLabelBound.height() / 2);
+  QSizeF margins;
+  margins.setWidth(rightLabelBound.height());
+  return margins;
 }
 
 
-void PlotItem::calculateTopLabelBound(QPainter *painter) {
-  QRectF topLabelBound = painter->boundingRect(horizontalLabelRect(), Qt::AlignCenter, topLabel());
-  if (topLabelBound.height() > marginHeight())
-    setMarginHeight(topLabelBound.height());
-  if (topLabelBound.width() > width() - 2 * marginWidth())
-    setMarginWidth(width() - topLabelBound.width() / 2);
+void PlotItem::paintTopLabel(QPainter *painter) {
+  painter->save();
+  QRectF topLabelRect = horizontalLabelRect();
+  topLabelRect.moveTopLeft(QPointF(marginWidth(), 0.0));
+  painter->drawText(topLabelRect, Qt::TextWordWrap | Qt::AlignCenter, bottomLabel());
+  painter->restore();
 }
 
+
+QSizeF PlotItem::calculateTopLabelBound(QPainter *painter) {
+  QRectF topLabelBound = painter->boundingRect(horizontalLabelRect(),
+                                               Qt::TextWordWrap | Qt::AlignCenter, topLabel());
+
+  QSizeF margins;
+  margins.setHeight(topLabelBound.height());
+  return margins;
 }
 
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.h #704332:704333
@@ -50,10 +50,14 @@
     QRectF horizontalLabelRect() const;
     QRectF verticalLabelRect() const;
 
-    void calculateLeftLabelBound(QPainter *painter);
-    void calculateBottomLabelBound(QPainter *painter);
-    void calculateRightLabelBound(QPainter *painter);
-    void calculateTopLabelBound(QPainter *painter);
+    void paintLeftLabel(QPainter *painter);
+    QSizeF calculateLeftLabelBound(QPainter *painter);
+    void paintBottomLabel(QPainter *painter);
+    QSizeF calculateBottomLabelBound(QPainter *painter);
+    void paintRightLabel(QPainter *painter);
+    QSizeF calculateRightLabelBound(QPainter *painter);
+    void paintTopLabel(QPainter *painter);
+    QSizeF calculateTopLabelBound(QPainter *painter);
 
   private:
     QList<PlotRenderItem*> _renderers;
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #704332:704333
@@ -74,7 +74,7 @@
   Q_UNUSED(option);
   Q_UNUSED(widget);
   painter->fillRect(rect(), Qt::white);
-//   paint(painter);
+  paint(painter);
 }
 
 


More information about the Kst mailing list