UBGraphicsTriangle.cpp 28.3 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3
/*
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10 11 12 13 14
 * (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/>.
 */
15

16 17 18
#include <QGraphicsPolygonItem>
#include <QPolygonF>

19
#include "tools/UBGraphicsTriangle.h"
20 21 22 23 24
#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "board/UBDrawingController.h"
#include "domain/UBGraphicsScene.h"

25 26
#include "core/memcheck.h"

27 28 29 30
const QRect UBGraphicsTriangle::sDefaultRect =  QRect(0, 0, 800, 400);
const UBGraphicsTriangle::UBGraphicsTriangleOrientation UBGraphicsTriangle::sDefaultOrientation = 
UBGraphicsTriangle::BottomLeft;

31
UBGraphicsTriangle::UBGraphicsTriangle()
Claudio Valerio's avatar
Claudio Valerio committed
32 33 34
    : UBAbstractDrawRuler()
    , QGraphicsPolygonItem()
    , angle(0)
35 36 37
    , mResizing1(false)
    , mResizing2(false)
    , mRotating(false)
unknown's avatar
unknown committed
38

39
{
40
    setRect(sDefaultRect, sDefaultOrientation);
41

42
    create(*this);
43

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

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

49
    mVFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this);
unknown's avatar
unknown committed
50 51 52
    mVFlipSvgItem->setVisible(false);
    mVFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

53
    mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this);
unknown's avatar
unknown committed
54 55 56
    mRotateSvgItem->setVisible(false);
    mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

shibakaneki's avatar
shibakaneki committed
57
    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
58
    setFlag(QGraphicsItem::ItemIsSelectable, false);
shibakaneki's avatar
shibakaneki committed
59

60
    updateResizeCursor();
61 62
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

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);
}


89
UBGraphicsTriangle::~UBGraphicsTriangle()
90 91 92 93 94
{
}

UBItem* UBGraphicsTriangle::deepCopy(void) const
{
95
    UBGraphicsTriangle* copy = new UBGraphicsTriangle();
96

97
    copyItemParameters(copy);
98 99 100 101 102 103 104

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

    return copy;

}

105 106 107 108 109 110 111 112 113 114 115
void UBGraphicsTriangle::copyItemParameters(UBItem *copy) const
{
    UBGraphicsTriangle* cp = dynamic_cast<UBGraphicsTriangle*>(copy);
    if (cp)
    {   
        cp->setPos(this->pos());
        cp->setPolygon(this->polygon());
        cp->setTransform(this->transform());
    }
}

116 117
void UBGraphicsTriangle::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation)
{
118 119 120
    QPolygonF polygon;
    polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h);
    setPolygon(polygon);
121

122
    setOrientation(orientation);
unknown's avatar
unknown committed
123
}
124

unknown's avatar
unknown committed
125 126
void UBGraphicsTriangle::setOrientation(UBGraphicsTriangleOrientation orientation)
{
127 128 129 130 131 132
    mOrientation = orientation;
    calculatePoints(boundingRect());

    QPolygonF polygon;
    polygon << A1 << B1 << C1;
    setPolygon(polygon);
133 134 135 136
}

UBGraphicsScene* UBGraphicsTriangle::scene() const
{
137
    return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene());
138 139
}

140
void UBGraphicsTriangle::calculatePoints(const QRectF& r)
141
{
142 143
    switch(mOrientation)
    {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    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;
165
    }
166

167 168 169
    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();
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    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;
    }
194 195
    W1 = rect().height() * d / C;
    H1 = rect().width() * d / C;
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    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;
    }
}
213

214 215
void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
216

217
    painter->setPen(Qt::NoPen);
218

219
    QPolygonF polygon;
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    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());
248

249 250 251
    polygon << A1 << B1 << C1;
    painter->drawPolygon(polygon);
    polygon.clear();
252

253 254
    polygon << A2 << B2 << C2;
    painter->drawPolygon(polygon);
