Fixed moving and scaling grainline and ensured that it stays in bounds
--HG-- branch : feature
This commit is contained in:
parent
8e0dc15e25
commit
de799569ef
|
@ -31,12 +31,15 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
#include <QGraphicsSceneMouseEvent>
|
#include <QGraphicsSceneMouseEvent>
|
||||||
|
#include <QStyleOptionGraphicsItem>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include "../vmisc/def.h"
|
||||||
|
|
||||||
#include "vgrainlineitem.h"
|
#include "vgrainlineitem.h"
|
||||||
|
|
||||||
#define ARROW_ANGLE 0.5
|
#define ARROW_ANGLE 0.35
|
||||||
#define ARROW_LENGTH 0.05
|
#define ARROW_LENGTH 15
|
||||||
#define RECT_WIDTH 30
|
#define RECT_WIDTH 30
|
||||||
#define ACTIVE_Z 10
|
#define ACTIVE_Z 10
|
||||||
#define INACTIVE_Z 5
|
#define INACTIVE_Z 5
|
||||||
|
@ -47,9 +50,10 @@
|
||||||
* @param pParent pointer to the parent item
|
* @param pParent pointer to the parent item
|
||||||
*/
|
*/
|
||||||
VGrainlineItem::VGrainlineItem(QGraphicsItem* pParent)
|
VGrainlineItem::VGrainlineItem(QGraphicsItem* pParent)
|
||||||
:QGraphicsObject(pParent), m_eMode(mNormal), m_dRotation(0), m_dLength(0), m_rectBoundingBox(),
|
:QGraphicsObject(pParent), m_eMode(VGrainlineItem::mNormal), m_dRotation(0), m_dLength(0), m_rectBoundingBox(),
|
||||||
m_polyBound(), m_ptStartPos(), m_ptStartMove()
|
m_polyBound(), m_ptStartPos(), m_ptStartMove(), m_dScale(1)
|
||||||
{
|
{
|
||||||
|
m_rectBoundingBox.setTopLeft(QPointF(0, 0));
|
||||||
setAcceptHoverEvents(true);
|
setAcceptHoverEvents(true);
|
||||||
Reset();
|
Reset();
|
||||||
UpdateRectangle();
|
UpdateRectangle();
|
||||||
|
@ -76,34 +80,37 @@ void VGrainlineItem::paint(QPainter* pP, const QStyleOptionGraphicsItem* pOption
|
||||||
pP->save();
|
pP->save();
|
||||||
QColor clr = Qt::black;
|
QColor clr = Qt::black;
|
||||||
pP->setPen(QPen(clr, 3));
|
pP->setPen(QPen(clr, 3));
|
||||||
QPointF pt1 = pos();
|
QPointF pt1(0, 0);
|
||||||
QPointF pt2;
|
QPointF pt2;
|
||||||
|
|
||||||
pt2.setX(pt1.x() + m_dLength * cos(m_dRotation));
|
pt2.setX(pt1.x() + m_dLength * cos(m_dRotation));
|
||||||
pt2.setY(pt1.y() + m_dLength * sin(m_dRotation));
|
pt2.setY(pt1.y() + m_dLength * sin(m_dRotation));
|
||||||
|
|
||||||
pP->setRenderHints(QPainter::Antialiasing);
|
pP->setRenderHints(QPainter::Antialiasing);
|
||||||
|
// line
|
||||||
pP->drawLine(pt1, pt2);
|
pP->drawLine(pt1, pt2);
|
||||||
|
|
||||||
|
// first arrow
|
||||||
QPolygonF poly;
|
QPolygonF poly;
|
||||||
poly << pt1;
|
poly << pt1;
|
||||||
QPointF ptA;
|
QPointF ptA;
|
||||||
ptA.setX(pt1.x() + m_dLength*ARROW_LENGTH*cos(m_dRotation + ARROW_ANGLE));
|
qreal dArrLen = ARROW_LENGTH*m_dScale;
|
||||||
ptA.setY(pt1.y() + m_dLength*ARROW_LENGTH*sin(m_dRotation + ARROW_ANGLE));
|
ptA.setX(pt1.x() + dArrLen*cos(m_dRotation + ARROW_ANGLE));
|
||||||
|
ptA.setY(pt1.y() + dArrLen*sin(m_dRotation + ARROW_ANGLE));
|
||||||
poly << ptA;
|
poly << ptA;
|
||||||
ptA.setX(pt1.x() + m_dLength*ARROW_LENGTH*cos(m_dRotation - ARROW_ANGLE));
|
ptA.setX(pt1.x() + dArrLen*cos(m_dRotation - ARROW_ANGLE));
|
||||||
ptA.setY(pt1.y() + m_dLength*ARROW_LENGTH*sin(m_dRotation - ARROW_ANGLE));
|
ptA.setY(pt1.y() + dArrLen*sin(m_dRotation - ARROW_ANGLE));
|
||||||
poly << ptA;
|
poly << ptA;
|
||||||
pP->setBrush(clr);
|
pP->setBrush(clr);
|
||||||
pP->drawPolygon(poly);
|
pP->drawPolygon(poly);
|
||||||
|
// second arrow
|
||||||
poly.clear();
|
poly.clear();
|
||||||
poly << pt2;
|
poly << pt2;
|
||||||
ptA.setX(pt2.x() + m_dLength*ARROW_LENGTH*cos(M_PI + m_dRotation + ARROW_ANGLE));
|
ptA.setX(pt2.x() + dArrLen*cos(M_PI + m_dRotation + ARROW_ANGLE));
|
||||||
ptA.setY(pt2.y() + m_dLength*ARROW_LENGTH*sin(M_PI + m_dRotation + ARROW_ANGLE));
|
ptA.setY(pt2.y() + dArrLen*sin(M_PI + m_dRotation + ARROW_ANGLE));
|
||||||
poly << ptA;
|
poly << ptA;
|
||||||
ptA.setX(pt2.x() + m_dLength*ARROW_LENGTH*cos(M_PI + m_dRotation - ARROW_ANGLE));
|
ptA.setX(pt2.x() + dArrLen*cos(M_PI + m_dRotation - ARROW_ANGLE));
|
||||||
ptA.setY(pt2.y() + m_dLength*ARROW_LENGTH*sin(M_PI + m_dRotation - ARROW_ANGLE));
|
ptA.setY(pt2.y() + dArrLen*sin(M_PI + m_dRotation - ARROW_ANGLE));
|
||||||
poly << ptA;
|
poly << ptA;
|
||||||
pP->drawPolygon(poly);
|
pP->drawPolygon(poly);
|
||||||
|
|
||||||
|
@ -111,6 +118,7 @@ void VGrainlineItem::paint(QPainter* pP, const QStyleOptionGraphicsItem* pOption
|
||||||
{
|
{
|
||||||
pP->setPen(QPen(clr, 2, Qt::DashLine));
|
pP->setPen(QPen(clr, 2, Qt::DashLine));
|
||||||
pP->setBrush(Qt::NoBrush);
|
pP->setBrush(Qt::NoBrush);
|
||||||
|
// bounding polygon
|
||||||
pP->drawPolygon(m_polyBound);
|
pP->drawPolygon(m_polyBound);
|
||||||
}
|
}
|
||||||
pP->restore();
|
pP->restore();
|
||||||
|
@ -128,7 +136,15 @@ void VGrainlineItem::UpdateGeometry(const QPointF& ptPos, qreal dRotation, qreal
|
||||||
m_dRotation = qDegreesToRadians(dRotation);
|
m_dRotation = qDegreesToRadians(dRotation);
|
||||||
m_dLength = dLength;
|
m_dLength = dLength;
|
||||||
|
|
||||||
setPos(ptPos);
|
qreal dX;
|
||||||
|
qreal dY;
|
||||||
|
QPointF pt = ptPos;
|
||||||
|
if (IsContained(pt, dX, dY) == false)
|
||||||
|
{
|
||||||
|
pt.setX(pt.x() + dX);
|
||||||
|
pt.setY(pt.y() + dY);
|
||||||
|
}
|
||||||
|
setPos(pt);
|
||||||
UpdateRectangle();
|
UpdateRectangle();
|
||||||
UpdateBox();
|
UpdateBox();
|
||||||
}
|
}
|
||||||
|
@ -169,26 +185,74 @@ bool VGrainlineItem::IsIdle() const
|
||||||
* @brief VGrainlineItem::IsContained checks, if both ends of the grainline, starting at pt, are contained in
|
* @brief VGrainlineItem::IsContained checks, if both ends of the grainline, starting at pt, are contained in
|
||||||
* parent widget.
|
* parent widget.
|
||||||
* @param pt starting point of the grainline.
|
* @param pt starting point of the grainline.
|
||||||
|
* @param dX horizontal translation needed to put the arrow inside parent item
|
||||||
|
* @param dY vertical translation needed to put the arrow inside parent item
|
||||||
* @return true, if both ends of the grainline, starting at pt, are contained in the parent widget and
|
* @return true, if both ends of the grainline, starting at pt, are contained in the parent widget and
|
||||||
* false otherwise.
|
* false otherwise.
|
||||||
*/
|
*/
|
||||||
bool VGrainlineItem::IsContained(const QPointF& pt) const
|
bool VGrainlineItem::IsContained(const QPointF& pt, qreal &dX, qreal &dY) const
|
||||||
{
|
{
|
||||||
|
dX = 0;
|
||||||
|
dY = 0;
|
||||||
|
QPointF apt[2];
|
||||||
|
apt[0] = pt;
|
||||||
|
apt[1].setX(pt.x() + m_dLength * cos(m_dRotation));
|
||||||
|
apt[1].setY(pt.y() + m_dLength * sin(m_dRotation));
|
||||||
|
// single point differences
|
||||||
|
qreal dPtX;
|
||||||
|
qreal dPtY;
|
||||||
|
bool bInside = true;
|
||||||
|
|
||||||
QRectF rectParent = parentItem()->boundingRect();
|
QRectF rectParent = parentItem()->boundingRect();
|
||||||
if (rectParent.contains(pt) == false)
|
for (int i = 0; i < 2; ++i)
|
||||||
{
|
{
|
||||||
qDebug() << "First point out" << pt << rectParent;
|
dPtX = 0;
|
||||||
return false;
|
dPtY = 0;
|
||||||
|
if (rectParent.contains(apt[i]) == false)
|
||||||
|
{
|
||||||
|
if (apt[i].x() < rectParent.left())
|
||||||
|
{
|
||||||
|
dPtX = rectParent.left() - apt[i].x();
|
||||||
|
}
|
||||||
|
else if (apt[i].x() > rectParent.right())
|
||||||
|
{
|
||||||
|
dPtX = rectParent.right() - apt[i].x();
|
||||||
|
}
|
||||||
|
if (apt[i].y() < rectParent.top())
|
||||||
|
{
|
||||||
|
dPtY = rectParent.top() - apt[i].y();
|
||||||
|
}
|
||||||
|
else if (apt[i].y() > rectParent.bottom())
|
||||||
|
{
|
||||||
|
dPtY = rectParent.bottom() - apt[i].y();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fabs(dPtX) > fabs(dX))
|
||||||
|
{
|
||||||
|
dX = dPtX;
|
||||||
|
}
|
||||||
|
if (fabs(dPtY) > fabs(dY))
|
||||||
|
{
|
||||||
|
dY = dPtY;
|
||||||
|
}
|
||||||
|
|
||||||
|
bInside = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
QPointF ptA;
|
return bInside;
|
||||||
ptA.setX(pt.x() + m_dLength * cos(m_dRotation));
|
|
||||||
ptA.setY(pt.y() + m_dLength * sin(m_dRotation));
|
|
||||||
if (rectParent.contains(ptA) == false)
|
|
||||||
{
|
|
||||||
qDebug() << "Second point out" << ptA << rectParent;
|
|
||||||
}
|
|
||||||
return (rectParent.contains(ptA));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* @brief VGrainlineItem::SetScale sets the scale for keeping the arrows of constant size
|
||||||
|
* @param dScale scale factor
|
||||||
|
*/
|
||||||
|
void VGrainlineItem::SetScale(qreal dScale)
|
||||||
|
{
|
||||||
|
m_dScale = dScale;
|
||||||
|
UpdateBox();
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* @brief VGrainlineItem::mousePressEvent handles left button mouse press events
|
* @brief VGrainlineItem::mousePressEvent handles left button mouse press events
|
||||||
|
@ -203,6 +267,7 @@ void VGrainlineItem::mousePressEvent(QGraphicsSceneMouseEvent* pME)
|
||||||
m_ptStartPos = pos();
|
m_ptStartPos = pos();
|
||||||
m_ptStartMove = pME->scenePos();
|
m_ptStartMove = pME->scenePos();
|
||||||
UpdateBox();
|
UpdateBox();
|
||||||
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,13 +281,17 @@ void VGrainlineItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
|
||||||
if (m_eMode == mMove)
|
if (m_eMode == mMove)
|
||||||
{
|
{
|
||||||
QPointF ptDiff = pME->scenePos() - m_ptStartMove;
|
QPointF ptDiff = pME->scenePos() - m_ptStartMove;
|
||||||
QPointF pt = m_ptStartPos + ptDiff/2;
|
QPointF pt = m_ptStartPos + ptDiff;
|
||||||
if (IsContained(pt) == true)
|
qreal dX;
|
||||||
|
qreal dY;
|
||||||
|
if (IsContained(pt, dX, dY) == false)
|
||||||
{
|
{
|
||||||
setPos(pt);
|
pt.setX(pt.x() + dX);
|
||||||
UpdateRectangle();
|
pt.setY(pt.y() + dY);
|
||||||
UpdateBox();
|
|
||||||
}
|
}
|
||||||
|
setPos(pt);
|
||||||
|
UpdateRectangle();
|
||||||
|
UpdateBox();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,6 +314,7 @@ void VGrainlineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
|
||||||
emit SignalMoved(pos());
|
emit SignalMoved(pos());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RestoreOverrideCursor(cursorArrowCloseHand);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,7 +335,7 @@ void VGrainlineItem::UpdateBox()
|
||||||
void VGrainlineItem::UpdateRectangle()
|
void VGrainlineItem::UpdateRectangle()
|
||||||
{
|
{
|
||||||
m_polyBound.clear();
|
m_polyBound.clear();
|
||||||
QPointF pt1 = pos();
|
QPointF pt1(0, 0);
|
||||||
QPointF pt2;
|
QPointF pt2;
|
||||||
|
|
||||||
pt2.setX(pt1.x() + m_dLength * cos(m_dRotation));
|
pt2.setX(pt1.x() + m_dLength * cos(m_dRotation));
|
||||||
|
@ -285,4 +355,6 @@ void VGrainlineItem::UpdateRectangle()
|
||||||
ptA.setY(pt2.y() + RECT_WIDTH*sin(m_dRotation + M_PI/2));
|
ptA.setY(pt2.y() + RECT_WIDTH*sin(m_dRotation + M_PI/2));
|
||||||
m_polyBound << ptA;
|
m_polyBound << ptA;
|
||||||
m_rectBoundingBox = m_polyBound.boundingRect();
|
m_rectBoundingBox = m_polyBound.boundingRect();
|
||||||
|
setTransformOriginPoint(m_rectBoundingBox.center());
|
||||||
|
prepareGeometryChange();
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,8 @@ public:
|
||||||
QRectF boundingRect() const;
|
QRectF boundingRect() const;
|
||||||
void Reset();
|
void Reset();
|
||||||
bool IsIdle() const;
|
bool IsIdle() const;
|
||||||
bool IsContained(const QPointF &pt) const;
|
bool IsContained(const QPointF &pt, qreal &dX, qreal &dY) const;
|
||||||
|
void SetScale(qreal dScale);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void mousePressEvent(QGraphicsSceneMouseEvent* pME);
|
void mousePressEvent(QGraphicsSceneMouseEvent* pME);
|
||||||
|
@ -77,6 +78,7 @@ private:
|
||||||
QPolygonF m_polyBound;
|
QPolygonF m_polyBound;
|
||||||
QPointF m_ptStartPos;
|
QPointF m_ptStartPos;
|
||||||
QPointF m_ptStartMove;
|
QPointF m_ptStartMove;
|
||||||
|
qreal m_dScale;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VGRAINLINEITEM_H
|
#endif // VGRAINLINEITEM_H
|
||||||
|
|
|
@ -408,11 +408,20 @@ void VToolDetail::FullUpdateFromGuiOk(int result)
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* @brief VToolDetail::paint draws a bounding box around detail, if one of its text items is not idle.
|
* @brief VToolDetail::paint draws a bounding box around detail, if one of its text or grainline items is not idle.
|
||||||
*/
|
*/
|
||||||
void VToolDetail::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
void VToolDetail::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||||
{
|
{
|
||||||
if (dataLabel->IsIdle() == false || patternInfo->IsIdle() == false)
|
if (scene()->views().count() > 0)
|
||||||
|
{
|
||||||
|
QPoint pt0 = scene()->views().at(0)->mapFromScene(0, 0);
|
||||||
|
QPoint pt = scene()->views().at(0)->mapFromScene(0, 100);
|
||||||
|
qreal dScale = qSqrt(QPoint::dotProduct(pt - pt0, pt - pt0));
|
||||||
|
grainLine->SetScale(100/dScale);
|
||||||
|
qDebug() << "SCALE" << dScale << 10/dScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dataLabel->IsIdle() == false || patternInfo->IsIdle() == false || grainLine->IsIdle() == false)
|
||||||
{
|
{
|
||||||
painter->save();
|
painter->save();
|
||||||
painter->setPen(QPen(Qt::black, 3, Qt::DashLine));
|
painter->setPen(QPen(Qt::black, 3, Qt::DashLine));
|
||||||
|
@ -915,7 +924,8 @@ void VToolDetail::UpdateGrainline()
|
||||||
grainLine->hide();
|
grainLine->hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
grainLine->UpdateGeometry(geom.GetPos(), dRotation, dLength);
|
|
||||||
|
grainLine->UpdateGeometry(geom.GetPos(), dRotation, ToPixel(dLength, *VDataTool::data.GetPatternUnit()));
|
||||||
grainLine->show();
|
grainLine->show();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue
Block a user