UBGraphicsTriangle.cpp 28.6 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4 5
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
10 11
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
12 13 14 15
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
16
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
19
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
20
 *
Claudio Valerio's avatar
Claudio Valerio committed
21
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
22
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
23
 */
24

25

Claudio Valerio's avatar
Claudio Valerio committed
26

Claudio Valerio's avatar
Claudio Valerio committed
27

28 29 30
#include <QGraphicsPolygonItem>
#include <QPolygonF>

31
#include "tools/UBGraphicsTriangle.h"
32 33 34 35 36
#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "board/UBDrawingController.h"
#include "domain/UBGraphicsScene.h"

37 38
#include "core/memcheck.h"

39
const QRect UBGraphicsTriangle::sDefaultRect =  QRect(0, 0, 800, 400);
Claudio Valerio's avatar
Claudio Valerio committed
40
const UBGraphicsTriangle::UBGraphicsTriangleOrientation UBGraphicsTriangle::sDefaultOrientation =
41 42
UBGraphicsTriangle::BottomLeft;

43
UBGraphicsTriangle::UBGraphicsTriangle()
Claudio Valerio's avatar
Claudio Valerio committed
44 45 46
    : UBAbstractDrawRuler()
    , QGraphicsPolygonItem()
    , angle(0)
47 48 49
    , mResizing1(false)
    , mResizing2(false)
    , mRotating(false)
unknown's avatar
unknown committed
50

51
{
52
    setRect(sDefaultRect, sDefaultOrientation);
53

54
    create(*this);
55

56
    mHFlipSvgItem = new QGraphicsSvgItem(":/images/hflipTool.svg", this);
unknown's avatar
unknown committed
57 58
    mHFlipSvgItem->setVisible(false);

shibakaneki's avatar
shibakaneki committed
59
    mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
unknown's avatar
unknown committed
60

61
    mVFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this);
unknown's avatar
unknown committed
62 63 64
    mVFlipSvgItem->setVisible(false);
    mVFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

65
    mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this);
unknown's avatar
unknown committed
66 67 68
    mRotateSvgItem->setVisible(false);
    mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

shibakaneki's avatar
shibakaneki committed
69
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly
Ivan Ilyin's avatar
Ivan Ilyin committed
70
    setFlag(QGraphicsItem::ItemIsSelectable, false);
shibakaneki's avatar
shibakaneki committed
71

72
    updateResizeCursor();
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

void UBGraphicsTriangle::updateResizeCursor()
{
    QPixmap pix(":/images/cursors/resize.png");
    QTransform itemTransform = sceneTransform();
    QRectF itemRect = boundingRect();

    QPointF topLeft = itemTransform.map(itemRect.topLeft());
    QPointF topRight = itemTransform.map(itemRect.topRight());
    QPointF bottomLeft = itemTransform.map(itemRect.bottomLeft());

    QLineF topLine(topLeft, topRight);
    QLineF leftLine(topLeft, bottomLeft);

    qreal angle = topLine.angle();
    QTransform tr1;
    tr1.rotate(- angle);
    mResizeCursor1  = QCursor(pix.transformed(tr1, Qt::SmoothTransformation), pix.width() / 2,  pix.height() / 2);

    angle = leftLine.angle();
    QTransform tr2;
    tr2.rotate(- angle);
    mResizeCursor2  = QCursor(pix.transformed(tr2, Qt::SmoothTransformation), pix.width() / 2,  pix.height() / 2);
}


101
UBGraphicsTriangle::~UBGraphicsTriangle()
102 103 104 105 106
{
}

UBItem* UBGraphicsTriangle::deepCopy(void) const
{
107
    UBGraphicsTriangle* copy = new UBGraphicsTriangle();
108

109
    copyItemParameters(copy);
110 111 112 113 114 115 116

    // TODO UB 4.7 ... complete all members ?

    return copy;

}

117 118 119 120
void UBGraphicsTriangle::copyItemParameters(UBItem *copy) const
{
    UBGraphicsTriangle* cp = dynamic_cast<UBGraphicsTriangle*>(copy);
    if (cp)
Claudio Valerio's avatar
Claudio Valerio committed
121
    {
122 123 124 125 126 127
        cp->setPos(this->pos());
        cp->setPolygon(this->polygon());
        cp->setTransform(this->transform());
    }
}

