#include "UICanvasItemBase.h"
#include "Utils.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QVector2D>
#include <QVector3D>
#include <cmath>
#include <math.h>
#include <QJsonObject>
#include "NDNodeBase.h"
#include "NDAttributeBase.h"
#include "NDRealAttribute.h"
#include "NDIntAttribute.h"
#include "RALLBlockSignal.h"
#include "UICanvasItemManager.h"
#include "UICanvasView.h"
QImage UICanvasItemBase::m_closeIcon;
QImage UICanvasItemBase::m_resizeIcon;
QImage UICanvasItemBase::m_rotateIcon;
UICanvasItemBase::UICanvasItemBase(QGraphicsItem* parentItem)
:QGraphicsItem(parentItem)
,m_cPenColor(255, 0, 0)
,m_cBrushColor(200, 100, 100)
{
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
initIcon();
// 初始化基本属性
initNodeBase();
// 获取中心位置
m_size = QSize(100, 100);
QPointF centerPos = g_currentCanvasManager->getCurrentCanvasView()->getCenterPos();
this->setPos(centerPos);
}
UICanvasItemBase::~UICanvasItemBase()
{
}
void UICanvasItemBase::setItemResizeable(bool resizeable)
{
m_isResizeable = resizeable;
}
NDNodeBase* UICanvasItemBase::getCurrentNode(void) const
{
return m_pNode;
}
void UICanvasItemBase::setItemResizeRatio(bool resizeRation, qreal rationValue)
{
m_isRatioScale = resizeRation;
m_ratioValue = rationValue;
}
QRectF UICanvasItemBase::boundingRect() const
{
QRectF rectF = getCustomRect();
if (!this->isSelected())
return rectF;
rectF.adjust(-m_nInterval, -m_nInterval, m_nInterval, m_nInterval);
rectF.adjust(-m_nEllipseWidth, -m_nEllipseWidth, m_nEllipseWidth, m_nEllipseWidth);
return rectF;
}
void UICanvasItemBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
painter->setRenderHint(QPainter::TextAntialiasing, true);
// 自定义绘制
customPaint(painter, option, widget);
if (!this->isSelected())
return;
// 设置画笔
QPen pen;
pen.setWidth(m_nPenWidth);
pen.setColor(m_cPenColor);
pen.setStyle(Qt::DashLine);
painter->setPen(pen);
QRectF itemRect = this->getCustomRect();
// 绘制轮廓线
QRectF outLintRect = itemRect.adjusted(-m_nInterval, -m_nInterval, m_nInterval, m_nInterval);
painter->drawRect(outLintRect);
painter->setPen(Qt::NoPen);
painter->setBrush(m_cBrushColor);
// 绘制控制点
painter->drawEllipse(outLintRect.topRight(), m_nEllipseWidth, m_nEllipseWidth);
if (!m_closePixmap.isNull())
painter->drawPixmap(QRect(outLintRect.topRight().x() - m_nEllipseWidth / 2, \
outLintRect.topRight().y() - m_nEllipseWidth / 2, \
m_nEllipseWidth, m_nEllipseWidth), m_closePixmap);
painter->drawEllipse(outLintRect.bottomLeft(), m_nEllipseWidth, m_nEllipseWidth);
if (!m_rotatePixmap.isNull())
painter->drawPixmap(QRect(outLintRect.bottomLeft().x() - m_nEllipseWidth / 2, \
outLintRect.bottomLeft().y() - m_nEllipseWidth / 2, \
m_nEllipseWidth, m_nEllipseWidth), m_rotatePixmap);
if (m_isResizeable)
{
painter->drawEllipse(outLintRect.bottomRight(), m_nEllipseWidth, m_nEllipseWidth);
if (!m_resizePixmap.isNull())
painter->drawPixmap(QRect(outLintRect.bottomRight().x() - m_nEllipseWidth / 2, \
outLintRect.bottomRight().y() - m_nEllipseWidth / 2, \
m_nEllipseWidth, m_nEllipseWidth), m_resizePixmap);
}
}
QPainterPath UICanvasItemBase::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
void UICanvasItemBase::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QRectF itemRect = this->getCustomRect();
QRectF outLintRect = itemRect.adjusted(-m_nInterval, -m_nInterval, m_nInterval, m_nInterval);
// 获取当前模式
QPointF pos = event->pos();
QPointF scenePos = event->scenePos();
if (itemRect.contains(pos))
m_itemOper = t_move;
else if (g_utilTool->getDistance(pos, outLintRect.topRight()) <= m_nEllipseWidth)
{
g_currentCanvasManager->deleteCanvasItem(m_pNode);
return;
}
else if (g_utilTool->getDistance(pos, outLintRect.bottomLeft()) <= m_nEllipseWidth)
m_itemOper = t_rotate;
else if (g_utilTool->getDistance(pos, outLintRect.bottomRight()) <= m_nEllipseWidth)
m_itemOper = t_resize;
// 保存当前的一些信息
m_pos = pos;
m_pressedPos = scenePos;
m_startPos = this->pos();
m_startRotate = m_rotate;
m_startSize = m_size;
return QGraphicsItem::mousePressEvent(event);
}
void UICanvasItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// 获取场景坐标和本地坐标
QPointF scenePos = event->scenePos();
QPointF pos = event->pos();
if (m_itemOper == t_move)
{
// 处理移动
mouseMoveMoveOperator(scenePos, pos);
}
else if (m_itemOper == t_resize)
{
// 处理更改大小
mouseMoveResizeOperator(scenePos, pos);
}
else if (m_itemOper == t_rotate)
{
// 处理旋转
mouseMoveRotateOperator(scenePos, pos);
}
return QGraphicsItem::mouseMoveEvent(event);
}
void UICanvasItemBase::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
// 获取场景坐标和本地坐标
QPointF scenePos = event->scenePos();
QPointF pos = event->pos();
if (m_itemOper == t_move)
{
// 处理移动
mouseReleaseMoveOperator(scenePos, pos);
}
else if (m_itemOper == t_resize)
{
// 处理更改大小
mouseReleaseResizeOperator(scenePos, pos);
}
else if (m_itemOper == t_rotate)
{
// 处理旋转
mouseReleaseRotateOperator(scenePos, pos);
}
m_itemOper = t_none;
return QGraphicsItem::mouseReleaseEvent(event);
}
QVariant UICanvasItemBase::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == QGraphicsItem::ItemSelectedChange)
prepareGeometryChange();
else if (change == QGraphicsItem::ItemPositionHasChanged)
{
updateAttribute();
}
return QGraphicsItem::itemChange(change, value);
}
void UICanvasItemBase::mouseMoveMoveOperator(const QPointF& scenePos, const QPointF& loacalPos)
{
qreal xInterval = scenePos.x() - m_pressedPos.x();
qreal yInterval = scenePos.y() - m_pressedPos.y();
this->setPos(m_startPos + QPointF(xInterval, yInterval));
this->update();
}
void UICanvasItemBase::mouseMoveResizeOperator(const QPointF& scenePos, const QPointF& loacalPos)
{
if (!m_isResizeable)
return;
qreal ratio = m_ratioValue;
qreal itemWidth = abs(loacalPos.x()) * 2 - m_nInterval - m_nEllipseWidth;
qreal itemHeight = abs(loacalPos.y()) * 2 - m_nInterval - m_nEllipseWidth;
if (m_isRatioScale)
itemHeight = itemWidth * 1.0 / ratio;
// 设置图片的最小大小为10
if (itemWidth < 10 || itemHeight < 10)
return;
m_size = QSize(itemWidth, itemHeight);
m_pWidthAttribute->setValue(m_size.width());
m_pHeightAttribute->setValue(m_size.height());
this->update();
}
void UICanvasItemBase::mouseMoveRotateOperator(const QPointF& scenePos, const QPointF& loacalPos)
{
// 获取并设置为单位向量
QVector2D startVec(m_pos.x() - 0, m_pos.y() - 0);
startVec.normalize();
QVector2D endVec(loacalPos.x() - 0, loacalPos.y() - 0);
endVec.normalize();
// 单位向量点乘,计算角度
qreal dotValue = QVector2D::dotProduct(startVec, endVec);
if (dotValue > 1.0)
d