/* * Copyright (C) 2013 Open Education Foundation * * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour * l'Education Numérique en Afrique (GIP ENA) * * This file is part of OpenBoard. * * OpenBoard 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, version 3 of the License, * 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). * * OpenBoard 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 OpenBoard. If not, see . */ #include "UBGraphicsProtractor.h" #include "core/UBApplication.h" #include "gui/UBResources.h" #include "domain/UBGraphicsScene.h" #include "board/UBBoardController.h" #include "board/UBDrawingController.h" #include "UBAbstractDrawRuler.h" #include "core/memcheck.h" #include //#include const QRectF UBGraphicsProtractor::sDefaultRect = QRectF(-175, -175, 350, 350); UBGraphicsProtractor::UBGraphicsProtractor() : QGraphicsEllipseItem(sDefaultRect) , mCurrentTool(None) , mShowButtons(false) , mCurrentAngle(0) , mSpan(180) , mStartAngle(0) , mScaleFactor(1) , mResetSvgItem(0) , mResizeSvgItem(0) , mMarkerSvgItem(0) { sFillTransparency = 127; sDrawTransparency = 192; create(*this); setCacheMode(QGraphicsItem::DeviceCoordinateCache); setStartAngle(0); setSpanAngle(180 * 16); 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)); mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this); mRotateSvgItem->setVisible(false); mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly setFlag(QGraphicsItem::ItemIsSelectable, false); setScale(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(), 2 * radius(), 2 * radius()), mStartAngle * 16, mSpan * 16); 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(); 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)); 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; QRectF markerRect = markerButtonRect(); QPointF center = rect().center(); qreal centerX = center.x(); qreal centerY = center.y(); buttonPath.addRect(resizeButtonRect().adjusted(centerX, centerY, centerX, centerY)); if (!resizeButtonRect().contains(markerRect)) { buttonPath.addRect(markerRect.adjusted(centerX - markerRect.left() * 2 - markerRect.width(), centerY , centerX - markerRect.left() * 2 - markerRect.width(), centerY)); buttonPath.addRect(markerRect.adjusted(centerX, centerY, centerX, centerY)); } buttonPath.addRect(closeButtonRect().adjusted(centerX, centerY, centerX, centerY)); buttonPath.addRect(resetButtonRect().adjusted(centerX, centerY, centerX, centerY)); 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(); qreal mod_angle = 0; if (angle>350) { mod_angle= angle - 360; // only for debugging angle= angle - 360; } switch (mCurrentTool) { case Rotate : prepareGeometryChange(); mStartAngle = mStartAngle + angle; setStartAngle(mStartAngle * 16); mPreviousMousePos = currentPoint; //qDebug() << "UBGraphicsProtractor Rotate"<<"mStartAngle= "< 270) mCurrentAngle = 0; else if ((int)mCurrentAngle % 360 >= 180) mCurrentAngle = 180; mPreviousMousePos = currentPoint; update(); break; case Move : QGraphicsEllipseItem::mouseMoveEvent(event); break; default : break; } if (mCurrentTool != Move) event->accept(); } void UBGraphicsProtractor::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { switch (mCurrentTool) { case Reset : setStartAngle(0); mStartAngle = 0; break; case Close : hide(); break; case MoveMarker : update(); break; case Resize: update(); break; default : QGraphicsEllipseItem::mouseReleaseEvent(event); break; } if (mCurrentTool != Move) event->accept(); if (scene()) scene()->setModified(true); if (!mShowButtons) { mShowButtons = true; update(); } mCurrentTool = None; } void UBGraphicsProtractor::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { if (UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Selector && UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Play) return; 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; unsetCursor(); update(); event->accept(); } void UBGraphicsProtractor::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { if (UBDrawingController::drawingController ()->stylusTool() != UBStylusTool::Selector) return; Tool currentTool = toolFromPos(event->pos()); if (currentTool == Move) setCursor(Qt::SizeAllCursor); else setCursor(Qt::ArrowCursor); event->accept(); } qreal UBGraphicsProtractor::antiScale() const { return 1 / (mScaleFactor * UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom()); } QRectF UBGraphicsProtractor::resetButtonRect () const { //qreal antiSc = antiScale(); qreal antiSc = 1; if (buttonSizeReference().width() * antiSc <= buttonSizeReference().width() * 15) return QRectF(-buttonSizeReference().width() * 7, -buttonSizeReference().height() * antiSc / 2, buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc); else return QRectF(-buttonSizeReference().width() * antiSc / 2, -buttonSizeReference().height() * antiSc / 2, buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc); } QRectF UBGraphicsProtractor::closeButtonRect () const { //qreal antiSc = antiScale(); qreal antiSc = 1; //qDebug() << "UBGraphicsProtractor closeButtonRect"<<"antiSc = "<setZValue(zValue()+10); return QRectF(-buttonSizeReference().width() * antiSc / 2, -buttonSizeReference().height() * antiSc / 2, buttonSizeReference().width() * antiSc, buttonSizeReference().height() * antiSc); } } 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_OS_OSX font1.setPointSizeF(font1.pointSizeF() - 3); #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); for (int angle = 1; angle < mSpan; angle++) { 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); if (0 == angle % 90) 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), QPointF(center.x()+ (rad - graduationLength)*co, center.y() - (rad - graduationLength)*si))); //internal arc painter->drawLine(QLineF(QPointF(center.x()+ rad/2*co, center.y() - rad/2*si), QPointF(center.x()+ (rad/2 + graduationLength)*co, center.y() - (rad/2 + graduationLength)*si))); 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, center.y() - (rad - graduationLength*1.5)*si - fm1.height()/2, fm1.width(grad), fm1.height()), Qt::AlignTop, grad); //internal arc painter->setFont(font2); painter->drawText(QRectF(center.x() + (rad/2 + graduationLength*1.5)*co - fm2.width(grad2)/2, center.y() - (rad/2 + graduationLength*1.5)*si - fm2.height()/2, fm2.width(grad2), fm2.height()), Qt::AlignTop, grad2); painter->setFont(font1); } } painter->restore(); } void UBGraphicsProtractor::paintButtons(QPainter *painter) { Q_UNUSED(painter); qreal co = cos(mStartAngle * PI/180); // cos(rad) qreal si = sin(mStartAngle * PI/180); //sin(rad) if (mShowButtons) { qreal scale = buttonSizeReference().width() / mCloseSvgItem->boundingRect().width(); /*qDebug() << "UBGraphicsProtractor paint()" <<"closeButtonRect_Topleft="<setPos(pos_close_x,pos_close_y); mCloseSvgItem->resetTransform(); //mCloseSvgItem->translate(-closeButtonRect().left(),-closeButtonRect().top()); -- mCloseSvgItem->setPos(-pos_close_x,-pos_close_y); //mCloseSvgItem->setPos(-closeButtonRect().left(),-closeButtonRect().top()); //mCloseSvgItem->rotate(-mStartAngle); -- mCloseSvgItem->setRotation(rotation() - mStartAngle); //mCloseSvgItem->translate(closeButtonRect().left(), closeButtonRect().top()); -- mCloseSvgItem->setPos(pos_close_x, pos_close_y); //mCloseSvgItem->setPos(closeButtonRect().left(), closeButtonRect().top()); //mCloseSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item... mCloseSvgItem->setScale(scale );//this do not impact the bounding box of thr svg item... // Determine and apply the position on the reset button object. //------------------------------------------------------------ qreal pos_reset_x = resetButtonRect().topLeft().x()* co; qreal pos_reset_y = qAbs(resetButtonRect().topLeft().x()) * si + resetButtonRect().topLeft().y(); mResetSvgItem->setPos(pos_reset_x,pos_reset_y); //mResetSvgItem->setPos(resetButtonRect().topLeft() + rect().center()); mResetSvgItem->resetTransform(); //mResetSvgItem->translate(-resetButtonRect().left(), -resetButtonRect().top());-- mResetSvgItem->setPos(-pos_reset_x, -pos_reset_y); //mResetSvgItem->rotate(-mStartAngle);-- mResetSvgItem->setRotation(-mStartAngle); //mResetSvgItem->translate(resetButtonRect().left(), resetButtonRect().top());-- mResetSvgItem->setPos(pos_reset_x, pos_reset_y); //mResetSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item... mResetSvgItem->setScale(scale );//this do not impact the bounding box of thr svg item... // Determine and apply the position on the resize button object. //------------------------------------------------------------ qreal pos_resize_x = resizeButtonRect().topLeft().x()* co; qreal pos_resize_y = -(resizeButtonRect().topLeft().x()) * si + resizeButtonRect().topLeft().y(); qDebug() << "pos_resize_x ="<setPos(resizeButtonRect().topLeft() + rect().center()); mResizeSvgItem->setPos(pos_resize_x,pos_resize_y); mResizeSvgItem->resetTransform(); mResizeSvgItem->setPos(-pos_resize_x, -pos_resize_y); //mResizeSvgItem->rotate(-mStartAngle);-- mResizeSvgItem->setRotation(-mStartAngle); //mResizeSvgItem->translate(-resizeButtonRect().left(), -resizeButtonRect().top());-- mResizeSvgItem->setPos(pos_resize_x, pos_resize_y); // mResizeSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item... mResizeSvgItem->setScale(scale);//this do not impact the bounding box of thr svg item... // Determine and apply the position on the rotate button object. //-------------------------------------------------------------- qreal qPow_x = qPow(rotateButtonRect().topLeft().x(),2); qreal qPow_y = qPow(rotateButtonRect().topLeft().y(),2); qreal module_rotate_pos = qSqrt(qPow_x + qPow_y); qreal arg_rotate_pos = (qAtan2(rotateButtonRect().topLeft().y(), rotateButtonRect().topLeft().x())); //argument in radian qreal co_r = cos(-mStartAngle * PI/180 + arg_rotate_pos); // cos(rad) qreal si_r = sin(-mStartAngle * PI/180 + arg_rotate_pos); //sin(rad) qreal pos_rotate_x = (module_rotate_pos * co_r) ; qreal pos_rotate_y = (module_rotate_pos * si_r) ; mRotateSvgItem->setPos(pos_rotate_x,pos_rotate_y); //mRotateSvgItem->setPos(rotateButtonRect().topLeft() + rect().center()); -- mRotateSvgItem->resetTransform(); mRotateSvgItem->setPos(-pos_rotate_x, -pos_rotate_y); //mRotateSvgItem->rotate(-mStartAngle);-- mRotateSvgItem->setRotation(-mStartAngle); mRotateSvgItem->setPos(pos_rotate_x, pos_rotate_y); //mRotateSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item... mRotateSvgItem->setScale(scale);//this do not impact the bounding box of thr svg item... //qDebug()<<"UBGraphicsProtractor scale()"<<"scale ="<font(); font2.setBold(true); QFontMetricsF fm2(font2); painter->setFont(font2); if (angle < 50) angle = 90; else angle = angle / 2; co = cos((mStartAngle + angle) * PI/180); 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); } 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); qDebug() << "UBGraphicsProtractor toolFromPos()"<<"p1="<