UBGraphicsProtractor.cpp 20.3 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3 4 5 6 7 8 9 10
 * 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.
Claudio Valerio's avatar
Claudio Valerio committed
11
 *
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
14 15 16 17 18 19 20
 */

#include "UBGraphicsProtractor.h"
#include "core/UBApplication.h"
#include "gui/UBResources.h"
#include "domain/UBGraphicsScene.h"
#include "board/UBBoardController.h"
21
#include "board/UBDrawingController.h"
Claudio Valerio's avatar
Claudio Valerio committed
22
#include "UBAbstractDrawRuler.h"
Claudio Valerio's avatar
Claudio Valerio committed
23

24 25
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
26

27
const QRectF UBGraphicsProtractor::sDefaultRect = QRectF(-175, -175, 350, 350);
Claudio Valerio's avatar
Claudio Valerio committed
28 29

UBGraphicsProtractor::UBGraphicsProtractor()
30 31 32 33 34 35 36 37 38 39
        : QGraphicsEllipseItem(sDefaultRect)
        , mCurrentTool(None)
        , mShowButtons(false)
        , mCurrentAngle(0)
        , mSpan(180)
        , mStartAngle(0)
        , mScaleFactor(1)
        , mResetSvgItem(0)
        , mResizeSvgItem(0)
        , mMarkerSvgItem(0)
Claudio Valerio's avatar
Claudio Valerio committed
40
{
Claudio Valerio's avatar
Claudio Valerio committed
41 42
    sFillTransparency = 127;
    sDrawTransparency = 192;
43
    create(*this);
44

45
    setCacheMode(QGraphicsItem::DeviceCoordinateCache);
Claudio Valerio's avatar
Claudio Valerio committed
46

47 48
    setStartAngle(0);
    setSpanAngle(180 * 16);
Claudio Valerio's avatar
Claudio Valerio committed
49 50 51 52 53 54 55 56 57 58 59 60

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

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

    mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this);
    mMarkerSvgItem->setVisible(false);
    mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool));
Claudio Valerio's avatar
Claudio Valerio committed
61

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

66
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly
Claudio Valerio's avatar
Claudio Valerio committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    scale(1.5, 1.5);
}


void UBGraphicsProtractor::paint(QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget)
{
    painter->save();

    Q_UNUSED(styleOption);
    Q_UNUSED(widget);

    painter->setFont(QFont("Arial"));
    painter->setPen(drawColor());
    painter->setBrush(fillBrush());
    painter->drawPie(QRectF(rect().center().x() - radius(), rect().center().y() - radius(),
83
                            2 * radius(), 2 * radius()), mStartAngle * 16, mSpan * 16);
Claudio Valerio's avatar
Claudio Valerio committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    paintGraduations(painter);
    paintButtons(painter);
    paintAngleMarker(painter);

    painter->restore();
}


QVariant UBGraphicsProtractor::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == QGraphicsItem::ItemSceneChange)
    {
        mCloseSvgItem->setParentItem(this);
        mResizeSvgItem->setParentItem(this);
        mResetSvgItem->setParentItem(this);
        mRotateSvgItem->setParentItem(this);
        mMarkerSvgItem->setParentItem(this);
    }

    return QGraphicsEllipseItem::itemChange(change, value);
}


QRectF UBGraphicsProtractor::boundingRect() const
{
    QPointF center = rect().center();
    qreal centerX = center.x();
    qreal centerY = center.y();

113 114 115
    QRectF bounds = resizeButtonRect().adjusted(centerX, centerY, centerX, centerY);
    bounds = bounds.united(closeButtonRect().adjusted(centerX, centerY, centerX, centerY));
    bounds = bounds.united(resetButtonRect().adjusted(centerX, centerY, centerX, centerY));
Claudio Valerio's avatar
Claudio Valerio committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    QTransform t;
    t.translate(centerX, centerY);
    t.rotate(-mStartAngle);
    t.translate(-centerX, -centerY);
    bounds = t.mapRect(bounds);

    bounds = bounds.united(QGraphicsEllipseItem::boundingRect());

    return bounds;
}


