UBGraphicsTriangle.cpp 28 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
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 47 48
    mHFlipSvgItem->setVisible(false);
    mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));


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

57
    updateResizeCursor();
58 59
}

60 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

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


86
UBGraphicsTriangle::~UBGraphicsTriangle()
87 88 89 90 91
{
}

UBItem* UBGraphicsTriangle::deepCopy(void) const
{
92
    UBGraphicsTriangle* copy = new UBGraphicsTriangle();
93 94

    copy->setPos(this->pos());
95
    copy->setPolygon(this->polygon());
96 97
//    copy->setZValue(this->zValue());
    UBGraphicsItem::assignZValue(copy, this->zValue());
98 99 100 101 102 103 104 105 106 107
    copy->setTransform(this->transform());

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

    return copy;

}

void UBGraphicsTriangle::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation)
{
108 109 110
    QPolygonF polygon;
    polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h);
    setPolygon(polygon);
111

112
    setOrientation(orientation);
unknown's avatar
unknown committed
113
}
114

unknown's avatar
unknown committed
115 116
void UBGraphicsTriangle::setOrientation(UBGraphicsTriangleOrientation orientation)
{
117 118 119 120 121 122
    mOrientation = orientation;
    calculatePoints(boundingRect());

    QPolygonF polygon;
    polygon << A1 << B1 << C1;
    setPolygon(polygon);
123 124 125 126
}

UBGraphicsScene* UBGraphicsTriangle::scene() const
{
127
    return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene());
128 129
}

130
void UBGraphicsTriangle::calculatePoints(const QRectF& r)
131
{
132 133
    switch(mOrientation)
    {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

    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;
155
    }
156

157 158 159
    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();
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    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;
    }
184 185
    W1 = rect().height() * d / C;
    H1 = rect().width() * d / C;
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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;
    }
}
203

204 205
void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
206

207
    painter->setPen(Qt::NoPen);
208

209
    QPolygonF polygon;
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    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());
238

239 240 241
    polygon << A1 << B1 << C1;
    painter->drawPolygon(polygon);
    polygon.clear();
242

243 244
    polygon << A2 << B2 << C2;
    painter->drawPolygon(polygon);
245

246
    paintGraduations(painter);
unknown's avatar
unknown committed
247

248
    mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
unknown's avatar
unknown committed
249 250 251 252
    QTransform antiScaleTransform;
    antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio);

    mCloseSvgItem->setTransform(antiScaleTransform);
253 254 255
    mHFlipSvgItem->setTransform(antiScaleTransform);
    mVFlipSvgItem->setTransform(antiScaleTransform);
    mRotateSvgItem->setTransform(antiScaleTransform);
unknown's avatar
unknown committed
256 257

    mCloseSvgItem->setPos(closeButtonRect().topLeft());
258 259 260 261 262 263 264 265 266 267 268 269
    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());
    }
270 271
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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;
}

288 289 290 291 292 293 294 295
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;

296 297 298
    qreal kx = (mOrientation == TopLeft || mOrientation == BottomLeft) ? 1 : -1;
    qreal ky = (mOrientation == BottomLeft || mOrientation == BottomRight) ? 1 : -1;

299 300 301 302 303
    painter->save();
    painter->setFont(font());
    QFontMetricsF fontMetrics(painter->font());
    for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
    {
304
        int graduationX = rotationCenter().x() + kx * sPixelsPerMillimeter * millimeters;
305 306 307 308 309
        int graduationHeight = (0 == millimeters % millimetersPerCentimeter) ?
            centimeterGraduationHeight :
            ((0 == millimeters % millimetersPerHalfCentimeter) ?
                halfCentimeterGraduationHeight : millimeterGraduationHeight);

310
        // Check that grad. line inside triangle
311 312 313 314
        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)
        {
315 316
            if (lineY >= rotationCenter().y() - ky * graduationHeight)
                break;
317 318 319
        }
        else
        {
320 321
            if (lineY <= rotationCenter().y() - ky * graduationHeight)
                break;
322
        }
323
        
324
        painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight));
325 326 327
        if (0 == millimeters % millimetersPerCentimeter)
        {
            QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter));
328 329
            int textXRight = graduationX + fontMetrics.width(text) / 2;
            qreal textWidth = fontMetrics.width(text);
