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.

177 lines
5.3 KiB
C++

#include "MyDrawRectItem.h"
MyDrawRectItem::~MyDrawRectItem()
{
}
void MyDrawRectItem::CountPerVertexCoord(double xCoord, double yCoord, int pointPos)
{
//计算左下角位置
QLineF leftDownLine = QLineF(
m_rectX * 1000,
m_rectY * 1000,
xCoord,
yCoord);
double angle1 = leftDownLine.angleTo(m_levelLine); // 自己线和水平线之间的夹角
double angle2 = angle1 - m_rotateAngle; //旋转之后的终点和水平线之间的夹角
if (pointPos == 0)
{
double lineLength = m_height * 1000;
m_leftDownX = (lineLength * cos(angle2 * PI_2 / H_ANGLE) + m_rectX * 1000) / 1000.00;
m_leftDownY = (lineLength * sin(angle2 * PI_2 / H_ANGLE) + m_rectY * 1000) / 1000.00;
}
else if (pointPos == 1)
{
double lineLength = m_width * 1000;
m_rightTopX = (lineLength * cos(angle2 * PI_2 / H_ANGLE) + m_rectX * 1000) / 1000.00;
m_rightTopY = (lineLength * sin(angle2 * PI_2 / H_ANGLE) + m_rectY * 1000) / 1000.00;
//qDebug() << "222:" << lineLength <<" "<<m_rightTopX << " " << m_rightTopY;
}else if (pointPos == 2)
{
double lineLength = sqrt(std::pow(m_width * 1000, 2) + std::pow(m_height * 1000, 2));
m_rightDownX = (lineLength * cos(angle2 * PI_2 / H_ANGLE) + m_rectX * 1000) / 1000.00;
m_rightDownY = (lineLength * sin(angle2 * PI_2 / H_ANGLE) + m_rectY * 1000) / 1000.00;
}
}
void MyDrawRectItem::CountRectVertexCoord()
{
CountPerVertexCoord(m_rectX * 1000, m_rectY * 1000 + m_height * 1000, 0);
CountPerVertexCoord(m_rectX * 1000 + m_width * 1000, m_rectY * 1000, 1);
CountPerVertexCoord(m_rectX * 1000 + m_width * 1000, m_rectY * 1000 + m_height * 1000, 2);
update();
}
void MyDrawRectItem::SetDrawRectAttr(double x1, double y1, double width, double height)
{
m_rectX = x1 / DIE_SCALE;
m_rectY = y1 / DIE_SCALE;
m_width = width / 100;
m_height = height / 100;
m_rotateAngle = 0.0;
//设置当前线位置
setPos(QPointF(x1, y1));
//设置ID
m_id = GlobalDefine::GetInstance()->GetItemId();
//设置水平线
m_levelLine.setP1(QPointF(x1, y1));
m_levelLine.setP2(QPointF(x1 + 100, y1));
m_translteType = NONE;
m_backupWidth = m_width;
m_backupHeight = m_height;
//计算4个顶点位置
CountRectVertexCoord();
}
void MyDrawRectItem::SetActualDieCoordAndUpdate(double x1, double y1, double width, double height, double angle)
{
m_rectX = x1;
m_rectY = y1;
m_width = width;
m_height = height;
m_rotateAngle = angle;
m_backupWidth = m_width;
m_backupHeight = m_height;
this->setRect(QRectF(0,0, width * DIE_SCALE, height * DIE_SCALE));
setPos(x1 * DIE_SCALE, y1 * DIE_SCALE);
// 计算4个顶点位置
CountRectVertexCoord();
setTransformOriginPoint(this->scenePos() - this->scenePos());
setRotation(-m_rotateAngle);
}
void MyDrawRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsRectItem::paint(painter, option, widget);
QPen pen;
pen.setWidth(FEATURE_POINT_LINE_WIDTH);
painter->setPen(pen);
painter->setBrush(FEATURE_POINT_COLOR);
painter->drawEllipse(QPointF(rect().left(), rect().top()), FEATURE_POINT_SIZE / 5, FEATURE_POINT_SIZE / 5);
painter->drawEllipse(QPointF(rect().left(), rect().bottom()), FEATURE_POINT_SIZE / 5, FEATURE_POINT_SIZE / 5);
painter->drawEllipse(QPointF(rect().right(), rect().top()), FEATURE_POINT_SIZE / 5, FEATURE_POINT_SIZE / 5);
painter->drawEllipse(QPointF(rect().right(), rect().bottom()), FEATURE_POINT_SIZE / 5, FEATURE_POINT_SIZE / 5);
}
void MyDrawRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
emit signal_select(m_id);
m_isMoustLeftPress = true;
m_mousePressDownPos = event->scenePos();
qDebug() <<"pos: "<< event->scenePos().x() << " " << event->scenePos().y() << " " << m_rightTopX << " " << m_rightTopY;
if (abs(event->scenePos().x() - m_rightTopX * 100) <= FEATURE_POINT_SIZE &&
abs(event->scenePos().y() - m_rightTopY * 100) <= FEATURE_POINT_SIZE)
{
m_translteType = SCALE;
}
else if (abs(event->scenePos().x() - m_leftDownX * 100) <= FEATURE_POINT_SIZE &&
abs(event->scenePos().y() - m_leftDownY * 100) <= FEATURE_POINT_SIZE)
{
m_translteType = ROTATE;
}
else
{
m_translteType = MOVE;
}
}
}
void MyDrawRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (m_translteType == MOVE)
{
setPos(event->scenePos().x() , event->scenePos().y());
m_rectX = (double)this->scenePos().x() / 100.00;
m_rectY = (double)this->scenePos().y() / 100.00;
}
else if (m_translteType == ROTATE)
{
QLineF mouseLine;
mouseLine.setP1(this->scenePos());
mouseLine.setP2(QPointF(event->scenePos().x(), event->scenePos().y()));
QLineF leftDown = QLineF(this->scenePos().x(), this->scenePos().y(),
this->scenePos().x(),
this->scenePos().y() + m_height * DIE_SCALE);
double angleNum = leftDown.angleTo(mouseLine);
m_rotateAngle = angleNum;
setRotation(-angleNum);
}
else if (m_translteType == SCALE)
{
auto& nowMouseCoord = event->scenePos();
double prop = ((double)event->scenePos().x() - (double)m_mousePressDownPos.x()) / 150.00;
double scaleNum = m_initScale + prop;
m_nowScale = scaleNum;
this->setScale(m_nowScale);
m_width = m_backupWidth * scaleNum;
m_height = m_backupHeight * scaleNum;
qDebug() << "after: " << m_nowScale <<" "<< m_width << " " << m_height;
}
//动作之后重新计算位置
CountRectVertexCoord();
}
void MyDrawRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (m_translteType == SCALE) {
m_initScale = m_nowScale;
}
}