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++

#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); // <20>Լ<EFBFBD><D4BC>ߺ<EFBFBD>ˮƽ<CBAE><C6BD>֮<EFBFBD><D6AE><EFBFBD>ļн<C4BC>
double angle2 = angle1 - m_rotateAngle; //<2F><>ת֮<D7AA><D6AE><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD>ˮƽ<CBAE><C6BD>֮<EFBFBD><D6AE><EFBFBD>ļн<C4BC>
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;
//<2F><><EFBFBD><EFBFBD>ˮƽ<CBAE><C6BD>
m_levelLine.setP1(m_firstPoint);
m_levelLine.setP2(QPointF(m_firstPoint.x() + 100, m_firstPoint.y()));
//<2F><><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><C7B0>λ<EFBFBD><CEBB>
setPos(m_firstPoint);
//<2F><><EFBFBD><EFBFBD>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);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
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)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3A3AC><EFBFBD>ڰ<EFBFBD>ס<EFBFBD><D7A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
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;
}
}