128 129
void UBGraphicsTriangle::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation)
{
130 131 132
    QPolygonF polygon;
    polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h);
    setPolygon(polygon);
133

134
    setOrientation(orientation);
unknown's avatar
unknown committed
135
}
136

unknown's avatar
unknown committed
137 138
void UBGraphicsTriangle::setOrientation(UBGraphicsTriangleOrientation orientation)
{
139 140 141 142 143 144
    mOrientation = orientation;
    calculatePoints(boundingRect());

    QPolygonF polygon;
    polygon << A1 << B1 << C1;
    setPolygon(polygon);
145 146 147 148
}

UBGraphicsScene* UBGraphicsTriangle::scene() const
{
149
    return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene());
150 151
}

152
void UBGraphicsTriangle::calculatePoints(const QRectF& r)
153
{
154 155
    switch(mOrientation)
    {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    case BottomLeft:
        A1.setX(r.left()); A1.setY(r.top());
        B1.setX(r.left()); B1.setY(r.bottom());
        C1.setX(r.right()); C1.setY(r.bottom());
        break;
    case TopLeft:
        A1.setX(r.left()); A1.setY(r.bottom());
        B1.setX(r.left()); B1.setY(r.top());
        C1.setX(r.right()); C1.setY(r.top());
        break;
    case TopRight:
        A1.setX(r.right()); A1.setY(r.bottom());
        B1.setX(r.right()); B1.setY(r.top());
        C1.setX(r.left()); C1.setY(r.top());
        break;
    case BottomRight:
        A1.setX(r.right()); A1.setY(r.top());
        B1.setX(r.right()); B1.setY(r.bottom());
        C1.setX(r.left()); C1.setY(r.bottom());
        break;
177
    }
178

179 180 181
    C = sqrt(rect().width() * rect().width() + rect().height() * rect().height());
    qreal L = (C * d + rect().width() * d)/ rect().height();
    qreal K = (C * d + rect().height() * d)/ rect().width();
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    switch(mOrientation)
    {
        case BottomLeft:
            A2.setX(r.left() + d); A2.setY(r.top() + K);
            B2.setX(r.left() + d); B2.setY(r.bottom() - d);
            C2.setX(r.right() - L); C2.setY(r.bottom() - d);
            break;
        case TopLeft:
            A2.setX(r.left() + d); A2.setY(r.bottom() - K);
            B2.setX(r.left() + d); B2.setY(r.top() + d);
            C2.setX(r.right() - L); C2.setY(r.top() + d);
            break;
        case TopRight:
            A2.setX(r.right() - d); A2.setY(r.bottom() - K);
            B2.setX(r.right() - d); B2.setY(r.top() + d);
            C2.setX(r.left() + L); C2.setY(r.top() + d);
            break;
        case BottomRight:
            A2.setX(r.right() - d); A2.setY(r.top() + K);
            B2.setX(r.right() - d); B2.setY(r.bottom() - d);
            C2.setX(r.left() + L); C2.setY(r.bottom() - d);
            break;
    }
206 207
    W1 = rect().height() * d / C;
    H1 = rect().width() * d / C;
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    switch(mOrientation)
    {
        case BottomLeft:
            CC.setX(r.right() - L + W1); CC.setY(r.bottom() - d - H1);
            break;
        case TopLeft:
            CC.setX(r.right() - L + W1); CC.setY(r.top() + d + H1);
            break;
        case TopRight:
            CC.setX(r.left() + L - W1); CC.setY(r.top() + d + H1);
            break;
        case BottomRight:
            CC.setX(r.left() + L - W1); CC.setY(r.top() - d - H1);
            break;
    }
}
225

226 227
void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
228

229
    painter->setPen(Qt::NoPen);
230

