@@ -32,9 +32,6 @@ void Coil::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid | |||||
painter->save(); | painter->save(); | ||||
painter->setFont(QFont("Arial", 8)); | painter->setFont(QFont("Arial", 8)); | ||||
painter->setPen(Qt::black); | painter->setPen(Qt::black); | ||||
// 在元件底部居中绘制寄存器ID | |||||
// painter->drawText(boundingRect().adjusted(0, 20, 0, 0), | |||||
// Qt::AlignCenter, registerId_); | |||||
QString text = QString("%1: %2").arg(registerId()).arg(registerValue_); | QString text = QString("%1: %2").arg(registerId()).arg(registerValue_); | ||||
painter->drawText(boundingRect(), Qt::AlignBottom | Qt::AlignHCenter, text); | painter->drawText(boundingRect(), Qt::AlignBottom | Qt::AlignHCenter, text); | ||||
painter->restore(); | painter->restore(); | ||||
@@ -7,6 +7,11 @@ Comparator::Comparator(const QString &type) : Item(type) | |||||
} | } | ||||
QRectF Comparator::boundingRect() const | |||||
{ | |||||
return QRectF(-22, -20, 44, 40); | |||||
} | |||||
void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) | void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) | ||||
{ | { | ||||
painter->setRenderHint(QPainter::Antialiasing); | painter->setRenderHint(QPainter::Antialiasing); | ||||
@@ -21,15 +26,20 @@ void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option | |||||
painter->drawEllipse(QPointF(-18, 0), 4, 4); | painter->drawEllipse(QPointF(-18, 0), 4, 4); | ||||
painter->drawEllipse(QPointF(18, 0), 4, 4); | painter->drawEllipse(QPointF(18, 0), 4, 4); | ||||
} | } | ||||
if (!registerId_.isEmpty()) { | |||||
painter->save(); | painter->save(); | ||||
painter->setFont(QFont("Arial", 8)); | painter->setFont(QFont("Arial", 8)); | ||||
painter->setPen(Qt::black); | painter->setPen(Qt::black); | ||||
// 在元件底部居中绘制寄存器ID | |||||
painter->drawText(boundingRect().adjusted(0, 20, 0, 0), | |||||
Qt::AlignCenter, registerId_); | |||||
if (!registerId_.isEmpty()) { | |||||
QString text = QString("%1: %2").arg(registerId_).arg(registerValue_); | |||||
painter->drawText(QRectF(-20, -25, 40, 20), Qt::AlignCenter, text); | |||||
} | |||||
// 在右上角显示第二个寄存器 | |||||
if (!registerId2_.isEmpty()) { | |||||
QString text = QString("%1: %2").arg(registerId2_).arg(registerValue2_); | |||||
painter->drawText(QRectF(-20, 5, 40, 20), Qt::AlignCenter, text); | |||||
} | |||||
painter->restore(); | painter->restore(); | ||||
} | |||||
if (option->state & QStyle::State_Selected) { | if (option->state & QStyle::State_Selected) { | ||||
QPen pen(Qt::DashLine); | QPen pen(Qt::DashLine); | ||||
pen.setColor(Qt::blue); | pen.setColor(Qt::blue); | ||||
@@ -39,3 +49,29 @@ void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option | |||||
painter->drawRect(boundingRect()); | painter->drawRect(boundingRect()); | ||||
} | } | ||||
} | } | ||||
void Comparator::setRegisterId(const QString &id) | |||||
{ | |||||
if (registerId_.isEmpty()) | |||||
{ | |||||
registerId_ = id; | |||||
} | |||||
else if(registerId2_.isEmpty()) | |||||
{ | |||||
registerId2_ = id; | |||||
} | |||||
} | |||||
void Comparator::setRegisterValue(const QString ®isterId, quint16 value) | |||||
{ | |||||
if (registerId_ == registerId) | |||||
{ | |||||
registerValue_ = value; | |||||
update(); // 触发重绘 | |||||
} | |||||
if (registerId2_ == registerId) | |||||
{ | |||||
registerValue2_ = value; | |||||
update(); // 触发重绘 | |||||
} | |||||
} |
@@ -6,9 +6,16 @@ class Comparator : public Item | |||||
{ | { | ||||
public: | public: | ||||
Comparator(const QString &type); | Comparator(const QString &type); | ||||
QRectF boundingRect() const override; | |||||
void paint(QPainter *painter, | void paint(QPainter *painter, | ||||
const QStyleOptionGraphicsItem *option, | const QStyleOptionGraphicsItem *option, | ||||
QWidget *) override; | QWidget *) override; | ||||
void setRegisterId(const QString &id) override; | |||||
void setRegisterValue(const QString ®isterId, quint16 value) override; | |||||
private: | |||||
QString registerId2_; | |||||
quint16 registerValue2_ = 0; | |||||
}; | }; | ||||
#endif // COMPARATOR_H | #endif // COMPARATOR_H |
@@ -58,10 +58,13 @@ QString Item::itemType() | |||||
return type_; | return type_; | ||||
} | } | ||||
void Item::setRegisterValue(quint16 value) | |||||
void Item::setRegisterValue(const QString ®isterId, quint16 value) | |||||
{ | { | ||||
registerValue_ = value; | |||||
update(); // 触发重绘 | |||||
if (registerId_ == registerId) | |||||
{ | |||||
registerValue_ = value; | |||||
update(); // 触发重绘 | |||||
} | |||||
} | } | ||||
void Item::MenuActions(QMenu *menu) | void Item::MenuActions(QMenu *menu) | ||||
@@ -21,9 +21,9 @@ public: | |||||
void removeConnection(Connection* conn); | void removeConnection(Connection* conn); | ||||
QList<Connection*> connections(); | QList<Connection*> connections(); | ||||
QString itemType(); | QString itemType(); | ||||
void setRegisterId(const QString& id) { registerId_ = id; } | |||||
virtual void setRegisterId(const QString& id) { registerId_ = id; } | |||||
QString registerId() const { return registerId_; } | QString registerId() const { return registerId_; } | ||||
void setRegisterValue(quint16 value); | |||||
virtual void setRegisterValue(const QString& registerId, quint16 value); | |||||
quint16 registerValue() const { return registerValue_; } | quint16 registerValue() const { return registerValue_; } | ||||
@@ -28,7 +28,7 @@ void RegisterManager::updateRegisterValue(const QString& registerId, quint16 val | |||||
// 更新所有绑定该寄存器的图元 | // 更新所有绑定该寄存器的图元 | ||||
for (Item* item : registerMap[registerId]) { | for (Item* item : registerMap[registerId]) { | ||||
item->setRegisterValue(value); | |||||
item->setRegisterValue(registerId, value); | |||||
item->update(); // 刷新显示 | item->update(); // 刷新显示 | ||||
} | } | ||||
} | } | ||||