330 331
            qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;

332
            int textY = (ky > 0) ? rotationCenter().y() - 5 - centimeterGraduationHeight - textHeight
333
                : rotationCenter().y() + 5 + centimeterGraduationHeight;
334

335 336
            bool bText = false;
            switch(mOrientation)
337
            {
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                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)
361 362
                painter->drawText(
                    QRectF(graduationX - textWidth / 2, 
363
                    textY, textWidth, textHeight),
364
                    Qt::AlignVCenter, text);
365

366 367 368 369 370 371
        }
    }
    painter->restore();
}


unknown's avatar
unknown committed
372
void UBGraphicsTriangle::rotateAroundCenter(qreal angle)
373
{
374
    qreal oldAngle = this->angle;
unknown's avatar
unknown committed
375
    this->angle = angle;
unknown's avatar
unknown committed
376
    QTransform transform;
377
    rotateAroundCenter(transform, rotationCenter());
unknown's avatar
unknown committed
378
    setTransform(transform, true);
379
    this->angle = oldAngle + angle; // We have to store absolute value for FLIP case
unknown's avatar
unknown committed
380 381
}

382
void UBGraphicsTriangle::rotateAroundCenter(QTransform& transform, QPointF center)
unknown's avatar
unknown committed
383
{
384
    transform.translate(center.x(), center.y());
unknown's avatar
unknown committed
385
    transform.rotate(angle);
386
    transform.translate(- center.x(), - center.y());
387 388
}

unknown's avatar
unknown committed
389

390
QPointF    UBGraphicsTriangle::rotationCenter() const
391
{
392 393 394 395 396 397 398 399 400 401
    switch(mOrientation)
    {
        case BottomLeft:
        case TopLeft:
            return B1 + QPointF(sLeftEdgeMargin, 0);
        case TopRight:
        case BottomRight:
            return B1 - QPointF(sLeftEdgeMargin, 0);
    }
    return QPointF(0, 0);
402 403
}

404
QRectF    UBGraphicsTriangle::closeButtonRect() const
405
{
406 407 408 409
    switch(mOrientation)
    {
        case BottomLeft:
            return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5, 
410 411 412
              B2.y() - mCloseSvgItem->boundingRect().height() - 5, 
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
413 414
        case TopLeft:
            return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5, 
415 416 417
              B2.y() + 5, 
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
418 419
        case TopRight:
            return QRectF(B2.x() + 5, 
420 421 422
              B2.y() + 5, 
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
423 424
        case BottomRight:
            return QRectF(B2.x() + 5, 
425 426 427
              B2.y() - mCloseSvgItem->boundingRect().height() - 5, 
              mCloseSvgItem->boundingRect().width(),
              mCloseSvgItem->boundingRect().height());
428 429
    }
    return QRectF(0,0,0,0);
unknown's avatar
unknown committed
430 431 432 433
}

QPolygonF UBGraphicsTriangle::resize1Polygon() const
{
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    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);
456 457 458
    QPolygonF p;
    p << C1 << P1 << P2;    
    return p;
unknown's avatar
unknown committed
459 460 461 462
}

QPolygonF UBGraphicsTriangle::resize2Polygon() const
{
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    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);
486 487 488
    QPolygonF p;
    p << A1 << P1 << P2;    
    return p;
unknown's avatar
unknown committed
489 490
}

491
QRectF    UBGraphicsTriangle::hFlipRect() const
unknown's avatar
unknown committed
492
{
493 494 495 496 497
    qreal dy = mVFlipSvgItem->boundingRect().height() + mCloseSvgItem->boundingRect().height() + 10;
    switch(mOrientation)
    {
        case BottomLeft:
            return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5, 
498
              B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy, 
499
              mHFlipSvgItem->boundingRect().width(),
500
              mHFlipSvgItem->boundingRect().height());
501 502
        case TopLeft:
            return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5, 
503 504 505
              B2.y() + 5 + dy, 
              mHFlipSvgItem->boundingRect().width(),
              mHFlipSvgItem->boundingRect().height());
506 507
        case TopRight:
            return QRectF(B2.x() + 5, 
508 509 510
              B2.y() + 5 + dy, 
              mHFlipSvgItem->boundingRect().width(),
              mHFlipSvgItem->boundingRect().height());
