! \internal Takes the fraction given by \a numerator and \a denominator and returns a string representation. The result depends on the configured fraction style (\ref setFractionStyle). This method is used to format the numerical/fractional part when generating tick labels. It simplifies the passed fraction to an irreducible form using \ref simplifyFraction and factors out any inte
| 7582 | any integer parts of the fraction (e.g. "10/4" becomes "2 1/2"). |
| 7583 | */ |
| 7584 | QString QCPAxisTickerPi::fractionToString(int numerator, int denominator) const |
| 7585 | { |
| 7586 | if (denominator == 0) |
| 7587 | { |
| 7588 | qDebug() << Q_FUNC_INFO << "called with zero denominator"; |
| 7589 | return QString(); |
| 7590 | } |
| 7591 | if (mFractionStyle == fsFloatingPoint) // should never be the case when calling this function |
| 7592 | { |
| 7593 | qDebug() << Q_FUNC_INFO << "shouldn't be called with fraction style fsDecimal"; |
| 7594 | return QString::number(numerator/double(denominator)); // failsafe |
| 7595 | } |
| 7596 | int sign = numerator*denominator < 0 ? -1 : 1; |
| 7597 | numerator = qAbs(numerator); |
| 7598 | denominator = qAbs(denominator); |
| 7599 | |
| 7600 | if (denominator == 1) |
| 7601 | { |
| 7602 | return QString::number(sign*numerator); |
| 7603 | } else |
| 7604 | { |
| 7605 | int integerPart = numerator/denominator; |
| 7606 | int remainder = numerator%denominator; |
| 7607 | if (remainder == 0) |
| 7608 | { |
| 7609 | return QString::number(sign*integerPart); |
| 7610 | } else |
| 7611 | { |
| 7612 | if (mFractionStyle == fsAsciiFractions) |
| 7613 | { |
| 7614 | return QString(QLatin1String("%1%2%3/%4")) |
| 7615 | .arg(sign == -1 ? QLatin1String("-") : QLatin1String("")) |
| 7616 | .arg(integerPart > 0 ? QString::number(integerPart)+QLatin1String(" ") : QString(QLatin1String(""))) |
| 7617 | .arg(remainder) |
| 7618 | .arg(denominator); |
| 7619 | } else if (mFractionStyle == fsUnicodeFractions) |
| 7620 | { |
| 7621 | return QString(QLatin1String("%1%2%3")) |
| 7622 | .arg(sign == -1 ? QLatin1String("-") : QLatin1String("")) |
| 7623 | .arg(integerPart > 0 ? QString::number(integerPart) : QLatin1String("")) |
| 7624 | .arg(unicodeFraction(remainder, denominator)); |
| 7625 | } |
| 7626 | } |
| 7627 | } |
| 7628 | return QString(); |
| 7629 | } |
| 7630 | |
| 7631 | /*! \internal |
| 7632 |