Fixed, improved, cleaned and commented aristo tool.

preferencesAboutTextFull
Guillaume Burel 12 years ago
parent a74b7ca134
commit 519c39c843
  1. 399
      src/tools/UBGraphicsAristo.cpp
  2. 266
      src/tools/UBGraphicsAristo.h

@ -13,37 +13,42 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QGraphicsPolygonItem> #include "UBGraphicsAristo.h"
#include <QPolygonF>
#include "tools/UBGraphicsAristo.h"
#include "core/UBApplication.h"
#include "board/UBBoardController.h" #include "board/UBBoardController.h"
#include "board/UBDrawingController.h" #include "board/UBDrawingController.h"
#include "core/UBApplication.h"
#include "domain/UBGraphicsScene.h" #include "domain/UBGraphicsScene.h"
#include <QColor>
#include <QFont>
#include <QFontMetricsF>
#include <QGraphicsItem>
#include <QLineF>
#include <QPolygonF>
#include <QRadialGradient>
#include <QString>
#include "core/memcheck.h" #include "core/memcheck.h"
const QRect UBGraphicsAristo::sDefaultRect = QRect(0, 0, 800, 400); const QRectF UBGraphicsAristo::sDefaultRect = QRectF(0, 0, 800, 500);
const UBGraphicsAristo::UBGraphicsAristoOrientation UBGraphicsAristo::sDefaultOrientation = UBGraphicsAristo::Bottom; const UBGraphicsAristo::Orientation UBGraphicsAristo::sDefaultOrientation = UBGraphicsAristo::Bottom;
UBGraphicsAristo::UBGraphicsAristo() UBGraphicsAristo::UBGraphicsAristo()
: UBAbstractDrawRuler() : UBAbstractDrawRuler()
, QGraphicsPolygonItem() , QGraphicsPathItem()
, angle(0) , mMarking(false)
, mResizing(false) , mResizing(false)
, mRotating(false) , mRotating(false)
, mMarking(false) , mOrientation(Undefined)
, mSpan(180) , mAngle(0)
, mStartAngle(0)
, mCurrentAngle(0) , mCurrentAngle(0)
, mStartAngle(0)
, mSpan(180)
, mHFlipSvgItem(0)
, mMarkerSvgItem(0)
, mResizeSvgItem(0)
, mRotateSvgItem(0)
{ {
setRect(sDefaultRect, sDefaultOrientation);
create(*this);
mHFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this); mHFlipSvgItem = new QGraphicsSvgItem(":/images/vflipTool.svg", this);
mHFlipSvgItem->setVisible(false); mHFlipSvgItem->setVisible(false);
mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mHFlipSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
@ -59,75 +64,138 @@ UBGraphicsAristo::UBGraphicsAristo()
mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this); mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this);
mMarkerSvgItem->setVisible(false); mMarkerSvgItem->setVisible(false);
mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool));
mMarkerSvgItem->setVisible(true);
create(*this);
setBoundingRect(sDefaultRect);
setOrientation(sDefaultOrientation);
} }
UBGraphicsAristo::~UBGraphicsAristo() UBGraphicsAristo::~UBGraphicsAristo()
{ {
/* NOOP */
} }
UBItem* UBGraphicsAristo::deepCopy(void) const /*
* setOrientation() modify the tool orientation.
* makeGeometryChange() is called so points are recomputed, control items are positionnated and shape is determined according to this modification.
*/
void UBGraphicsAristo::setOrientation(Orientation orientation)
{ {
UBGraphicsAristo* copy = new UBGraphicsAristo(); mOrientation = orientation;
copyItemParameters(copy); makeGeometryChange();
return copy;
} }
void UBGraphicsAristo::copyItemParameters(UBItem *copy) const /* calculatePoints() is used to calculate polygon's apexes coordinates.
* This function handles orientation changes too.
*/
void UBGraphicsAristo::calculatePoints()
{ {
UBGraphicsAristo* cp = dynamic_cast<UBGraphicsAristo*>(copy); switch (mOrientation) {
if (cp) case Bottom:
{ C.setX(boundingRect().center().x());
/* TODO: copy all members */ C.setY(boundingRect().bottom());
cp->setPos(this->pos());
cp->setPolygon(this->polygon()); A.setX(boundingRect().left());
cp->setTransform(this->transform()); A.setY(boundingRect().bottom() - boundingRect().width() / 2);
B.setX(boundingRect().right());
B.setY(boundingRect().bottom() - boundingRect().width() / 2);
break;
case Top:
C.setX(boundingRect().center().x());
C.setY(boundingRect().top());
A.setX(boundingRect().left());
A.setY(boundingRect().top() + boundingRect().width() / 2);
B.setX(boundingRect().right());
B.setY(boundingRect().top() + boundingRect().width() / 2);
break;
default:
break;
} }
} }
void UBGraphicsAristo::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsAristoOrientation orientation) /*
* setItemsPos() places control items according to A, B and C positions.
* Call this function after A, B or C position modification, mostly after calling calculatePoints().
* These positions has to be set when calling setPath() to allow hover events on items which are not into the main polygon.
*/
void UBGraphicsAristo::setItemsPos()
{ {
QPolygonF polygon; mCloseSvgItem->setPos(closeButtonRect().topLeft() + rotationCenter());
polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h); mHFlipSvgItem->setPos(hFlipRect().topLeft() + rotationCenter());
setPolygon(polygon); mRotateSvgItem->setPos(rotateRect().topLeft() + rotationCenter());
mResizeSvgItem->setPos(resizeButtonRect().topLeft() + rotationCenter());
setOrientation(orientation); mMarkerSvgItem->setPos(markerButtonRect().topLeft() + rotationCenter());
} }
void UBGraphicsAristo::setOrientation(UBGraphicsAristoOrientation orientation) /*
* determinePath() modify the shape according to apexes coordinates and control item positions.
* This is useful when orientation is modified.
* Returns the painter path corresponding to object parameters.
*/
QPainterPath UBGraphicsAristo::determinePath()
{ {
mOrientation = orientation; QPainterPath path;
calculatePoints(rect());
QPolygonF polygon; QPolygonF polygon;
polygon << A << B << C; polygon << A << B << C;
setPolygon(polygon); path.addPolygon(polygon);
path.addPath(mResizeSvgItem->shape().translated(mResizeSvgItem->pos()));
path.addPath(mMarkerSvgItem->shape().translated(mMarkerSvgItem->pos()));
return path;
} }
UBGraphicsScene* UBGraphicsAristo::scene() const /*
* setBoundingRect() is a helper to set the given rectangle as the new shape to limit apexes coordinates.
* This is useful when instanciating or resizing the object.
* makeGeometryChange() is called so points are recomputed, control items are positionnated and shape is determined according to this modification.
* Setting bounds' width less than 300 is not allowed.
*/
void UBGraphicsAristo::setBoundingRect(QRectF boundingRect)
{ {
return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene()); if (boundingRect.width() < 300)
return;
QPainterPath path;
path.addRect(boundingRect);
setPath(path);
if (mOrientation != Undefined)
makeGeometryChange();
} }
/* calculatePoints() is used to calculate polygon's apexes coordinates. void UBGraphicsAristo::makeGeometryChange()
* This function handles orientation changes too. {
*/ calculatePoints();
void UBGraphicsAristo::calculatePoints(const QRectF& r) setItemsPos();
setPath(determinePath());
}
UBItem* UBGraphicsAristo::deepCopy(void) const
{
UBGraphicsAristo* copy = new UBGraphicsAristo();
copyItemParameters(copy);
return copy;
}
void UBGraphicsAristo::copyItemParameters(UBItem *copy) const
{ {
switch(mOrientation) UBGraphicsAristo* cp = dynamic_cast<UBGraphicsAristo*>(copy);
if (cp)
{ {
case Bottom: /* TODO: copy all members */
A.setX(r.left()); A.setY(r.top()); cp->setPos(this->pos());
B.setX(r.right()); B.setY(r.top()); cp->setPath(this->path());
C.setX(r.center().x()); C.setY(A.y() + r.width() / 2); cp->setTransform(this->transform());
break;
case Top:
A.setX(r.left()); A.setY(r.bottom());
B.setX(r.right()); B.setY(r.bottom());
C.setX(r.center().x()); C.setY(A.y() - r.width() / 2);
break;
} }
} }
void UBGraphicsAristo::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) void UBGraphicsAristo::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{ {
QPolygonF polygon; QPolygonF polygon;
@ -140,82 +208,19 @@ void UBGraphicsAristo::paint(QPainter *painter, const QStyleOptionGraphicsItem *
polygon.clear(); polygon.clear();
paintGraduations(painter); paintGraduations(painter);
mCloseSvgItem->setPos(closeButtonRect().topLeft() + rotationCenter());
mHFlipSvgItem->setPos(hFlipRect().topLeft() + rotationCenter());
mRotateSvgItem->setPos(rotateRect().topLeft() + rotationCenter());
mResizeSvgItem->setPos(resizeButtonRect().topLeft() + rotationCenter());
paintMarker(painter); paintMarker(painter);
mMarkerSvgItem->setVisible(true);
}
QPainterPath UBGraphicsAristo::shape() const
{
QPainterPath tShape;
QPolygonF tPolygon;
tPolygon << A << B << C;
tShape.addPolygon(tPolygon);
tPolygon.clear();
return tShape;
} }
/* paintMarker() adjust marker button according to the current angle, draw the line allowing user to set precisely the angle, and draw the current angle's value. */ QBrush UBGraphicsAristo::fillBrush() const
void UBGraphicsAristo::paintMarker(QPainter *painter)
{ {
/* adjusting marker button */ QColor fillColor = edgeFillColor();// scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor;
mMarkerSvgItem->setPos(markerButtonRect().topLeft() + rotationCenter()); QColor fillColorCenter = middleFillColor();//scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter;
mMarkerSvgItem->resetTransform(); QColor transparentWhite = Qt::white;
mMarkerSvgItem->translate(-markerButtonRect().left(), -markerButtonRect().top()); transparentWhite.setAlpha(scene()->isDarkBackground() ? sDrawTransparency : sFillTransparency);
mMarkerSvgItem->rotate(mCurrentAngle); QRadialGradient radialGradient(boundingRect().center(), radius(), boundingRect().center());
mMarkerSvgItem->translate(markerButtonRect().left(), markerButtonRect().top()); radialGradient.setColorAt(0, fillColorCenter);
radialGradient.setColorAt(1, fillColor);
return radialGradient;
qreal co = cos((mCurrentAngle) * PI/180);
qreal si = sin((mCurrentAngle) * PI/180);
/* Setting point composing the line (from point C) which intersects the line we want to draw. */
QPointF referencePoint;
if (mOrientation == Bottom) {
if ((int)mCurrentAngle % 360 < 90)
referencePoint = B;
else
referencePoint = A;
}
else if (mOrientation == Top) {
if ((int)mCurrentAngle % 360 < 270 && (int)mCurrentAngle % 360 > 0)
referencePoint = A;
else
referencePoint = B;
}
/* getting intersection point to draw the wanted line */
QLineF intersectedLine(rotationCenter(), QPointF(rotationCenter().x()+co, rotationCenter().y()+si));
QPointF intersectionPoint;
if (intersectedLine.intersect(QLineF(referencePoint, C), &intersectionPoint))
painter->drawLine(QLineF(intersectionPoint, rotationCenter()));
/* drawing angle value */
qreal rightAngle = mOrientation == Bottom ? mCurrentAngle : 360 - mCurrentAngle;
QString angleText = QString("%1°").arg(rightAngle, 0, 'f', 1);
QFont font1 = painter->font();
#ifdef Q_WS_MAC
font1.setPointSizeF(font1.pointSizeF() - 3);
#endif
QFontMetricsF fm1(font1);
if (mOrientation == Bottom)
painter->drawText(rotationCenter().x() - fm1.width(angleText)/2 - radius()/8, rotationCenter().y() + radius()/8 - fm1.height()/2, fm1.width(angleText), fm1.height(), Qt::AlignCenter, angleText);
else
painter->drawText(rotationCenter().x() - fm1.width(angleText)/2 - radius()/8, rotationCenter().y() - radius()/8 - fm1.height()/2, fm1.width(angleText), fm1.height(), Qt::AlignCenter, angleText);
} }
/* paintGraduations() paints graduations on the ruler side (length graduations) and the two other sides (angle graduation) */ /* paintGraduations() paints graduations on the ruler side (length graduations) and the two other sides (angle graduation) */
@ -228,18 +233,18 @@ void UBGraphicsAristo::paintGraduations(QPainter *painter)
void UBGraphicsAristo::paintRulerGraduations(QPainter *painter) void UBGraphicsAristo::paintRulerGraduations(QPainter *painter)
{ {
/* defining useful constants */ /* defining useful constants */
const int centimeterGraduationHeight = 15; const int centimeterGraduationHeight = 15;
const int halfCentimeterGraduationHeight = 10; const int halfCentimeterGraduationHeight = 10;
const int millimeterGraduationHeight = 5; const int millimeterGraduationHeight = 5;
const int millimetersPerCentimeter = 10; const int millimetersPerCentimeter = 10;
const int millimetersPerHalfCentimeter = 5; const int millimetersPerHalfCentimeter = 5;
painter->save(); painter->save();
painter->setFont(font()); painter->setFont(font());
QFontMetricsF fontMetrics(painter->font()); QFontMetricsF fontMetrics(painter->font());
/* Browsing milliters in half width of ruler side */ /* Browsing milliters in half width of ruler side */
for (int millimeters = 0; millimeters < (rect().width() / 2 - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++) for (int millimeters = 0; millimeters < (boundingRect().width() / 2 - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
{ {
/* defining graduationHeight ; values are different to draw bigger lines if millimiter considered is a centimeter or a half centimeter */ /* defining graduationHeight ; values are different to draw bigger lines if millimiter considered is a centimeter or a half centimeter */
int graduationHeight = (0 == millimeters % millimetersPerCentimeter) ? int graduationHeight = (0 == millimeters % millimetersPerCentimeter) ?
@ -262,13 +267,13 @@ void UBGraphicsAristo::paintRulerGraduations(QPainter *painter)
QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter)); QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter));
/* staying inside polygon */ /* staying inside polygon */
if (rotationCenter().x() + sPixelsPerMillimeter * millimeters + fontMetrics.width(text) / 2 < rect().right()) if (rotationCenter().x() + sPixelsPerMillimeter * millimeters + fontMetrics.width(text) / 2 < boundingRect().right())
{ {
qreal textWidth = fontMetrics.width(text); qreal textWidth = fontMetrics.width(text);
qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5; qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;
/* text y-coordinate is different according to tool's orientation */ /* text y-coordinate is different according to tool's orientation */
qreal textY = mOrientation == Bottom ? rect().top() + 5 + centimeterGraduationHeight : rect().bottom() - 5 - centimeterGraduationHeight + graduationHeight; qreal textY = mOrientation == Bottom ? A.y() + 5 + centimeterGraduationHeight : A.y() - 5 - centimeterGraduationHeight + graduationHeight;
/* if text's rect is not out of polygon's bounds, drawing value below or above graduation */ /* if text's rect is not out of polygon's bounds, drawing value below or above graduation */
QPointF intersectionPoint; QPointF intersectionPoint;
@ -390,32 +395,73 @@ void UBGraphicsAristo::paintProtractorGraduations(QPainter* painter)
painter->restore(); painter->restore();
} }
QBrush UBGraphicsAristo::fillBrush() const /* paintMarker() adjust marker button according to the current angle, draw the line allowing user to set precisely the angle, and draw the current angle's value. */
void UBGraphicsAristo::paintMarker(QPainter *painter)
{ {
QColor fillColor = edgeFillColor();// scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor; /* adjusting marker button */
QColor fillColorCenter = middleFillColor();//scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter; mMarkerSvgItem->resetTransform();
QColor transparentWhite = Qt::white; mMarkerSvgItem->translate(-markerButtonRect().left(), -markerButtonRect().top());
transparentWhite.setAlpha(scene()->isDarkBackground() ? sDrawTransparency : sFillTransparency); mMarkerSvgItem->rotate(mCurrentAngle);
QRadialGradient radialGradient(rect().center(), radius(), rect().center()); mMarkerSvgItem->translate(markerButtonRect().left(), markerButtonRect().top());
radialGradient.setColorAt(0, fillColorCenter);
radialGradient.setColorAt(1, fillColor);
return radialGradient; qreal co = cos((mCurrentAngle) * PI/180);
qreal si = sin((mCurrentAngle) * PI/180);
/* Setting point composing the line (from point C) which intersects the line we want to draw. */
QPointF referencePoint;
if (mOrientation == Bottom) {
if ((int)mCurrentAngle % 360 < 90)
referencePoint = B;
else
referencePoint = A;
}
else if (mOrientation == Top) {
if ((int)mCurrentAngle % 360 < 270 && (int)mCurrentAngle % 360 > 0)
referencePoint = A;
else
referencePoint = B;
}
/* getting intersection point to draw the wanted line */
QLineF intersectedLine(rotationCenter(), QPointF(rotationCenter().x()+co, rotationCenter().y()+si));
QPointF intersectionPoint;
if (intersectedLine.intersect(QLineF(referencePoint, C), &intersectionPoint))
painter->drawLine(QLineF(intersectionPoint, rotationCenter()));
/* drawing angle value */
qreal rightAngle = mOrientation == Bottom ? mCurrentAngle : 360 - mCurrentAngle;
QString angleText = QString("%1°").arg(rightAngle, 0, 'f', 1);
QFont font1 = painter->font();
#ifdef Q_WS_MAC
font1.setPointSizeF(font1.pointSizeF() - 3);
#endif
QFontMetricsF fm1(font1);
if (mOrientation == Bottom)
painter->drawText(rotationCenter().x() - fm1.width(angleText)/2 - radius()/8, rotationCenter().y() + radius()/8 - fm1.height()/2, fm1.width(angleText), fm1.height(), Qt::AlignCenter, angleText);
else
painter->drawText(rotationCenter().x() - fm1.width(angleText)/2 - radius()/8, rotationCenter().y() - radius()/8 - fm1.height()/2, fm1.width(angleText), fm1.height(), Qt::AlignCenter, angleText);
} }
void UBGraphicsAristo::rotateAroundCenter(qreal angle) void UBGraphicsAristo::rotateAroundCenter(qreal angle)
{ {
qreal oldAngle = this->angle; qreal oldAngle = mAngle;
this->angle = angle; mAngle = angle;
QTransform transform; QTransform transform;
rotateAroundCenter(transform, rotationCenter()); rotateAroundCenter(transform, rotationCenter());
setTransform(transform, true); setTransform(transform, true);
this->angle = oldAngle + angle; // We have to store absolute value for FLIP case mAngle = oldAngle + angle; // We have to store absolute value for FLIP case
} }
void UBGraphicsAristo::rotateAroundCenter(QTransform& transform, QPointF center) void UBGraphicsAristo::rotateAroundCenter(QTransform& transform, QPointF center)
{ {
transform.translate(center.x(), center.y()); transform.translate(center.x(), center.y());
transform.rotate(angle); transform.rotate(mAngle);
transform.translate(- center.x(), - center.y()); transform.translate(- center.x(), - center.y());
} }
@ -441,11 +487,6 @@ QRectF UBGraphicsAristo::closeButtonRect() const
return QRectF(- mCloseSvgItem->boundingRect().width() / 2, y, mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().height()); return QRectF(- mCloseSvgItem->boundingRect().width() / 2, y, mCloseSvgItem->boundingRect().width(), mCloseSvgItem->boundingRect().height());
} }
QRectF UBGraphicsAristo::resizeButtonRect() const
{
return QRectF((B - rotationCenter()).x() - 100 - mResizeSvgItem->boundingRect().width()/2, - mResizeSvgItem->boundingRect().height()/2, mResizeSvgItem->boundingRect().width(), mResizeSvgItem->boundingRect().height());
}
QRectF UBGraphicsAristo::hFlipRect() const QRectF UBGraphicsAristo::hFlipRect() const
{ {
qreal y = radius() / 4; qreal y = radius() / 4;
@ -455,6 +496,16 @@ QRectF UBGraphicsAristo::hFlipRect() const
return QRectF(- mHFlipSvgItem->boundingRect().width() / 2, y, mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().height()); return QRectF(- mHFlipSvgItem->boundingRect().width() / 2, y, mHFlipSvgItem->boundingRect().width(), mHFlipSvgItem->boundingRect().height());
} }
QRectF UBGraphicsAristo::markerButtonRect() const
{
return QRectF (radius()/2 - mMarkerSvgItem->boundingRect().width(), - mMarkerSvgItem->boundingRect().height()/2, mMarkerSvgItem->boundingRect().width(), mMarkerSvgItem->boundingRect().height());
}
QRectF UBGraphicsAristo::resizeButtonRect() const
{
return QRectF((B - rotationCenter()).x() - 100 - mResizeSvgItem->boundingRect().width()/2, - mResizeSvgItem->boundingRect().height()/2, mResizeSvgItem->boundingRect().width(), mResizeSvgItem->boundingRect().height());
}
QRectF UBGraphicsAristo::rotateRect() const QRectF UBGraphicsAristo::rotateRect() const
{ {
qreal y = radius() / 4 + hFlipRect().height() + 3; qreal y = radius() / 4 + hFlipRect().height() + 3;
@ -464,26 +515,22 @@ QRectF UBGraphicsAristo::rotateRect() const
} }
QRectF UBGraphicsAristo::markerButtonRect() const
{
return QRectF (radius()/2 - mMarkerSvgItem->boundingRect().width(), - mMarkerSvgItem->boundingRect().height()/2, mMarkerSvgItem->boundingRect().width(), mMarkerSvgItem->boundingRect().height());
}
QCursor UBGraphicsAristo::flipCursor() const QCursor UBGraphicsAristo::flipCursor() const
{ {
return Qt::ArrowCursor; return Qt::ArrowCursor;
} }
QCursor UBGraphicsAristo::resizeCursor() const QCursor UBGraphicsAristo::markerCursor() const
{ {
return Qt::ArrowCursor; return Qt::ArrowCursor;
} }
QCursor UBGraphicsAristo::markerCursor() const QCursor UBGraphicsAristo::resizeCursor() const
{ {
return Qt::ArrowCursor; return Qt::ArrowCursor;
} }
void UBGraphicsAristo::mousePressEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsAristo::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
switch (toolFromPos(event->pos())) { switch (toolFromPos(event->pos())) {
@ -530,7 +577,7 @@ void UBGraphicsAristo::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
} }
else if (mResizing) { else if (mResizing) {
QPointF delta = event->pos() - event->lastPos(); QPointF delta = event->pos() - event->lastPos();
setRect(QRectF(rect().topLeft(), QSizeF(rect().width() + delta.x(), rect().height() + delta.x() / 2)), mOrientation); setBoundingRect(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);
@ -584,6 +631,8 @@ void UBGraphicsAristo::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
case Top: case Top:
setOrientation(Bottom); setOrientation(Bottom);
break; break;
default:
break;
} }
default: default:
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
@ -700,7 +749,7 @@ UBGraphicsAristo::Tool UBGraphicsAristo::toolFromPos(QPointF pos)
{ {
pos = pos - rotationCenter(); pos = pos - rotationCenter();
qreal rotationAngle = mOrientation == Bottom ? - mCurrentAngle : 360 * (int)(mCurrentAngle / 360 + 1) - mCurrentAngle; qreal rotationAngle = mOrientation == Bottom ? - mCurrentAngle : Top ? 360 * (int)(mCurrentAngle / 360 + 1) - mCurrentAngle : 0;
QTransform t; QTransform t;
t.rotate(rotationAngle); t.rotate(rotationAngle);
@ -722,6 +771,7 @@ UBGraphicsAristo::Tool UBGraphicsAristo::toolFromPos(QPointF pos)
return None; return None;
} }
void UBGraphicsAristo::StartLine(const QPointF &scenePos, qreal width) void UBGraphicsAristo::StartLine(const QPointF &scenePos, qreal width)
{ {
QPointF itemPos = mapFromScene(scenePos); QPointF itemPos = mapFromScene(scenePos);
@ -730,10 +780,10 @@ void UBGraphicsAristo::StartLine(const QPointF &scenePos, qreal width)
y = rotationCenter().y(); y = rotationCenter().y();
if (itemPos.x() < rect().x() + sLeftEdgeMargin) if (itemPos.x() < boundingRect().x() + sLeftEdgeMargin)
itemPos.setX(rect().x() + sLeftEdgeMargin); itemPos.setX(boundingRect().x() + sLeftEdgeMargin);
if (itemPos.x() > rect().x() + rect().width() - sLeftEdgeMargin) if (itemPos.x() > boundingRect().x() + boundingRect().width() - sLeftEdgeMargin)
itemPos.setX(rect().x() + rect().width() - sLeftEdgeMargin); itemPos.setX(boundingRect().x() + boundingRect().width() - sLeftEdgeMargin);
itemPos.setY(y); itemPos.setY(y);
itemPos = mapToScene(itemPos); itemPos = mapToScene(itemPos);
@ -750,10 +800,10 @@ void UBGraphicsAristo::DrawLine(const QPointF &scenePos, qreal width)
y = rotationCenter().y(); y = rotationCenter().y();
if (itemPos.x() < rect().x() + sLeftEdgeMargin) if (itemPos.x() < boundingRect().x() + sLeftEdgeMargin)
itemPos.setX(rect().x() + sLeftEdgeMargin); itemPos.setX(boundingRect().x() + sLeftEdgeMargin);
if (itemPos.x() > rect().x() + rect().width() - sLeftEdgeMargin) if (itemPos.x() > boundingRect().x() + boundingRect().width() - sLeftEdgeMargin)
itemPos.setX(rect().x() + rect().width() - sLeftEdgeMargin); itemPos.setX(boundingRect().x() + boundingRect().width() - sLeftEdgeMargin);
itemPos.setY(y); itemPos.setY(y);
itemPos = mapToScene(itemPos); itemPos = mapToScene(itemPos);
@ -765,4 +815,11 @@ void UBGraphicsAristo::DrawLine(const QPointF &scenePos, qreal width)
void UBGraphicsAristo::EndLine() void UBGraphicsAristo::EndLine()
{ {
/* NOOP */
}
UBGraphicsScene* UBGraphicsAristo::scene() const
{
return static_cast<UBGraphicsScene*>(QGraphicsPathItem::scene());
} }

@ -16,150 +16,138 @@
#ifndef UBGRAPHICSARISTO_H_ #ifndef UBGRAPHICSARISTO_H_
#define UBGRAPHICSARISTO_H_ #define UBGRAPHICSARISTO_H_
#include <QtGui>
#include <QtSvg>
#include <QGraphicsPolygonItem>
#include "core/UB.h" #include "core/UB.h"
#include "domain/UBItem.h" #include "domain/UBItem.h"
#include "tools/UBAbstractDrawRuler.h" #include "tools/UBAbstractDrawRuler.h"
#include <QBrush>
class UBGraphicsScene; #include <QCursor>
class UBItem; #include <QGraphicsPathItem>
#include <QGraphicsSceneHoverEvent>
class UBGraphicsAristo : public UBAbstractDrawRuler, public QGraphicsPolygonItem, public UBItem #include <QGraphicsSceneMouseEvent>
#include <QGraphicsSvgItem>
#include <QPainter>
#include <QPainterPath>
#include <QPointF>
#include <QRectF>
#include <QStyleOptionGraphicsItem>
#include <QTransform>
#include <QWidget>
class UBGraphicsAristo : public UBAbstractDrawRuler, public QGraphicsPathItem, public UBItem
{ {
Q_OBJECT Q_OBJECT
public: public:
UBGraphicsAristo(); UBGraphicsAristo();
virtual ~UBGraphicsAristo(); virtual ~UBGraphicsAristo();
enum { Type = UBGraphicsItemType::AristoItemType }; enum {
enum Tool {None, Move, Resize, Rotate, Close, MoveMarker, HorizontalFlip}; Type = UBGraphicsItemType::AristoItemType
};
virtual int type() const
{ enum Tool {
return Type; None,
} Move,
Resize,
virtual UBItem* deepCopy(void) const; Rotate,
virtual void copyItemParameters(UBItem *copy) const; Close,
MoveMarker,
virtual void StartLine(const QPointF& scenePos, qreal width); HorizontalFlip
virtual void DrawLine(const QPointF& position, qreal width); };
virtual void EndLine();
enum Orientation
enum UBGraphicsAristoOrientation {
{ Bottom = 0,
Bottom = 0, Top,
Top Undefined
}; };
static UBGraphicsAristoOrientation orientationFromStr(QStringRef& str) void setOrientation(Orientation orientation);
{ void setBoundingRect(QRectF boundingRect);
if (str == "Bottom") return Bottom;
if (str == "Top") return Top; virtual UBItem* deepCopy() const;
return sDefaultOrientation; virtual void copyItemParameters(UBItem *copy) const;
}
static QString orientationToStr(UBGraphicsAristoOrientation orientation) virtual void StartLine(const QPointF& scenePos, qreal width);
{ virtual void DrawLine(const QPointF& position, qreal width);
QString result; virtual void EndLine();
if (orientation == 0) result = "Bottom";
else if (orientation == 1) result = "Top"; virtual int type() const
{
return result; return Type;
} }
UBGraphicsScene* scene() const;
void setRect(const QRectF &rect, UBGraphicsAristoOrientation orientation)
{ protected:
setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation); virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget);
}
virtual void rotateAroundCenter(qreal angle);
void setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsAristoOrientation orientation); virtual void resize(qreal factor);
void setOrientation(UBGraphicsAristoOrientation orientation); virtual QPointF rotationCenter() const;
UBGraphicsAristoOrientation getOrientation() const {return mOrientation;} virtual QRectF closeButtonRect() const;
QRectF hFlipRect() const;
QRectF rect() const {return boundingRect();} QRectF markerButtonRect() const;
QRectF resizeButtonRect () const;
UBGraphicsScene* scene() const; QRectF rotateRect() const;
protected: QCursor flipCursor() const;
QCursor markerCursor() const;
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget); QCursor resizeCursor() const;
virtual QPainterPath shape() const;
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void rotateAroundCenter(qreal angle); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void resize(qreal factor); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual QPointF rotationCenter() const; virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
virtual QRectF closeButtonRect() const;
QRectF resizeButtonRect () const; private:
QRectF hFlipRect() const; Tool toolFromPos(QPointF pos);
QRectF rotateRect() const;
QRectF markerButtonRect() const; QTransform calculateRotationTransform();
void rotateAroundCenter(QTransform& transform, QPointF center);
QCursor flipCursor() const;
QCursor resizeCursor() const; void calculatePoints();
QCursor markerCursor() const; QPainterPath determinePath();
void setItemsPos();
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); void makeGeometryChange();
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); QBrush fillBrush() const;
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void paintGraduations(QPainter *painter);
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void paintMarker(QPainter *painter);
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); void paintProtractorGraduations(QPainter* painter);
void paintRulerGraduations(QPainter *painter);
private:
UBGraphicsAristo::Tool toolFromPos(QPointF pos); inline qreal radius () const
QTransform calculateRotationTransform(); {
qreal angle; return sqrt(((B.x() - A.x())*(B.x() - A.x()))+((B.y() - A.y())*(B.y() - A.y()))) * 9 / 16 - 20;
}
void rotateAroundCenter(QTransform& transform, QPointF center);
bool mMarking;
bool mResizing; bool mResizing;
bool mRotating; bool mRotating;
bool mMarking;
QRect lastRect; Orientation mOrientation;
qreal mSpan;
qreal mAngle;
// Coordinates are transformed.... qreal mCurrentAngle;
QPoint lastPos; qreal mStartAngle;
QGraphicsSvgItem* mHFlipSvgItem;
QGraphicsSvgItem* mRotateSvgItem; qreal mSpan;
QGraphicsSvgItem* mResizeSvgItem;
QGraphicsSvgItem* mMarkerSvgItem; QGraphicsSvgItem* mHFlipSvgItem;
qreal mStartAngle; QGraphicsSvgItem* mMarkerSvgItem;
qreal mCurrentAngle; QGraphicsSvgItem* mResizeSvgItem;
QGraphicsSvgItem* mRotateSvgItem;
static const QRect sDefaultRect;
static const UBGraphicsAristoOrientation sDefaultOrientation; QPointF A, B, C;
void paintGraduations(QPainter *painter); static const int sArcAngleMargin = 5;
void paintRulerGraduations(QPainter *painter); static const Orientation sDefaultOrientation;
void paintProtractorGraduations(QPainter* painter); static const QRectF sDefaultRect;
void paintMarker(QPainter *painter);
inline qreal radius () const
{
return sqrt(((B.x() - A.x())*(B.x() - A.x()))+((B.y() - A.y())*(B.y() - A.y()))) * 9 / 16 - 20;
}
QBrush fillBrush() const;
UBGraphicsAristoOrientation mOrientation;
void calculatePoints(const QRectF& rect);
QPointF A, B, C;
static const int d = 70; // width of triangle border
static const int sArrowLength = 30;
static const int sMinWidth = 380;
static const int sMinHeight = 200;
static const int sArcAngleMargin = 5;
}; };
#endif /* UBGRAPHICSARISTO_H_ */ #endif /* UBGRAPHICSARISTO_H_ */

Loading…
Cancel
Save