UBGraphicsAristo.cpp 30.6 KB
Newer Older
Yimgo's avatar
Yimgo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

16
#include "UBGraphicsAristo.h"
Yimgo's avatar
Yimgo committed
17 18
#include "board/UBBoardController.h"
#include "board/UBDrawingController.h"
19
#include "core/UBApplication.h"
Yimgo's avatar
Yimgo committed
20 21
#include "domain/UBGraphicsScene.h"

22 23 24 25 26 27 28 29 30
#include <QColor>
#include <QFont>
#include <QFontMetricsF>
#include <QGraphicsItem>
#include <QLineF>
#include <QPolygonF>
#include <QRadialGradient>
#include <QString>

Yimgo's avatar
Yimgo committed
31 32
#include "core/memcheck.h"

33
const QRectF UBGraphicsAristo::sDefaultRect =  QRectF(0, 0, 800, 500);
34
const UBGraphicsAristo::Orientation UBGraphicsAristo::sDefaultOrientation = UBGraphicsAristo::Top;
Yimgo's avatar
Yimgo committed
35 36 37

UBGraphicsAristo::UBGraphicsAristo()
    : UBAbstractDrawRuler()
38 39
    , QGraphicsPathItem()
    , mMarking(false)
Yimgo's avatar
Yimgo committed
40 41
    , mResizing(false)
    , mRotating(false)
42
    , mOrientation(Undefined)
Guillaume Burel's avatar
Guillaume Burel committed
43 44
    , mRotatedAngle(0)
    , mMarkerAngle(0)
45 46 47 48 49 50
    , mStartAngle(0)
    , mSpan(180)
    , mHFlipSvgItem(0)
    , mMarkerSvgItem(0)
    , mResizeSvgItem(0)
    , mRotateSvgItem(0)
Yimgo's avatar
Yimgo committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
    mHFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this);
    mHFlipSvgItem->setVisible(false);
    mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

    mResizeSvgItem = new QGraphicsSvgItem(":/images/resizeTool.svg", this);
    mResizeSvgItem->setVisible(false);
    mResizeSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

    mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this);
    mRotateSvgItem->setVisible(false);
    mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

    mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this);
    mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool));
66
    mMarkerSvgItem->setVisible(true);
Yimgo's avatar
Yimgo committed
67

68
    create(*this);
Guillaume Burel's avatar
Guillaume Burel committed
69 70 71 72

    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly
    setFlag(QGraphicsItem::ItemIsSelectable, false);

73
    setOrientation(sDefaultOrientation);
Yimgo's avatar
Yimgo committed
74 75
}

76
UBGraphicsAristo::~UBGraphicsAristo()
Yimgo's avatar
Yimgo committed
77
{
78
    /* NOOP */
Yimgo's avatar
Yimgo committed
79 80
}

81 82
/*
 * setOrientation() modify the tool orientation.
Guillaume Burel's avatar
Guillaume Burel committed
83
 * Apexes coordinates are alors recomputed.
84 85
 */
void UBGraphicsAristo::setOrientation(Orientation orientation)
Yimgo's avatar
Yimgo committed
86
{
Guillaume Burel's avatar
Guillaume Burel committed
87 88 89 90
    /* substracting difference to zero [2pi] twice, to obtain the desired angle */
    mMarkerAngle -= 2 * (mMarkerAngle - (int)(mMarkerAngle/360)*360) - 360;

    bool wasUndefined = mOrientation == Undefined;
Yimgo's avatar
Yimgo committed
91
    mOrientation = orientation;
Guillaume Burel's avatar
Guillaume Burel committed
92 93 94 95 96
    
    if (wasUndefined)
        calculatePoints(sDefaultRect);
    else
        calculatePoints(boundingRect());
Yimgo's avatar
Yimgo committed
97 98 99 100
}

/* calculatePoints() is used to calculate polygon's apexes coordinates.
 * This function handles orientation changes too.
Guillaume Burel's avatar
Guillaume Burel committed
101
 * Items are repositionated and path is redeterminates.
Yimgo's avatar
Yimgo committed
102
 */
