You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.7 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "MyQGraphicsLineItem.h"
MyQGraphicsLineItem::MyQGraphicsLineItem(QObject *parent)
: QObject(parent)
{
}
MyQGraphicsLineItem::~MyQGraphicsLineItem()
{
}
bool MyQGraphicsLineItem::JudgeIsFeatruePoint(QPointF& mousePos)
{
QLineF mySelfScenePosLine = QLineF(this->scenePos().x(),this->scenePos().y(),
this->scenePos().x() + (m_secondPoint.x() - m_firstPoint.x()),
this->scenePos().y() + (m_secondPoint.y() - m_firstPoint.y()));
double angle1 = mySelfScenePosLine.angleTo(m_levelLine); // 自己线和水平线之间的夹角
double angle2 = angle1 - m_rotateAngle; //旋转之后的终点和水平线之间的夹角
double lineLength = this->line().length();
double afterRoateSceneX = lineLength * cos(angle2 * PI_2 / H_ANGLE) + this->scenePos().x();
double afterRotateSceneY = lineLength * sin(angle2 * PI_2 / H_ANGLE) + this->scenePos().y();
//qDebug() << mousePos.x() << " " << afterRoateSceneX<<" " << mousePos.y() << " " << afterRotateSceneY;
if (abs(mousePos.x() - afterRoateSceneX) <= FEATURE_POINT_SIZE && abs(mousePos.y() - afterRotateSceneY) <= FEATURE_POINT_SIZE)
{
return true;
}
return false;
}
void MyQGraphicsLineItem::SetLineItemAttr(QPointF& firstPoint, QPointF& secondPoint)
{
m_firstPoint = firstPoint;
m_secondPoint = secondPoint;
//设置水平线
m_levelLine.setP1(m_firstPoint);
m_levelLine.setP2(QPointF(m_firstPoint.x() + 100, m_firstPoint.y()));
//设置当前线位置
setPos(m_firstPoint);
//设置ID
m_id = GlobalDefine::GetInstance()->GetItemId();
}
void MyQGraphicsLineItem::SetActualCoordAndUpdate(double actualP1DieCoordX, double actualP1DieCoordY,
double actualP2DieCoordX, double actualP2DieCoordY)
{
m_actualP1DieCoordX = actualP1DieCoordX;
m_actualP1DieCoordY = actualP1DieCoordY;
m_actualP2DieCoordX = actualP2DieCoordX;
m_actualP2DieCoordY = actualP2DieCoordY;
m_firstPoint.setX(m_actualP1DieCoordX * DIE_SCALE);
m_firstPoint.setY(m_actualP1DieCoordY * DIE_SCALE);
m_secondPoint.setX(m_actualP2DieCoordX * DIE_SCALE);
m_secondPoint.setY(m_actualP2DieCoordY * DIE_SCALE);
//如果存在旋转必须归0不然会存在新线段依然存在旋转
setRotation(0);
m_rotateAngle = 0;
this->setLine(0,0,
m_actualP2DieCoordX * DIE_SCALE - m_actualP1DieCoordX * DIE_SCALE,
m_actualP2DieCoordY * DIE_SCALE - m_actualP1DieCoordY * DIE_SCALE);
setPos(0, 0);
setPos(m_actualP1DieCoordX * DIE_SCALE, m_actualP1DieCoordY * DIE_SCALE);
}
double MyQGraphicsLineItem::GetStartAngle()
{
//qDebug() << m_firstPoint.x()<<" "<< m_firstPoint.y()<<m_secondPoint.x() <<" "<< m_secondPoint.y();
return m_levelLine.angleTo(QLineF(m_firstPoint, m_secondPoint));
}
void MyQGraphicsLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsLineItem::paint(painter, option, widget);
if (!m_isCoordAxis)
{
//画特征点,用于按住特征点旋转
QPen pen;
pen.setWidth(2);
painter->setPen(pen);
painter->setBrush(Qt::blue);
double featurePointX = 0.0;
double freaturePointY = 0.0;
line().p2().x() > line().p1().x() ? featurePointX = line().p2().x() - FEATURE_POINT_SIZE / 2 :
featurePointX = line().p2().x() + FEATURE_POINT_SIZE / 2;
line().p2().y() > line().p1().y() ? freaturePointY = line().p2().y() - FEATURE_POINT_SIZE / 2 :
freaturePointY = line().p2().y() + FEATURE_POINT_SIZE / 2;
painter->drawEllipse(QPointF(featurePointX, freaturePointY), FEATURE_POINT_SIZE / 2, FEATURE_POINT_SIZE / 2);
}
}
void MyQGraphicsLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (m_isCoordAxis)
{
return;
}
if (event->button() == Qt::LeftButton)
{
emit signal_select(m_id);
m_mousePressDownPos = event->scenePos();
m_isPressDown = true;
m_isInFraturePoint = false;
if (JudgeIsFeatruePoint(m_mousePressDownPos))
{
m_isInFraturePoint = true;
}
}
}
void MyQGraphicsLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (m_isCoordAxis)
{
return;
}
if (m_isPressDown)
{
if (m_isInFraturePoint)
{
setTransformOriginPoint(this->scenePos() - this->scenePos());
QLineF mouseLine;
mouseLine.setP1(this->scenePos());
mouseLine.setP2(QPointF(event->scenePos().x(), event->scenePos().y()));
QLineF mySelfScenePosLine = QLineF(this->scenePos().x(), this->scenePos().y(),
this->scenePos().x() + (m_secondPoint.x() - m_firstPoint.x()),
this->scenePos().y() + (m_secondPoint.y() - m_firstPoint.y()));
double angleNum = mySelfScenePosLine.angleTo(mouseLine);
m_rotateAngle = angleNum;
setRotation(-angleNum);
}
else
{
setPos(event->scenePos().x() , event->scenePos().y());
}
}
}
void MyQGraphicsLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (m_isCoordAxis)
{
return;
}
if (event->button() == Qt::LeftButton)
{
m_isPressDown = false;
m_isInFraturePoint = false;
}
}