! \internal This is a \ref placeTickLabel helper function. Transforms the passed \a text and \a font to a tickLabelData structure that can then be further processed by \ref getTickLabelDrawOffset and \ref drawTickLabel. It splits the text into base and exponent if necessary (member substituteExponent) and calculates appropriate bounding boxes. */
| 9625 | exponent if necessary (member substituteExponent) and calculates appropriate bounding boxes. |
| 9626 | */ |
| 9627 | QCPAxisPainterPrivate::TickLabelData QCPAxisPainterPrivate::getTickLabelData(const QFont &font, const QString &text) const |
| 9628 | { |
| 9629 | TickLabelData result; |
| 9630 | |
| 9631 | // determine whether beautiful decimal powers should be used |
| 9632 | bool useBeautifulPowers = false; |
| 9633 | int ePos = -1; // first index of exponent part, text before that will be basePart, text until eLast will be expPart |
| 9634 | int eLast = -1; // last index of exponent part, rest of text after this will be suffixPart |
| 9635 | if (substituteExponent) |
| 9636 | { |
| 9637 | ePos = text.indexOf(QLatin1Char('e')); |
| 9638 | if (ePos > 0 && text.at(ePos-1).isDigit()) |
| 9639 | { |
| 9640 | eLast = ePos; |
| 9641 | while (eLast+1 < text.size() && (text.at(eLast+1) == QLatin1Char('+') || text.at(eLast+1) == QLatin1Char('-') || text.at(eLast+1).isDigit())) |
| 9642 | ++eLast; |
| 9643 | if (eLast > ePos) // only if also to right of 'e' is a digit/+/- interpret it as beautifiable power |
| 9644 | useBeautifulPowers = true; |
| 9645 | } |
| 9646 | } |
| 9647 | |
| 9648 | // calculate text bounding rects and do string preparation for beautiful decimal powers: |
| 9649 | result.baseFont = font; |
| 9650 | if (result.baseFont.pointSizeF() > 0) // might return -1 if specified with setPixelSize, in that case we can't do correction in next line |
| 9651 | result.baseFont.setPointSizeF(result.baseFont.pointSizeF()+0.05); // QFontMetrics.boundingRect has a bug for exact point sizes that make the results oscillate due to internal rounding |
| 9652 | if (useBeautifulPowers) |
| 9653 | { |
| 9654 | // split text into parts of number/symbol that will be drawn normally and part that will be drawn as exponent: |
| 9655 | result.basePart = text.left(ePos); |
| 9656 | result.suffixPart = text.mid(eLast+1); // also drawn normally but after exponent |
| 9657 | // in log scaling, we want to turn "1*10^n" into "10^n", else add multiplication sign and decimal base: |
| 9658 | if (abbreviateDecimalPowers && result.basePart == QLatin1String("1")) |
| 9659 | result.basePart = QLatin1String("10"); |
| 9660 | else |
| 9661 | result.basePart += (numberMultiplyCross ? QString(QChar(215)) : QString(QChar(183))) + QLatin1String("10"); |
| 9662 | result.expPart = text.mid(ePos+1, eLast-ePos); |
| 9663 | // clip "+" and leading zeros off expPart: |
| 9664 | while (result.expPart.length() > 2 && result.expPart.at(1) == QLatin1Char('0')) // length > 2 so we leave one zero when numberFormatChar is 'e' |
| 9665 | result.expPart.remove(1, 1); |
| 9666 | if (!result.expPart.isEmpty() && result.expPart.at(0) == QLatin1Char('+')) |
| 9667 | result.expPart.remove(0, 1); |
| 9668 | // prepare smaller font for exponent: |
| 9669 | result.expFont = font; |
| 9670 | if (result.expFont.pointSize() > 0) |
| 9671 | result.expFont.setPointSize(result.expFont.pointSize()*0.75); |
| 9672 | else |
| 9673 | result.expFont.setPixelSize(result.expFont.pixelSize()*0.75); |
| 9674 | // calculate bounding rects of base part(s), exponent part and total one: |
| 9675 | result.baseBounds = QFontMetrics(result.baseFont).boundingRect(0, 0, 0, 0, Qt::TextDontClip, result.basePart); |
| 9676 | result.expBounds = QFontMetrics(result.expFont).boundingRect(0, 0, 0, 0, Qt::TextDontClip, result.expPart); |
| 9677 | if (!result.suffixPart.isEmpty()) |
| 9678 | result.suffixBounds = QFontMetrics(result.baseFont).boundingRect(0, 0, 0, 0, Qt::TextDontClip, result.suffixPart); |
| 9679 | result.totalBounds = result.baseBounds.adjusted(0, 0, result.expBounds.width()+result.suffixBounds.width()+2, 0); // +2 consists of the 1 pixel spacing between base and exponent (see drawTickLabel) and an extra pixel to include AA |
| 9680 | } else // useBeautifulPowers == false |
| 9681 | { |
| 9682 | result.basePart = text; |
| 9683 | result.totalBounds = QFontMetrics(result.baseFont).boundingRect(0, 0, 0, 0, Qt::TextDontClip | Qt::AlignHCenter, result.basePart); |
| 9684 | } |