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

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34
#include "UBGraphicsProtractor.h"
#include "core/UBApplication.h"
#include "gui/UBResources.h"
#include "domain/UBGraphicsScene.h"
#include "board/UBBoardController.h"
35
#include "board/UBDrawingController.h"
Claudio Valerio's avatar
Claudio Valerio committed
36
#include "UBAbstractDrawRuler.h"
Claudio Valerio's avatar
Claudio Valerio committed
37

38 39
#include "core/memcheck.h"

40 41 42
#include <QtWidgets/QGraphicsView>


Craig Watson's avatar
Craig Watson committed
43
const QRectF UBGraphicsProtractor::sDefaultRect = QRectF(-250, -250, 500, 500);
44
const qreal UBGraphicsProtractor::minRadius = 70;
Claudio Valerio's avatar
Claudio Valerio committed
45 46

UBGraphicsProtractor::UBGraphicsProtractor()
47 48 49 50 51 52 53 54 55 56
        : 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
57
{
Claudio Valerio's avatar
Claudio Valerio committed
58 59
    sFillTransparency = 127;
    sDrawTransparency = 192;
60
    create(*this);
61

62
    setCacheMode(QGraphicsItem::DeviceCoordinateCache);
Claudio Valerio's avatar
Claudio Valerio committed
63

64 65
    setStartAngle(0);
    setSpanAngle(180 * 16);
Claudio Valerio's avatar
Claudio Valerio committed
66 67 68 69 70 71 72 73 74 75 76 77

    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
78

unknown's avatar
unknown committed
79 80 81 82
    mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this);
    mRotateSvgItem->setVisible(false);
    mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

shibakaneki's avatar
shibakaneki committed
83
    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
84
    setFlag(QGraphicsItem::ItemIsSelectable, false);
Claudio Valerio's avatar
Claudio Valerio committed
85

Craig Watson's avatar
Craig Watson committed
86 87 88 89 90
    mCloseSvgItem->setPos(closeButtonRect().topLeft());
    mResetSvgItem->setPos(resetButtonRect().topLeft());
    mResizeSvgItem->setPos(resizeButtonRect().topLeft());
    mMarkerSvgItem->setPos(markerButtonRect().topLeft());
    mRotateSvgItem->setPos(rotateButtonRect().topLeft());
Claudio Valerio's avatar
Claudio Valerio committed
91 92 93 94 95 96 97 98 99 100
}


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

    Q_UNUSED(styleOption);
    Q_UNUSED(widget);

Craig Watson's avatar
Craig Watson committed
101 102 103 104
    QPen pen_(drawColor());
    pen_.setWidth(0); // Line width = 1 pixel regardless of scale / zoom
    painter->setPen(pen_);
    
105
    painter->setFont(QFont("Arial", 11));
Claudio Valerio's avatar
Claudio Valerio committed
106
    painter->setBrush(fillBrush());
107
    painter->drawPie(QRectF(rect().center().x() - radius(), rect().center().y() - radius(), 2 * radius(), 2 * radius()), mStartAngle * 16, mSpan * 16);
Claudio Valerio's avatar
Claudio Valerio committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    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();

137 138 139
    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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    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;
157
    QRectF markerRect = markerButtonRect();
Claudio Valerio's avatar
Claudio Valerio committed
158 159 160 161 162

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

163
    buttonPath.addRect(resizeButtonRect().adjusted(centerX, centerY, centerX, centerY));
164
    if (!resizeButtonRect().contains(markerRect))
Claudio Valerio's avatar
Claudio Valerio committed
165
    {
166
        buttonPath.addRect(markerRect.adjusted(centerX - markerRect.left() * 2 - markerRect.width(), centerY
167
                                               , centerX - markerRect.left() * 2 - markerRect.width(), centerY));
168
        buttonPath.addRect(markerRect.adjusted(centerX, centerY, centerX, centerY));
Claudio Valerio's avatar
Claudio Valerio committed
169
    }
170 171
    buttonPath.addRect(closeButtonRect().adjusted(centerX, centerY, centerX, centerY));
    buttonPath.addRect(resetButtonRect().adjusted(centerX, centerY, centerX, centerY));
Claudio Valerio's avatar
Claudio Valerio committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    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();

204

205
    switch (mCurrentTool)