Guillaume Burel's avatar
Guillaume Burel committed
103
void UBGraphicsAristo::calculatePoints(QRectF bounds)
Yimgo's avatar
Yimgo committed
104
{
105
    switch (mOrientation) {
Yimgo's avatar
Yimgo committed
106
    case Bottom:
Guillaume Burel's avatar
Guillaume Burel committed
107 108
        C.setX(bounds.center().x());
        C.setY(bounds.bottom());
109

Guillaume Burel's avatar
Guillaume Burel committed
110 111
        A.setX(bounds.left());
        A.setY(bounds.bottom() - bounds.width() / 2);
112

Guillaume Burel's avatar
Guillaume Burel committed
113 114
        B.setX(bounds.right());
        B.setY(bounds.bottom() - bounds.width() / 2);
Yimgo's avatar
Yimgo committed
115 116
        break;
    case Top:
Guillaume Burel's avatar
Guillaume Burel committed
117 118
        C.setX(bounds.center().x());
        C.setY(bounds.top());
119

Guillaume Burel's avatar
Guillaume Burel committed
120 121
        A.setX(bounds.left());
        A.setY(bounds.top() + bounds.width() / 2);
122

Guillaume Burel's avatar
Guillaume Burel committed
123 124
        B.setX(bounds.right());
        B.setY(bounds.top() + bounds.width() / 2);
125 126
        break;
    default:
Yimgo's avatar
Yimgo committed
127 128
        break;
    }
Guillaume Burel's avatar
Guillaume Burel committed
129 130 131

    setItemsPos();
    setPath(determinePath());
Yimgo's avatar
Yimgo committed
132 133
}

134 135 136 137 138 139
/*
 * setItemsPos() places control items according to A, B and C positions.
 * Call this function after A, B or C position modification, mostly after calling calculatePoints().
 * These positions has to be set when calling setPath() to allow hover events on items which are not into the main polygon.
 */
void UBGraphicsAristo::setItemsPos()
Yimgo's avatar
Yimgo committed
140
{
141 142 143 144 145 146
    mCloseSvgItem->setPos(closeButtonRect().topLeft() + rotationCenter());
    mHFlipSvgItem->setPos(hFlipRect().topLeft() + rotationCenter());
    mRotateSvgItem->setPos(rotateRect().topLeft() + rotationCenter());
    mResizeSvgItem->setPos(resizeButtonRect().topLeft() + rotationCenter()); 
    mMarkerSvgItem->setPos(markerButtonRect().topLeft() + rotationCenter());
}
Yimgo's avatar
Yimgo committed
147

148 149 150 151 152 153 154 155
/*
 * determinePath() modify the shape according to apexes coordinates and control item positions.
 * This is useful when orientation is modified.
 * Returns the painter path corresponding to object parameters.
 */
QPainterPath UBGraphicsAristo::determinePath()
{
    QPainterPath path;
Yimgo's avatar
Yimgo committed
156

157
    QPolygonF polygon;
Yimgo's avatar
Yimgo committed
158 159
    polygon << A << B << C;

Guillaume Burel's avatar
Guillaume Burel committed
160 161 162 163 164 165 166 167
    qreal rotationAngle = mOrientation == Bottom ? mMarkerAngle : mMarkerAngle - 360 * (int)(mMarkerAngle / 360);
    QTransform t;
    t.rotate(rotationAngle);
    bool markerIsInPolygon = true;
    if ((mOrientation == Bottom && mMarkerAngle < 90) || (mOrientation == Top && mMarkerAngle < 270))
        markerIsInPolygon = polygon.containsPoint(t.map(markerButtonRect().topLeft()) + rotationCenter(), Qt::OddEvenFill);
    else
        markerIsInPolygon = polygon.containsPoint(t.map(markerButtonRect().bottomLeft()) + rotationCenter(), Qt::OddEvenFill);
Yimgo's avatar
Yimgo committed
168

Guillaume Burel's avatar
Guillaume Burel committed
169
    path.moveTo(A);
Yimgo's avatar
Yimgo committed
170

Guillaume Burel's avatar
Guillaume Burel committed
171 172 173 174 175 176 177 178 179 180 181 182 183
    QRectF mappedRect = t.mapRect(markerButtonRect());
    path.lineTo(QPointF(mappedRect.translated(rotationCenter()).left(), A.y()));
    if (!markerIsInPolygon) {
        if (mOrientation == Top) {
            path.lineTo(QPointF(mappedRect.translated(rotationCenter()).left(), mappedRect.translated(rotationCenter()).bottom()));
            path.lineTo(QPointF(mappedRect.translated(rotationCenter()).right(), mappedRect.translated(rotationCenter()).bottom()));
        }
        else if (mOrientation == Bottom) {
            path.lineTo(QPointF(mappedRect.translated(rotationCenter()).left(), mappedRect.translated(rotationCenter()).top()));
            path.lineTo(QPointF(mappedRect.translated(rotationCenter()).right(), mappedRect.translated(rotationCenter()).top()));
        }
    }
    path.lineTo(QPointF(mappedRect.translated(rotationCenter()).right(), A.y()));
Yimgo's avatar
Yimgo committed
184

Guillaume Burel's avatar
Guillaume Burel committed
185 186 187 188 189 190 191 192 193 194
    path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).left(), A.y()));
    if (mOrientation == Top) {
        path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).left(), resizeButtonRect().translated(rotationCenter()).bottom()));
        path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).right(), resizeButtonRect().translated(rotationCenter()).bottom()));
    }
    else if (mOrientation == Bottom) {
        path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).left(), resizeButtonRect().translated(rotationCenter()).top()));
        path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).right(), resizeButtonRect().translated(rotationCenter()).top()));
    }
    path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).right(), A.y()));