231
    QPolygonF polygon;
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    QLinearGradient gradient1(QPointF(A1.x(), 0), QPointF(A2.x(), 0));
    gradient1.setColorAt(0, edgeFillColor());
    gradient1.setColorAt(1, middleFillColor());
    painter->setBrush(gradient1);
    polygon << A1 << A2 << B2 << B1;
    painter->drawPolygon(polygon);
    polygon.clear();

    QLinearGradient gradient2(QPointF(0, B1.y()), QPointF(0, B2.y()));
    gradient2.setColorAt(0, edgeFillColor());
    gradient2.setColorAt(1, middleFillColor());
    painter->setBrush(gradient2);
    polygon << B1 << B2 << C2 << C1;
    painter->drawPolygon(polygon);
    polygon.clear();

    QLinearGradient gradient3(CC, C2);
    gradient3.setColorAt(0, edgeFillColor());
    gradient3.setColorAt(1, middleFillColor());
    painter->setBrush(gradient3);
    polygon << C1 << C2 << A2 << A1;
    painter->drawPolygon(polygon);
    polygon.clear();


    painter->setBrush(Qt::NoBrush);
    painter->setPen(drawColor());
260

261 262 263
    polygon << A1 << B1 << C1;
    painter->drawPolygon(polygon);
    polygon.clear();
264

265 266
    polygon << A2 << B2 << C2;
    painter->drawPolygon(polygon);
267

268
    paintGraduations(painter);
unknown's avatar
unknown committed
269

270
    mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
unknown's avatar
unknown committed
271 272 273 274
    QTransform antiScaleTransform;
    antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio);

    mCloseSvgItem->setTransform(antiScaleTransform);
275 276 277
    mHFlipSvgItem->setTransform(antiScaleTransform);
    mVFlipSvgItem->setTransform(antiScaleTransform);
    mRotateSvgItem->setTransform(antiScaleTransform);
unknown's avatar
unknown committed
278 279

    mCloseSvgItem->setPos(closeButtonRect().topLeft());
280 281 282 283 284 285 286 287 288 289 290 291
    mHFlipSvgItem->setPos(hFlipRect().topLeft());
    mVFlipSvgItem->setPos(vFlipRect().topLeft());
    mRotateSvgItem->setPos(rotateRect().topLeft());

    if (mShowButtons || mResizing1 || mResizing2)
    {
        painter->setBrush(QColor(0, 0, 0));
        if (mShowButtons || mResizing1)
            painter->drawPolygon(resize1Polygon());
        if (mShowButtons || mResizing2)
            painter->drawPolygon(resize2Polygon());
    }
292 293
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
QPainterPath UBGraphicsTriangle::shape() const
{
    QPainterPath tShape;
    QPolygonF tPolygon;

    tPolygon << A1 << B1 << C1;
    tShape.addPolygon(tPolygon);
    tPolygon.clear();

    tPolygon << A2 << B2 << C2;
    tShape.addPolygon(tPolygon);
    tPolygon.clear();

    return tShape;
}

310 311
void UBGraphicsTriangle::paintGraduations(QPainter *painter)
{
312 313 314
    qreal kx = (mOrientation == TopLeft || mOrientation == BottomLeft) ? 1 : -1;
    qreal ky = (mOrientation == BottomLeft || mOrientation == BottomRight) ? 1 : -1;

315 316 317 318 319
    painter->save();
    painter->setFont(font());
    QFontMetricsF fontMetrics(painter->font());
    for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
    {
320
        int graduationX = rotationCenter().x() + kx * sPixelsPerMillimeter * millimeters;
321 322 323 324
        int graduationHeight = (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter) ?
            UBGeometryUtils::centimeterGraduationHeight :
            ((0 == millimeters % UBGeometryUtils::millimetersPerHalfCentimeter) ?
                UBGeometryUtils::halfCentimeterGraduationHeight : UBGeometryUtils::millimeterGraduationHeight);
325

326
        // Check that grad. line inside triangle
327 328 329 330
        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)
        {
331 332
            if (lineY >= rotationCenter().y() - ky * graduationHeight)
                break;
333 334 335
        }
        else
        {
336 337
            if (lineY <= rotationCenter().y() - ky * graduationHeight)
                break;
338
        }
Claudio Valerio's avatar
Claudio Valerio committed
339

340
        painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight));
