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

Mike Fenton mike at staikos.net
Wed May 20 22:43:28 CEST 2009


SVN commit 970803 by fenton:

Add Axis Label rotation support.


 M  +12 -0     axistab.cpp  
 M  +3 -0      axistab.h  
 M  +43 -4     axistab.ui  
 M  +20 -1     plotaxis.cpp  
 M  +5 -0      plotaxis.h  
 M  +84 -9     plotitem.cpp  
 M  +4 -0      plotitemdialog.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/axistab.cpp #970802:970803
@@ -76,6 +76,8 @@
   connect(_scaleAutoBaseOffset, SIGNAL(stateChanged(int)), this, SIGNAL(modified()));
   connect(_scaleBaseOffset, SIGNAL(stateChanged(int)), this, SIGNAL(modified()));
   connect(_significantDigits, SIGNAL(valueChanged(int)), this, SIGNAL(modified()));
+
+  connect(_rotation, SIGNAL(valueChanged(int)), this, SIGNAL(modified()));
 }
 
 
@@ -263,6 +265,16 @@
 }
 
 
+int AxisTab::labelRotation() const {
+  return _rotation->value();
+}
+
+
+void AxisTab::setLabelRotation(const int rotation) {
+  _rotation->setValue(rotation);
+}
+
+
 void AxisTab::updateButtons() {
   _scaleBaseOffset->setEnabled(!(isInterpret() || isAutoBaseOffset()));
 }
--- branches/work/kst/portto4/kst/src/libkstapp/axistab.h #970802:970803
@@ -81,6 +81,9 @@
     AxisInterpretationType axisInterpretation() const;
     void setAxisInterpretation(AxisInterpretationType interpretation);
 
+    int labelRotation() const;
+    void setLabelRotation(const int rotation);
+
   public Q_SLOTS:
     void updateButtons();
 
--- branches/work/kst/portto4/kst/src/libkstapp/axistab.ui #970802:970803
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>644</width>
-    <height>382</height>
+    <width>537</width>
+    <height>451</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -174,7 +174,46 @@
      </layout>
     </widget>
    </item>
-   <item row="2" column="0">
+   <item row="2" column="0" colspan="2">
+    <widget class="QGroupBox" name="groupBox_2">
+     <property name="title">
+      <string>Label Rotation</string>
+     </property>
+     <layout class="QHBoxLayout" name="horizontalLayout_3">
+      <item>
+       <widget class="QLabel" name="label_4">
+        <property name="text">
+         <string>Rotation :</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QSpinBox" name="_rotation">
+        <property name="minimum">
+         <number>-90</number>
+        </property>
+        <property name="maximum">
+         <number>90</number>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <spacer name="horizontalSpacer">
+        <property name="orientation">
+         <enum>Qt::Horizontal</enum>
+        </property>
+        <property name="sizeHint" stdset="0">
+         <size>
+          <width>40</width>
+          <height>20</height>
+         </size>
+        </property>
+       </spacer>
+      </item>
+     </layout>
+    </widget>
+   </item>
+   <item row="3" column="0">
     <widget class="QGroupBox" name="_axisMajorGridGroup">
      <property name="title">
       <string>Major Ticks and Lines</string>
@@ -305,7 +344,7 @@
      </layout>
     </widget>
    </item>
-   <item row="2" column="1">
+   <item row="3" column="1">
     <widget class="QGroupBox" name="_axisMinorGridGroup">
      <property name="title">
       <string>Minor Ticks and Lines</string>
--- branches/work/kst/portto4/kst/src/libkstapp/plotaxis.cpp #970802:970803
@@ -52,7 +52,8 @@
   _axisMinorGridLineColor(Qt::gray),
   _axisMajorGridLineStyle(Qt::DashLine),
   _axisMinorGridLineStyle(Qt::DashLine),