Yimgo's avatar
Yimgo committed
195

Guillaume Burel's avatar
Guillaume Burel committed
196 197 198
    path.lineTo(B);
    path.lineTo(C);
    path.lineTo(A);
Yimgo's avatar
Yimgo committed
199

Guillaume Burel's avatar
Guillaume Burel committed
200 201
    return path;
}
Yimgo's avatar
Yimgo committed
202

203 204 205 206 207
UBItem* UBGraphicsAristo::deepCopy(void) const
{
    UBGraphicsAristo* copy = new UBGraphicsAristo();
    copyItemParameters(copy);
    return copy;
Yimgo's avatar
Yimgo committed
208 209
}

210
void UBGraphicsAristo::copyItemParameters(UBItem *copy) const
Yimgo's avatar
Yimgo committed
211
{
212 213 214 215 216 217
    UBGraphicsAristo* cp = dynamic_cast<UBGraphicsAristo*>(copy);
    if (cp)
    {   
        /* TODO: copy all members */
        cp->setPos(this->pos());
        cp->setTransform(this->transform());
Guillaume Burel's avatar
Guillaume Burel committed
218 219 220
        cp->setOrientation(mOrientation);
        cp->mRotatedAngle = mRotatedAngle;
        cp->mMarkerAngle = mMarkerAngle;
Yimgo's avatar
Yimgo committed
221
    }
222
}
Yimgo's avatar
Yimgo committed
223 224


225 226 227
void UBGraphicsAristo::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QPolygonF polygon;
Yimgo's avatar
Yimgo committed
228

229 230
    painter->setBrush(fillBrush());
    painter->setPen(drawColor());
Yimgo's avatar
Yimgo committed
231

232 233 234
    polygon << A << B << C;
    painter->drawPolygon(polygon);
    polygon.clear();
Yimgo's avatar
Yimgo committed
235

236 237 238
    paintGraduations(painter);
    paintMarker(painter);    
}
Yimgo's avatar
Yimgo committed
239

240 241 242 243 244 245 246 247 248 249
QBrush UBGraphicsAristo::fillBrush() const
{
    QColor fillColor = edgeFillColor();// scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor;
    QColor fillColorCenter = middleFillColor();//scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter;
    QColor transparentWhite = Qt::white;
    transparentWhite.setAlpha(scene()->isDarkBackground() ? sDrawTransparency : sFillTransparency);
    QRadialGradient radialGradient(boundingRect().center(), radius(), boundingRect().center());
    radialGradient.setColorAt(0, fillColorCenter);
    radialGradient.setColorAt(1, fillColor);
    return radialGradient;
Yimgo's avatar
Yimgo committed
250 251 252 253 254 255 256 257 258 259 260 261
}

/* paintGraduations() paints graduations on the ruler side (length graduations) and the two other sides (angle graduation) */
void UBGraphicsAristo::paintGraduations(QPainter *painter)
{
    paintRulerGraduations(painter);
    paintProtractorGraduations(painter);
}

