! \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
| 6891 | any integer parts of the fraction (e.g. "10/4" becomes "2 1/2"). |
| 6892 | */ |
| 6893 | QString QCPAxisTickerPi::fractionToString(int numerator, int denominator) const |
| 6894 | { |
| 6895 | if (denominator == 0) |
| 6896 | { |
| 6897 | qDebug() << Q_FUNC_INFO << "called with zero denominator"; |
| 6898 | return QString(); |
| 6899 | } |
| 6900 | if (mFractionStyle == fsFloatingPoint) // should never be the case when calling this function |
| 6901 | { |
| 6902 | qDebug() << Q_FUNC_INFO << "shouldn't be called with fraction style fsDecimal"; |
| 6903 | return QString::number(numerator/(double)denominator); // failsafe |
| 6904 | } |
| 6905 | int sign = numerator*denominator < 0 ? -1 : 1; |
| 6906 | numerator = qAbs(numerator); |
| 6907 | denominator = qAbs(denominator); |
| 6908 | |
| 6909 | if (denominator == 1) |
| 6910 | { |
| 6911 | return QString::number(sign*numerator); |
| 6912 | } else |
| 6913 | { |
| 6914 | int integerPart = numerator/denominator; |
| 6915 | int remainder = numerator%denominator; |
| 6916 | if (remainder == 0) |
| 6917 | { |
| 6918 | return QString::number(sign*integerPart); |
| 6919 | } else |
| 6920 | { |
| 6921 | if (mFractionStyle == fsAsciiFractions) |
| 6922 | { |
| 6923 | return QString(QLatin1String("%1%2%3/%4")) |
| 6924 | .arg(sign == -1 ? QLatin1String("-") : QLatin1String("")) |
| 6925 | .arg(integerPart > 0 ? QString::number(integerPart)+QLatin1String(" ") : QLatin1String("")) |
| 6926 | .arg(remainder) |
| 6927 | .arg(denominator); |
| 6928 | } else if (mFractionStyle == fsUnicodeFractions) |
| 6929 | { |
| 6930 | return QString(QLatin1String("%1%2%3")) |
| 6931 | .arg(sign == -1 ? QLatin1String("-") : QLatin1String("")) |
| 6932 | .arg(integerPart > 0 ? QString::number(integerPart) : QLatin1String("")) |
| 6933 | .arg(unicodeFraction(remainder, denominator)); |
| 6934 | } |
| 6935 | } |
| 6936 | } |
| 6937 | return QString(); |
| 6938 | } |
| 6939 | |
| 6940 | /*! \internal |
| 6941 |
nothing calls this directly
no outgoing calls
no test coverage detected