QPainterPath UBGraphicsProtractor::shape() const
{
    QPainterPath path = QGraphicsEllipseItem::shape();
    QPainterPath buttonPath;
133
    QRectF markerRect = markerButtonRect();
Claudio Valerio's avatar
Claudio Valerio committed
134 135 136 137 138

    QPointF center = rect().center();
    qreal centerX = center.x();
    qreal centerY = center.y();

139
    buttonPath.addRect(resizeButtonRect().adjusted(centerX, centerY, centerX, centerY));
140
    if (!resizeButtonRect().contains(markerRect))
Claudio Valerio's avatar
Claudio Valerio committed
141
    {
142
        buttonPath.addRect(markerRect.adjusted(centerX - markerRect.left() * 2 - markerRect.width(), centerY
143
                                               , centerX - markerRect.left() * 2 - markerRect.width(), centerY));
144
        buttonPath.addRect(markerRect.adjusted(centerX, centerY, centerX, centerY));
Claudio Valerio's avatar
Claudio Valerio committed
145
    }
146 147
    buttonPath.addRect(closeButtonRect().adjusted(centerX, centerY, centerX, centerY));
    buttonPath.addRect(resetButtonRect().adjusted(centerX, centerY, centerX, centerY));
Claudio Valerio's avatar
Claudio Valerio committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    QTransform t;
    t.translate(centerX, centerY);
    t.rotate(-mStartAngle);
    t.translate(-centerX, -centerY);
    buttonPath = t.map(buttonPath);
    buttonPath = buttonPath.subtracted(path);
    path.addPath(buttonPath);

    return path;
}


void UBGraphicsProtractor::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    mPreviousMousePos = event->pos();
    mCurrentTool = toolFromPos(event->pos());
    mShowButtons = mCurrentTool == Reset || mCurrentTool == Close;

    if (mCurrentTool == None || mCurrentTool == Move)
        QGraphicsEllipseItem::mousePressEvent(event);
    else
        event->accept();
}


void UBGraphicsProtractor::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF currentPoint = event->pos();
    QLineF startLine(rect().center(), mPreviousMousePos);
    QLineF currentLine(rect().center(), currentPoint);
    qreal angle = startLine.angleTo(currentLine);
    qreal scaleFactor = currentLine.length()/startLine.length();

181
    switch (mCurrentTool)
Claudio Valerio's avatar
Claudio Valerio committed
182
    {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    case Rotate :
        prepareGeometryChange();
        mStartAngle = mStartAngle + angle;
        setStartAngle(mStartAngle * 16);
        mPreviousMousePos = currentPoint;
        break;

    case Resize :
        prepareGeometryChange();
        translate(rect().center().x(), rect().center().y());
        scale(scaleFactor, scaleFactor);
        translate(-rect().center().x(), -rect().center().y());
        mScaleFactor *= scaleFactor;
        break;

    case MoveMarker :

        mCurrentAngle += angle;
        if ((int)mCurrentAngle % 360 > 270)
            mCurrentAngle = 0;
        else if ((int)mCurrentAngle % 360 >= 180)
            mCurrentAngle = 180;

        mPreviousMousePos = currentPoint;
        update();
        break;

    case Move :
        QGraphicsEllipseItem::mouseMoveEvent(event);
        break;

    default :
        break;
Claudio Valerio's avatar
Claudio Valerio committed
216 217
    }

218
    if (mCurrentTool != Move)
Claudio Valerio's avatar
Claudio Valerio committed
219 220 221 222 223 224 225
        event->accept();

}


void UBGraphicsProtractor::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
226
    switch (mCurrentTool)
Claudio Valerio's avatar
Claudio Valerio committed
227
    {
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    case Reset :
        setStartAngle(0);
        mStartAngle = 0;
        break;

    case Close :
        hide();
        emit hidden();
        break;

    case MoveMarker :
        update();
        break;

    default :
        QGraphicsEllipseItem::mouseReleaseEvent(event);
        break;
Claudio Valerio's avatar
Claudio Valerio committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }

    if (mCurrentTool != Move)
        event->accept();

    if (scene())
        scene()->setModified(true);

    mCurrentTool = None;
}