void UBGraphicsAristo::paintRulerGraduations(QPainter *painter)
{
    /* defining useful constants */
262
    const int centimeterGraduationHeight = 15;
Yimgo's avatar
Yimgo committed
263
    const int halfCentimeterGraduationHeight = 10;
264 265 266
    const int millimeterGraduationHeight = 5;
    const int millimetersPerCentimeter = 10;
    const int millimetersPerHalfCentimeter = 5;
Yimgo's avatar
Yimgo committed
267 268 269 270 271 272

    painter->save();
    painter->setFont(font());
    QFontMetricsF fontMetrics(painter->font());

    /* Browsing milliters in half width of ruler side */
273
    for (int millimeters = 0; millimeters < (boundingRect().width() / 2 - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
Yimgo's avatar
Yimgo committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    {
        /* defining graduationHeight ; values are different to draw bigger lines if millimiter considered is a centimeter or a half centimeter */
        int graduationHeight = (0 == millimeters % millimetersPerCentimeter) ?
            centimeterGraduationHeight :
            ((0 == millimeters % millimetersPerHalfCentimeter) ?
                halfCentimeterGraduationHeight : millimeterGraduationHeight);

        /* correcting graduationHeight: draw the line in the other direction in case ruler is top-oriented, to stay inside the tool and inside the rect */
        graduationHeight = mOrientation == Bottom ? graduationHeight : - graduationHeight;
        
        /* drawing graduation to the left and to the right of origin, which is the center of graduated side */
        painter->drawLine(QLine(rotationCenter().x() + sPixelsPerMillimeter * millimeters, rotationCenter().y(), rotationCenter().x() + sPixelsPerMillimeter * millimeters, rotationCenter().y() + graduationHeight));
        if (millimeters != 0)
            painter->drawLine(QLine(rotationCenter().x() - sPixelsPerMillimeter * millimeters, rotationCenter().y(), rotationCenter().x() - sPixelsPerMillimeter * millimeters, rotationCenter().y() + graduationHeight));

        /* drawing associated value if considered graduation is a centimeter */
        if (0 == millimeters % millimetersPerCentimeter)
        {
            /* defining graduation value */
            QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter));
            
            /* staying inside polygon */
296
            if (rotationCenter().x() + sPixelsPerMillimeter * millimeters + fontMetrics.width(text) / 2 < boundingRect().right())
Yimgo's avatar
Yimgo committed
297 298 299 300 301
            {
                qreal textWidth = fontMetrics.width(text);
                qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;
                
                /* text y-coordinate is different according to tool's orientation */
302
                qreal textY = mOrientation == Bottom ? A.y() + 5 + centimeterGraduationHeight : A.y() - 5 - centimeterGraduationHeight + graduationHeight;
Yimgo's avatar
Yimgo committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
                
                /* if text's rect is not out of polygon's bounds, drawing value below or above graduation */
                QPointF intersectionPoint;
                
                bool paint = false;

                if (mOrientation == Bottom && QLineF(QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters - textWidth / 2, rotationCenter().y()), QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters - textWidth / 2, textY + textHeight)).intersect(QLineF(A, C), &intersectionPoint) != QLineF::BoundedIntersection && QLineF(QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters + textWidth / 2, rotationCenter().y()), QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters + textWidth / 2, textY + textHeight)).intersect(QLineF(A, C), &intersectionPoint) != QLineF::BoundedIntersection) {
                    paint = true;
                }
                else if (mOrientation == Top && QLineF(QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters - textWidth / 2, rotationCenter().y()), QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters - textWidth / 2, textY - textHeight)).intersect(QLineF(A, C), &intersectionPoint) != QLineF::BoundedIntersection && QLineF(QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters + textWidth / 2, rotationCenter().y()), QPointF(rotationCenter().x() - sPixelsPerMillimeter * millimeters + textWidth / 2, textY - textHeight)).intersect(QLineF(A, C), &intersectionPoint) != QLineF::BoundedIntersection) {
                    paint = true;
                }

                if (paint) {
                    painter->drawText(
                        QRectF(rotationCenter().x() + sPixelsPerMillimeter * millimeters - textWidth / 2, textY, textWidth, textHeight),
                        Qt::AlignVCenter, text);
                    if (millimeters != 0)
                        painter->drawText(
                        QRectF(rotationCenter().x() - sPixelsPerMillimeter * millimeters - textWidth / 2, textY, textWidth, textHeight),
                        Qt::AlignVCenter, text);
                }
            }
        }
    }
    painter->restore();
}