Claudio Valerio's avatar
Claudio Valerio committed
206
    {
207 208 209 210 211
    case Rotate :
        prepareGeometryChange();
        mStartAngle = mStartAngle + angle;
        setStartAngle(mStartAngle * 16);
        mPreviousMousePos = currentPoint;
212

213 214
        break;

215

216
    case Resize :
217 218 219 220 221 222 223
        if (radius() * mScaleFactor * scaleFactor > minRadius) {
            prepareGeometryChange();
            setTransform(QTransform::fromTranslate(rect().center().x(), rect().center().y()), true);
            setTransform(QTransform::fromScale(scaleFactor, scaleFactor), true);
            setTransform(QTransform::fromTranslate(-rect().center().x(), -rect().center().y()), true);
            mScaleFactor *= scaleFactor;
        }
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        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
244 245
    }

246
    if (mCurrentTool != Move)
Claudio Valerio's avatar
Claudio Valerio committed
247 248 249 250 251 252 253
        event->accept();

}


void UBGraphicsProtractor::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
254
    switch (mCurrentTool)
Claudio Valerio's avatar
Claudio Valerio committed
255
    {
256 257 258 259 260 261 262 263 264 265 266 267 268
    case Reset :
        setStartAngle(0);
        mStartAngle = 0;
        break;

    case Close :
        hide();
        break;

    case MoveMarker :
        update();
        break;

269 270 271 272
    case Resize:
        update();
        break;

273 274 275
    default :
        QGraphicsEllipseItem::mouseReleaseEvent(event);
        break;
Claudio Valerio's avatar
Claudio Valerio committed
276 277 278 279 280 281 282 283
    }

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

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

284 285 286 287 288 289
    if (!mShowButtons)
    {
        mShowButtons = true;
        update();
    }

Claudio Valerio's avatar
Claudio Valerio committed
290 291 292 293 294 295
    mCurrentTool = None;
}


void UBGraphicsProtractor::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
296
    if (UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Selector && UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Play)
297
        return;
298

Claudio Valerio's avatar
Claudio Valerio committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    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;
319
    unsetCursor();
Claudio Valerio's avatar
Claudio Valerio committed
320 321 322 323 324 325 326
    update();
    event->accept();
}


void UBGraphicsProtractor::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
327 328
    if (UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Selector)
        return;
329

Claudio Valerio's avatar
Claudio Valerio committed
330 331
    Tool currentTool = toolFromPos(event->pos());

332
    if (currentTool == Move)
Claudio Valerio's avatar
Claudio Valerio committed
333
        setCursor(Qt::SizeAllCursor);
334 335
    else if (currentTool == Rotate)
        setCursor(rotateCursor());
Claudio Valerio's avatar
Claudio Valerio committed
336 337 338 339 340 341 342 343 344 345 346 347 348
    else
        setCursor(Qt::ArrowCursor);

    event->accept();
}


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


349
QRectF UBGraphicsProtractor::resetButtonRect () const
Claudio Valerio's avatar
Claudio Valerio committed
350
{
Craig Watson's avatar
Craig Watson committed
351 352 353
    qreal refWidth = buttonSizeReference().width();
    qreal width = mResetSvgItem->boundingRect().width();
    qreal height = mResetSvgItem->boundingRect().height();
354

Craig Watson's avatar
Craig Watson committed
355
    return QRectF(-refWidth * 7, -height/2.0, width, height);
Claudio Valerio's avatar
Claudio Valerio committed
356

Craig Watson's avatar
Craig Watson committed
357 358 359
    // Note: these hardcoded position values make it impossible
    // to use setScale() on the buttons without affecting their
    // relative position on the tool.
Claudio Valerio's avatar
Claudio Valerio committed
360 361
}

Craig Watson's avatar
Craig Watson committed
362

363
QRectF UBGraphicsProtractor::closeButtonRect () const
Claudio Valerio's avatar
Claudio Valerio committed
364
{
Craig Watson's avatar
Craig Watson committed
365 366 367
    qreal refWidth = buttonSizeReference().width();
    qreal width = mCloseSvgItem->boundingRect().width();
    qreal height = mCloseSvgItem->boundingRect().height();
368

Craig Watson's avatar
Craig Watson committed
369 370
    return QRectF(-refWidth * 9, -height/2.0, width, height);
}
371

Claudio Valerio's avatar
Claudio Valerio committed
372

Craig Watson's avatar
Craig Watson committed
373 374 375 376 377
QRectF UBGraphicsProtractor::resizeButtonRect () const
{
    qreal refWidth = buttonSizeReference().width();
    qreal width = mResizeSvgItem->boundingRect().width();
    qreal height = mResizeSvgItem->boundingRect().height();
378

Craig Watson's avatar
Craig Watson committed
379
    return QRectF(refWidth * 8, -height/2.0, width, height);
Claudio Valerio's avatar
Claudio Valerio committed
380 381 382
}


