[Kst] branches/work/kst/portto4/kst/src/libkstapp
Mike Fenton
mike at staikos.net
Thu May 1 17:06:01 CEST 2008
SVN commit 803011 by fenton:
Add logic to decrease the number of Major Ticks / Switch to Base Offset mode dependent on the drawn length of lengths to prevent overlap of x Axis labels.
M +77 -6 plotaxis.cpp
M +10 -4 plotaxis.h
M +2 -0 plotitem.cpp
--- branches/work/kst/portto4/kst/src/libkstapp/plotaxis.cpp #803010:803011
@@ -31,10 +31,12 @@
_axisLog(false),
_axisReversed(false),
_axisBaseOffset(false),
+ _axisBaseOffsetOverride(false),
_axisInterpret(false),
_axisDisplay(AXIS_DISPLAY_QTLOCALDATEHHMMSS_SS),
_axisInterpretation(AXIS_INTERP_CTIME),
_axisMajorTickMode(Normal),
+ _axisOverrideMajorTicks(Normal),
_axisMinorTickCount(4),
_axisSignificantDigits(9),
_drawAxisMajorTicks(true),
@@ -583,8 +585,77 @@
}
-void PlotAxis::update() {
+// Function validates that the labels will not overlap. Only functions for x Axis.
+void PlotAxis::validateDrawingRegion(int flags, QPainter *painter) {
+ if (_orientation != Qt::Horizontal) {
+ return;
+ }
+ // Always try to use the settings requested.
+ if (_axisOverrideMajorTicks != _axisMajorTickMode) {
+ _axisBaseOffsetOverride = false;
+ update();
+ }
+
+ int longest = 0;
+ QMapIterator<qreal, QString> iLongestLabelCheck(_axisLabels);
+ while (iLongestLabelCheck.hasNext()) {
+ iLongestLabelCheck.next();
+ QRectF bound = painter->boundingRect(QRectF(), flags, iLongestLabelCheck.value());
+ if (bound.width() > longest) {
+ longest = bound.width();
+ }
+ }
+
+// Make local... Use begin.
+ qreal firstTick = 0, secondTick = 0;
+ QMapIterator<qreal, QString> iLabelCheck(_axisLabels);
+ if (iLabelCheck.hasNext()) {
+ iLabelCheck.next();
+ firstTick = iLabelCheck.key();
+ if (iLabelCheck.hasNext()) {
+ iLabelCheck.next();
+ secondTick = iLabelCheck.key();
+ }
+ }
+
+ qreal labelSpace = plotItem()->mapXToPlot(secondTick) - plotItem()->mapXToPlot(firstTick);
+ if (labelSpace < (longest + 2)) {
+ _axisOverrideMajorTicks = convertToMajorTickMode((plotItem()->plotRect().width() / (longest + 2)) - 1);
+ if (_axisOverrideMajorTicks == None) {
+ _axisBaseOffsetOverride = true;
+ _axisOverrideMajorTicks = Coarse;
+ }
+ update(true);
+ }
+}
+
+
+PlotAxis::MajorTickMode PlotAxis::convertToMajorTickMode(int tickCount) {
+ MajorTickMode mode = None;
+ if (tickCount >= VeryFine) {
+ mode = VeryFine;
+ } else if (tickCount >= Fine) {
+ mode = Fine;
+ } else if (tickCount >= Normal) {
+ mode = Normal;
+ } else if (tickCount >= Coarse) {
+ mode = Coarse;
+ }
+ return mode;
+}
+
+
+void PlotAxis::update(bool useOverrideTicks) {
+ MajorTickMode majorTickCount;
+ if (useOverrideTicks) {
+ majorTickCount = _axisOverrideMajorTicks;
+ } else {
+ _axisOverrideMajorTicks = _axisMajorTickMode;
+ majorTickCount = _axisMajorTickMode;
+ _axisBaseOffsetOverride = false;
+ }
+
QMap<qreal, QString> labels;
QList<qreal> ticks;
QList<qreal> minTicks;
@@ -592,11 +663,11 @@
if (_axisLog) {
qreal min = _orientation == Qt::Horizontal ? plotItem()->xMin() : plotItem()->yMin();
qreal max = _orientation == Qt::Horizontal ? plotItem()->xMax() : plotItem()->yMax();
- computeLogTicks(&ticks, &minTicks, &labels, min, max, _axisMajorTickMode);
+ computeLogTicks(&ticks, &minTicks, &labels, min, max, majorTickCount);
} else {
qreal min = _orientation == Qt::Horizontal ? plotItem()->projectionRect().left() : plotItem()->projectionRect().top();
qreal max = _orientation == Qt::Horizontal ? plotItem()->projectionRect().right() : plotItem()->projectionRect().bottom();
- qreal majorTickSpacing = computedMajorTickSpacing(_orientation);
+ qreal majorTickSpacing = computedMajorTickSpacing(majorTickCount, _orientation);
qreal firstTick = ceil(min / majorTickSpacing) * majorTickSpacing;
int i = 0;
@@ -650,7 +721,7 @@
}
}
- if (_axisBaseOffset || _axisInterpret || (longest > _axisSignificantDigits) ) {
+ if (_axisBaseOffset || _axisInterpret || (longest > _axisSignificantDigits) || _axisBaseOffsetOverride ) {
if (_axisInterpret) {
_baseLabel = interpretLabel(_axisInterpretation, _axisDisplay, base, (_axisMajorTicks).last());
} else {
@@ -692,9 +763,9 @@
* on the axis (but at least 2). The value of M is set by the requested
* MajorTickMode.
*/
-qreal PlotAxis::computedMajorTickSpacing(Qt::Orientation orientation) {
+qreal PlotAxis::computedMajorTickSpacing(MajorTickMode majorTickCount, Qt::Orientation orientation) {
qreal R = orientation == Qt::Horizontal ? plotItem()->projectionRect().width() : plotItem()->projectionRect().height();
- qreal M = axisMajorTickMode();
+ qreal M = majorTickCount;
qreal B = floor(log10(R/M));
qreal d1 = 1 * pow(10, B);
--- branches/work/kst/portto4/kst/src/libkstapp/plotaxis.h #803010:803011
@@ -25,6 +25,7 @@
public:
enum MajorTickMode {
+ None = 0,
Coarse = 2,
Normal = 5,
Fine = 10,
@@ -106,11 +107,13 @@
void saveInPlot(QXmlStreamWriter &xml, QString axisId);
bool configureFromXml(QXmlStreamReader &xml, ObjectStore *store);
+ void validateDrawingRegion(int flags, QPainter *painter);
+
Q_SIGNALS:
void marginsChanged();
public Q_SLOTS:
- void update();
+ void update(bool useOverrideTicks = false);
private:
@@ -120,11 +123,10 @@
double convertTimeDiffValueToDays(KstAxisInterpretation axisInterpretation, double offsetIn);
double interpretOffset(KstAxisInterpretation axisInterpretation, KstAxisDisplay axisDisplay, double base, double value);
- qreal computedMajorTickSpacing(Qt::Orientation orientation);
+ qreal computedMajorTickSpacing(MajorTickMode majorTickCount, Qt::Orientation orientation);
void computeLogTicks(QList<qreal> *MajorTicks, QList<qreal> *MinorTicks, QMap<qreal, QString> *Labels, qreal min, qreal max, MajorTickMode tickMode);
- QSizeF calculateBottomTickLabelBound(QPainter *painter);
- QSizeF calculateLeftTickLabelBound(QPainter *painter);
+ MajorTickMode convertToMajorTickMode(int tickCount);
private:
@@ -144,11 +146,15 @@
bool _axisLog;
bool _axisReversed;
bool _axisBaseOffset;
+ bool _axisBaseOffsetOverride;
+
bool _axisInterpret;
KstAxisDisplay _axisDisplay;
KstAxisInterpretation _axisInterpretation;
MajorTickMode _axisMajorTickMode;
+ MajorTickMode _axisOverrideMajorTicks;
+
int _axisMinorTickCount;
int _axisSignificantDigits;
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #803010:803011
@@ -495,6 +495,8 @@
QRectF xLabelRect;
int flags = Qt::TextSingleLine | Qt::AlignCenter;
+ _xAxis->validateDrawingRegion(flags, painter);
+
QMapIterator<qreal, QString> xLabelIt(_xAxis->axisLabels());
while (xLabelIt.hasNext()) {
xLabelIt.next();
More information about the Kst
mailing list