511 512
        case BottomRight:
            return QRectF(B2.x() + 5, 
513 514 515
              B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy, 
              mHFlipSvgItem->boundingRect().width(),
              mHFlipSvgItem->boundingRect().height());
516 517
    }
    return QRectF(0,0,0,0);
unknown's avatar
unknown committed
518 519
}

520
QRectF    UBGraphicsTriangle::vFlipRect() const
unknown's avatar
unknown committed
521
{
522 523 524 525 526
    qreal dy = mCloseSvgItem->boundingRect().height() + 5;
    switch(mOrientation)
    {
        case BottomLeft:
            return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5, 
527
              B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy, 
528
              mVFlipSvgItem->boundingRect().width(),
529
              mVFlipSvgItem->boundingRect().height());
530 531
        case TopLeft:
            return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5, 
532 533 534
              B2.y() + 5 + dy, 
              mVFlipSvgItem->boundingRect().width(),
              mVFlipSvgItem->boundingRect().height());
535 536
        case TopRight:
            return QRectF(B2.x() + 5, 
537 538 539
              B2.y() + 5 + dy, 
              mVFlipSvgItem->boundingRect().width(),
              mVFlipSvgItem->boundingRect().height());
540 541
        case BottomRight:
            return QRectF(B2.x() + 5, 
542 543 544
              B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy, 
              mVFlipSvgItem->boundingRect().width(),
              mVFlipSvgItem->boundingRect().height());
545 546
    }
    return QRectF(0,0,0,0);
unknown's avatar
unknown committed
547 548
}

549
QRectF    UBGraphicsTriangle::rotateRect() const
unknown's avatar
unknown committed
550
{
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    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;
    }
567
    return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size()));
568
}
unknown's avatar
unknown committed
569

570
QCursor    UBGraphicsTriangle::resizeCursor1() const
571 572
{
    return mResizeCursor1;
unknown's avatar
unknown committed
573 574
}

575
QCursor    UBGraphicsTriangle::resizeCursor2() const
unknown's avatar
unknown committed
576
{
577
    return mResizeCursor2;
unknown's avatar
unknown committed
578 579
}

580
QCursor    UBGraphicsTriangle::flipCursor() const
unknown's avatar
unknown committed
581
{
582
    return Qt::ArrowCursor;
unknown's avatar
unknown committed
583 584 585 586 587
}


void UBGraphicsTriangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
588 589 590 591
    lastRect = rect().toRect();
    lastPos = event->screenPos();

    if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
unknown's avatar
unknown committed
592 593 594 595
    {
        mResizing1 = true;
        event->accept();
    }
596 597 598 599 600 601 602 603
    else 
    if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
    {
        mResizing2 = true;
        event->accept();
    }
    else 
    if(rotateRect().contains(event->pos()))
unknown's avatar
unknown committed
604
    {
605
        mRotating = true;
unknown's avatar
unknown committed
606 607 608 609
        event->accept();
    }
    else
    {
610
        QGraphicsItem::mousePressEvent(event);
unknown's avatar
unknown committed
611
    }
612 613 614 615 616 617
    mShowButtons = false;
    mCloseSvgItem->setVisible(false);
    mHFlipSvgItem->setVisible(false);
    mVFlipSvgItem->setVisible(false);
    mRotateSvgItem->setVisible(mRotating);
    update();
unknown's avatar
unknown committed
618 619 620 621
}

void UBGraphicsTriangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
622 623 624 625 626
 
    QPoint currPos = event->screenPos();
//     qDebug() << QString(" X: %1 ").arg(currPos.x());
//     qDebug() << QString(" Y: %1 ").arg(currPos.y());

unknown's avatar
unknown committed
627 628
    if (!mResizing1 && !mResizing2 && !mRotating)
    {
629
        QGraphicsItem::mouseMoveEvent(event);
unknown's avatar
unknown committed
630 631 632
    }
    else
    {
633 634 635 636 637

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

        if (mResizing1)
        {
638 639
            if (mOrientation == TopLeft || mOrientation == BottomLeft)
            {
640 641 642 643 644 645
                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);
646 647 648
            }
            else
            {
649 650 651 652 653 654
                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);
655
            }