Craig Watson's avatar
Craig Watson committed
383
QRectF UBGraphicsProtractor::rotateButtonRect () const
Claudio Valerio's avatar
Claudio Valerio committed
384
{
Craig Watson's avatar
Craig Watson committed
385 386 387 388 389
    qreal refWidth = buttonSizeReference().width();
    qreal width = mRotateSvgItem->boundingRect().width();
    qreal height = mRotateSvgItem->boundingRect().height();

    return QRectF(refWidth * 5.5, -refWidth * 5, width, height);
390

Claudio Valerio's avatar
Claudio Valerio committed
391 392

}
Craig Watson's avatar
Craig Watson committed
393 394 395 396
QRectF UBGraphicsProtractor::markerButtonRect () const
{
    qreal width = mMarkerSvgItem->boundingRect().width();
    qreal height = mMarkerSvgItem->boundingRect().height();
Claudio Valerio's avatar
Claudio Valerio committed
397

Craig Watson's avatar
Craig Watson committed
398 399
    return QRectF(radius() + 3, -height/2, width, height);
}
Claudio Valerio's avatar
Claudio Valerio committed
400 401 402 403 404

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

Craig Watson's avatar
Craig Watson committed
405 406 407
    const int  tenDegreeGraduationLength = 22;
    const int fiveDegreeGraduationLength = 15;
    const int  oneDegreeGraduationLength = 7;
Claudio Valerio's avatar
Claudio Valerio committed
408 409

    QFont font1 = painter->font();
410

411
#ifdef Q_OS_OSX
412
    font1.setPointSizeF(font1.pointSizeF() + 3);
Craig Watson's avatar
Craig Watson committed
413
    font1.setWeight(QFont::Thin);
Claudio Valerio's avatar
Claudio Valerio committed
414 415 416 417 418 419 420 421 422 423 424 425 426
#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);

427
    for (int angle = 1; angle < mSpan; angle++)
Claudio Valerio's avatar
Claudio Valerio committed
428 429 430 431
    {
        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);
432
        if (0 == angle % 90)
Craig Watson's avatar
Craig Watson committed
433 434
            painter->drawLine(QLineF(QPointF(center.x(), center.y()), 
                        QPointF(center.x() + co*tenDegreeGraduationLength, center.y() - si*tenDegreeGraduationLength)));
Claudio Valerio's avatar
Claudio Valerio committed
435 436 437

        //external arc
        painter->drawLine(QLineF(QPointF(center.x()+ rad*co, center.y() - rad*si),
438
                                 QPointF(center.x()+ (rad - graduationLength)*co, center.y() - (rad - graduationLength)*si)));
Claudio Valerio's avatar
Claudio Valerio committed
439 440
        //internal arc
        painter->drawLine(QLineF(QPointF(center.x()+ rad/2*co, center.y() - rad/2*si),
441 442
                                 QPointF(center.x()+ (rad/2 + graduationLength)*co,
                                         center.y() - (rad/2 + graduationLength)*si)));
Claudio Valerio's avatar
Claudio Valerio committed
443 444 445 446 447 448 449 450 451

        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,
452 453
                                     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
454 455 456 457

            //internal arc
            painter->setFont(font2);
            painter->drawText(QRectF(center.x() + (rad/2 + graduationLength*1.5)*co  - fm2.width(grad2)/2,
458 459
                                     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
460 461 462 463 464 465 466 467 468 469 470 471 472
            painter->setFont(font1);

        }
    }

    painter->restore();
}


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

473 474
    if (mShowButtons)
    {        
Craig Watson's avatar
Craig Watson committed
475 476 477 478
        // The buttons (close, reset, resize & rotate) are rotated around the center
        // of the protractor, to follow it as it is rotated.
        // As coordinates are local to each QGraphicsItem, the position of the rotation 
        // center relative to each button is the inverse of that button's position.
479

Craig Watson's avatar
Craig Watson committed
480
        mCloseSvgItem->setTransformOriginPoint(closeButtonRect().topLeft() * -1); 
481 482
        mCloseSvgItem->setRotation(rotation() - mStartAngle);

Craig Watson's avatar
Craig Watson committed
483 484
        mResetSvgItem->setTransformOriginPoint(resetButtonRect().topLeft() * -1);
        mResetSvgItem->setRotation(rotation() - mStartAngle);
485

Craig Watson's avatar
Craig Watson committed
486 487
        mResizeSvgItem->setTransformOriginPoint(resizeButtonRect().topLeft() * -1);
        mResizeSvgItem->setRotation(rotation() - mStartAngle);
488

Craig Watson's avatar
Craig Watson committed
489 490
        mRotateSvgItem->setTransformOriginPoint(rotateButtonRect().topLeft() * -1);
        mRotateSvgItem->setRotation(rotation() - mStartAngle);
Claudio Valerio's avatar
Claudio Valerio committed
491 492
    }