void UBGraphicsAristo::paintProtractorGraduations(QPainter* painter)
{
    /* defining useful constants */
    const int  tenDegreeGraduationLength = 15;
    const int fiveDegreeGraduationLength = 10;
    const int  oneDegreeGraduationLength = 5;

    painter->save();

    QFont font1 = painter->font();
#ifdef Q_WS_MAC
    font1.setPointSizeF(font1.pointSizeF() - 3);
#endif
    QFontMetricsF fm1(font1);

    //Font for internal arc
    QFont font2 = painter->font();
    font2.setPointSizeF(font1.pointSizeF()/1.5);
    QFontMetricsF fm2(font2);

    /* defining virtual arc diameter */
    qreal rad = radius();

    QPointF center = rotationCenter();

    /* browsing angles */
    for (int angle = 1; angle < mSpan; angle++)
    {
        int graduationLength = (0 == angle % 10) ? tenDegreeGraduationLength : ((0 == angle % 5) ? fiveDegreeGraduationLength : oneDegreeGraduationLength);
        
        qreal co = cos(((qreal)angle + mStartAngle) * PI/180);
        qreal si = sin(((qreal)angle + mStartAngle) * PI/180);

        /* inverse sinus according to the orientation, to draw graduations on the polygon */
        si = mOrientation == Bottom ? -si : si;

        /* drawing the graduation around the virtual arc */
        if (angle >= sArcAngleMargin && angle <= mSpan - sArcAngleMargin)
            painter->drawLine(QLineF(QPointF(center.x()+ rad/2*co, center.y() - rad/2*si),
                                    QPointF(center.x()+ (rad/2 + graduationLength)*co,
                                            center.y() - (rad/2 + graduationLength)*si)));


        QPointF intersectionPoint;
        QLineF referenceLine;
        if (angle < 90)
            referenceLine.setP1(B);
        else
            referenceLine.setP1(A);
        referenceLine.setP2(C);

        /* if angle is 10-multiple, drawing it's value, rotated to be easily red */
        if (0 == angle % 10) {
            QString grad = QString("%1").arg((int)(angle));
            QString grad2 = QString("%1").arg((int)mSpan - angle);

            painter->setFont(font2);

            painter->save();
            painter->translate(center.x() + (rad/2 + graduationLength*1.5)*co, center.y() - (rad/2 + graduationLength*1.5)*si);
            int degrees = mOrientation == Bottom ? angle : -angle;
            painter->rotate(-90 + degrees);
            painter->drawText(- fm2.width(grad)/2, - fm2.height()/2, fm2.width(grad), fm2.height(), Qt::AlignCenter, grad);
            painter->restore();

            painter->setFont(font1);

            
            /* drawing the graduation near tool's side */
            if (QLineF(QPointF(center.x()+ rad/2*co, center.y() - rad/2*si),
                                     QPointF(center.x()+ (rad/2 + graduationLength)*co,
                                             center.y() - (rad/2 + graduationLength)*si)).intersect(referenceLine, &intersectionPoint) == QLineF::UnboundedIntersection)

                painter->drawLine(QLineF(QPointF(center.x() + (rad/2 + graduationLength*1.5 + fm2.width(grad)/2)*co,
                                                 center.y() - (rad/2 + graduationLength*1.5 + fm2.height()/2)*si),
                                        intersectionPoint));

        }

        /* drawing the graduation near tool's side */
        else
            if (QLineF(QPointF(center.x()+ rad/2*co, center.y() - rad/2*si),
                                     QPointF(center.x()+ (rad/2 + graduationLength)*co,
                                             center.y() - (rad/2 + graduationLength)*si)).intersect(referenceLine, &intersectionPoint) == QLineF::UnboundedIntersection)

                painter->drawLine(QLineF(QPointF(intersectionPoint.x() - (graduationLength*1.5)*co,
                                                 intersectionPoint.y() + (graduationLength*1.5)*si),
                                        intersectionPoint));
    }

    painter->restore();
}

424 425
/* paintMarker() adjust marker button according to the current angle, draw the line allowing user to set precisely the angle, and draw the current angle's value. */
void UBGraphicsAristo::paintMarker(QPainter *painter)
Yimgo's avatar
Yimgo committed
426
{
427 428 429
    /* adjusting marker button */
    mMarkerSvgItem->resetTransform();
    mMarkerSvgItem->translate(-markerButtonRect().left(), -markerButtonRect().top());
Guillaume Burel's avatar
Guillaume Burel committed
430
    mMarkerSvgItem->rotate(mMarkerAngle);
431 432 433
    mMarkerSvgItem->translate(markerButtonRect().left(), markerButtonRect().top());

    
Guillaume Burel's avatar
Guillaume Burel committed
434 435
    qreal co = cos((mMarkerAngle) * PI/180);
    qreal si = sin((mMarkerAngle) * PI/180);
436 437 438 439

    /* Setting point composing the line (from point C) which intersects the line we want to draw. */
    QPointF referencePoint;
    if (mOrientation == Bottom) {
Guillaume Burel's avatar
Guillaume Burel committed
440
        if ((int)mMarkerAngle % 360 < 90)
441 442 443 444 445
            referencePoint = B;
        else
            referencePoint = A;
    }
    else if (mOrientation == Top) {
Guillaume Burel's avatar
Guillaume Burel committed
446
        if ((int)mMarkerAngle % 360 < 270 && (int)mMarkerAngle % 360 > 0)
447 448 449 450 451 452 453 454 455 456 457 458
            referencePoint = A;
        else
            referencePoint = B;
    }
    
    /* getting intersection point to draw the wanted line */
    QLineF intersectedLine(rotationCenter(), QPointF(rotationCenter().x()+co, rotationCenter().y()+si)); 
    QPointF intersectionPoint;
    if (intersectedLine.intersect(QLineF(referencePoint, C), &intersectionPoint))
        painter->drawLine(QLineF(intersectionPoint, rotationCenter()));

    /* drawing angle value */
Guillaume Burel's avatar
Guillaume Burel committed
459
    qreal rightAngle = mOrientation == Bottom ? mMarkerAngle : 360 - mMarkerAngle;
460 461 462 463 464 465 466 467 468 469 470 471 472 473


    QString angleText = QString("%1°").arg(rightAngle, 0, 'f', 1);

    QFont font1 = painter->font();
#ifdef Q_WS_MAC
    font1.setPointSizeF(font1.pointSizeF() - 3);
#endif
    QFontMetricsF fm1(font1);

    if (mOrientation == Bottom)
        painter->drawText(rotationCenter().x() - fm1.width(angleText)/2 - radius()/8, rotationCenter().y() + radius()/8 - fm1.height()/2, fm1.width(angleText), fm1.height(), Qt::AlignCenter, angleText);
    else
        painter->drawText(rotationCenter().x() - fm1.width(angleText)/2 - radius()/8, rotationCenter().y() - radius()/8 - fm1.height()/2, fm1.width(angleText), fm1.height(), Qt::AlignCenter, angleText);
Yimgo's avatar
Yimgo committed
474
}
Yimgo's avatar
Yimgo committed
475