-  _axisPlotMarkers(orientation == Qt::Horizontal)
+  _axisPlotMarkers(orientation == Qt::Horizontal),
+  _labelRotation(0)
  {
   connect(_plotItem, SIGNAL(updateAxes()), this, SLOT(updateTicks()));
 }
@@ -608,6 +609,19 @@
 }
 
 
+int PlotAxis::axisLabelRotation() const {
+  return _labelRotation;
+}
+
+
+void PlotAxis::setAxisLabelRotation(const int rotation) {
+  if (_labelRotation != rotation) {
+    _labelRotation = rotation;
+    _dirty = true;
+  }
+}
+
+
 void PlotAxis::computeLogTicks(QList<qreal> *MajorTicks, QList<qreal> *MinorTicks, QMap<qreal, QString> *Labels, qreal min, qreal max, MajorTickMode tickMode) {
 
   qreal tick;
@@ -935,6 +949,7 @@
   xml.writeAttribute("drawmajorgridlinestyle", QVariant(axisMajorGridLineStyle()).toString());
   xml.writeAttribute("drawminorgridlinestyle", QVariant(axisMinorGridLineStyle()).toString());
   xml.writeAttribute("significantdigits", QVariant(axisSignificantDigits()).toString());
+  xml.writeAttribute("rotation", QVariant(axisLabelRotation()).toString());
   xml.writeAttribute("zoommode", QVariant(axisZoomMode()).toString());
   _axisPlotMarkers.saveInPlot(xml);
   xml.writeEndElement();
@@ -1022,6 +1037,10 @@
   if (!av.isNull()) {
     setAxisSignificantDigits(QVariant(av.toString()).toInt());
   }
+  av = attrs.value("rotation");
+  if (!av.isNull()) {
+    setAxisLabelRotation(QVariant(av.toString()).toInt());
+  }
   av = attrs.value("zoommode");
   if (!av.isNull()) {
     setAxisZoomMode((PlotAxis::ZoomMode)av.toString().toInt());
--- branches/work/kst/portto4/kst/src/libkstapp/plotaxis.h #970802:970803
@@ -99,6 +99,9 @@
     AxisInterpretationType axisInterpretation() const;
     void setAxisInterpretation(const AxisInterpretationType interpret);
 
+    int axisLabelRotation() const;
+    void setAxisLabelRotation(const int rotation);
+
     PlotMarkers axisPlotMarkers() { return _axisPlotMarkers; }
     void setAxisPlotMarkers(const PlotMarkers &plotMarkers) { _axisPlotMarkers = plotMarkers; _ticksUpdated = true; }
 
@@ -180,6 +183,8 @@
     Qt::PenStyle _axisMajorGridLineStyle;
     Qt::PenStyle _axisMinorGridLineStyle;
 
+    int _labelRotation;
+
     PlotMarkers _axisPlotMarkers;
 };
 
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #970802:970803
@@ -764,15 +764,31 @@
 void PlotItem::updateXAxisLabels(QPainter* painter) {
   int flags = Qt::TextSingleLine | Qt::AlignCenter;
   _xPlotLabels.clear();
+
+  int rotation = _xAxis->axisLabelRotation();
+  painter->save();
+  QTransform t;
+  t.rotate(rotation);
+
   QMapIterator<qreal, QString> xLabelIt(_xAxis->axisLabels());
   while (xLabelIt.hasNext()) {
     xLabelIt.next();
 
     QRectF bound = painter->boundingRect(QRectF(), flags, xLabelIt.value());
-    bound.setWidth(bound.width());
-    QPointF p = QPointF(mapXToPlot(xLabelIt.key()), plotRect().bottom() + 
-        bound.height()*0.5 + _calculatedAxisMarginVLead);
-    bound.moveCenter(p);
+    QPointF p;
+    if (rotation == 0) {
+      p = QPointF(mapXToPlot(xLabelIt.key()), plotRect().bottom() + bound.height()*0.5 + _calculatedAxisMarginVLead);
+      bound.moveCenter(p);
+    } else {
+      if (rotation < 0) {
+        bound = t.mapRect(bound);
+        p = QPointF(mapXToPlot(xLabelIt.key()) - bound.width() / 2, plotRect().bottom() + _calculatedAxisMarginVLead);
+      } else {
+        p = QPointF(mapXToPlot(xLabelIt.key()) - bound.height() * 0.5, plotRect().bottom() + _calculatedAxisMarginVLead);
+        bound = t.mapRect(bound);
+      }
+      bound.moveTopLeft(p);
+    }
 
     if (rect().left() > bound.left()) bound.setLeft(rect().left());
     if (rect().right() < bound.right()) bound.setRight(rect().right());
@@ -782,6 +798,7 @@
     label.value = xLabelIt.value();
     _xPlotLabels.append(label);
   }
+  painter->restore();
 
   if (!_xAxis->baseLabel().isEmpty()) {
     QRectF bound = painter->boundingRect(QRectF(), flags, _xAxis->baseLabel());
@@ -853,14 +870,31 @@
 void PlotItem::updateYAxisLabels(QPainter* painter) {
   int flags = Qt::TextSingleLine | Qt::AlignCenter;
   _yPlotLabels.clear();
+
+  int rotation = _yAxis->axisLabelRotation();
+
+  QTransform t;
+  t.rotate(rotation);
+
   QMapIterator<qreal, QString> yLabelIt(_yAxis->axisLabels());
   while (yLabelIt.hasNext()) {
     yLabelIt.next();
 
     QRectF bound = painter->boundingRect(QRectF(), flags, yLabelIt.value());
-    bound.setWidth(bound.width());
-    QPointF p = QPointF(plotRect().left() - (bound.width() / 2.0) - _calculatedAxisMarginHLead, mapYToPlot(yLabelIt.key()));
-    bound.moveCenter(p);
+    QPointF p;
+    if (rotation < 0) {
+      p = QPointF(plotRect().left() - _calculatedAxisMarginHLead, mapYToPlot(yLabelIt.key()) - bound.height() * 0.5);
+      bound = t.mapRect(bound);
+      bound.moveLeft(plotRect().left() - bound.width());
+      bound.moveTop(p.y());
+    } else {
+      bound = t.mapRect(bound);
+      p = QPointF(plotRect().left() - _calculatedAxisMarginHLead, mapYToPlot(yLabelIt.key()) - bound.height() * 0.5);
+      bound.moveTopRight(p);
+      if (rotation != 0) {
+        bound.moveLeft(plotRect().left() - bound.width());
+      }
+    }
 
     if (rect().top() > bound.top()) bound.setTop(rect().top());
     if (rect().bottom() < bound.bottom()) bound.setBottom(rect().bottom());
@@ -952,8 +986,20 @@
   painter->save();
   painter->setPen(_numberLabelDetails->fontColor());
 
+  int rotation = _xAxis->axisLabelRotation();
+
   foreach(CachedPlotLabel label, _xPlotLabels) {
-    painter->drawText(label.bound, flags, label.value);
+    if (rotation != 0) {
+      painter->save();
+      QTransform t;
+      t.rotate(-1*rotation);
+      painter->rotate(rotation);
+
+      painter->drawText(t.mapRect(label.bound), flags, label.value);
+      painter->restore();
+    } else {
+      painter->drawText(label.bound, flags, label.value);
+    }
   }
   painter->restore();
 
@@ -981,6 +1027,8 @@
   painter->save();
   painter->setPen(_numberLabelDetails->fontColor());
 
+  int rotation = _yAxis->axisLabelRotation();
+
   foreach(CachedPlotLabel label, _yPlotLabels) {
     if (label.baseLabel) {
       painter->save();
@@ -991,7 +1039,17 @@
       painter->drawText(t.mapRect(label.bound), flags, label.value);
       painter->restore();
     } else {
-      painter->drawText(label.bound, flags, label.value);
+      if (rotation != 0) {
+        painter->save();
+        QTransform t;
+        t.rotate(-1*rotation);
+        painter->rotate(rotation);
+
+        painter->drawText(t.mapRect(label.bound), flags, label.value);
+        painter->restore();
+      } else {
+        painter->drawText(label.bound, flags, label.value);
+      }
     }
   }
   painter->restore();
@@ -2001,6 +2059,14 @@
       QPointF p(mapXToPlot(xLabelIt.key()), plotRect().bottom() + bound.height() / 2.0 + _calculatedAxisMarginVLead);
       bound.moveCenter(p);
 
+      int rotation = _xAxis->axisLabelRotation();
+      QTransform t;
+      t.rotate(rotation);
+
+      if (rotation != 0) {
+        bound.setHeight(t.mapRect(bound).height());
+      }
+
       if (xLabelRect.isValid()) {
         xLabelRect = xLabelRect.united(bound);
       } else {
@@ -2052,6 +2118,15 @@
       QPointF p(plotRect().left() - bound.width() / 2.0 - _calculatedAxisMarginHLead, mapYToPlot(yLabelIt.key()));
       bound.moveCenter(p);
 
+      int rotation = _yAxis->axisLabelRotation();
+      QTransform t;
+      t.rotate(rotation);
+
+      if (rotation != 0) {
+        bound.setWidth(t.mapRect(bound).width());
+        bound.setHeight(t.mapRect(bound).height());
+      }
+
       if (yLabelRect.isValid()) {
         yLabelRect = yLabelRect.united(bound);
       } else {
--- branches/work/kst/portto4/kst/src/libkstapp/plotitemdialog.cpp #970802:970803
@@ -200,6 +200,7 @@
   _xAxisTab->setAxisInterpretation(_plotItem->xAxis()->axisInterpretation());
   _xAxisTab->setAxisMinorTickCount(_plotItem->xAxis()->axisMinorTickCount());
   _xAxisTab->setSignificantDigits(_plotItem->xAxis()->axisSignificantDigits());
+  _xAxisTab->setLabelRotation(_plotItem->xAxis()->axisLabelRotation());
 
   _yAxisTab->setAxisMajorTickSpacing(_plotItem->yAxis()->axisMajorTickMode());
   _yAxisTab->setDrawAxisMajorTicks(_plotItem->yAxis()->drawAxisMajorTicks());
@@ -219,6 +220,7 @@
   _yAxisTab->setAxisInterpretation(_plotItem->yAxis()->axisInterpretation());
   _yAxisTab->setAxisMinorTickCount(_plotItem->yAxis()->axisMinorTickCount());
   _yAxisTab->setSignificantDigits(_plotItem->yAxis()->axisSignificantDigits());
+  _yAxisTab->setLabelRotation(_plotItem->yAxis()->axisLabelRotation());
 }
 
 
@@ -527,6 +529,7 @@
   _plotItem->xAxis()->setAxisAutoBaseOffset(_xAxisTab->isAutoBaseOffset());
   _plotItem->xAxis()->setAxisMinorTickCount(_xAxisTab->axisMinorTickCount());
   _plotItem->xAxis()->setAxisSignificantDigits(_xAxisTab->significantDigits());
+  _plotItem->xAxis()->setAxisLabelRotation(_xAxisTab->labelRotation());
   _plotItem->setProjectionRect(_plotItem->projectionRect(), _plotItem->xAxis()->isDirty());
 }
 
@@ -552,6 +555,7 @@
   _plotItem->yAxis()->setAxisBaseOffset(_yAxisTab->isBaseOffset());
   _plotItem->yAxis()->setAxisMinorTickCount(_yAxisTab->axisMinorTickCount());
   _plotItem->yAxis()->setAxisSignificantDigits(_yAxisTab->significantDigits());
+  _plotItem->yAxis()->setAxisLabelRotation(_yAxisTab->labelRotation());
   _plotItem->setProjectionRect(_plotItem->projectionRect(), _plotItem->yAxis()->isDirty());
 }
 


More information about the Kst mailing list