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

Mike Fenton mike at staikos.net
Fri Mar 20 20:33:13 CET 2009


SVN commit 941959 by fenton:

Add caching for PlotItem labels as QPixmap's.


 M  +140 -65   plotitem.cpp  
 M  +25 -0     plotitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #941958:941959
@@ -147,6 +147,7 @@
 
   setPlotBordersDirty(true);
   connect(this, SIGNAL(updatePlotRect()), this, SLOT(redrawPlot()));
+  connect(this, SIGNAL(geometryChanged()), this, SLOT(setLabelsDirty()));
 }
 
 
@@ -507,6 +508,7 @@
           projectionRect().width(),
           compute.height()));
   }
+  setLabelsDirty();
   update();
 }
 
@@ -1301,6 +1303,7 @@
 void PlotItem::setRightLabelFont(const QFont &font) {
   _rightLabelFont = font;
   setPlotBordersDirty(true);
+  setRightLabelDirty();
 }
 
 
@@ -1312,6 +1315,7 @@
 void PlotItem::setTopLabelFont(const QFont &font) {
   _topLabelFont = font;
   setPlotBordersDirty(true);
+  setTopLabelDirty();
 }
 
 
@@ -1333,6 +1337,7 @@
 void PlotItem::setLeftLabelFont(const QFont &font) {
   _leftLabelFont = font;
   setPlotBordersDirty(true);
+  setLeftLabelDirty();
 }
 
 
@@ -1344,6 +1349,7 @@
 void PlotItem::setBottomLabelFont(const QFont &font) {
   _bottomLabelFont = font;
   setPlotBordersDirty(true);
+  setBottomLabelDirty();
 }
 
 
@@ -1366,6 +1372,7 @@
 void PlotItem::setRightLabelFontScale(const qreal scale) {
   _rightLabelFontScale = scale;
   setPlotBordersDirty(true);
+  setRightLabelDirty();
 }
 
 
@@ -1387,6 +1394,7 @@
 void PlotItem::setLeftLabelFontScale(const qreal scale) {
   _leftLabelFontScale = scale;
   setPlotBordersDirty(true);
+  setLeftLabelDirty();
 }
 
 
@@ -1398,6 +1406,7 @@
 void PlotItem::setTopLabelFontScale(const qreal scale) {
   _topLabelFontScale = scale;
   setPlotBordersDirty(true);
+  setTopLabelDirty();
 }
 
 
@@ -1409,6 +1418,7 @@
 void PlotItem::setBottomLabelFontScale(const qreal scale) {
   _bottomLabelFontScale = scale;
   setPlotBordersDirty(true);
+  setBottomLabelDirty();
 }
 
 
@@ -1440,6 +1450,7 @@
 
 void PlotItem::setLeftLabelFontColor(const QColor &color) {
   _leftLabelFontColor = color;
+  setLeftLabelDirty();
 }
 
 
@@ -1450,6 +1461,7 @@
 
 void PlotItem::setRightLabelFontColor(const QColor &color) {
   _rightLabelFontColor = color;
+  setRightLabelDirty();
 }
 
 
@@ -1460,6 +1472,7 @@
 
 void PlotItem::setTopLabelFontColor(const QColor &color) {
   _topLabelFontColor = color;
+  setTopLabelDirty();
 }
 
 
@@ -1470,6 +1483,7 @@
 
 void PlotItem::setBottomLabelFontColor(const QColor &color) {
   _bottomLabelFontColor = color;
+  setBottomLabelDirty();
 }
 
 
@@ -1499,6 +1513,7 @@
     _leftLabelOverride = label;
   }
   setPlotBordersDirty(true);
+  setLeftLabelDirty();
 }
 
 
@@ -1518,6 +1533,7 @@
     _bottomLabelOverride = label;
   }
   setPlotBordersDirty(true);
+  setBottomLabelDirty();
 }
 
 
@@ -1537,6 +1553,7 @@
     _topLabelOverride = label;
   }
   setPlotBordersDirty(true);
+  setTopLabelDirty();
 }
 
 
@@ -1556,6 +1573,7 @@
     _rightLabelOverride = label;
   }
   setPlotBordersDirty(true);