476

Yimgo's avatar
Yimgo committed
477 478
void UBGraphicsAristo::rotateAroundCenter(qreal angle)
{
Guillaume Burel's avatar
Guillaume Burel committed
479 480
    qreal oldAngle = mRotatedAngle;
    mRotatedAngle = angle;
Yimgo's avatar
Yimgo committed
481 482 483
    QTransform transform;
    rotateAroundCenter(transform, rotationCenter());
    setTransform(transform, true);
Guillaume Burel's avatar
Guillaume Burel committed
484
    mRotatedAngle = oldAngle + angle; // We have to store absolute value for FLIP case
Yimgo's avatar
Yimgo committed
485 486 487 488 489
}

void UBGraphicsAristo::rotateAroundCenter(QTransform& transform, QPointF center)
{
    transform.translate(center.x(), center.y());
Guillaume Burel's avatar
Guillaume Burel committed
490
    transform.rotate(mRotatedAngle);
Yimgo's avatar
Yimgo committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    transform.translate(- center.x(), - center.y());
}

void UBGraphicsAristo::resize(qreal factor)
{
    prepareGeometryChange();
    translate(rotationCenter().x(), rotationCenter().y());
    scale(factor, factor);
    translate(-rotationCenter().x(), -rotationCenter().y());
}


QPointF UBGraphicsAristo::rotationCenter() const
{
    return QPointF((A.x() + B.x()) / 2, (A.y() + B.y()) / 2);
}

QRectF UBGraphicsAristo::closeButtonRect() const
{
    qreal y = radius() / 4 + hFlipRect().height() + 3 + rotateRect().height() + 3;
    if (mOrientation == Top)
        y = -y;
    return QRectF(- mCloseSvgItem->boundingRect().width() / 2, y, mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().height());
}

QRectF UBGraphicsAristo::hFlipRect() const
{
    qreal y = radius() / 4;
    if (mOrientation == Top)
        y = -y;

     return QRectF(- mHFlipSvgItem->boundingRect().width() / 2, y, mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().height());
}

525 526
QRectF UBGraphicsAristo::markerButtonRect() const
{
Guillaume Burel's avatar
Guillaume Burel committed
527 528
    qreal y = - mMarkerSvgItem->boundingRect().height()/2;
    return QRectF (radius()/2 - mMarkerSvgItem->boundingRect().width(), y, mMarkerSvgItem->boundingRect().width(), mMarkerSvgItem->boundingRect().height());
529 530 531 532 533 534 535
}

QRectF  UBGraphicsAristo::resizeButtonRect() const
{
    return QRectF((B - rotationCenter()).x() - 100 - mResizeSvgItem->boundingRect().width()/2, - mResizeSvgItem->boundingRect().height()/2, mResizeSvgItem->boundingRect().width(), mResizeSvgItem->boundingRect().height());   
}

Yimgo's avatar
Yimgo committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549
QRectF UBGraphicsAristo::rotateRect() const
{
    qreal y = radius() / 4 + hFlipRect().height() + 3;
    if (mOrientation == Top)
        y = -y;
    return QRectF(- mRotateSvgItem->boundingRect().width() / 2, y, mRotateSvgItem->boundingRect().width(), mRotateSvgItem->boundingRect().height());

}

QCursor UBGraphicsAristo::flipCursor() const
{
    return Qt::ArrowCursor;
}

550
QCursor UBGraphicsAristo::markerCursor() const
Yimgo's avatar
Yimgo committed
551 552 553 554
{
    return Qt::ArrowCursor;
}

555
QCursor UBGraphicsAristo::resizeCursor() const
Yimgo's avatar
Yimgo committed
556 557 558 559
{
    return Qt::ArrowCursor;
}

560