void UBGraphicsProtractor::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
259 260
    if (UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Selector)
        return;
261

Claudio Valerio's avatar
Claudio Valerio committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    if (!mShowButtons)
    {
        mShowButtons = true;

        mCloseSvgItem->setParentItem(this);
        mResizeSvgItem->setParentItem(this);
        mResetSvgItem->setParentItem(this);
        mRotateSvgItem->setParentItem(this);
        mMarkerSvgItem->setParentItem(this);

        update();
    }

    event->accept();
}


void UBGraphicsProtractor::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    mShowButtons = false;
282
    unsetCursor();
Claudio Valerio's avatar
Claudio Valerio committed
283 284 285 286 287 288 289
    update();
    event->accept();
}


void UBGraphicsProtractor::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
290 291
    if (UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Selector)
        return;
292

Claudio Valerio's avatar
Claudio Valerio committed
293 294 295 296 297 298 299 300
    Tool currentTool = toolFromPos(event->pos());

    if (!mShowButtons)
    {
        mShowButtons = true;
        update();
    }

301
    if (currentTool == Move)
Claudio Valerio's avatar
Claudio Valerio committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315
        setCursor(Qt::SizeAllCursor);
    else
        setCursor(Qt::ArrowCursor);

    event->accept();
}


qreal UBGraphicsProtractor::antiScale() const
{
    return 1 / (mScaleFactor * UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
}


316
QRectF UBGraphicsProtractor::resetButtonRect () const
Claudio Valerio's avatar
Claudio Valerio committed
317 318 319
{
    qreal antiSc = antiScale();

320 321 322
    if (buttonSizeReference().width() * antiSc <=  buttonSizeReference().width() * 15)
        return QRectF(-buttonSizeReference().width() * 7, -buttonSizeReference().height() * antiSc / 2,
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
Claudio Valerio's avatar
Claudio Valerio committed
323
    else
324 325
        return QRectF(-buttonSizeReference().width() * antiSc / 2, -buttonSizeReference().height() * antiSc / 2,
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
Claudio Valerio's avatar
Claudio Valerio committed
326 327 328
}


329
QRectF UBGraphicsProtractor::closeButtonRect () const
Claudio Valerio's avatar
Claudio Valerio committed
330 331 332 333 334
{
    qreal antiSc = antiScale();

    if (buttonSizeReference().width() * antiSc <=  buttonSizeReference().width() * 2)
        return QRectF(-buttonSizeReference().width() * 9, -buttonSizeReference().height() * antiSc / 2,
335
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
Claudio Valerio's avatar
Claudio Valerio committed
336 337
    else if (buttonSizeReference().width() * antiSc <=  buttonSizeReference().width() * 15)
        return QRectF(-buttonSizeReference().width() * 7 -buttonSizeReference().width() * antiSc, -buttonSizeReference().height() * antiSc / 2,
338
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
Claudio Valerio's avatar
Claudio Valerio committed
339 340
    else
        return QRectF(-buttonSizeReference().width() * antiSc / 2, -buttonSizeReference().height() * antiSc / 2,
341
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
Claudio Valerio's avatar
Claudio Valerio committed
342 343 344
}


345
QRectF UBGraphicsProtractor::resizeButtonRect () const
Claudio Valerio's avatar
Claudio Valerio committed
346 347 348
{
    qreal antiSc = antiScale();

349
    if (buttonSizeReference().width() * antiSc <=  buttonSizeReference().width() * 15)
Claudio Valerio's avatar
Claudio Valerio committed
350 351 352 353 354 355
        return QRectF(buttonSizeReference().width() * 8, -buttonSizeReference().height() * antiSc / 2,
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
    else
    {
        mResizeSvgItem->setZValue(zValue()+10);
        return QRectF(-buttonSizeReference().width() * antiSc / 2, -buttonSizeReference().height() * antiSc / 2,
356
                      buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc);
Claudio Valerio's avatar
Claudio Valerio committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370
    }
}


void UBGraphicsProtractor::paintGraduations(QPainter *painter)
{
    painter->save();

    const int  tenDegreeGraduationLength = 15;
    const int fiveDegreeGraduationLength = 10;
    const int  oneDegreeGraduationLength = 5;

    QFont font1 = painter->font();
#ifdef Q_WS_MAC
371
    font1.setPointSizeF(font1.pointSizeF() - 3);
Claudio Valerio's avatar
Claudio Valerio committed
372 373 374 375 376 377 378 379 380 381 382 383 384
#endif
    QFontMetricsF fm1(font1);

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

    qreal rad = radius();

    QPointF center = rect().center();
    painter->drawArc(QRectF(center.x() - rad/2, center.y() - rad/2, rad, rad), mStartAngle*16, mSpan*16);

385
    for (int angle = 1; angle < mSpan; angle++)
Claudio Valerio's avatar
Claudio Valerio committed
386 387 388 389
    {
        int graduationLength = (0 == angle % 10) ? tenDegreeGraduationLength : ((0 == angle % 5) ? fiveDegreeGraduationLength : oneDegreeGraduationLength);
        qreal co = cos(((qreal)angle + mStartAngle) * PI/180);
        qreal si = sin(((qreal)angle + mStartAngle) * PI/180);
390
        if (0 == angle % 90)
Claudio Valerio's avatar
Claudio Valerio committed
391 392 393 394
            painter->drawLine(QLineF(QPointF(center.x(), center.y()), QPointF(center.x() + co*tenDegreeGraduationLength, center.y() - si*tenDegreeGraduationLength)));

        //external arc
        painter->drawLine(QLineF(QPointF(center.x()+ rad*co, center.y() - rad*si),
395
                                 QPointF(center.x()+ (rad - graduationLength)*co, center.y() - (rad - graduationLength)*si)));
Claudio Valerio's avatar
Claudio Valerio committed
396 397
        //internal arc
        painter->drawLine(QLineF(QPointF(center.x()+ rad/2*co, center.y() - rad/2*si),
398 399
                                 QPointF(center.x()+ (rad/2 + graduationLength)*co,
                                         center.y() - (rad/2 + graduationLength)*si)));
Claudio Valerio's avatar
Claudio Valerio committed
400 401 402 403 404 405 406 407 408

        if (0 == angle % 10)
        {
            //external arc
            painter->setFont(font1);
            QString grad = QString("%1").arg((int)(angle));
            QString grad2 = QString("%1").arg((int)(mSpan - angle));

            painter->drawText(QRectF(center.x() + (rad - graduationLength*1.5)*co  - fm1.width(grad)/2,
409 410
                                     center.y() - (rad - graduationLength*1.5)*si - fm1.height()/2,
                                     fm1.width(grad), fm1.height()), Qt::AlignTop, grad);
Claudio Valerio's avatar
Claudio Valerio committed
411 412 413 414

            //internal arc
            painter->setFont(font2);
            painter->drawText(QRectF(center.x() + (rad/2 + graduationLength*1.5)*co  - fm2.width(grad2)/2,
415 416
                                     center.y() - (rad/2 + graduationLength*1.5)*si - fm2.height()/2,
                                     fm2.width(grad2), fm2.height()), Qt::AlignTop, grad2);
Claudio Valerio's avatar
Claudio Valerio committed
417 418 419 420 421 422 423 424 425 426 427 428 429
            painter->setFont(font1);

        }
    }

    painter->restore();
}


void UBGraphicsProtractor::paintButtons(QPainter *painter)
{
    Q_UNUSED(painter);

430
    if (mShowButtons)
Claudio Valerio's avatar
Claudio Valerio committed
431 432 433 434
    {
        qreal antiSc = antiScale();

        qreal scale = buttonSizeReference().width() / mCloseSvgItem->boundingRect().width();
435
        mCloseSvgItem->setPos(closeButtonRect().topLeft() + rect().center());
Claudio Valerio's avatar
Claudio Valerio committed
436
        mCloseSvgItem->resetTransform();
437
        mCloseSvgItem->translate(-closeButtonRect().left(),-closeButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
438
        mCloseSvgItem->rotate(-mStartAngle);
439
        mCloseSvgItem->translate(closeButtonRect().left(), closeButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
440 441
        mCloseSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item...

442
        mResetSvgItem->setPos(resetButtonRect().topLeft() + rect().center());
Claudio Valerio's avatar
Claudio Valerio committed
443
        mResetSvgItem->resetTransform();
444
        mResetSvgItem->translate(-resetButtonRect().left(), -resetButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
445
        mResetSvgItem->rotate(-mStartAngle);
446
        mResetSvgItem->translate(resetButtonRect().left(), resetButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
447 448
        mResetSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item...

449
        mResizeSvgItem->setPos(resizeButtonRect().topLeft() + rect().center());
Claudio Valerio's avatar
Claudio Valerio committed
450
        mResizeSvgItem->resetTransform();
451
        mResizeSvgItem->translate(-resizeButtonRect().left(), -resizeButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
452
        mResizeSvgItem->rotate(-mStartAngle);
453
        mResizeSvgItem->translate(resizeButtonRect().left(), resizeButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
454 455
        mResizeSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item...

456
        mRotateSvgItem->setPos(rotateButtonRect().topLeft() + rect().center());
Claudio Valerio's avatar
Claudio Valerio committed
457
        mRotateSvgItem->resetTransform();
458
        mRotateSvgItem->translate(-rotateButtonRect().left(), -rotateButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
459
        mRotateSvgItem->rotate(-mStartAngle);
460
        mRotateSvgItem->translate(rotateButtonRect().left(), rotateButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
461 462 463 464
        mRotateSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item...
    }

    qreal scale = markerSizeReference().width()/mMarkerSvgItem->boundingRect().width();
465
    mMarkerSvgItem->setPos(markerButtonRect().topLeft() + rect().center());
Claudio Valerio's avatar
Claudio Valerio committed
466
    mMarkerSvgItem->resetTransform();
467
    mMarkerSvgItem->translate(-markerButtonRect().left(), -markerButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
468
    mMarkerSvgItem->rotate(- mStartAngle - mCurrentAngle);
469
    mMarkerSvgItem->translate(markerButtonRect().left(), markerButtonRect().top());
Claudio Valerio's avatar
Claudio Valerio committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    mMarkerSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item...

    mCloseSvgItem->setVisible(mShowButtons);
    mResetSvgItem->setVisible(mShowButtons);
    mResizeSvgItem->setVisible(mShowButtons);
    mRotateSvgItem->setVisible(mShowButtons);
    mMarkerSvgItem->setVisible(true);

}


void UBGraphicsProtractor::paintAngleMarker(QPainter *painter)
{
    painter->save();

    painter->translate(rect().center());
    painter->rotate(-mStartAngle);
    painter->translate(-rect().center().x(), -rect().center().y());
    qreal co = cos(mCurrentAngle * PI/180);
    qreal si = sin(mCurrentAngle * PI/180);
    qreal rad = radius();

    painter->drawLine(QLineF(rect().center(), QPointF(rect().center().x()+ (rad+ 20)*co, rect().center().y() - (rad + 20)*si)));
    QPointF center = rect().center();
    painter->drawArc(QRectF(center.x() - rad/8, center.y() - rad/8, rad / 4, rad / 4), 0
495
                     , (mCurrentAngle - (int)(mCurrentAngle/360)*360)*16);
Claudio Valerio's avatar
Claudio Valerio committed
496 497 498 499
    painter->translate(rect().center());
    painter->rotate(-mCurrentAngle);
    painter->translate(-rect().center().x(), -rect().center().y());

500
    //Paint Angle text (horizontally)
Claudio Valerio's avatar
Claudio Valerio committed
501

502
    //restore transformations
Claudio Valerio's avatar
Claudio Valerio committed
503 504 505 506 507 508 509
    painter->translate(rect().center());
    painter->rotate(mCurrentAngle);
    painter->rotate(mStartAngle);
    painter->translate(-rect().center().x(), -rect().center().y());

    qreal angle = mCurrentAngle - (int)(mCurrentAngle/360)*360;

510
    if (angle != 0)
Claudio Valerio's avatar
Claudio Valerio committed
511
    {
Ivan Ilin's avatar
Ivan Ilin committed
512
        QString ang = QString("%1°").arg(angle,0, 'f', 1);
513
        QFont font2 = painter->font();
Claudio Valerio's avatar
Claudio Valerio committed
514
        font2.setBold(true);
515 516
        QFontMetricsF fm2(font2);
        painter->setFont(font2);
Claudio Valerio's avatar
Claudio Valerio committed
517 518 519 520 521 522
        if (angle < 50)
            angle = 90;
        else
            angle = angle / 2;

        co = cos((mStartAngle + angle) * PI/180);
523 524 525 526
        si = sin((mStartAngle + angle) * PI/180);
        painter->drawText(QRectF(rect().center().x() + (rad*2.5/10)*co  - fm2.width(ang)/2,
                                 rect().center().y() - (rad*2.5/10)*si - fm2.height()/2,
                                 fm2.width(ang), fm2.height()), Qt::AlignTop, ang);
Claudio Valerio's avatar
Claudio Valerio committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    }

    painter->restore();
}


UBGraphicsProtractor::Tool UBGraphicsProtractor::toolFromPos(QPointF pos)
{
    pos = pos - rect().center();
    QLineF line(QPointF(0,0), pos);
    QTransform t;
    t.rotate(mStartAngle);
    QPointF p1 = t.map(pos);
    t.rotate(mCurrentAngle);
    QPointF p2 = t.map(pos);

543
    if (resizeButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
544
        return Resize;
545
    else if (closeButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
546
        return Close;
547
    else if (resetButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
548
        return Reset;
549
    else if (rotateButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
550
        return Rotate;
551
    else if (markerButtonRect().contains(p2))
Claudio Valerio's avatar
Claudio Valerio committed
552
        return MoveMarker;
553
    else if (line.length() <= radius())
Claudio Valerio's avatar
Claudio Valerio committed
554 555 556 557 558 559 560 561 562 563 564 565 566
        return Move;
    else
        return None;
}


UBGraphicsScene* UBGraphicsProtractor::scene() const
{
    return static_cast<UBGraphicsScene*>(QGraphicsEllipseItem::scene());
}

QBrush UBGraphicsProtractor::fillBrush() const
{
567 568
    QColor fillColor = edgeFillColor();// scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor;
    QColor fillColorCenter = middleFillColor();//scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter;
Claudio Valerio's avatar
Claudio Valerio committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    QColor transparentWhite = Qt::white;
    transparentWhite.setAlpha(scene()->isDarkBackground() ? sDrawTransparency : sFillTransparency);
    QRadialGradient radialGradient(rect().center(), radius(), rect().center());
    radialGradient.setColorAt(0, fillColorCenter);
    radialGradient.setColorAt(1, fillColor);
    return radialGradient;
}


UBItem* UBGraphicsProtractor::deepCopy() const
{
    UBGraphicsProtractor* copy = new UBGraphicsProtractor();

    copy->setPos(this->pos());
    copy->setRect(this->rect());
    copy->setTransform(this->transform());

    copy->mCurrentAngle = this->mCurrentAngle;
    copy->mSpan = this->mSpan;
    copy->mStartAngle = this->mStartAngle;
    copy->mScaleFactor = this->mScaleFactor;

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

    return copy;
}
595 596


unknown's avatar
unknown committed
597
void UBGraphicsProtractor::rotateAroundCenter(qreal angle)
598 599 600
{
    Q_UNUSED(angle);
}
601

unknown's avatar
unknown committed
602
QPointF UBGraphicsProtractor::rotationCenter() const
603
{
604
    return QPointF(rect().x(), rect().y());
605
}