Triangle resizing bug fix

preferencesAboutTextFull
Anatoly Mihalchenko 13 years ago
parent bba6631b63
commit de2fb68f0b
  1. 578
      src/tools/UBGraphicsTriangle.cpp
  2. 126
      src/tools/UBGraphicsTriangle.h

@ -29,28 +29,28 @@ const UBGraphicsTriangle::UBGraphicsTriangleOrientation UBGraphicsTriangle::sDef
UBGraphicsTriangle::BottomLeft; UBGraphicsTriangle::BottomLeft;
UBGraphicsTriangle::UBGraphicsTriangle() UBGraphicsTriangle::UBGraphicsTriangle()
: QGraphicsPolygonItem() : QGraphicsPolygonItem()
, UBAbstractDrawRuler() , UBAbstractDrawRuler()
, mResizing1(false) , mResizing1(false)
, mResizing2(false) , mResizing2(false)
, mRotating(false) , mRotating(false)
, angle(0) , angle(0)
{ {
setRect(sDefaultRect, sDefaultOrientation); setRect(sDefaultRect, sDefaultOrientation);
create(*this); create(*this);
mHFlipSvgItem = new QGraphicsSvgItem(":/images/hflipTool.svg", this); mHFlipSvgItem = new QGraphicsSvgItem(":/images/hflipTool.svg", this);
mHFlipSvgItem->setVisible(false); mHFlipSvgItem->setVisible(false);
mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mVFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this); mVFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this);
mVFlipSvgItem->setVisible(false); mVFlipSvgItem->setVisible(false);
mVFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mVFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this); mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this);
mRotateSvgItem->setVisible(false); mRotateSvgItem->setVisible(false);
mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
@ -89,10 +89,10 @@ UBGraphicsTriangle::~UBGraphicsTriangle()
UBItem* UBGraphicsTriangle::deepCopy(void) const UBItem* UBGraphicsTriangle::deepCopy(void) const
{ {
UBGraphicsTriangle* copy = new UBGraphicsTriangle(); UBGraphicsTriangle* copy = new UBGraphicsTriangle();
copy->setPos(this->pos()); copy->setPos(this->pos());
copy->setPolygon(this->polygon()); copy->setPolygon(this->polygon());
copy->setZValue(this->zValue()); copy->setZValue(this->zValue());
copy->setTransform(this->transform()); copy->setTransform(this->transform());
@ -104,11 +104,11 @@ UBItem* UBGraphicsTriangle::deepCopy(void) const
void UBGraphicsTriangle::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation) void UBGraphicsTriangle::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation)
{ {
QPolygonF polygon; QPolygonF polygon;
polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h); polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h);
setPolygon(polygon); setPolygon(polygon);
setOrientation(orientation); setOrientation(orientation);
} }
void UBGraphicsTriangle::setOrientation(UBGraphicsTriangleOrientation orientation) void UBGraphicsTriangle::setOrientation(UBGraphicsTriangleOrientation orientation)
@ -123,7 +123,7 @@ void UBGraphicsTriangle::setOrientation(UBGraphicsTriangleOrientation orientatio
UBGraphicsScene* UBGraphicsTriangle::scene() const UBGraphicsScene* UBGraphicsTriangle::scene() const
{ {
return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene()); return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene());
} }
void UBGraphicsTriangle::calculatePoints(const QRectF& r) void UBGraphicsTriangle::calculatePoints(const QRectF& r)
@ -152,9 +152,9 @@ void UBGraphicsTriangle::calculatePoints(const QRectF& r)
break; break;
} }
C = sqrt(rect().width() * rect().width() + rect().height() * rect().height()); C = sqrt(rect().width() * rect().width() + rect().height() * rect().height());
qreal L = (C * d + rect().width() * d)/ rect().height(); qreal L = (C * d + rect().width() * d)/ rect().height();
qreal K = (C * d + rect().height() * d)/ rect().width(); qreal K = (C * d + rect().height() * d)/ rect().width();
switch(mOrientation) switch(mOrientation)
{ {
@ -179,8 +179,8 @@ void UBGraphicsTriangle::calculatePoints(const QRectF& r)
C2.setX(r.left() + L); C2.setY(r.bottom() - d); C2.setX(r.left() + L); C2.setY(r.bottom() - d);
break; break;
} }
W1 = rect().height() * d / C; W1 = rect().height() * d / C;
H1 = rect().width() * d / C; H1 = rect().width() * d / C;
switch(mOrientation) switch(mOrientation)
{ {
@ -202,69 +202,69 @@ void UBGraphicsTriangle::calculatePoints(const QRectF& r)
void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{ {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
QPolygonF polygon;
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();
QPolygonF polygon;
painter->setBrush(Qt::NoBrush); QLinearGradient gradient1(QPointF(A1.x(), 0), QPointF(A2.x(), 0));
painter->setPen(drawColor()); 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());
polygon << A1 << B1 << C1; polygon << A1 << B1 << C1;
painter->drawPolygon(polygon); painter->drawPolygon(polygon);
polygon.clear(); polygon.clear();
polygon << A2 << B2 << C2; polygon << A2 << B2 << C2;
painter->drawPolygon(polygon); painter->drawPolygon(polygon);
paintGraduations(painter); paintGraduations(painter);
mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom()); mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
QTransform antiScaleTransform; QTransform antiScaleTransform;
antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio); antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio);
mCloseSvgItem->setTransform(antiScaleTransform); mCloseSvgItem->setTransform(antiScaleTransform);
mHFlipSvgItem->setTransform(antiScaleTransform); mHFlipSvgItem->setTransform(antiScaleTransform);
mVFlipSvgItem->setTransform(antiScaleTransform); mVFlipSvgItem->setTransform(antiScaleTransform);
mRotateSvgItem->setTransform(antiScaleTransform); mRotateSvgItem->setTransform(antiScaleTransform);
mCloseSvgItem->setPos(closeButtonRect().topLeft()); mCloseSvgItem->setPos(closeButtonRect().topLeft());
mHFlipSvgItem->setPos(hFlipRect().topLeft()); mHFlipSvgItem->setPos(hFlipRect().topLeft());
mVFlipSvgItem->setPos(vFlipRect().topLeft()); mVFlipSvgItem->setPos(vFlipRect().topLeft());
mRotateSvgItem->setPos(rotateRect().topLeft()); mRotateSvgItem->setPos(rotateRect().topLeft());
if (mShowButtons || mResizing1 || mResizing2) if (mShowButtons || mResizing1 || mResizing2)
{ {
painter->setBrush(QColor(0, 0, 0)); painter->setBrush(QColor(0, 0, 0));
if (mShowButtons || mResizing1) if (mShowButtons || mResizing1)
painter->drawPolygon(resize1Polygon()); painter->drawPolygon(resize1Polygon());
if (mShowButtons || mResizing2) if (mShowButtons || mResizing2)
painter->drawPolygon(resize2Polygon()); painter->drawPolygon(resize2Polygon());
} }
} }
void UBGraphicsTriangle::paintGraduations(QPainter *painter) void UBGraphicsTriangle::paintGraduations(QPainter *painter)
@ -289,29 +289,29 @@ void UBGraphicsTriangle::paintGraduations(QPainter *painter)
((0 == millimeters % millimetersPerHalfCentimeter) ? ((0 == millimeters % millimetersPerHalfCentimeter) ?
halfCentimeterGraduationHeight : millimeterGraduationHeight); halfCentimeterGraduationHeight : millimeterGraduationHeight);
// Check that grad. line inside triangle // Check that grad. line inside triangle
qreal dx = (kx > 0) ? rect().width() - graduationX : graduationX - rect().x(); qreal dx = (kx > 0) ? rect().width() - graduationX : graduationX - rect().x();
qreal lineY = rotationCenter().y() - ky * rect().height()/rect().width() * dx; qreal lineY = rotationCenter().y() - ky * rect().height()/rect().width() * dx;
if (mOrientation == BottomLeft || mOrientation == BottomRight) if (mOrientation == BottomLeft || mOrientation == BottomRight)
{ {
if (lineY >= rotationCenter().y() - ky * graduationHeight) if (lineY >= rotationCenter().y() - ky * graduationHeight)
break; break;
} }
else else
{ {
if (lineY <= rotationCenter().y() - ky * graduationHeight) if (lineY <= rotationCenter().y() - ky * graduationHeight)
break; break;
} }
painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight)); painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight));
if (0 == millimeters % millimetersPerCentimeter) if (0 == millimeters % millimetersPerCentimeter)
{ {
QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter)); QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter));
int textXRight = graduationX + fontMetrics.width(text) / 2; int textXRight = graduationX + fontMetrics.width(text) / 2;
qreal textWidth = fontMetrics.width(text); qreal textWidth = fontMetrics.width(text);
qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5; qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;
int textY = (ky > 0) ? rotationCenter().y() - 5 - centimeterGraduationHeight - textHeight int textY = (ky > 0) ? rotationCenter().y() - 5 - centimeterGraduationHeight - textHeight
: rotationCenter().y() + 5 + centimeterGraduationHeight; : rotationCenter().y() + 5 + centimeterGraduationHeight;
bool bText = false; bool bText = false;
@ -342,7 +342,7 @@ void UBGraphicsTriangle::paintGraduations(QPainter *painter)
if (bText) if (bText)
painter->drawText( painter->drawText(
QRectF(graduationX - textWidth / 2, QRectF(graduationX - textWidth / 2,
textY, textWidth, textHeight), textY, textWidth, textHeight),
Qt::AlignVCenter, text); Qt::AlignVCenter, text);
} }
@ -369,7 +369,7 @@ void UBGraphicsTriangle::rotateAroundCenter(QTransform& transform, QPointF cente
} }
QPointF UBGraphicsTriangle::rotationCenter() const QPointF UBGraphicsTriangle::rotationCenter() const
{ {
switch(mOrientation) switch(mOrientation)
{ {
@ -383,30 +383,30 @@ QPointF UBGraphicsTriangle::rotationCenter() const
return QPointF(0, 0); return QPointF(0, 0);
} }
QRectF UBGraphicsTriangle::closeButtonRect() const QRectF UBGraphicsTriangle::closeButtonRect() const
{ {
switch(mOrientation) switch(mOrientation)
{ {
case BottomLeft: case BottomLeft:
return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5, return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5,
B2.y() - mCloseSvgItem->boundingRect().height() - 5, B2.y() - mCloseSvgItem->boundingRect().height() - 5,
mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().width(),
mCloseSvgItem->boundingRect().height()); mCloseSvgItem->boundingRect().height());
case TopLeft: case TopLeft:
return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5, return QRectF(B2.x() - mCloseSvgItem->boundingRect().width() - 5,
B2.y() + 5, B2.y() + 5,
mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().width(),
mCloseSvgItem->boundingRect().height()); mCloseSvgItem->boundingRect().height());
case TopRight: case TopRight:
return QRectF(B2.x() + 5, return QRectF(B2.x() + 5,
B2.y() + 5, B2.y() + 5,
mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().width(),
mCloseSvgItem->boundingRect().height()); mCloseSvgItem->boundingRect().height());
case BottomRight: case BottomRight:
return QRectF(B2.x() + 5, return QRectF(B2.x() + 5,
B2.y() - mCloseSvgItem->boundingRect().height() - 5, B2.y() - mCloseSvgItem->boundingRect().height() - 5,
mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().width(),
mCloseSvgItem->boundingRect().height()); mCloseSvgItem->boundingRect().height());
} }
return QRectF(0,0,0,0); return QRectF(0,0,0,0);
} }
@ -435,9 +435,9 @@ QPolygonF UBGraphicsTriangle::resize1Polygon() const
} }
QPointF P1(C1.x() + x1 * sArrowLength, C1.y()); QPointF P1(C1.x() + x1 * sArrowLength, C1.y());
QPointF P2(C1.x() + x1 * sArrowLength * rect().width()/C, C1.y() + y1 * sArrowLength * rect().height() / C); QPointF P2(C1.x() + x1 * sArrowLength * rect().width()/C, C1.y() + y1 * sArrowLength * rect().height() / C);
QPolygonF p; QPolygonF p;
p << C1 << P1 << P2; p << C1 << P1 << P2;
return p; return p;
} }
QPolygonF UBGraphicsTriangle::resize2Polygon() const QPolygonF UBGraphicsTriangle::resize2Polygon() const
@ -465,70 +465,70 @@ QPolygonF UBGraphicsTriangle::resize2Polygon() const
QPointF P1(A1.x(), A1.y() + y1 * sArrowLength); QPointF P1(A1.x(), A1.y() + y1 * sArrowLength);
QPointF P2(A1.x() + x1 * sArrowLength * rect().width()/C, QPointF P2(A1.x() + x1 * sArrowLength * rect().width()/C,
A1.y() + y1 * sArrowLength * rect().height() / C); A1.y() + y1 * sArrowLength * rect().height() / C);
QPolygonF p; QPolygonF p;
p << A1 << P1 << P2; p << A1 << P1 << P2;
return p; return p;
} }
QRectF UBGraphicsTriangle::hFlipRect() const QRectF UBGraphicsTriangle::hFlipRect() const
{ {
qreal dy = mVFlipSvgItem->boundingRect().height() + mCloseSvgItem->boundingRect().height() + 10; qreal dy = mVFlipSvgItem->boundingRect().height() + mCloseSvgItem->boundingRect().height() + 10;
switch(mOrientation) switch(mOrientation)
{ {
case BottomLeft: case BottomLeft:
return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5, return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5,
B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy, B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy,
mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().width(),
mHFlipSvgItem->boundingRect().height()); mHFlipSvgItem->boundingRect().height());
case TopLeft: case TopLeft:
return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5, return QRectF(B2.x() - mHFlipSvgItem->boundingRect().width() - 5,
B2.y() + 5 + dy, B2.y() + 5 + dy,
mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().width(),
mHFlipSvgItem->boundingRect().height()); mHFlipSvgItem->boundingRect().height());
case TopRight: case TopRight:
return QRectF(B2.x() + 5, return QRectF(B2.x() + 5,
B2.y() + 5 + dy, B2.y() + 5 + dy,
mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().width(),
mHFlipSvgItem->boundingRect().height()); mHFlipSvgItem->boundingRect().height());
case BottomRight: case BottomRight:
return QRectF(B2.x() + 5, return QRectF(B2.x() + 5,
B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy, B2.y() - mHFlipSvgItem->boundingRect().height() - 5 - dy,
mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().width(),
mHFlipSvgItem->boundingRect().height()); mHFlipSvgItem->boundingRect().height());
} }
return QRectF(0,0,0,0); return QRectF(0,0,0,0);
} }
QRectF UBGraphicsTriangle::vFlipRect() const QRectF UBGraphicsTriangle::vFlipRect() const
{ {
qreal dy = mCloseSvgItem->boundingRect().height() + 5; qreal dy = mCloseSvgItem->boundingRect().height() + 5;
switch(mOrientation) switch(mOrientation)
{ {
case BottomLeft: case BottomLeft:
return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5, return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5,
B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy, B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy,
mVFlipSvgItem->boundingRect().width(), mVFlipSvgItem->boundingRect().width(),
mVFlipSvgItem->boundingRect().height()); mVFlipSvgItem->boundingRect().height());
case TopLeft: case TopLeft:
return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5, return QRectF(B2.x() - mVFlipSvgItem->boundingRect().width() - 5,
B2.y() + 5 + dy, B2.y() + 5 + dy,
mVFlipSvgItem->boundingRect().width(), mVFlipSvgItem->boundingRect().width(),
mVFlipSvgItem->boundingRect().height()); mVFlipSvgItem->boundingRect().height());
case TopRight: case TopRight:
return QRectF(B2.x() + 5, return QRectF(B2.x() + 5,
B2.y() + 5 + dy, B2.y() + 5 + dy,
mVFlipSvgItem->boundingRect().width(), mVFlipSvgItem->boundingRect().width(),
mVFlipSvgItem->boundingRect().height()); mVFlipSvgItem->boundingRect().height());
case BottomRight: case BottomRight:
return QRectF(B2.x() + 5, return QRectF(B2.x() + 5,
B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy, B2.y() - mVFlipSvgItem->boundingRect().height() - 5 - dy,
mVFlipSvgItem->boundingRect().width(), mVFlipSvgItem->boundingRect().width(),
mVFlipSvgItem->boundingRect().height()); mVFlipSvgItem->boundingRect().height());
} }
return QRectF(0,0,0,0); return QRectF(0,0,0,0);
} }
QRectF UBGraphicsTriangle::rotateRect() const QRectF UBGraphicsTriangle::rotateRect() const
{ {
QPointF p(C2); QPointF p(C2);
switch(mOrientation) switch(mOrientation)
@ -546,122 +546,124 @@ QRectF UBGraphicsTriangle::rotateRect() const
p += QPointF(-20 - mRotateSvgItem->boundingRect().width(), 5); p += QPointF(-20 - mRotateSvgItem->boundingRect().width(), 5);
break; break;
} }
return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size())); return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size()));
} }
QCursor UBGraphicsTriangle::resizeCursor1() const QCursor UBGraphicsTriangle::resizeCursor1() const
{ {
return mResizeCursor1; return mResizeCursor1;
} }
QCursor UBGraphicsTriangle::resizeCursor2() const QCursor UBGraphicsTriangle::resizeCursor2() const
{ {
return mResizeCursor2; return mResizeCursor2;
} }
QCursor UBGraphicsTriangle::flipCursor() const QCursor UBGraphicsTriangle::flipCursor() const
{ {
return Qt::ArrowCursor; return Qt::ArrowCursor;
} }
void UBGraphicsTriangle::mousePressEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsTriangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) lastRect = rect().toRect();
lastPos = event->screenPos();
if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
{ {
mResizing1 = true; mResizing1 = true;
event->accept(); event->accept();
} }
else if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) else
if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
{
mResizing2 = true;
event->accept();
}
else
if(rotateRect().contains(event->pos()))
{ {
mResizing2 = true; mRotating = true;
event->accept(); event->accept();
} }
else if(rotateRect().contains(event->pos()))
{
mRotating = true;
event->accept();
}
else else
{ {
QGraphicsItem::mousePressEvent(event); QGraphicsItem::mousePressEvent(event);
} }
mShowButtons = false; mShowButtons = false;
mCloseSvgItem->setVisible(false); mCloseSvgItem->setVisible(false);
mHFlipSvgItem->setVisible(false); mHFlipSvgItem->setVisible(false);
mVFlipSvgItem->setVisible(false); mVFlipSvgItem->setVisible(false);
mRotateSvgItem->setVisible(mRotating); mRotateSvgItem->setVisible(mRotating);
update(); update();
} }
void UBGraphicsTriangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsTriangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
QPoint currPos = event->screenPos();
// qDebug() << QString(" X: %1 ").arg(currPos.x());
// qDebug() << QString(" Y: %1 ").arg(currPos.y());
if (!mResizing1 && !mResizing2 && !mRotating) if (!mResizing1 && !mResizing2 && !mRotating)
{ {
QGraphicsItem::mouseMoveEvent(event); QGraphicsItem::mouseMoveEvent(event);
} }
else else
{ {
if (mResizing1)
{ //-----------------------------------------------//
QPointF delta = event->pos() - event->lastPos();
if (mResizing1)
{
if (mOrientation == TopLeft || mOrientation == BottomLeft) if (mOrientation == TopLeft || mOrientation == BottomLeft)
{ {
if (rect().width() + delta.x() < (qreal)sMinWidth) int deltaX = currPos.x() - lastPos.x();
delta.setX((qreal)sMinWidth - rect().width()); if (lastRect.width() + deltaX < sMinWidth)
deltaX = sMinWidth - lastRect.width();
setRect(QRectF(lastRect.left(), lastRect.top(),
lastRect.width() + deltaX, lastRect.height()), mOrientation);
} }
else else
{ {
if (rect().width() - delta.x() < (qreal)sMinWidth) int deltaX = lastPos.x() - currPos.x();
delta.setX((qreal)sMinWidth - rect().width()); if (lastRect.width() + deltaX < sMinWidth)
deltaX = sMinWidth - lastRect.width();
setRect(QRectF(lastRect.left() - deltaX, lastRect.top(),
lastRect.width() + deltaX, lastRect.height()), mOrientation);
} }
if (mOrientation == TopLeft || mOrientation == BottomLeft) }
{
setRect(QRectF( //-----------------------------------------------//
rect().topLeft(),
QSizeF(rect().width() + delta.x(), if (mResizing2)
rect().height())), {
mOrientation);
}
else
{
setRect(QRectF(
rect().left() + delta.x(),
rect().top(),
rect().width() - delta.x(),
rect().height()),
mOrientation
);
}
}
if (mResizing2)
{
QPointF delta = event->pos() - event->lastPos();
if (mOrientation == BottomRight || mOrientation == BottomLeft) if (mOrientation == BottomRight || mOrientation == BottomLeft)
{ {
if (rect().height() - delta.y() < (qreal)sMinHeight) int deltaY = lastPos.y() - currPos.y();
delta.setY((qreal)sMinHeight - rect().height()); if (lastRect.height() + deltaY < sMinHeight)
deltaY = sMinHeight - lastRect.height();
setRect(QRectF(lastRect.left(), lastRect.top() - deltaY,
lastRect.width(), lastRect.height() + deltaY), mOrientation);
} }
else else
{ {
if (rect().height() + delta.y() < (qreal)sMinHeight) int deltaY = currPos.y() - lastPos.y();
delta.setY((qreal)sMinHeight - rect().height()); if (lastRect.height() + deltaY < sMinHeight)
deltaY = sMinHeight - lastRect.height();
setRect(QRectF(lastRect.left(), lastRect.top(),
lastRect.width(), lastRect.height() + deltaY), mOrientation);
} }
if (mOrientation == BottomRight || mOrientation == BottomLeft)
setRect(QRectF( }
rect().left(),
rect().top() + delta.y(), //-----------------------------------------------//
rect().width(),
rect().height() - delta.y()),
mOrientation);
else
setRect(QRectF(
rect().left(),
rect().top(),
rect().width(),
rect().height() + delta.y()),
mOrientation);
}
if (mRotating) if (mRotating)
{ {
QLineF currentLine(rotationCenter(), event->pos()); QLineF currentLine(rotationCenter(), event->pos());
@ -669,104 +671,106 @@ void UBGraphicsTriangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
rotateAroundCenter(currentLine.angleTo(lastLine)); rotateAroundCenter(currentLine.angleTo(lastLine));
} }
//-----------------------------------------------//
event->accept(); event->accept();
} }
} }
void UBGraphicsTriangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsTriangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
if (mResizing1 || mResizing2 || mRotating) if (mResizing1 || mResizing2 || mRotating)
{ {
if (mRotating) if (mRotating)
updateResizeCursor(); updateResizeCursor();
mResizing1 = false; mResizing1 = false;
mResizing2 = false; mResizing2 = false;
mRotating = false; mRotating = false;
event->accept(); event->accept();
} }
else if (closeButtonRect().contains(event->pos())) else if (closeButtonRect().contains(event->pos()))
{ {
hide(); hide();
emit hidden(); emit hidden();
event->accept(); event->accept();
} }
else if (hFlipRect().contains(event->pos())) else if (hFlipRect().contains(event->pos()))
{ {
switch(mOrientation) switch(mOrientation)
{ {
case BottomLeft: case BottomLeft:
setOrientation(BottomRight); setOrientation(BottomRight);
break; break;
case BottomRight: case BottomRight:
setOrientation(BottomLeft); setOrientation(BottomLeft);
break; break;
case TopLeft: case TopLeft:
setOrientation(TopRight); setOrientation(TopRight);
break; break;
case TopRight: case TopRight:
setOrientation(TopLeft); setOrientation(TopLeft);
break; break;
} }
} }
else if (vFlipRect().contains(event->pos())) else if (vFlipRect().contains(event->pos()))
{ {
switch(mOrientation) switch(mOrientation)
{ {
case BottomLeft: case BottomLeft:
setOrientation(TopLeft); setOrientation(TopLeft);
break; break;
case BottomRight: case BottomRight:
setOrientation(TopRight); setOrientation(TopRight);
break; break;
case TopLeft: case TopLeft:
setOrientation(BottomLeft); setOrientation(BottomLeft);
break; break;
case TopRight: case TopRight:
setOrientation(BottomRight); setOrientation(BottomRight);
break; break;
} }
} }
else else
{ {
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
} }
mShowButtons = true; mShowButtons = true;
update(); update();
if (scene()) if (scene())
scene()->setModified(true); scene()->setModified(true);
} }
void UBGraphicsTriangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void UBGraphicsTriangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool (); UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
if (currentTool == UBStylusTool::Selector) if (currentTool == UBStylusTool::Selector)
{ {
mCloseSvgItem->setParentItem(this); mCloseSvgItem->setParentItem(this);
mShowButtons = true; mShowButtons = true;
mCloseSvgItem->setVisible(true); mCloseSvgItem->setVisible(true);
mHFlipSvgItem->setVisible(true); mHFlipSvgItem->setVisible(true);
mVFlipSvgItem->setVisible(true); mVFlipSvgItem->setVisible(true);
mRotateSvgItem->setVisible(true); mRotateSvgItem->setVisible(true);
if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
setCursor(resizeCursor1()); setCursor(resizeCursor1());
else if(resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) else if(resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
setCursor(resizeCursor2()); setCursor(resizeCursor2());
else if (closeButtonRect().contains(event->pos())) else if (closeButtonRect().contains(event->pos()))
setCursor(closeCursor()); setCursor(closeCursor());
else if (hFlipRect().contains(event->pos()) else if (hFlipRect().contains(event->pos())
|| vFlipRect().contains(event->pos())) || vFlipRect().contains(event->pos()))
setCursor(flipCursor()); setCursor(flipCursor());
else if (rotateRect().contains(event->pos())) else if (rotateRect().contains(event->pos()))
setCursor(rotateCursor()); setCursor(rotateCursor());
else else
setCursor(moveCursor()); setCursor(moveCursor());
event->accept(); event->accept();
update(); update();
} }
} }
void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
@ -774,8 +778,8 @@ void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
mShowButtons = false; mShowButtons = false;
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
mCloseSvgItem->setVisible(false); mCloseSvgItem->setVisible(false);
mVFlipSvgItem->setVisible(false); mVFlipSvgItem->setVisible(false);
mHFlipSvgItem->setVisible(false); mHFlipSvgItem->setVisible(false);
mRotateSvgItem->setVisible(false); mRotateSvgItem->setVisible(false);
event->accept(); event->accept();
update(); update();
@ -783,29 +787,29 @@ void UBGraphicsTriangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
void UBGraphicsTriangle::hoverMoveEvent(QGraphicsSceneHoverEvent *event) void UBGraphicsTriangle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{ {
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool (); UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
if (currentTool == UBStylusTool::Selector) if (currentTool == UBStylusTool::Selector)
{ {
mCloseSvgItem->setVisible(mShowButtons); mCloseSvgItem->setVisible(mShowButtons);
mVFlipSvgItem->setVisible(mShowButtons); mVFlipSvgItem->setVisible(mShowButtons);
mHFlipSvgItem->setVisible(mShowButtons); mHFlipSvgItem->setVisible(mShowButtons);
mRotateSvgItem->setVisible(mShowButtons); mRotateSvgItem->setVisible(mShowButtons);
if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
setCursor(resizeCursor1()); setCursor(resizeCursor1());
else if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) else if (resize2Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
setCursor(resizeCursor2()); setCursor(resizeCursor2());
else if (closeButtonRect().contains(event->pos())) else if (closeButtonRect().contains(event->pos()))
setCursor(closeCursor()); setCursor(closeCursor());
else if (hFlipRect().contains(event->pos()) else if (hFlipRect().contains(event->pos())
|| vFlipRect().contains(event->pos())) || vFlipRect().contains(event->pos()))
setCursor(flipCursor()); setCursor(flipCursor());
else if (rotateRect().contains(event->pos())) else if (rotateRect().contains(event->pos()))
setCursor(rotateCursor()); setCursor(rotateCursor());
else else
setCursor(moveCursor()); setCursor(moveCursor());
event->accept(); event->accept();
} }
} }