Yimgo's avatar
Yimgo committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
void UBGraphicsAristo::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    switch (toolFromPos(event->pos())) {
    case Rotate:
        mRotating = true;
        event->accept();
        break;
    case Resize:
        mResizing = true;
        event->accept();
        break;
    case MoveMarker:
        mMarking = true;
        event->accept();
        break;
    default:
        QGraphicsItem::mousePressEvent(event);
        break;
    }

    mShowButtons = false;
    mHFlipSvgItem->setVisible(false);
    mCloseSvgItem->setVisible(false);
    mRotateSvgItem->setVisible(mRotating);
    mResizeSvgItem->setVisible(mResizing);
    update();
}

void UBGraphicsAristo::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

    if (!mResizing && !mRotating && !mMarking)
    {
        QGraphicsItem::mouseMoveEvent(event);
    }
    else
    {
        QLineF currentLine(rotationCenter(), event->pos());
        QLineF lastLine(rotationCenter(), event->lastPos());

        if (mRotating) {
            
            rotateAroundCenter(currentLine.angleTo(lastLine));
        }
        else if (mResizing) {
            QPointF delta = event->pos() - event->lastPos();
Guillaume Burel's avatar
Guillaume Burel committed
607
            calculatePoints(QRectF(boundingRect().topLeft(), QSizeF(boundingRect().width() + delta.x(), boundingRect().height() + delta.x())));
Yimgo's avatar
Yimgo committed
608 609 610 611
        }
        else if(mMarking) {
            qreal angle = currentLine.angleTo(lastLine);

Guillaume Burel's avatar
Guillaume Burel committed
612 613
            mMarkerAngle += angle;
            mMarkerAngle -= (int)(mMarkerAngle/360)*360;
Yimgo's avatar
Yimgo committed
614 615

            if (mOrientation == Bottom) {
Guillaume Burel's avatar
Guillaume Burel committed
616 617 618 619
                if (mMarkerAngle >= 270)
                    mMarkerAngle = 0;
                else if (mMarkerAngle > 180)
                    mMarkerAngle = 180;
Yimgo's avatar
Yimgo committed
620 621
            }
            else if (mOrientation == Top) {
Guillaume Burel's avatar
Guillaume Burel committed
622 623 624 625
                if (mMarkerAngle < 90)
                    mMarkerAngle = 360;
                else if (mMarkerAngle < 180)
                    mMarkerAngle = 180;
Yimgo's avatar
Yimgo committed
626 627 628 629 630 631 632 633 634 635 636 637
            }
            update();
        }

        event->accept();
    }
}

void UBGraphicsAristo::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (mResizing || mRotating || mMarking)
    {
Guillaume Burel's avatar
Guillaume Burel committed
638 639
        if (mMarking)
            setPath(determinePath());
Yimgo's avatar
Yimgo committed
640 641 642 643 644 645 646 647 648 649 650 651
        mResizing = false;
        mRotating = false;
        mMarking = false;
        event->accept();
    }
    else
    {
        switch (toolFromPos(event->pos())) {
        case Close :
            hide();
            emit hidden();
            break;
Guillaume Burel's avatar
Guillaume Burel committed
652
        case HorizontalFlip:            
Yimgo's avatar
Yimgo committed
653 654 655 656 657 658 659 660
            /* setting new orientation */
            switch(mOrientation) {
            case Bottom:
                setOrientation(Top);
                break;
            case Top:
                setOrientation(Bottom);
                break;
661 662
            default:
                break;
Yimgo's avatar
Yimgo committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            }
        default:
            QGraphicsItem::mouseReleaseEvent(event);
            break;
        }
    }

    mShowButtons = true;
    update();
    if (scene())
        scene()->setModified(true);
}

void UBGraphicsAristo::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();

    if (currentTool == UBStylusTool::Selector)  {
        mShowButtons = true;
        mHFlipSvgItem->setVisible(true);
        mRotateSvgItem->setVisible(true);
        mResizeSvgItem->setVisible(true);
        mCloseSvgItem->setVisible(true);

        switch (toolFromPos(event->pos())) {
        case HorizontalFlip:
            setCursor(flipCursor());
            break;
        case Rotate:
            setCursor(rotateCursor());
            break;
        case Resize:
            setCursor(resizeCursor());
            break;
        case MoveMarker:
            setCursor(markerCursor());
            break;
        case Close:
            setCursor(closeCursor());
            break;
        default:
            setCursor(moveCursor());
            break;
        }

        event->accept();
        update();

    } else if (UBDrawingController::drawingController()->isDrawingTool())  {
            setCursor(drawRulerLineCursor());
            UBDrawingController::drawingController()->mActiveRuler = this;
            event->accept();
    }
}