255

256
    paintGraduations(painter);
unknown's avatar
unknown committed
257

258
    mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
unknown's avatar
unknown committed
259 260 261 262
    QTransform antiScaleTransform;
    antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio);

    mCloseSvgItem->setTransform(antiScaleTransform);
263 264 265
    mHFlipSvgItem->setTransform(antiScaleTransform);
    mVFlipSvgItem->setTransform(antiScaleTransform);
    mRotateSvgItem->setTransform(antiScaleTransform);
unknown's avatar
unknown committed
266 267

    mCloseSvgItem->setPos(closeButtonRect().topLeft());
268 269 270 271 272 273 274 275 276 277 278 279
    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());
    }
280 281
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
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;
}

298 299 300 301 302 303 304 305
void UBGraphicsTriangle::paintGraduations(QPainter *painter)
{
    const int     centimeterGraduationHeight = 15;
    const int halfCentimeterGraduationHeight = 10;
    const int     millimeterGraduationHeight = 5;
    const int       millimetersPerCentimeter = 10;
    const int   millimetersPerHalfCentimeter = 5;

306 307 308
    qreal kx = (mOrientation == TopLeft || mOrientation == BottomLeft) ? 1 : -1;
    qreal ky = (mOrientation == BottomLeft || mOrientation == BottomRight) ? 1 : -1;

309 310 311 312 313
    painter->save();
    painter->setFont(font());
    QFontMetricsF fontMetrics(painter->font());
    for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
    {
314
        int graduationX = rotationCenter().x() + kx * sPixelsPerMillimeter * millimeters;
315 316 317 318 319
        int graduationHeight = (0 == millimeters % millimetersPerCentimeter) ?
            centimeterGraduationHeight :
            ((0 == millimeters % millimetersPerHalfCentimeter) ?
                halfCentimeterGraduationHeight : millimeterGraduationHeight);

320
        // Check that grad. line inside triangle
321 322 323 324
        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)
        {
325 326
            if (lineY >= rotationCenter().y() - ky * graduationHeight)
                break;
327 328 329
        }
        else
        {
330 331
            if (lineY <= rotationCenter().y() - ky * graduationHeight)
                break;
332
        }
333
        
334
        painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight));
335 336 337
        if (0 == millimeters % millimetersPerCentimeter)
        {
            QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter));
338 339
            int textXRight = graduationX + fontMetrics.width(text) / 2;
            qreal textWidth = fontMetrics.width(text);
340 341
            qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;

342
            int textY = (ky > 0) ? rotationCenter().y() - 5 - centimeterGraduationHeight - textHeight
343
                : rotationCenter().y() + 5 + centimeterGraduationHeight;
344

345 346
            bool bText = false;
            switch(mOrientation)
347
            {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
                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)
371 372
                painter->drawText(
                    QRectF(graduationX - textWidth / 2, 
373
                    textY, textWidth, textHeight),
374
                    Qt::AlignVCenter, text);
375

376 377 378 379 380 381
        }
    }
    painter->restore();
}


unknown's avatar
unknown committed
382
void UBGraphicsTriangle::rotateAroundCenter(qreal angle)
383
{
384
    qreal oldAngle = this->angle;
unknown's avatar
unknown committed
385
    this->angle = angle;
unknown's avatar
unknown committed
386
    QTransform transform;
387
    rotateAroundCenter(transform, rotationCenter());
unknown's avatar
unknown committed
388
    setTransform(transform, true);
389
    this->angle = oldAngle + angle; // We have to store absolute value for FLIP case
unknown's avatar
unknown committed
390 391
}

392
void UBGraphicsTriangle::rotateAroundCenter(QTransform& transform, QPointF center)
unknown's avatar
unknown committed
393
{
394
    transform.translate(center.x(), center.y());
unknown's avatar
unknown committed
395
    transform.rotate(angle);
396
    transform.translate(- center.x(), - center.y());
397 398
}