+  setRightLabelDirty();
 }
 
 
@@ -1887,13 +1905,14 @@
 }
 
 
-void PlotItem::paintLeftLabel(QPainter *painter) {
-  if (!isLeftLabelVisible() || leftLabelOverride().isEmpty())
+void PlotItem::generateLeftLabel() {
+  if (!_leftLabel.dirty) {
     return;
-
+  }
+  _leftLabel.valid = false;
+  _leftLabel.dirty = false;
   Label::Parsed *parsed = Label::parse(leftLabelOverride());
   if (parsed) {
-
     parsed->chunk->attributes.color = _leftLabelFontColor;
 
     QRectF leftLabel = leftLabelRect(false);
@@ -1909,19 +1928,33 @@
     leftLabel.moveTopRight(plotAxisRect().topLeft());
     leftLabel.moveBottomLeft(QPointF(leftLabel.bottomLeft().x(), plotRect().center().y()+ rc.x / 2));
 
-    painter->save();
     QTransform t;
-    t.rotate(90.0);
-    painter->rotate(-90.0);
+    t.rotate(-90.0);
+    _leftLabel.pixmap = pixmap.transformed(t);
+    _leftLabel.rect = leftLabel;
+    _leftLabel.location = rc.x;
+    if (rc.x > 0) {
+      _leftLabel.valid = true;
+    }
 
-    if (rc.x > 0)
-      painter->drawPixmap(t.mapRect(leftLabel).topLeft(), pixmap, QRectF(0, 0, rc.x, leftLabel.height()));
-
-    painter->restore();
     delete parsed;
     parsed = 0;
   }
+}
 
+
+void PlotItem::paintLeftLabel(QPainter *painter) {
+  if (!isLeftLabelVisible() || leftLabelOverride().isEmpty())
+    return;
+
+  generateLeftLabel();
+
+  if (_leftLabel.valid) {
+    painter->save();
+    painter->drawPixmap(_leftLabel.rect.topLeft(), _leftLabel.pixmap, QRectF(0, 0, _leftLabel.location, _leftLabel.rect.height()));
+    painter->restore();
+  }
+
 #if DEBUG_LABEL_REGION
   painter->save();
   QTransform t;
@@ -1964,15 +1997,14 @@
 }
 
 
-void PlotItem::paintBottomLabel(QPainter *painter) {
-  if (!isBottomLabelVisible() || bottomLabelOverride().isEmpty())
+void PlotItem::generateBottomLabel() {
+  if (!_bottomLabel.dirty) {
     return;
-
+  }
+  _bottomLabel.valid = false;
+  _bottomLabel.dirty = false;
   Label::Parsed *parsed = Label::parse(bottomLabelOverride());
-
   if (parsed) {
-    painter->save();
-
     parsed->chunk->attributes.color = _bottomLabelFontColor;
 
     QRectF bottomLabel = bottomLabelRect(false);
@@ -1987,14 +2019,31 @@
 
     bottomLabel.moveBottomLeft(QPointF(plotRect().center().x()-rc.x/2, rect().bottomLeft().y()));
 
-    if (rc.x > 0)
-      painter->drawPixmap(bottomLabel.topLeft(), pixmap, QRectF(0, 0, rc.x, bottomLabel.height()));
+    _bottomLabel.pixmap = pixmap;
+    _bottomLabel.rect = bottomLabel;
+    _bottomLabel.location = rc.x;
+    if (rc.x > 0) {
+      _bottomLabel.valid = true;
+    }
 
-    painter->restore();
     delete parsed;
     parsed = 0;
   }
+}
 
+
+void PlotItem::paintBottomLabel(QPainter *painter) {
+  if (!isBottomLabelVisible() || bottomLabelOverride().isEmpty())
+    return;
+
+  generateBottomLabel();
+
+  if (_bottomLabel.valid) {
+    painter->save();
+    painter->drawPixmap(_bottomLabel.rect.topLeft(), _bottomLabel.pixmap, QRectF(0, 0, _bottomLabel.location, _bottomLabel.rect.height()));
+    painter->restore();
+  }
+
 #if DEBUG_LABEL_REGION
   painter->save();
 
@@ -2030,44 +2079,55 @@
 }
 
 
-void PlotItem::paintRightLabel(QPainter *painter) {
-  if (!isRightLabelVisible() || rightLabelOverride().isEmpty())
+void PlotItem::generateRightLabel() {
+  if (!_rightLabel.dirty) {
     return;
-
+  }
+  _rightLabel.valid = false;
+  _rightLabel.dirty = false;
   Label::Parsed *parsed = Label::parse(rightLabelOverride());
-
-  if (parsed) {
-    QRectF rightLabel = rightLabelRect(false);
-
+  QRectF rightLabel = rightLabelRect(false);
+  if (parsed && rightLabel.isValid()) {
     parsed->chunk->attributes.color = _rightLabelFontColor;
 
-    if (rightLabel.isValid()) {
-      QPixmap pixmap(rightLabel.height(), rightLabel.width());
-      pixmap.fill(Qt::transparent);
-      QPainter pixmapPainter(&pixmap);
+    QPixmap pixmap(rightLabel.height(), rightLabel.width());
+    pixmap.fill(Qt::transparent);
+    QPainter pixmapPainter(&pixmap);
 
-      Label::RenderContext rc(calculatedRightLabelFont(), &pixmapPainter);
-      QFontMetrics fm(calculatedRightLabelFont());
-      rc.y = fm.ascent();
-      Label::renderLabel(rc, parsed->chunk);
+    Label::RenderContext rc(calculatedRightLabelFont(), &pixmapPainter);
+    QFontMetrics fm(calculatedRightLabelFont());
+    rc.y = fm.ascent();
+    Label::renderLabel(rc, parsed->chunk);
 
-      rightLabel.moveTopLeft(QPointF(plotAxisRect().right(), plotRect().center().y() - rc.x / 2));
+    rightLabel.moveTopLeft(QPointF(plotAxisRect().right(), plotRect().center().y() - rc.x / 2));
 
-      painter->save();
-      QTransform t;
-      t.rotate(-90.0);
-      painter->rotate(90.0);
-
-      if (rc.x > 0)
-        painter->drawPixmap(t.mapRect(rightLabel).topLeft(), pixmap, QRectF(0, 0, rc.x, rightLabel.height()));
-
-      painter->restore();
+    QTransform t;
+    t.rotate(90.0);
+    _rightLabel.pixmap = pixmap.transformed(t);
+    _rightLabel.rect = rightLabel;
+    _rightLabel.location = rc.x;
+    if (rc.x > 0) {
+      _rightLabel.valid = true;
     }
 
     delete parsed;
     parsed = 0;
   }
+}
 
+
+void PlotItem::paintRightLabel(QPainter *painter) {
+  if (!isRightLabelVisible() || rightLabelOverride().isEmpty())
+    return;
+
+  generateRightLabel();
+
+  if (_rightLabel.valid) {
+    painter->save();
+    painter->drawPixmap(_rightLabel.rect.topLeft(), _rightLabel.pixmap, QRectF(0, 0, _rightLabel.location, _rightLabel.rect.height()));
+    painter->restore();
+  }
+
 #if DEBUG_LABEL_REGION
   painter->save();
   QTransform t;
@@ -2109,38 +2169,53 @@
 }
 
 
-void PlotItem::paintTopLabel(QPainter *painter) {
-  if (!isTopLabelVisible() || topLabelOverride().isEmpty())
+void PlotItem::generateTopLabel() {
+  if (!_topLabel.dirty) {
     return;
-
+  }
+  _topLabel.valid = false;
+  _topLabel.dirty = false;
   Label::Parsed *parsed = Label::parse(topLabelOverride());
+  QRectF topLabel = topLabelRect(false);
+  if (parsed && topLabel.isValid()) {
+    parsed->chunk->attributes.color = _topLabelFontColor;
 
-  if (parsed) {
-    painter->save();
+    QPixmap pixmap(topLabel.width(), topLabel.height());
+    pixmap.fill(Qt::transparent);
+    QPainter pixmapPainter(&pixmap);
 
-    parsed->chunk->attributes.color = _topLabelFontColor;
+    Label::RenderContext rc(calculatedTopLabelFont(), &pixmapPainter);
+    QFontMetrics fm(calculatedTopLabelFont());
+    rc.y = fm.ascent();
+    Label::renderLabel(rc, parsed->chunk);
 
-    QRectF topLabel = topLabelRect(false);
-    if (topLabel.isValid()) {
-      QPixmap pixmap(topLabel.width(), topLabel.height());
-      pixmap.fill(Qt::transparent);
-      QPainter pixmapPainter(&pixmap);
+    topLabel.moveBottomLeft(QPointF(plotRect().center().x()-rc.x/2, plotAxisRect().topLeft().y()));
 
-      Label::RenderContext rc(calculatedTopLabelFont(), &pixmapPainter);
-      QFontMetrics fm(calculatedTopLabelFont());
-      rc.y = fm.ascent();
-      Label::renderLabel(rc, parsed->chunk);
-
-      topLabel.moveTopLeft(QPointF(plotRect().center().x()-rc.x/2, 0));
-      if (rc.x > 0)
-        painter->drawPixmap(topLabel.topLeft(), pixmap, QRectF(0, 0, rc.x, topLabel.height()));
+    _topLabel.pixmap = pixmap;
+    _topLabel.rect = topLabel;
+    _topLabel.location = rc.x;
+    if (rc.x > 0) {
+      _topLabel.valid = true;
     }
-    painter->restore();
 
     delete parsed;
     parsed = 0;
   }
+}
 
+
+void PlotItem::paintTopLabel(QPainter *painter) {
+  if (!isTopLabelVisible() || topLabelOverride().isEmpty())
+    return;
+
+  generateTopLabel();
+
+  if (_topLabel.valid) {
+    painter->save();
+    painter->drawPixmap(_topLabel.rect.topLeft(), _topLabel.pixmap, QRectF(0, 0, _topLabel.location, _topLabel.rect.height()));
+    painter->restore();
+  }
+
 #if DEBUG_LABEL_REGION
   painter->save();
 
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.h #941958:941959
@@ -47,6 +47,16 @@
 };
 
 
+struct DrawnLabel {
+  DrawnLabel() { valid = false; dirty = true; };
+
+  bool valid;
+  bool dirty;
+  QPixmap pixmap;
+  QRectF rect;
+  int location;
+};
+
 class PlotItem : public ViewItem, public PlotItemInterface, public NamedObject
 {
   Q_OBJECT
@@ -286,6 +296,12 @@
     void showFilterDialog(QAction*);
     void showFitDialog(QAction*);
 
+    void setLeftLabelDirty() { _leftLabel.dirty = true; }
+    void setRightLabelDirty() { _rightLabel.dirty = true; }
+    void setTopLabelDirty() { _topLabel.dirty = true; }
+    void setBottomLabelDirty() { _bottomLabel.dirty = true; }
+    void setLabelsDirty() { _leftLabel.dirty = true; _rightLabel.dirty = true; _topLabel.dirty = true; _bottomLabel.dirty = true; }
+
   private:
     void createActions();
     void createZoomMenu();
@@ -330,12 +346,16 @@
     QRectF rightLabelRect(bool calc) const;
 
     void paintLeftLabel(QPainter *painter);
+    void generateLeftLabel();
     QSizeF calculateLeftLabelBound(QPainter *painter);
     void paintBottomLabel(QPainter *painter);
+    void generateBottomLabel();
     QSizeF calculateBottomLabelBound(QPainter *painter);
     void paintRightLabel(QPainter *painter);
+    void generateRightLabel();
     QSizeF calculateRightLabelBound(QPainter *painter);
     void paintTopLabel(QPainter *painter);
+    void generateTopLabel();
     QSizeF calculateTopLabelBound(QPainter *painter);
 
     QFont calculatedTopLabelFont();
@@ -438,6 +458,11 @@
 
     LegendItem* _legend;
 
+    DrawnLabel _leftLabel;
+    DrawnLabel _rightLabel;
+    DrawnLabel _topLabel;
+    DrawnLabel _bottomLabel;
+
     QMenu *_zoomMenu;
     QAction *_zoomMaximum;
     QAction *_zoomMaxSpikeInsensitive;


More information about the Kst mailing list