Smaller Triangle tool

Added checks for the size of the interior, cut-out triangle to make sure
everything is drawn correctly at small sizes; buttons are now also
hidden if they overflow from the tool.
preferencesAboutTextFull
Craig Watson 7 years ago
parent 31f16b6abc
commit 8d9fc7b071
  1. 161
      src/tools/UBGraphicsTriangle.cpp
  2. 10
      src/tools/UBGraphicsTriangle.h

@ -49,6 +49,7 @@ UBGraphicsTriangle::UBGraphicsTriangle()
, mResizing1(false) , mResizing1(false)
, mResizing2(false) , mResizing2(false)
, mRotating(false) , mRotating(false)
, mShouldPaintInnerTriangle(true)
{ {
setRect(sDefaultRect, sDefaultOrientation); setRect(sDefaultRect, sDefaultOrientation);
@ -224,6 +225,29 @@ 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;
} }
bool paintInnerTriangle = true;
switch(mOrientation)
{
case BottomLeft:
if (B2.x() > C2.x() || B2.y() < A2.y())
paintInnerTriangle = false;
break;
case TopLeft:
if (B2.x() > C2.x() || B2.y() > A2.y())
paintInnerTriangle = false;
break;
case TopRight:
if (B2.x() < C2.x() || B2.y() > A2.y())
paintInnerTriangle = false;
break;
case BottomRight:
if (B2.x() < C2.x() || B2.y() < A2.y())
paintInnerTriangle = false;
break;
}
mShouldPaintInnerTriangle = paintInnerTriangle;
W1 = rect().height() * d / C; W1 = rect().height() * d / C;
H1 = rect().width() * d / C; H1 = rect().width() * d / C;
@ -251,40 +275,54 @@ void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem
QPolygonF polygon; QPolygonF polygon;
QLinearGradient gradient1(QPointF(A1.x(), 0), QPointF(A2.x(), 0)); if (mShouldPaintInnerTriangle) {
gradient1.setColorAt(0, edgeFillColor()); QLinearGradient gradient1(QPointF(A1.x(), 0), QPointF(A2.x(), 0));
gradient1.setColorAt(1, middleFillColor()); gradient1.setColorAt(0, edgeFillColor());
painter->setBrush(gradient1); gradient1.setColorAt(1, middleFillColor());
polygon << A1 << A2 << B2 << B1; painter->setBrush(gradient1);
painter->drawPolygon(polygon); polygon << A1 << A2 << B2 << B1;
polygon.clear(); painter->drawPolygon(polygon);
polygon.clear();
QLinearGradient gradient2(QPointF(0, B1.y()), QPointF(0, B2.y()));
gradient2.setColorAt(0, edgeFillColor()); QLinearGradient gradient2(QPointF(0, B1.y()), QPointF(0, B2.y()));
gradient2.setColorAt(1, middleFillColor()); gradient2.setColorAt(0, edgeFillColor());
painter->setBrush(gradient2); gradient2.setColorAt(1, middleFillColor());
polygon << B1 << B2 << C2 << C1; painter->setBrush(gradient2);
painter->drawPolygon(polygon); polygon << B1 << B2 << C2 << C1;
polygon.clear(); painter->drawPolygon(polygon);
polygon.clear();
QLinearGradient gradient3(CC, C2);
gradient3.setColorAt(0, edgeFillColor()); QLinearGradient gradient3(CC, C2);
gradient3.setColorAt(1, middleFillColor()); gradient3.setColorAt(0, edgeFillColor());
painter->setBrush(gradient3); gradient3.setColorAt(1, middleFillColor());
polygon << C1 << C2 << A2 << A1; painter->setBrush(gradient3);
painter->drawPolygon(polygon); polygon << C1 << C2 << A2 << A1;
polygon.clear(); painter->drawPolygon(polygon);
polygon.clear();
painter->setBrush(Qt::NoBrush);
painter->setPen(drawColor()); painter->setBrush(Qt::NoBrush);
painter->setPen(drawColor());
polygon << A1 << B1 << C1;
painter->drawPolygon(polygon);
polygon.clear();
polygon << A2 << B2 << C2;
painter->drawPolygon(polygon);
}
polygon << A1 << B1 << C1; else {
painter->drawPolygon(polygon); QLinearGradient gradient(QPointF(A1.x(), 0), QPointF(C1.x(), 0));
polygon.clear(); gradient.setColorAt(0, edgeFillColor());
gradient.setColorAt(1, middleFillColor());
painter->setBrush(gradient);
painter->setPen(drawColor());
polygon << A1 << B1 << C1;
painter->drawPolygon(polygon);
polygon.clear();
}
polygon << A2 << B2 << C2;
painter->drawPolygon(polygon);
paintGraduations(painter); paintGraduations(painter);
@ -329,9 +367,11 @@ QPainterPath UBGraphicsTriangle::shape() const
tShape.addPolygon(tPolygon); tShape.addPolygon(tPolygon);
tPolygon.clear(); tPolygon.clear();
tPolygon << A2 << B2 << C2; if (mShouldPaintInnerTriangle) {
tShape.addPolygon(tPolygon); tPolygon << A2 << B2 << C2;
tPolygon.clear(); tShape.addPolygon(tPolygon);
tPolygon.clear();
}
//qDebug() << "UBGraphicsTriangle shape()"<<"A1 ="<<A1<<"B1 ="<<B1<<"C1 ="<<C1; //qDebug() << "UBGraphicsTriangle shape()"<<"A1 ="<<A1<<"B1 ="<<B1<<"C1 ="<<C1;
//qDebug() << "UBGraphicsTriangle shape()"<<"A2 ="<<A2<<"B2 ="<<B2<<"C2 ="<<C2; //qDebug() << "UBGraphicsTriangle shape()"<<"A2 ="<<A2<<"B2 ="<<B2<<"C2 ="<<C2;
@ -592,19 +632,21 @@ QRectF UBGraphicsTriangle::vFlipRect() const
QRectF UBGraphicsTriangle::rotateRect() const QRectF UBGraphicsTriangle::rotateRect() const
{ {
QPointF p(C2); QPointF p(C2);
qreal buttonsX = vFlipRect().left();
switch(mOrientation) switch(mOrientation)
{ {
case BottomLeft: case BottomLeft:
p += QPointF(20, 5); p = QPointF(qMax(p.x() + 20, buttonsX), p.y() + 5);
break; break;
case TopLeft: case TopLeft:
p += QPointF(20, -5 - mRotateSvgItem->boundingRect().height()); p = QPointF(qMax(p.x() + 20, buttonsX), p.y() -5 - mRotateSvgItem->boundingRect().height());
break; break;
case TopRight: case TopRight:
p += QPointF(-20 - mRotateSvgItem->boundingRect().width(), -5 - mRotateSvgItem->boundingRect().height()); p = QPointF(qMin(p.x() -20 - mRotateSvgItem->boundingRect().width(), buttonsX), p.y() - 5 - mRotateSvgItem->boundingRect().height());
break; break;
case BottomRight: case BottomRight:
p += QPointF(-20 - mRotateSvgItem->boundingRect().width(), 5); p = QPointF(qMin(p.x() -20 - mRotateSvgItem->boundingRect().width(), buttonsX), p.y() + 5);
break; break;
} }
return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size())); return QRectF(p, QSizeF(mRotateSvgItem->boundingRect().size()));
@ -806,9 +848,9 @@ void UBGraphicsTriangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
mShowButtons = true; mShowButtons = true;
mCloseSvgItem->setVisible(true); mCloseSvgItem->setVisible(true);
mHFlipSvgItem->setVisible(true); mHFlipSvgItem->setVisible(contains(hFlipRect()));
mVFlipSvgItem->setVisible(true); mVFlipSvgItem->setVisible(contains(vFlipRect()));
mRotateSvgItem->setVisible(true); mRotateSvgItem->setVisible(contains(rotateRect()));
if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
setCursor(resizeCursor1()); setCursor(resizeCursor1());
@ -860,9 +902,9 @@ void UBGraphicsTriangle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
currentTool == UBStylusTool::Play) currentTool == UBStylusTool::Play)
{ {
mCloseSvgItem->setVisible(mShowButtons); mCloseSvgItem->setVisible(mShowButtons);
mVFlipSvgItem->setVisible(mShowButtons); mVFlipSvgItem->setVisible(mShowButtons && contains(vFlipRect()));
mHFlipSvgItem->setVisible(mShowButtons); mHFlipSvgItem->setVisible(mShowButtons && contains(hFlipRect()));
mRotateSvgItem->setVisible(mShowButtons); mRotateSvgItem->setVisible(mShowButtons && contains(rotateRect()));
if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill)) if (resize1Polygon().containsPoint(event->pos().toPoint(), Qt::OddEvenFill))
setCursor(resizeCursor1()); setCursor(resizeCursor1());
@ -935,6 +977,35 @@ void UBGraphicsTriangle::DrawLine(const QPointF &scenePos, qreal width)
UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker); UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker);
} }
/**
* @brief Check whether a given QRectF is inside the triangle (A1, B1, C1).
*
* Returns true if any corner of the rectangle is within the triangle or, if strict is set to true,
* if all corners of the rectangle are within the triangle.
*/
bool UBGraphicsTriangle::contains(const QRectF &rect, bool strict)
{
QPolygonF poly;
poly << A1 << B1 << C1 << A1;
QPainterPath path;
path.addPolygon(poly);
QList<QPointF> points;
points << rect.bottomRight() << rect.topLeft() << rect.topRight();
bool inside = path.contains(rect.bottomLeft());
foreach(QPointF p, points) {
if (strict)
inside = inside && path.contains(p);
else
inside = inside || path.contains(p);
}
return inside;
}
void UBGraphicsTriangle::EndLine() void UBGraphicsTriangle::EndLine()
{ {
} }

@ -98,7 +98,7 @@ class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonIt
void setRect(const QRectF &rect, UBGraphicsTriangleOrientation orientation) void setRect(const QRectF &rect, UBGraphicsTriangleOrientation orientation)
{ {
qDebug() << "setRect"<<"rect = "<<rect<<"orientation :"<<orientation; //qDebug() << "setRect"<<"rect = "<<rect<<"orientation :"<<orientation;
setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation); setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation);
} }
@ -178,11 +178,15 @@ 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);
bool mShouldPaintInnerTriangle;
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 = 240;
static const int sMinHeight = 200; static const int sMinHeight = 120;
qreal mStrokeWidth; qreal mStrokeWidth;
bool contains(const QRectF &rect, bool strict = true);
}; };
#endif /* UBGRAPHICSTRIANGLE_H_ */ #endif /* UBGRAPHICSTRIANGLE_H_ */

Loading…
Cancel
Save