unknown's avatar
unknown committed
399

400
QPointF    UBGraphicsTriangle::rotationCenter() const
401
{
402 403 404 405 406 407 408 409 410 411
    switch(mOrientation)
    {
        case BottomLeft:
        case TopLeft:
            return B1 + QPointF(sLeftEdgeMargin, 0);
        case TopRight:
        case BottomRight:
            return B1 - QPointF(sLeftEdgeMargin, 0);
    }
    return QPointF(0, 0);
412 413
}

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

QPolygonF UBGraphicsTriangle::resize1Polygon() const
{
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
    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);
466 467 468
    QPolygonF p;
    p << C1 << P1 << P2;    
    return p;
unknown's avatar
unknown committed
469 470 471 472
}

QPolygonF UBGraphicsTriangle::resize2Polygon() const
{
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    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);
    QPointF P2(A1.x() + x1 * sArrowLength * rect().width()/C, 
        A1.y() + y1 * sArrowLength * rect().height() / C);
496 497 498
    QPolygonF p;
    p << A1 << P1 << P2;    
    return p;
unknown's avatar
unknown committed
499 500
}

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

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

559
QRectF    UBGraphicsTriangle::rotateRect() const
unknown's avatar
unknown committed
560
{
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    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;
    }
577
    return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size()));
578
}
unknown's avatar
unknown committed
579

580
QCursor    UBGraphicsTriangle::resizeCursor1() const
581 582
{
    return mResizeCursor1;
unknown's avatar
unknown committed
583 584
}

585
QCursor    UBGraphicsTriangle::resizeCursor2() const
unknown's avatar
unknown committed
586
{
587
    return mResizeCursor2;
unknown's avatar
unknown committed
588 589
}

590
QCursor    UBGraphicsTriangle::flipCursor() const
unknown's avatar
unknown committed
591
{
592
    return Qt::ArrowCursor;
unknown's avatar
unknown committed
593 594 595 596 597
}


void UBGraphicsTriangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
598
    lastRect = rect().toRect();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
599
    lastPos = transform().inverted().map(event->screenPos());
600 601

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

void UBGraphicsTriangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
632
 
unknown's avatar
unknown committed
633 634
    if (!mResizing1 && !mResizing2 && !mRotating)
    {
635
        QGraphicsItem::mouseMoveEvent(event);
unknown's avatar
unknown committed
636 637 638
    }
    else
    {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
639
        QPoint currPos = transform().inverted().map(event->screenPos());
640 641 642

        if (mResizing1)
        {
643 644
            if (mOrientation == TopLeft || mOrientation == BottomLeft)
            {
645 646 647 648 649 650
                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);
651 652 653
            }
            else
            {
654 655 656 657 658 659
                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);
660
            }
661 662 663 664 665 666
        }

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

        if (mResizing2)
        {
667 668
            if (mOrientation == BottomRight || mOrientation == BottomLeft)
            {
669 670 671 672 673 674
                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);
675 676 677
            }
            else
            {
678 679 680 681 682 683
                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);
684
            }
685 686 687 688 689

        }

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

unknown's avatar
unknown committed
690 691 692 693 694 695 696
        if (mRotating)
        {
            QLineF currentLine(rotationCenter(), event->pos());
            QLineF lastLine(rotationCenter(), event->lastPos());
            rotateAroundCenter(currentLine.angleTo(lastLine));
        }

697 698
        //-----------------------------------------------//

unknown's avatar
unknown committed
699 700 701 702 703 704
        event->accept();
    }
}

void UBGraphicsTriangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
705
    if (mResizing1 || mResizing2 || mRotating)