341
        if (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter)
342
        {
343
            QString text = QString("%1").arg((int)(millimeters / UBGeometryUtils::millimetersPerCentimeter));
344 345
            int textXRight = graduationX + fontMetrics.width(text) / 2;
            qreal textWidth = fontMetrics.width(text);
346 347
            qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;

348 349
            int textY = (ky > 0) ? rotationCenter().y() - 5 - UBGeometryUtils::centimeterGraduationHeight - textHeight
                : rotationCenter().y() + 5 + UBGeometryUtils::centimeterGraduationHeight;
350

351 352
            bool bText = false;
            switch(mOrientation)
353
            {
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
                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;
            }

            if (bText)
377
                painter->drawText(
Claudio Valerio's avatar
Claudio Valerio committed
378
                    QRectF(graduationX - textWidth / 2,
379
                    textY, textWidth, textHeight),
380
                    Qt::AlignVCenter, text);
381

382 383 384 385 386 387
        }
    }
    painter->restore();
}


unknown's avatar
unknown committed
388
void UBGraphicsTriangle::rotateAroundCenter(qreal angle)
389
{
390
    qreal oldAngle = this->angle;
unknown's avatar
unknown committed
391
    this->angle = angle;
unknown's avatar
unknown committed
392
    QTransform transform;
393
    rotateAroundCenter(transform, rotationCenter());
unknown's avatar
unknown committed
394
    setTransform(transform, true);
395
    this->angle = oldAngle + angle; // We have to store absolute value for FLIP case
unknown's avatar
unknown committed
396 397
}

398
void UBGraphicsTriangle::rotateAroundCenter(QTransform& transform, QPointF center)
unknown's avatar
unknown committed
399
{
400
    transform.translate(center.x(), center.y());
unknown's avatar
unknown committed
401
    transform.rotate(angle);
402
    transform.translate(- center.x(), - center.y());
403 404
}

unknown's avatar
unknown committed
405

406
QPointF    UBGraphicsTriangle::rotationCenter() const
407
{
408 409 410 411 412 413 414 415 416 417
    switch(mOrientation)
    {
        case BottomLeft:
        case TopLeft:
            return B1 + QPointF(sLeftEdgeMargin, 0);
        case TopRight:
        case BottomRight:
            return B1 - QPointF(sLeftEdgeMargin, 0);
    }
    return QPointF(0, 0);
418 419
}