@ -36,7 +36,7 @@ class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonIt
UBGraphicsTriangle(); UBGraphicsTriangle();
virtual ~UBGraphicsTriangle(); virtual ~UBGraphicsTriangle();
enum { Type = UBGraphicsItemType::TriangleItemType }; enum { Type = UBGraphicsItemType::TriangleItemType };
virtual int type() const virtual int type() const
{ {
@ -44,64 +44,64 @@ class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonIt
} }
virtual UBItem* deepCopy(void) const; virtual UBItem* deepCopy(void) const;
enum UBGraphicsTriangleOrientation enum UBGraphicsTriangleOrientation
{ {
BottomLeft = 0, BottomLeft = 0,
BottomRight, BottomRight,
TopLeft, TopLeft,
TopRight TopRight
}; };
static UBGraphicsTriangleOrientation orientationFromStr(QStringRef& str) static UBGraphicsTriangleOrientation orientationFromStr(QStringRef& str)
{ {
if (str == "BottomLeft") return BottomLeft; if (str == "BottomLeft") return BottomLeft;
if (str == "BottomRight") return BottomRight; if (str == "BottomRight") return BottomRight;
if (str == "TopLeft") return TopLeft; if (str == "TopLeft") return TopLeft;
if (str == "TopRight") return TopRight; if (str == "TopRight") return TopRight;
return sDefaultOrientation; return sDefaultOrientation;
} }
void setRect(const QRectF &rect, UBGraphicsTriangleOrientation orientation) void setRect(const QRectF &rect, UBGraphicsTriangleOrientation orientation)
{ {
setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation); setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation);
} }
void setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation); void setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation);
void setOrientation(UBGraphicsTriangleOrientation orientation); void setOrientation(UBGraphicsTriangleOrientation orientation);
QRectF rect() const {return boundingRect();} QRectF rect() const {return boundingRect();}
UBGraphicsScene* scene() const; UBGraphicsScene* scene() const;
protected: protected:
void updateResizeCursor(); void updateResizeCursor();
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget); virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget);
virtual void rotateAroundCenter(qreal angle); virtual void rotateAroundCenter(qreal angle);
virtual QPointF rotationCenter() const; virtual QPointF rotationCenter() const;
virtual QRectF closeButtonRect() const; virtual QRectF closeButtonRect() const;
QPolygonF resize1Polygon() const; QPolygonF resize1Polygon() const;
QPolygonF resize2Polygon() const; QPolygonF resize2Polygon() const;
QRectF hFlipRect() const; QRectF hFlipRect() const;
QRectF vFlipRect() const; QRectF vFlipRect() const;
QRectF rotateRect() const; QRectF rotateRect() const;
QCursor resizeCursor1() const; QCursor resizeCursor1() const;
QCursor resizeCursor2() const; QCursor resizeCursor2() const;
QCursor flipCursor() const; QCursor flipCursor() const;
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
private: private:
QCursor mResizeCursor1; QCursor mResizeCursor1;
QCursor mResizeCursor2; QCursor mResizeCursor2;
@ -110,21 +110,23 @@ class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonIt
qreal angle; qreal angle;
void rotateAroundCenter(QTransform& transform, QPointF center); void rotateAroundCenter(QTransform& transform, QPointF center);
bool mResizing1; bool mResizing1;
bool mResizing2; bool mResizing2;
bool mRotating; bool mRotating;
QRect lastRect;
QPoint lastPos;
QGraphicsSvgItem* mHFlipSvgItem; QGraphicsSvgItem* mHFlipSvgItem;
QGraphicsSvgItem* mVFlipSvgItem; QGraphicsSvgItem* mVFlipSvgItem;
QGraphicsSvgItem* mRotateSvgItem; QGraphicsSvgItem* mRotateSvgItem;
static const QRect sDefaultRect; static const QRect sDefaultRect;
static const UBGraphicsTriangleOrientation sDefaultOrientation; static const UBGraphicsTriangleOrientation sDefaultOrientation;
void paintGraduations(QPainter *painter); void paintGraduations(QPainter *painter);
UBGraphicsTriangleOrientation mOrientation; UBGraphicsTriangleOrientation mOrientation;
QPointF A1, B1, C1, A2, B2, C2; // coordinates of points in ext and int triangles QPointF A1, B1, C1, A2, B2, C2; // coordinates of points in ext and int triangles
qreal C; qreal C;
@ -132,10 +134,10 @@ class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonIt
QPointF CC; // Hyp. fillining gradient - top point QPointF CC; // Hyp. fillining gradient - top point
void calculatePoints(const QRectF& rect); void calculatePoints(const QRectF& rect);
static const int d = 70; // width of triangle border static const int d = 70; // width of triangle border
static const int sArrowLength = 30; static const int sArrowLength = 30;
static const int sMinWidth = 380; static const int sMinWidth = 380;
static const int sMinHeight = 200; static const int sMinHeight = 200;
}; };
#endif /* UBGRAPHICSTRIANGLE_H_ */ #endif /* UBGRAPHICSTRIANGLE_H_ */

Loading…
Cancel
Save