void UBGraphicsAristo::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    mShowButtons = false;
    setCursor(Qt::ArrowCursor);
    mHFlipSvgItem->setVisible(false);
    mRotateSvgItem->setVisible(false);
    mResizeSvgItem->setVisible(false);
    mCloseSvgItem->setVisible(false);
    UBDrawingController::drawingController()->mActiveRuler = NULL;
    event->accept();
    update();
}

void UBGraphicsAristo::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();

    if (currentTool == UBStylusTool::Selector)
    {
        mShowButtons = true;
        mHFlipSvgItem->setVisible(true);
        mRotateSvgItem->setVisible(true);
        mResizeSvgItem->setVisible(true);
        mCloseSvgItem->setVisible(true);

        switch (toolFromPos(event->pos())) {
        case HorizontalFlip:
            setCursor(flipCursor());
            break;
        case Rotate:
            setCursor(rotateCursor());
            break;
        case Resize:
            setCursor(resizeCursor());
            break;
        case MoveMarker:
            setCursor(markerCursor());
            break;
        case Close:
            setCursor(closeCursor());
            break;
        default:
            setCursor(moveCursor());
            break;
        }

        event->accept();
    }
    else if (UBDrawingController::drawingController()->isDrawingTool())
        event->accept();
}

/*
Yimgo's avatar
Yimgo committed
771
 * toolfromPos() returns the item type corresponding to the given position.
Yimgo's avatar
Yimgo committed
772 773 774 775 776 777 778
 * This method is used to reduce the amount of code in each event function and improve class' maintainability.
 * pos: event's position ; a rotation is done to counter elements rotation, like the marker button.
 */ 
UBGraphicsAristo::Tool UBGraphicsAristo::toolFromPos(QPointF pos)
{
    pos = pos - rotationCenter();

Guillaume Burel's avatar
Guillaume Burel committed
779
    qreal rotationAngle = mOrientation == Bottom ? - mMarkerAngle : Top ? 360 * (int)(mMarkerAngle / 360 + 1) - mMarkerAngle : 0;
Yimgo's avatar
Yimgo committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

    QTransform t;
    t.rotate(rotationAngle);
    QPointF p2 = t.map(pos);

    if (resizeButtonRect().contains(pos))
        return Resize;
    else if (closeButtonRect().contains(pos))
        return Close;
    else if (rotateRect().contains(pos))
        return Rotate;
    else if (markerButtonRect().contains(p2))
        return MoveMarker;
    else if (hFlipRect().contains(pos))
        return HorizontalFlip;
    else if (shape().contains(pos))
        return Move;
    else
        return None;
}

801

Yimgo's avatar
Yimgo committed
802 803 804 805 806 807 808 809
void UBGraphicsAristo::StartLine(const QPointF &scenePos, qreal width)
{
    QPointF itemPos = mapFromScene(scenePos);

    qreal y;

    y = rotationCenter().y();

810 811 812 813
    if (itemPos.x() < boundingRect().x() + sLeftEdgeMargin)
            itemPos.setX(boundingRect().x() + sLeftEdgeMargin);
    if (itemPos.x() > boundingRect().x() + boundingRect().width() - sLeftEdgeMargin)
            itemPos.setX(boundingRect().x() + boundingRect().width() - sLeftEdgeMargin);
Yimgo's avatar
Yimgo committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

    itemPos.setY(y);
    itemPos = mapToScene(itemPos);

    scene()->moveTo(itemPos);
    scene()->drawLineTo(itemPos, width, true);
}

void UBGraphicsAristo::DrawLine(const QPointF &scenePos, qreal width)
{
    QPointF itemPos = mapFromScene(scenePos);

    qreal y;

    y = rotationCenter().y();

830 831 832 833
    if (itemPos.x() < boundingRect().x() + sLeftEdgeMargin)
            itemPos.setX(boundingRect().x() + sLeftEdgeMargin);
    if (itemPos.x() > boundingRect().x() + boundingRect().width() - sLeftEdgeMargin)
            itemPos.setX(boundingRect().x() + boundingRect().width() - sLeftEdgeMargin);
Yimgo's avatar
Yimgo committed
834 835 836 837 838 839 840 841 842 843 844

    itemPos.setY(y);
    itemPos = mapToScene(itemPos);

    // We have to use "pointed" line for marker tool
    scene()->drawLineTo(itemPos, width,
            UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker);
}

void UBGraphicsAristo::EndLine()
{
845 846 847 848 849 850 851
    /* NOOP */
}


UBGraphicsScene* UBGraphicsAristo::scene() const
{
    return static_cast<UBGraphicsScene*>(QGraphicsPathItem::scene());
Yimgo's avatar
Yimgo committed
852
}