Fixed SANKORE-1177.

preferencesAboutTextFull
Guillaume Burel 12 years ago
parent f2f7e94777
commit 15ba2c4ab1
  1. 126
      src/tools/UBGraphicsAristo.cpp
  2. 6
      src/tools/UBGraphicsAristo.h

@ -62,13 +62,15 @@ UBGraphicsAristo::UBGraphicsAristo()
mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this); mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this);
mMarkerSvgItem->setVisible(false);
mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool));
mMarkerSvgItem->setVisible(true); mMarkerSvgItem->setVisible(true);
create(*this); create(*this);
setBoundingRect(sDefaultRect);
setOrientation(sDefaultOrientation); setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly
setFlag(QGraphicsItem::ItemIsSelectable, false);
setOrientation(Top);
} }
UBGraphicsAristo::~UBGraphicsAristo() UBGraphicsAristo::~UBGraphicsAristo()
@ -78,43 +80,55 @@ UBGraphicsAristo::~UBGraphicsAristo()
/* /*
* setOrientation() modify the tool orientation. * setOrientation() modify the tool orientation.
* makeGeometryChange() is called so points are recomputed, control items are positionnated and shape is determined according to this modification. * Apexes coordinates are alors recomputed.
*/ */
void UBGraphicsAristo::setOrientation(Orientation orientation) void UBGraphicsAristo::setOrientation(Orientation orientation)
{ {
/* substracting difference to zero [2pi] twice, to obtain the desired angle */
mMarkerAngle -= 2 * (mMarkerAngle - (int)(mMarkerAngle/360)*360) - 360;
bool wasUndefined = mOrientation == Undefined;
mOrientation = orientation; mOrientation = orientation;
makeGeometryChange();
if (wasUndefined)
calculatePoints(sDefaultRect);
else
calculatePoints(boundingRect());
} }
/* calculatePoints() is used to calculate polygon's apexes coordinates. /* calculatePoints() is used to calculate polygon's apexes coordinates.
* This function handles orientation changes too. * This function handles orientation changes too.
* Items are repositionated and path is redeterminates.
*/ */
void UBGraphicsAristo::calculatePoints() void UBGraphicsAristo::calculatePoints(QRectF bounds)
{ {
switch (mOrientation) { switch (mOrientation) {
case Bottom: case Bottom:
C.setX(boundingRect().center().x()); C.setX(bounds.center().x());
C.setY(boundingRect().bottom()); C.setY(bounds.bottom());
A.setX(boundingRect().left()); A.setX(bounds.left());
A.setY(boundingRect().bottom() - boundingRect().width() / 2); A.setY(bounds.bottom() - bounds.width() / 2);
B.setX(boundingRect().right()); B.setX(bounds.right());
B.setY(boundingRect().bottom() - boundingRect().width() / 2); B.setY(bounds.bottom() - bounds.width() / 2);
break; break;
case Top: case Top:
C.setX(boundingRect().center().x()); C.setX(bounds.center().x());
C.setY(boundingRect().top()); C.setY(bounds.top());
A.setX(boundingRect().left()); A.setX(bounds.left());
A.setY(boundingRect().top() + boundingRect().width() / 2); A.setY(bounds.top() + bounds.width() / 2);
B.setX(boundingRect().right()); B.setX(bounds.right());
B.setY(boundingRect().top() + boundingRect().width() / 2); B.setY(bounds.top() + bounds.width() / 2);
break; break;
default: default:
break; break;
} }
setItemsPos();
setPath(determinePath());
} }
/* /*
@ -142,39 +156,49 @@ QPainterPath UBGraphicsAristo::determinePath()
QPolygonF polygon; QPolygonF polygon;
polygon << A << B << C; polygon << A << B << C;
path.addPolygon(polygon);
path.addPath(mResizeSvgItem->shape().translated(mResizeSvgItem->pos())); qreal rotationAngle = mOrientation == Bottom ? mMarkerAngle : mMarkerAngle - 360 * (int)(mMarkerAngle / 360);
path.addPath(mMarkerSvgItem->shape().translated(mMarkerSvgItem->pos())); QTransform t;
t.rotate(rotationAngle);
bool markerIsInPolygon = true;
if ((mOrientation == Bottom && mMarkerAngle < 90) || (mOrientation == Top && mMarkerAngle < 270))
markerIsInPolygon = polygon.containsPoint(t.map(markerButtonRect().topLeft()) + rotationCenter(), Qt::OddEvenFill);
else
markerIsInPolygon = polygon.containsPoint(t.map(markerButtonRect().bottomLeft()) + rotationCenter(), Qt::OddEvenFill);
return path; path.moveTo(A);
}
/* QRectF mappedRect = t.mapRect(markerButtonRect());
* setBoundingRect() is a helper to set the given rectangle as the new shape to limit apexes coordinates. path.lineTo(QPointF(mappedRect.translated(rotationCenter()).left(), A.y()));
* This is useful when instanciating or resizing the object. if (!markerIsInPolygon) {
* makeGeometryChange() is called so points are recomputed, control items are positionnated and shape is determined according to this modification. if (mOrientation == Top) {
* Setting bounds' width less than 300 is not allowed. path.lineTo(QPointF(mappedRect.translated(rotationCenter()).left(), mappedRect.translated(rotationCenter()).bottom()));
*/ path.lineTo(QPointF(mappedRect.translated(rotationCenter()).right(), mappedRect.translated(rotationCenter()).bottom()));
void UBGraphicsAristo::setBoundingRect(QRectF boundingRect) }
{ else if (mOrientation == Bottom) {
if (boundingRect.width() < 300) path.lineTo(QPointF(mappedRect.translated(rotationCenter()).left(), mappedRect.translated(rotationCenter()).top()));
return; path.lineTo(QPointF(mappedRect.translated(rotationCenter()).right(), mappedRect.translated(rotationCenter()).top()));
}
}
path.lineTo(QPointF(mappedRect.translated(rotationCenter()).right(), A.y()));
QPainterPath path; path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).left(), A.y()));
path.addRect(boundingRect); if (mOrientation == Top) {
setPath(path); path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).left(), resizeButtonRect().translated(rotationCenter()).bottom()));
if (mOrientation != Undefined) path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).right(), resizeButtonRect().translated(rotationCenter()).bottom()));
makeGeometryChange(); }
} else if (mOrientation == Bottom) {
path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).left(), resizeButtonRect().translated(rotationCenter()).top()));
path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).right(), resizeButtonRect().translated(rotationCenter()).top()));
}
path.lineTo(QPointF(resizeButtonRect().translated(rotationCenter()).right(), A.y()));
void UBGraphicsAristo::makeGeometryChange() path.lineTo(B);
{ path.lineTo(C);
calculatePoints(); path.lineTo(A);
setItemsPos();
setPath(determinePath());
}
return path;
}
UBItem* UBGraphicsAristo::deepCopy(void) const UBItem* UBGraphicsAristo::deepCopy(void) const
{ {
@ -191,7 +215,6 @@ void UBGraphicsAristo::copyItemParameters(UBItem *copy) const
/* TODO: copy all members */ /* TODO: copy all members */
cp->setPos(this->pos()); cp->setPos(this->pos());
cp->setTransform(this->transform()); cp->setTransform(this->transform());
cp->setBoundingRect(boundingRect());
cp->setOrientation(mOrientation); cp->setOrientation(mOrientation);
cp->mRotatedAngle = mRotatedAngle; cp->mRotatedAngle = mRotatedAngle;
cp->mMarkerAngle = mMarkerAngle; cp->mMarkerAngle = mMarkerAngle;
@ -501,7 +524,8 @@ QRectF UBGraphicsAristo::hFlipRect() const
QRectF UBGraphicsAristo::markerButtonRect() const QRectF UBGraphicsAristo::markerButtonRect() const
{ {
return QRectF (radius()/2 - mMarkerSvgItem->boundingRect().width(), - mMarkerSvgItem->boundingRect().height()/2, mMarkerSvgItem->boundingRect().width(), mMarkerSvgItem->boundingRect().height()); qreal y = - mMarkerSvgItem->boundingRect().height()/2;
return QRectF (radius()/2 - mMarkerSvgItem->boundingRect().width(), y, mMarkerSvgItem->boundingRect().width(), mMarkerSvgItem->boundingRect().height());
} }
QRectF UBGraphicsAristo::resizeButtonRect() const QRectF UBGraphicsAristo::resizeButtonRect() const
@ -580,7 +604,7 @@ void UBGraphicsAristo::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
} }
else if (mResizing) { else if (mResizing) {
QPointF delta = event->pos() - event->lastPos(); QPointF delta = event->pos() - event->lastPos();
setBoundingRect(QRectF(boundingRect().topLeft(), QSizeF(boundingRect().width() + delta.x(), boundingRect().height() + delta.x()))); calculatePoints(QRectF(boundingRect().topLeft(), QSizeF(boundingRect().width() + delta.x(), boundingRect().height() + delta.x())));
} }
else if(mMarking) { else if(mMarking) {
qreal angle = currentLine.angleTo(lastLine); qreal angle = currentLine.angleTo(lastLine);
@ -611,6 +635,8 @@ void UBGraphicsAristo::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
if (mResizing || mRotating || mMarking) if (mResizing || mRotating || mMarking)
{ {
if (mMarking)
setPath(determinePath());
mResizing = false; mResizing = false;
mRotating = false; mRotating = false;
mMarking = false; mMarking = false;
@ -623,9 +649,7 @@ void UBGraphicsAristo::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
hide(); hide();
emit hidden(); emit hidden();
break; break;
case HorizontalFlip: case HorizontalFlip:
/* substracting difference to zero [2pi] twice, to obtain the desired angle */
mMarkerAngle -= 2 * (mMarkerAngle - (int)(mMarkerAngle/360)*360) - 360;
/* setting new orientation */ /* setting new orientation */
switch(mOrientation) { switch(mOrientation) {
case Bottom: case Bottom:

@ -66,8 +66,7 @@ public:
Undefined Undefined
}; };
void setOrientation(Orientation orientation); void setOrientation(Orientation orientation);
void setBoundingRect(QRectF boundingRect);
virtual UBItem* deepCopy() const; virtual UBItem* deepCopy() const;
virtual void copyItemParameters(UBItem *copy) const; virtual void copyItemParameters(UBItem *copy) const;
@ -113,10 +112,9 @@ private:
QTransform calculateRotationTransform(); QTransform calculateRotationTransform();
void rotateAroundCenter(QTransform& transform, QPointF center); void rotateAroundCenter(QTransform& transform, QPointF center);
void calculatePoints(); void calculatePoints(QRectF bounds);
QPainterPath determinePath(); QPainterPath determinePath();
void setItemsPos(); void setItemsPos();
void makeGeometryChange();
QBrush fillBrush() const; QBrush fillBrush() const;
void paintGraduations(QPainter *painter); void paintGraduations(QPainter *painter);

Loading…
Cancel
Save