#include "coil.h" #include #include Coil::Coil(const QString &type) : Item(type) { } void Coil::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { painter->setRenderHint(QPainter::Antialiasing); if (state()) { painter->setBrush(Qt::green); // 激活状态 } else { painter->setBrush(Qt::white); // 未激活状态 } if (type_ == "线圈") { // 绘制线圈样式: 两边线段+中间椭圆 painter->drawLine(-12, 0, -5, 0); painter->drawEllipse(QRectF(-5, -8, 10, 16)); painter->drawLine(5, 0, 12, 0); // 画锚点 painter->setBrush(Qt::darkGray); painter->setPen(Qt::NoPen); painter->drawEllipse(QPointF(-12, 0), 4, 4); // 左锚点 painter->drawEllipse(QPointF(12, 0), 4, 4); // 右锚点 } if (!registerId_.isEmpty()) { painter->save(); painter->setFont(QFont("Arial", 8)); painter->setPen(Qt::black); QString text = QString("%1: %2").arg(registerId()).arg(registerValue_); painter->drawText(boundingRect(), Qt::AlignBottom | Qt::AlignHCenter, text); painter->restore(); } if (option->state & QStyle::State_Selected) { QPen pen(Qt::DashLine); pen.setColor(Qt::blue); pen.setWidth(2); painter->setPen(pen); painter->setBrush(Qt::NoBrush); painter->drawRect(boundingRect()); } } bool Coil::state() const { return registerValue_ > 0; }