108 lines
2.8 KiB
C++
108 lines
2.8 KiB
C++
/*
|
|
* @file ModulePortGraphicsItem.cpp
|
|
* @brief 模块端口
|
|
*/
|
|
#include <QPainter>
|
|
#include <QGradient>
|
|
#include <QtDebug>
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include "ModulePortGraphicsItem.h"
|
|
|
|
using namespace pai::graphics2d;
|
|
|
|
ModulePortGraphicsItem::ModulePortGraphicsItem(pai::graphics2d::PaiModuleStyle* pStyle,
|
|
const QPointF& center,qreal radius,
|
|
PortDirection portDirection) :
|
|
m_pStyle(pStyle),
|
|
m_center(center),
|
|
m_diameter(radius),
|
|
m_selected(false),
|
|
m_OnHovering(false),
|
|
m_light(false),
|
|
m_PortDirection(portDirection)
|
|
{
|
|
m_path.addEllipse(center, radius / 2, radius / 2);
|
|
}
|
|
|
|
ModulePortGraphicsItem::~ModulePortGraphicsItem()
|
|
{
|
|
// TODO Auto-generated destructor stub
|
|
}
|
|
QRectF ModulePortGraphicsItem::boundingRect() const
|
|
{
|
|
//暂时采用场景坐标
|
|
const int hostSpot = 3;
|
|
return QRectF(m_center.x() - m_diameter / 2 - hostSpot, m_center.y()
|
|
- m_diameter / 2 - hostSpot, m_diameter + 2 * hostSpot, m_diameter + 2 * hostSpot);
|
|
}
|
|
|
|
void ModulePortGraphicsItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem */*pOption*/, QWidget */*pWidget*/)
|
|
{
|
|
pPainter->save();
|
|
pai::graphics2d::PaiModuleStyle::STATUS eStatus = pai::graphics2d::PaiModuleStyle::Normal;
|
|
|
|
if (m_selected || m_light)//选中的优先级最高,无论什么状态,选中的绘制都一样
|
|
{
|
|
eStatus = pai::graphics2d::PaiModuleStyle::Selected;
|
|
}
|
|
else
|
|
{
|
|
if (m_OnHovering)
|
|
{
|
|
eStatus = pai::graphics2d::PaiModuleStyle::MouseOn;
|
|
}
|
|
}
|
|
QRadialGradient radialGrad(m_center, m_diameter / 2 - 1, QPointF(m_center.x(), m_center.y()));
|
|
QColor beginColor;
|
|
QColor endColor;
|
|
if(m_pStyle != NULL)
|
|
{
|
|
m_pStyle->GetPortColor(eStatus, beginColor, endColor);
|
|
}
|
|
radialGrad.setColorAt(0, beginColor);
|
|
radialGrad.setColorAt(1, endColor);
|
|
|
|
QColor marginColor;
|
|
if(m_pStyle != NULL)
|
|
{
|
|
m_pStyle->GetPortMarginColor(eStatus, marginColor);
|
|
}
|
|
|
|
//开始绘制
|
|
QBrush oldBrush = pPainter->brush();
|
|
QPen oldPen = pPainter->pen();
|
|
|
|
QBrush brush(radialGrad);
|
|
QPen pen(marginColor);
|
|
pen.setWidth(1.0);
|
|
pPainter->setBrush(brush);
|
|
pPainter->setPen(pen);
|
|
pPainter->drawPath(m_path);
|
|
|
|
pPainter->setPen(oldPen);
|
|
pPainter->setBrush(oldBrush);
|
|
|
|
pPainter->restore();
|
|
}
|
|
|
|
void ModulePortGraphicsItem::SetPortSelected(bool selected)
|
|
{
|
|
m_selected = selected;
|
|
}
|
|
|
|
void ModulePortGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent */*pEvent*/)
|
|
{
|
|
m_OnHovering = true;
|
|
|
|
}
|
|
void ModulePortGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent */*pEvent*/)
|
|
{
|
|
m_OnHovering = false;
|
|
}
|
|
|
|
void ModulePortGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent */*pEvent*/)
|
|
{
|
|
m_selected = !m_selected;
|
|
}
|