420
QRectF    UBGraphicsTriangle::closeButtonRect() const
421
{
422 423 424
    switch(mOrientation)
    {
        case BottomLeft:
Claudio Valerio's avatar
Claudio Valerio committed
425 426
            return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5,
              B2.y() - mCloseSvgItem->boundingRect().height() - 5,
427 428
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
429
        case TopLeft:
Claudio Valerio's avatar
Claudio Valerio committed
430 431
            return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5,
              B2.y() + 5,
432 433
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
434
        case TopRight:
Claudio Valerio's avatar
Claudio Valerio committed
435 436
            return QRectF(B2.x() + 5,
              B2.y() + 5,
437 438
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
439
        case BottomRight:
Claudio Valerio's avatar
Claudio Valerio committed
440 441
            return QRectF(B2.x() + 5,
              B2.y() - mCloseSvgItem->boundingRect().height() - 5,
442 443
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
444 445
    }
    return QRectF(0,0,0,0);
unknown's avatar
unknown committed
446 447 448 449
}

QPolygonF UBGraphicsTriangle::resize1Polygon() const
{
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    qreal x1, y1;
    switch(mOrientation)
    {
        case BottomLeft:
            x1 = -1;
            y1 = -1;
            break;
        case TopLeft:
            x1 = -1;
            y1 = 1;
            break;
        case TopRight:
            x1 = 1;
            y1 = 1;
            break;
        case BottomRight:
            x1 = 1;
            y1 = -1;
            break;
    }
    QPointF P1(C1.x() + x1 * sArrowLength, C1.y());
    QPointF P2(C1.x() + x1 * sArrowLength * rect().width()/C, C1.y() + y1 * sArrowLength * rect().height() / C);
472
    QPolygonF p;
Claudio Valerio's avatar
Claudio Valerio committed
473
    p << C1 << P1 << P2;
474
    return p;
unknown's avatar
unknown committed
475 476 477 478
}

QPolygonF UBGraphicsTriangle::resize2Polygon() const
{
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    qreal x1, y1;
    switch(mOrientation)
    {
        case BottomLeft:
            x1 = 1;
            y1 = 1;
            break;
        case TopLeft:
            x1 = 1;
            y1 = -1;
            break;
        case TopRight:
            x1 = -1;
            y1 = -1;
            break;
        case BottomRight:
            x1 = -1;
            y1 = 1;
            break;
    }
    QPointF P1(A1.x(), A1.y() + y1 * sArrowLength);
Claudio Valerio's avatar
Claudio Valerio committed
500
    QPointF P2(A1.x() + x1 * sArrowLength * rect().width()/C,
501
        A1.y() + y1 * sArrowLength * rect().height() / C);
502
    QPolygonF p;
Claudio Valerio's avatar
Claudio Valerio committed
503
    p << A1 << P1 << P2;
504
    return p;
unknown's avatar
unknown committed
505 506
}

507
QRectF    UBGraphicsTriangle::hFlipRect() const
unknown's avatar
unknown committed
508
{
509 510 511 512
    qreal dy = mVFlipSvgItem->boundingRect().height() + mCloseSvgItem->boundingRect().height() + 10;
    switch(mOrientation)
    {
        case BottomLeft:
Claudio Valerio's avatar
Claudio Valerio committed
513 514
            return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5,
              B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy,
515
              mHFlipSvgItem->boundingRect().width(),
516
              mHFlipSvgItem->boundingRect().height());
517
        case TopLeft:
Claudio Valerio's avatar
Claudio Valerio committed
518 519
            return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5,
              B2.y() + 5 + dy,
520 521
              mHFlipSvgItem->boundingRect().width(),
              mHFlipSvgItem->boundingRect().height());
522
        case TopRight:
Claudio Valerio's avatar
Claudio Valerio committed
523 524
            return QRectF(B2.x() + 5,
              B2.y() + 5 + dy,
525 526
              mHFlipSvgItem->boundingRect().width(),
              mHFlipSvgItem->boundingRect().height());
527
        case BottomRight:
Claudio Valerio's avatar
Claudio Valerio committed
528 529
            return QRectF(B2.x() + 5,
              B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy,
530 531
              mHFlipSvgItem->boundingRect().width(),
              mHFlipSvgItem->boundingRect().height());
532 533
    }
    return QRectF(0,0,0,0);
unknown's avatar
unknown committed
534 535
}

536
QRectF    UBGraphicsTriangle::vFlipRect() const
unknown's avatar
unknown committed
537
{
538 539 540 541
    qreal dy = mCloseSvgItem->boundingRect().height() + 5;
    switch(mOrientation)
    {
        case BottomLeft:
Claudio Valerio's avatar
Claudio Valerio committed
542 543
            return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5,
              B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy,
544
              mVFlipSvgItem->boundingRect().width(),
545
              mVFlipSvgItem->boundingRect().height());
546
        case TopLeft:
Claudio Valerio's avatar
Claudio Valerio committed
547 548
            return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5,
              B2.y() + 5 + dy,
549 550
              mVFlipSvgItem->boundingRect().width(),
              mVFlipSvgItem->boundingRect().height());
551
        case TopRight:
Claudio Valerio's avatar
Claudio Valerio committed
552 553
            return QRectF(B2.x() + 5,
              B2.y() + 5 + dy,
554 555
              mVFlipSvgItem->boundingRect().width(),
              mVFlipSvgItem->boundingRect().height());
556
        case BottomRight:
Claudio Valerio's avatar
Claudio Valerio committed
557 558
            return QRectF(B2.x() + 5,
              B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy,
559 560
              mVFlipSvgItem->boundingRect().width(),
              mVFlipSvgItem->boundingRect().height());
561 562
    }
    return QRectF(0,0,0,0);
unknown's avatar
unknown committed
563 564
}