656 657 658 659 660 661
        }

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

        if (mResizing2)
        {
662 663
            if (mOrientation == BottomRight || mOrientation == BottomLeft)
            {
664 665 666 667 668 669
                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);
670 671 672
            }
            else
            {
673 674 675 676 677 678
                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);
679
            }
680 681 682 683 684

        }

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

unknown's avatar
unknown committed
685 686 687 688 689 690 691
        if (mRotating)
        {
            QLineF currentLine(rotationCenter(), event->pos());
            QLineF lastLine(rotationCenter(), event->lastPos());
            rotateAroundCenter(currentLine.angleTo(lastLine));
        }

692 693
        //-----------------------------------------------//

unknown's avatar
unknown committed
694 695 696 697 698 699
        event->accept();
    }
}

void UBGraphicsTriangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
700
    if (mResizing1 || mResizing2 || mRotating)
unknown's avatar
unknown committed
701
    {
702 703
        if (mRotating)
            updateResizeCursor();
unknown's avatar
unknown committed
704
        mResizing1 = false;
705 706
        mResizing2 = false;
        mRotating = false;
unknown's avatar
unknown committed
707 708 709 710
        event->accept();
    }
    else if (closeButtonRect().contains(event->pos()))
    {
711 712
        hide();
        emit hidden();
unknown's avatar
unknown committed
713 714
        event->accept();
    }
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
    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
751 752
    else
    {
753
        QGraphicsItem::mouseReleaseEvent(event);
unknown's avatar
unknown committed
754
    }
755 756
    mShowButtons = true;
    update();
unknown's avatar
unknown committed
757 758
    if (scene())
        scene()->setModified(true);
759 760
}

unknown's avatar
unknown committed
761
void UBGraphicsTriangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
762
{
763
    UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
unknown's avatar
unknown committed
764

765
    if (currentTool == UBStylusTool::Selector)  {
766
        mCloseSvgItem->setParentItem(this);
unknown's avatar
unknown committed
767

768 769 770 771 772
        mShowButtons = true;
        mCloseSvgItem->setVisible(true);
        mHFlipSvgItem->setVisible(true);
        mVFlipSvgItem->setVisible(true);
        mRotateSvgItem->setVisible(true);
unknown's avatar
unknown committed
773

774
        if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
775 776
            setCursor(resizeCursor1());
        else if(resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
777 778 779 780 781 782 783 784 785 786 787 788 789
            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();
790 791 792 793 794

    } else if (UBDrawingController::drawingController()->isDrawingTool())  {
            setCursor(drawRulerLineCursor());
            UBDrawingController::drawingController()->mActiveRuler = this;
            event->accept();
795
    }
unknown's avatar
unknown committed
796 797 798 799 800 801 802
}

void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    mShowButtons = false;
    setCursor(Qt::ArrowCursor);
    mCloseSvgItem->setVisible(false);
803 804
    mVFlipSvgItem->setVisible(false);
    mHFlipSvgItem->setVisible(false);
unknown's avatar
unknown committed
805
    mRotateSvgItem->setVisible(false);
806
    UBDrawingController::drawingController()->mActiveRuler = NULL;
unknown's avatar
unknown committed
807 808 809 810 811 812
    event->accept();
    update();
}

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

815 816 817 818 819 820
    if (currentTool == UBStylusTool::Selector)
    {
        mCloseSvgItem->setVisible(mShowButtons);
        mVFlipSvgItem->setVisible(mShowButtons);
        mHFlipSvgItem->setVisible(mShowButtons);
        mRotateSvgItem->setVisible(mShowButtons);
unknown's avatar
unknown committed
821

822
        if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
823 824
            setCursor(resizeCursor1());
        else if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
825 826 827 828
            setCursor(resizeCursor2());
        else if (closeButtonRect().contains(event->pos()))
            setCursor(closeCursor());
        else if (hFlipRect().contains(event->pos())
829 830
                 || vFlipRect().contains(event->pos()))
            setCursor(flipCursor());
831 832 833 834 835
        else if (rotateRect().contains(event->pos()))
            setCursor(rotateCursor());
        else
            setCursor(moveCursor());

836 837
        event->accept();
    }  else if (UBDrawingController::drawingController()->isDrawingTool())  {
838 839
        event->accept();
    }
840
}
841 842 843 844 845 846 847 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
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()
{
}