logplus/Workflow/WFEngine/Component/WorkflowWidget/src/ModuleMonitorGraphicsItem.cpp
2026-01-16 17:18:41 +08:00

271 lines
7.2 KiB
C++

/**
* @file ModuleMonitorGraphicsItem.cpp
* @date 2015-01-06
*/
#include <QPixmap>
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QDebug>
#include <QObject>
#include <QImageReader>
#include "ModuleMonitorGraphicsItem.h"
#include "ModuleGraphicsItem.h"
#include "ModuleInformation.h"
#include "ModuleParameter.h"
#include "ParameterItem.h"
using namespace pai::graphics2d;
using namespace pai::workflow;
ModuleMonitorGraphicsItem::ModuleMonitorGraphicsItem(QGraphicsItem *pParent) :
QGraphicsItem(pParent),
m_pColorEditor(NULL),
m_index(0),
m_hovering(false),
m_pModuleInfo(NULL),
m_pStyle(NULL),
m_ReadOnly(false)
{
InitPixmaps();
m_CurrentPixmap = QPixmap(m_pixmaps["grey"]);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);
setAcceptHoverEvents(true);
}
ModuleMonitorGraphicsItem::ModuleMonitorGraphicsItem(CModuleInformation *pModule,
pai::graphics2d::PaiModuleStyle *pStyle,
bool readOnly,
QGraphicsItem *pParent) :
QGraphicsItem(pParent),
m_pColorEditor(NULL),
m_index(0),
m_hovering(false),
m_pModuleInfo(pModule),
m_pStyle(pStyle),
m_ReadOnly(readOnly)
{
InitPixmaps();
m_CurrentPixmap = QPixmap(m_pixmaps["grey"]);
if(!m_ReadOnly)
{
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);
setAcceptHoverEvents(true);
}
if (m_pStyle->GetPosition() != QPointF(0, 0))
{
setPos(m_pStyle->GetPosition());
}
}
ModuleMonitorGraphicsItem::~ModuleMonitorGraphicsItem()
{
}
void ModuleMonitorGraphicsItem::InitPixmaps()
{
m_pixmaps["grey"] = ":/grey_eye.png";
m_pixmaps["brown"] = ":/brown_eye.png";
m_pixmaps["purple"] = ":/purple_eye.png";
m_pixmaps["green"] = ":/green_eye.png";
m_pixmaps["blue"] = ":/blue_eye.png";
m_pixmaps["red"] = ":/red_eye.png";
}
void ModuleMonitorGraphicsItem::UpdatePixmap(const QString& color)
{
m_CurrentPixmap = QPixmap(m_pixmaps[color]);
if(m_pModuleInfo && m_pModuleInfo->GetModuleParameter())
{
pai::module::CParameterItem *pItem = m_pModuleInfo->GetModuleParameter()->GetParameterItem("monitorColor");
if(pItem)
{
pItem->SetStringValue(color.toStdString());
}
}
}
void ModuleMonitorGraphicsItem::SetIndex(int index)
{
m_index = index;
if(m_pModuleInfo && m_pModuleInfo->GetModuleParameter())
{
//设置索引
pai::module::CParameterItem *pItem = m_pModuleInfo->GetModuleParameter()->GetParameterItem("index");
if(pItem)
{
pItem->SetStringValue(QString::number(m_index).toStdString());
}
}
}
void ModuleMonitorGraphicsItem::Refresh()
{
//设置索引
if(m_pModuleInfo && m_pModuleInfo->GetModuleParameter())
{
pai::module::CParameterItem *pParameterItem = m_pModuleInfo->GetModuleParameter()->GetParameterItem("index");
if(pParameterItem)
{
m_index = QString::fromStdString(pParameterItem->GetStringValue()).toInt();
}
pParameterItem = m_pModuleInfo->GetModuleParameter()->GetParameterItem("monitorColor");
if(pParameterItem)
{
m_CurrentPixmap = QPixmap(m_pixmaps[QString::fromStdString(pParameterItem->GetStringValue())]);
}
}
}
CModuleInformation* ModuleMonitorGraphicsItem::GetModule() const
{
return m_pModuleInfo;
}
pai::graphics2d::PaiModuleStyle* ModuleMonitorGraphicsItem::GetStyle() const
{
return m_pStyle;
}
bool ModuleMonitorGraphicsItem::IsReadOnly() const
{
return m_ReadOnly;
}
int ModuleMonitorGraphicsItem::GetIndex() const
{
return m_index;
}
void ModuleMonitorGraphicsItem::AttachColorEditor(MonitorColorEditor *pColorEditor)
{
m_pColorEditor = pColorEditor;
}
void ModuleMonitorGraphicsItem::RemoveColorEditor()
{
m_pColorEditor = NULL;
}
bool ModuleMonitorGraphicsItem::IsCurrentMonitor()
{
return (m_pColorEditor != NULL);
}
int ModuleMonitorGraphicsItem::type() const
{
return Type;
}
QRectF ModuleMonitorGraphicsItem::boundingRect() const
{
if(m_index == 0)
{
return QRectF(-11,-8,22,16);
}
else
{
return QRectF(-11,-14,22,28);
}
}
void ModuleMonitorGraphicsItem::hoverEnterEvent (QGraphicsSceneHoverEvent *pEvent)
{
m_hovering = true;
QGraphicsItem::hoverEnterEvent(pEvent);
}
void ModuleMonitorGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *pEvent)
{
m_hovering = false;
QGraphicsItem::hoverLeaveEvent(pEvent);
}
void ModuleMonitorGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pEvent)
{
QGraphicsItem::mousePressEvent(pEvent);
}
void ModuleMonitorGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *pEvent)
{
QGraphicsItem::mouseReleaseEvent(pEvent);
pai::graphics2d::ModuleGraphicsItem::m_EventAgent.ShowColorEditor(this);
}
void ModuleMonitorGraphicsItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem */*pOption*/, QWidget */*pWidget*/)
{
QPainterPath path;
QRectF realRect(boundingRect());
path.addRoundedRect(realRect,2.0,2.0);
//开始绘制
QBrush oldBrush=pPainter->brush();
QPen oldPen=pPainter->pen();
QBrush brush(QColor("#F5FDFA"));
QPen pen(QColor("#C8DEE6"));
pen.setWidthF(0.0);
pPainter->setPen(pen);
pPainter->setBrush(brush);
if(m_index != 0)
{
//数字部分
QPainterPath textPath;
QRectF textRect(realRect.topLeft().x(),realRect.topLeft().y(),realRect.width(),13);
textPath.addRoundedRect(textRect,2.0,2.0);
pPainter->drawPath(textPath);
//eye 部分
QPainterPath eyePath;
QRectF eyeRect(realRect.topLeft().x(),realRect.topLeft().y()+15,realRect.width(),13);
eyePath.addRoundedRect(eyeRect,2.0,2.0);
pPainter->drawPath(eyePath);
}
else
{
//绘制边框和背景
pPainter->drawPath(path);
}
if(m_hovering)
{
if(m_index == 0)
{
pPainter->drawRect(realRect.topLeft().x()+2,realRect.topLeft().y()+2,realRect.width()-4,realRect.height()-4);
}
else
{
pPainter->drawRect(realRect.topLeft().x()+3,realRect.topLeft().y()+15,realRect.width()-6,QPixmap(m_CurrentPixmap).height()+1);
}
}
pPainter->setBrush(oldBrush);
pPainter->setPen(oldPen);
if(m_index == 0)
{
pPainter->drawPixmap(realRect.topLeft().x()+3,realRect.topLeft().y()+3,QPixmap(m_CurrentPixmap),0,0,0,0);
}
else
{
pPainter->drawText(realRect.topLeft().x()+3,realRect.topLeft().y()+3,16,10,Qt::AlignCenter,QString::number(m_index));
pPainter->drawPixmap(realRect.topLeft().x()+3,realRect.topLeft().y()+15,QPixmap(m_CurrentPixmap),0,0,0,0);
}
}