565
QRectF    UBGraphicsTriangle::rotateRect() const
unknown's avatar
unknown committed
566
{
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    QPointF p(C2);
    switch(mOrientation)
    {
        case BottomLeft:
            p += QPointF(20, 5);
            break;
        case TopLeft:
            p += QPointF(20, -5 - mRotateSvgItem->boundingRect().height());
            break;
        case TopRight:
            p += QPointF(-20 - mRotateSvgItem->boundingRect().width(), -5 - mRotateSvgItem->boundingRect().height());
            break;
        case BottomRight:
            p += QPointF(-20 - mRotateSvgItem->boundingRect().width(), 5);
            break;
    }
583
    return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size()));
584
}
unknown's avatar
unknown committed
585

586
QCursor    UBGraphicsTriangle::resizeCursor1() const
587 588
{
    return mResizeCursor1;
unknown's avatar
unknown committed
589 590
}

591
QCursor    UBGraphicsTriangle::resizeCursor2() const
unknown's avatar
unknown committed
592
{
593
    return mResizeCursor2;
unknown's avatar
unknown committed
594 595
}

596
QCursor    UBGraphicsTriangle::flipCursor() const
unknown's avatar
unknown committed
597
{
598
    return Qt::ArrowCursor;
unknown's avatar
unknown committed
599 600 601 602 603
}


void UBGraphicsTriangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
604
    lastRect = rect().toRect();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
605
    lastPos = sceneTransform().inverted().map(event->screenPos());
606 607

    if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
unknown's avatar
unknown committed
608 609 610 611
    {
        mResizing1 = true;
        event->accept();
    }
Claudio Valerio's avatar
Claudio Valerio committed
612
    else
613 614 615 616 617
    if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
    {
        mResizing2 = true;
        event->accept();
    }
Claudio Valerio's avatar
Claudio Valerio committed
618
    else
619
    if(rotateRect().contains(event->pos()))
unknown's avatar
unknown committed
620
    {
621
        mRotating = true;
unknown's avatar
unknown committed
622 623 624 625
        event->accept();
    }
    else
    {
626
        QGraphicsItem::mousePressEvent(event);
unknown's avatar
unknown committed
627
    }
628 629 630 631 632 633
    mShowButtons = false;
    mCloseSvgItem->setVisible(false);
    mHFlipSvgItem->setVisible(false);
    mVFlipSvgItem->setVisible(false);
    mRotateSvgItem->setVisible(mRotating);
    update();
unknown's avatar
unknown committed
634 635 636 637
}

void UBGraphicsTriangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
638

unknown's avatar
unknown committed
639 640
    if (!mResizing1 && !mResizing2 && !mRotating)
    {
641
        QGraphicsItem::mouseMoveEvent(event);
unknown's avatar
unknown committed
642 643 644
    }
    else
    {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
645
        QPoint currPos = sceneTransform().inverted().map(event->screenPos());
646 647 648

        if (mResizing1)
        {
649 650
            if (mOrientation == TopLeft || mOrientation == BottomLeft)
            {
651 652 653 654 655 656
                int deltaX = currPos.x() - lastPos.x();
                if (lastRect.width() + deltaX < sMinWidth)
                    deltaX = sMinWidth - lastRect.width();

                setRect(QRectF(lastRect.left(), lastRect.top(),
                    lastRect.width() + deltaX, lastRect.height()), mOrientation);
657 658 659
            }
            else
            {
660 661 662 663 664 665
                int deltaX = lastPos.x() - currPos.x();
                if (lastRect.width() + deltaX < sMinWidth)
                    deltaX = sMinWidth - lastRect.width();

                setRect(QRectF(lastRect.left() - deltaX, lastRect.top(),
                    lastRect.width() + deltaX, lastRect.height()), mOrientation);
666
            }
667 668 669 670 671 672
        }

        //-----------------------------------------------//

        if (mResizing2)
        {
673 674
            if (mOrientation == BottomRight || mOrientation == BottomLeft)
            {
675 676 677 678 679 680
                int deltaY = lastPos.y() - currPos.y();
                if (lastRect.height() + deltaY < sMinHeight)
                    deltaY = sMinHeight - lastRect.height();

                setRect(QRectF(lastRect.left(), lastRect.top() - deltaY,
                        lastRect.width(), lastRect.height() + deltaY), mOrientation);
681 682 683
            }
            else
            {
684 685 686 687 688 689
                int deltaY = currPos.y() - lastPos.y();
                if (lastRect.height() + deltaY < sMinHeight)
                    deltaY = sMinHeight - lastRect.height();

                setRect(QRectF(lastRect.left(), lastRect.top(),
                        lastRect.width(), lastRect.height() + deltaY), mOrientation);
690
            }
691 692 693 694 695

        }

        //-----------------------------------------------//

unknown's avatar
unknown committed
696 697 698 699 700 701 702
        if (mRotating)
        {
            QLineF currentLine(rotationCenter(), event->pos());
            QLineF lastLine(rotationCenter(), event->lastPos());
            rotateAroundCenter(currentLine.angleTo(lastLine));
        }

703 704
        //-----------------------------------------------//

unknown's avatar
unknown committed
705 706 707 708 709 710
        event->accept();
    }
}

void UBGraphicsTriangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
711
    if (mResizing1 || mResizing2 || mRotating)
unknown's avatar
unknown committed
712
    {
713 714
        if (mRotating)
            updateResizeCursor();
unknown's avatar
unknown committed
715
        mResizing1 = false;
716 717
        mResizing2 = false;
        mRotating = false;
unknown's avatar
unknown committed
718 719 720 721
        event->accept();
    }
    else if (closeButtonRect().contains(event->pos()))
    {
722
        hide();
unknown's avatar
unknown committed
723 724
        event->accept();
    }
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
    else if (hFlipRect().contains(event->pos()))
    {
        switch(mOrientation)
        {
        case BottomLeft:
            setOrientation(BottomRight);
            break;
        case BottomRight:
            setOrientation(BottomLeft);
            break;
        case TopLeft:
            setOrientation(TopRight);
            break;
        case TopRight:
            setOrientation(TopLeft);
            break;
        }
    }
    else if (vFlipRect().contains(event->pos()))
    {
        switch(mOrientation)
        {
        case BottomLeft:
            setOrientation(TopLeft);
            break;
        case BottomRight:
            setOrientation(TopRight);
            break;
        case TopLeft:
            setOrientation(BottomLeft);
            break;
        case TopRight:
            setOrientation(BottomRight);
            break;
        }
    }
unknown's avatar
unknown committed
761 762
    else
    {
763
        QGraphicsItem::mouseReleaseEvent(event);
unknown's avatar
unknown committed
764
    }
765 766
    mShowButtons = true;
    update();
unknown's avatar
unknown committed
767 768
    if (scene())
        scene()->setModified(true);
769 770
}

unknown's avatar
unknown committed
771
void UBGraphicsTriangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
772
{
773
    UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
unknown's avatar
unknown committed
774

775 776 777
    if (currentTool == UBStylusTool::Selector ||
        currentTool == UBStylusTool::Play)
    {
778
        mCloseSvgItem->setParentItem(this);
unknown's avatar
unknown committed
779

780 781 782 783 784
        mShowButtons = true;
        mCloseSvgItem->setVisible(true);
        mHFlipSvgItem->setVisible(true);
        mVFlipSvgItem->setVisible(true);
        mRotateSvgItem->setVisible(true);
unknown's avatar
unknown committed
785

786
        if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
787 788
            setCursor(resizeCursor1());
        else if(resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
789 790 791 792 793 794 795 796 797 798 799 800 801
            setCursor(resizeCursor2());
        else if (closeButtonRect().contains(event->pos()))
            setCursor(closeCursor());
        else if (hFlipRect().contains(event->pos())
            || vFlipRect().contains(event->pos()))
                setCursor(flipCursor());
        else if (rotateRect().contains(event->pos()))
            setCursor(rotateCursor());
        else
            setCursor(moveCursor());

        event->accept();
        update();
802 803 804 805 806

    } else if (UBDrawingController::drawingController()->isDrawingTool())  {
            setCursor(drawRulerLineCursor());
            UBDrawingController::drawingController()->mActiveRuler = this;
            event->accept();
807
    }
unknown's avatar
unknown committed
808 809 810 811
}

void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
812
    mResizing1 = mResizing2 = mRotating = false;
unknown's avatar
unknown committed
813 814 815
    mShowButtons = false;
    setCursor(Qt::ArrowCursor);
    mCloseSvgItem->setVisible(false);
816 817
    mVFlipSvgItem->setVisible(false);
    mHFlipSvgItem->setVisible(false);
unknown's avatar
unknown committed
818
    mRotateSvgItem->setVisible(false);
819
    UBDrawingController::drawingController()->mActiveRuler = NULL;
unknown's avatar
unknown committed
820 821 822 823 824 825
    event->accept();
    update();
}

