|
- #include "comparator.h"
- #include <QMenu>
- #include <QPainter>
- #include <QStyleOptionGraphicsItem>
-
- 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 *)
- {
- painter->setRenderHint(QPainter::Antialiasing);
- if (state()) {
- painter->setBrush(Qt::green); // 激活状态
- } else {
- painter->setBrush(Qt::white); // 未激活状态
- }
- if (type_ == "比较") {
- painter->drawRect(QRectF(-12, -8, 24, 16));
- painter->setFont(QFont("Arial", 8));
- painter->drawText(QRectF(-10, -8, 20, 16), Qt::AlignCenter, compare_);
-
- // 锚点
- painter->setBrush(Qt::darkGray);
- painter->setPen(Qt::NoPen);
- painter->drawEllipse(QPointF(-18, 0), 4, 4);
- painter->drawEllipse(QPointF(18, 0), 4, 4);
- }
- painter->save();
- painter->setFont(QFont("Arial", 8));
- painter->setPen(Qt::black);
- 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();
- 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 Comparator::setRegisterId(const QString &id)
- {
- if (registerId_.isEmpty())
- {
- registerId_ = id;
- return true;
- }
- else if(registerId2_.isEmpty())
- {
- registerId2_ = id;
- return true;
- }
- return false;
- }
-
- void Comparator::setRegisterValue(const QString ®isterId, quint16 value)
- {
- if (registerId_ == registerId)
- {
- registerValue_ = value;
- update(); // 触发重绘
- }
- if (registerId2_ == registerId)
- {
- registerValue2_ = value;
- update(); // 触发重绘
- }
- }
-
- void Comparator::addMenuActions(QMenu *menu)
- {
- menu->addAction("比较");
- }
-
- void Comparator::handleMenuAction(QAction *action)
- {
- if (action->text() == "复制") {
- emit requestCopy(this);
- }
- if (action->text() == "删除") {
- emit requestDelete(this);
- }
- if (action->text() == "对象"){
- emit requestBindRegister(this);
- }
- if (action->text() == "比较"){
- emit requestCompare(this);
- }
- if (action->text() == "重置") {
- emit requestReset(this);
- }
- }
-
- void Comparator::setCompare(QString compare)
- {
- compare_ = compare;
- update();
- }
-
- bool Comparator::state() const
- {
- if (compare_ == ">") return registerValue_ > registerValue2_;
- if (compare_ == "<") return registerValue_ < registerValue2_;
- if (compare_ == "=") return registerValue_ == registerValue2_;
- if (compare_ == ">=") return registerValue_ >= registerValue2_;
- if (compare_ == "<") return registerValue_ <= registerValue2_;
- return false;
- }
-
- QStringList Comparator::resetRegister()
- {
- QStringList unboundRegs;
-
- // 重置第一个寄存器
- if (!registerId_.isEmpty()) {
- unboundRegs << registerId_;
- registerId_.clear();
- }
-
- // 重置第二个寄存器
- if (!registerId2_.isEmpty()) {
- unboundRegs << registerId2_;
- registerId2_.clear();
- }
-
- // 清除寄存器值
- registerValue_ = 0;
- registerValue2_ = 0;
-
- update(); // 刷新显示
- return unboundRegs;
- }
|