From b26469042fdb9afbf9b8f0af37007686a6333056 Mon Sep 17 00:00:00 2001
From: cfauconnier <cfauconnier@alti.fr>
Date: Thu, 7 Nov 2013 10:25:41 +0100
Subject: [PATCH] Sankore 570

---
 src/tools/UBGraphicsTriangle.cpp | 81 +++++++++++++++-----------------
 1 file changed, 38 insertions(+), 43 deletions(-)

diff --git a/src/tools/UBGraphicsTriangle.cpp b/src/tools/UBGraphicsTriangle.cpp
index 32fd17d1..c18c90a3 100644
--- a/src/tools/UBGraphicsTriangle.cpp
+++ b/src/tools/UBGraphicsTriangle.cpp
@@ -309,76 +309,71 @@ QPainterPath UBGraphicsTriangle::shape() const
 
 void UBGraphicsTriangle::paintGraduations(QPainter *painter)
 {
+    const int SEPARATOR = 5;
     qreal kx = (mOrientation == TopLeft || mOrientation == BottomLeft) ? 1 : -1;
     qreal ky = (mOrientation == BottomLeft || mOrientation == BottomRight) ? 1 : -1;
 
     painter->save();
     painter->setFont(font());
     QFontMetricsF fontMetrics(painter->font());
+
     for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
     {
         int graduationX = rotationCenter().x() + kx * sPixelsPerMillimeter * millimeters;
+
         int graduationHeight = (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter) ?
             UBGeometryUtils::centimeterGraduationHeight :
             ((0 == millimeters % UBGeometryUtils::millimetersPerHalfCentimeter) ?
                 UBGeometryUtils::halfCentimeterGraduationHeight : UBGeometryUtils::millimeterGraduationHeight);
 
-        // Check that grad. line inside triangle
-        qreal dx = (kx > 0) ? rect().width() - graduationX : graduationX - rect().x();
-        qreal lineY = rotationCenter().y() - ky * rect().height()/rect().width() * dx;
-        if (mOrientation == BottomLeft || mOrientation == BottomRight)
+        qreal requiredSpace = graduationHeight + SEPARATOR ;
+        /*     B____C
+                |  /
+               D|_/E <-- availableSpace Thalès
+                |/
+                A
+        */
+
+        qreal AD;
+        switch(mOrientation)
         {
-            if (lineY >= rotationCenter().y() - ky * graduationHeight)
+            case BottomLeft:
+                AD = QLineF(rect().bottomRight(), QPointF(graduationX, rotationCenter().y())).length();
                 break;
-        }
-        else
-        {
-            if (lineY <= rotationCenter().y() - ky * graduationHeight)
+            case TopLeft:
+                AD = QLineF(rect().topRight(), QPointF(graduationX, rotationCenter().y())).length();
+                break;
+            case TopRight:
+                AD = QLineF(rect().topLeft(), QPointF(graduationX, rotationCenter().y())).length();
+                break;
+            case BottomRight:
+                AD = QLineF(rect().bottomLeft(), QPointF(graduationX , rotationCenter().y())).length();
                 break;
         }
 
-        painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight));
+        qreal AB = rect().width();
+        qreal BC = rect().height();
+        qreal DE =  AD * BC / AB, availableSpace = DE;
+
+        if (requiredSpace <= availableSpace)
+            painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight));
+        else
+            break;
+
         if (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter)
         {
             QString text = QString("%1").arg((int)(millimeters / UBGeometryUtils::millimetersPerCentimeter));
-            int textXRight = graduationX + fontMetrics.width(text) / 2;
             qreal textWidth = fontMetrics.width(text);
-            qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;
+            qreal textHeight = fontMetrics.tightBoundingRect(text).height();
 
-            int textY = (ky > 0) ? rotationCenter().y() - 5 - UBGeometryUtils::centimeterGraduationHeight - textHeight
-                : rotationCenter().y() + 5 + UBGeometryUtils::centimeterGraduationHeight;
+            requiredSpace = graduationHeight + textHeight + textWidth + SEPARATOR ;
 
-            bool bText = false;
-            switch(mOrientation)
+            if (requiredSpace <= availableSpace)
             {
-                case BottomLeft:
-                    dx = rect().width() - textXRight;
-                    lineY = rotationCenter().y() - rect().height()/rect().width() * dx;
-                    bText = lineY <= textY;
-                    break;
-                case TopLeft:
-                    dx = rect().width() - textXRight;
-                    lineY = rotationCenter().y() + rect().height()/rect().width() * dx;
-                    bText = lineY >= textY + textHeight;
-                    break;
-                case TopRight:
-                    dx = textXRight - textWidth - rect().left();
-                    lineY = rotationCenter().y() + rect().height()/rect().width() * dx;
-                    bText = lineY >= textY + textHeight;
-                    break;
-                case BottomRight:
-                    dx = textXRight - textWidth - rect().left();
-                    lineY = rotationCenter().y() - rect().height()/rect().width() * dx;
-                    bText = lineY <= textY;
-                    break;
+                int textY = (ky > 0) ? rotationCenter().y() - SEPARATOR - UBGeometryUtils::centimeterGraduationHeight - textHeight
+                    : rotationCenter().y() + SEPARATOR + UBGeometryUtils::centimeterGraduationHeight;
+                painter->drawText(QRectF(graduationX - textWidth / 2, textY, textWidth, textHeight),Qt::AlignVCenter, text);
             }
-
-            if (bText)
-                painter->drawText(
-                    QRectF(graduationX - textWidth / 2,
-                    textY, textWidth, textHeight),
-                    Qt::AlignVCenter, text);
-
         }
     }
     painter->restore();