void UBGraphicsTriangle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
826
    UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
unknown's avatar
unknown committed
827

828 829
    if (currentTool == UBStylusTool::Selector ||
        currentTool == UBStylusTool::Play)
830 831 832 833 834
    {
        mCloseSvgItem->setVisible(mShowButtons);
        mVFlipSvgItem->setVisible(mShowButtons);
        mHFlipSvgItem->setVisible(mShowButtons);
        mRotateSvgItem->setVisible(mShowButtons);
unknown's avatar
unknown committed
835

836
        if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
837 838
            setCursor(resizeCursor1());
        else if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
839 840 841 842
            setCursor(resizeCursor2());
        else if (closeButtonRect().contains(event->pos()))
            setCursor(closeCursor());
        else if (hFlipRect().contains(event->pos())
843 844
                 || vFlipRect().contains(event->pos()))
            setCursor(flipCursor());
845 846 847 848 849
        else if (rotateRect().contains(event->pos()))
            setCursor(rotateCursor());
        else
            setCursor(moveCursor());

850 851
        event->accept();
    }  else if (UBDrawingController::drawingController()->isDrawingTool())  {
852 853
        event->accept();
    }
854
}
855 856 857
void UBGraphicsTriangle::StartLine(const QPointF &scenePos, qreal width)
{
    QPointF itemPos = mapFromScene(scenePos);
858
    mStrokeWidth = width;
859 860 861 862

    qreal y;

    if (mOrientation == 0 || mOrientation == 1) {
863
        y = rect().y() + rect().height() + mStrokeWidth / 2;
864
    } else if (mOrientation == 2 || mOrientation == 3) {
865
        y = rect().y() - mStrokeWidth / 2;
866 867 868 869 870 871 872 873 874 875 876
    }

    if (itemPos.x() < rect().x() + sLeftEdgeMargin)
            itemPos.setX(rect().x() + sLeftEdgeMargin);
    if (itemPos.x() > rect().x() + rect().width() - sLeftEdgeMargin)
            itemPos.setX(rect().x() + rect().width() - sLeftEdgeMargin);

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

    scene()->moveTo(itemPos);
877
    scene()->drawLineTo(itemPos, mStrokeWidth, true);
878 879 880 881
}

void UBGraphicsTriangle::DrawLine(const QPointF &scenePos, qreal width)
{
882
    Q_UNUSED(width);
883 884 885 886 887
    QPointF itemPos = mapFromScene(scenePos);

    qreal y;

    if (mOrientation == 0 || mOrientation == 1) {
888
        y = rect().y() + rect().height() + mStrokeWidth / 2;
889
    } else if (mOrientation == 2 || mOrientation == 3) {
890
        y = rect().y() - mStrokeWidth / 2;
891 892 893 894 895 896 897 898 899 900 901
    }

    if (itemPos.x() < rect().x() + sLeftEdgeMargin)
            itemPos.setX(rect().x() + sLeftEdgeMargin);
    if (itemPos.x() > rect().x() + rect().width() - sLeftEdgeMargin)
            itemPos.setX(rect().x() + rect().width() - sLeftEdgeMargin);

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

    // We have to use "pointed" line for marker tool
902
    scene()->drawLineTo(itemPos, mStrokeWidth,
903 904 905 906 907 908 909
            UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker);
}

void UBGraphicsTriangle::EndLine()
{
}