unknown's avatar
unknown committed
706
    {
707 708
        if (mRotating)
            updateResizeCursor();
unknown's avatar
unknown committed
709
        mResizing1 = false;
710 711
        mResizing2 = false;
        mRotating = false;
unknown's avatar
unknown committed
712 713 714 715
        event->accept();
    }
    else if (closeButtonRect().contains(event->pos()))
    {
716 717
        hide();
        emit hidden();
unknown's avatar
unknown committed
718 719
        event->accept();
    }
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
    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
756 757
    else
    {
758
        QGraphicsItem::mouseReleaseEvent(event);
unknown's avatar
unknown committed
759
    }
760 761
    mShowButtons = true;
    update();
unknown's avatar
unknown committed
762 763
    if (scene())
        scene()->setModified(true);
764 765
}

unknown's avatar
unknown committed
766
void UBGraphicsTriangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
767
{
768
    UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
unknown's avatar
unknown committed
769

770 771 772
    if (currentTool == UBStylusTool::Selector ||
        currentTool == UBStylusTool::Play)
    {
773
        mCloseSvgItem->setParentItem(this);
unknown's avatar
unknown committed
774

775 776 777 778 779
        mShowButtons = true;
        mCloseSvgItem->setVisible(true);
        mHFlipSvgItem->setVisible(true);
        mVFlipSvgItem->setVisible(true);
        mRotateSvgItem->setVisible(true);
unknown's avatar
unknown committed
780

781
        if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
782 783
            setCursor(resizeCursor1());
        else if(resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
784 785 786 787 788 789 790 791 792 793 794 795 796
            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();
797 798 799 800 801

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

void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    mShowButtons = false;
    setCursor(Qt::ArrowCursor);
    mCloseSvgItem->setVisible(false);
810 811
    mVFlipSvgItem->setVisible(false);
    mHFlipSvgItem->setVisible(false);
unknown's avatar
unknown committed
812
    mRotateSvgItem->setVisible(false);
813
    UBDrawingController::drawingController()->mActiveRuler = NULL;
unknown's avatar
unknown committed
814 815 816 817 818 819
    event->accept();
    update();
}

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

822 823
    if (currentTool == UBStylusTool::Selector ||
        currentTool == UBStylusTool::Play)
824 825 826 827 828
    {
        mCloseSvgItem->setVisible(mShowButtons);
        mVFlipSvgItem->setVisible(mShowButtons);
        mHFlipSvgItem->setVisible(mShowButtons);
        mRotateSvgItem->setVisible(mShowButtons);
unknown's avatar
unknown committed
829

830
        if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
831 832
            setCursor(resizeCursor1());
        else if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
833 834 835 836
            setCursor(resizeCursor2());
        else if (closeButtonRect().contains(event->pos()))
            setCursor(closeCursor());
        else if (hFlipRect().contains(event->pos())
837 838
                 || vFlipRect().contains(event->pos()))
            setCursor(flipCursor());
839 840 841 842 843
        else if (rotateRect().contains(event->pos()))
            setCursor(rotateCursor());
        else
            setCursor(moveCursor());

844 845
        event->accept();
    }  else if (UBDrawingController::drawingController()->isDrawingTool())  {
846 847
        event->accept();
    }
848
}
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
void UBGraphicsTriangle::StartLine(const QPointF &scenePos, qreal width)
{
    QPointF itemPos = mapFromScene(scenePos);

    qreal y;

    if (mOrientation == 0 || mOrientation == 1) {
        y = rect().y() + rect().height() + width / 2;
    } else if (mOrientation == 2 || mOrientation == 3) {
        y = rect().y() - width / 2;
    }

    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);
    scene()->drawLineTo(itemPos, width, true);
}

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

    qreal y;

    if (mOrientation == 0 || mOrientation == 1) {
        y = rect().y() + rect().height() + width / 2;
    } else if (mOrientation == 2 || mOrientation == 3) {
        y = rect().y() - width / 2;
    }

    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
    scene()->drawLineTo(itemPos, width,
            UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker);
}

void UBGraphicsTriangle::EndLine()
{
}