Craig Watson's avatar
Craig Watson committed
493 494
    mMarkerSvgItem->setTransformOriginPoint(markerButtonRect().topLeft() * -1);
    mMarkerSvgItem->setRotation(rotation() - mStartAngle - mCurrentAngle);
Claudio Valerio's avatar
Claudio Valerio committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

    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
517
                     , (mCurrentAngle - (int)(mCurrentAngle/360)*360)*16);
Claudio Valerio's avatar
Claudio Valerio committed
518 519 520 521
    painter->translate(rect().center());
    painter->rotate(-mCurrentAngle);
    painter->translate(-rect().center().x(), -rect().center().y());

522
    //Paint Angle text (horizontally)
Claudio Valerio's avatar
Claudio Valerio committed
523

524
    //restore transformations
Claudio Valerio's avatar
Claudio Valerio committed
525 526 527 528 529 530 531
    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;

532
    if (angle != 0)
Claudio Valerio's avatar
Claudio Valerio committed
533
    {
Ivan Ilin's avatar
Ivan Ilin committed
534
        QString ang = QString("%1°").arg(angle,0, 'f', 1);
535
        QFont font2 = painter->font();
Claudio Valerio's avatar
Claudio Valerio committed
536
        font2.setBold(true);
537 538
        QFontMetricsF fm2(font2);
        painter->setFont(font2);
Claudio Valerio's avatar
Claudio Valerio committed
539 540 541 542 543 544
        if (angle < 50)
            angle = 90;
        else
            angle = angle / 2;

        co = cos((mStartAngle + angle) * PI/180);
545 546 547 548
        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
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    }

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

564 565

    if (resizeButtonRect().contains(p1.x(),p1.y()))
Claudio Valerio's avatar
Claudio Valerio committed
566
        return Resize;
Craig Watson's avatar
Craig Watson committed
567

568
    else if (closeButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
569
        return Close;
Craig Watson's avatar
Craig Watson committed
570

571
    else if (resetButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
572
        return Reset;
Craig Watson's avatar
Craig Watson committed
573

574
    else if (rotateButtonRect().contains(p1))
Claudio Valerio's avatar
Claudio Valerio committed
575
        return Rotate;
Craig Watson's avatar
Craig Watson committed
576

577
    else if (markerButtonRect().contains(p2))
Claudio Valerio's avatar
Claudio Valerio committed
578
        return MoveMarker;
Craig Watson's avatar
Craig Watson committed
579

580
    else if (line.length() <= radius())
Claudio Valerio's avatar
Claudio Valerio committed
581
        return Move;
Craig Watson's avatar
Craig Watson committed
582

Claudio Valerio's avatar
Claudio Valerio committed
583 584 585 586 587 588 589 590 591 592 593 594
    else
        return None;
}


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

QBrush UBGraphicsProtractor::fillBrush() const
{
595 596
    QColor fillColor = edgeFillColor();// scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor;
    QColor fillColorCenter = middleFillColor();//scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter;
Claudio Valerio's avatar
Claudio Valerio committed
597 598 599 600 601 602 603 604 605 606 607 608 609
    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();

610
    copyItemParameters(copy);
Claudio Valerio's avatar
Claudio Valerio committed
611 612 613 614 615

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

    return copy;
}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
void UBGraphicsProtractor::copyItemParameters(UBItem *copy) const
{
    UBGraphicsProtractor *cp = dynamic_cast<UBGraphicsProtractor*>(copy);
    if (cp)
    {
        cp->setPos(this->pos());
        cp->setRect(this->rect());
        cp->setTransform(this->transform());

        cp->mCurrentAngle = this->mCurrentAngle;
        cp->mSpan = this->mSpan;
        cp->mStartAngle = this->mStartAngle;
        cp->mScaleFactor = this->mScaleFactor;
    }
}
631

unknown's avatar
unknown committed
632
void UBGraphicsProtractor::rotateAroundCenter(qreal angle)
633
{
634 635 636 637 638 639 640 641 642
    // Q_UNUSED(angle);

    // Align the implementation with the others apps objects.
    QTransform transform;
    transform.translate(rotationCenter().x(), rotationCenter().y());
    transform.rotate(angle);
    transform.translate(- rotationCenter().x(), - rotationCenter().y());
    setTransform(transform, true);

643
}
644

unknown's avatar
unknown committed
645
QPointF UBGraphicsProtractor::rotationCenter() const
646
{
647
    return QPointF(rect().x(), rect().y());
648

649
}