#include "button.h" #include #include #include #include #include #include Button::Button(const QString &type) : Item(type) { } QRectF Button::boundingRect() const { return QRectF(0, 0, currentSize_.width(), currentSize_.height()); } void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { painter->setRenderHint(QPainter::Antialiasing); if (type_ == "按钮") { if (!imagePath_.isEmpty() && !customPixmap_.isNull()) { painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), customPixmap_); } else{ painter->drawRect(0, 0, currentSize_.width(), currentSize_.height()); } } if (!registerId_.isEmpty()) { painter->save(); painter->setFont(QFont("Arial", 8)); painter->setPen(Qt::red); QString text; if (registerValue_ > 0) { text = QString("%1\nON").arg(registerId()); } else { text = QString("%1\nOFF").arg(registerId()); } painter->drawText(QRectF(0, 0, currentSize_.width(), currentSize_.height()), 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()); } } void Button::addMenuActions(QMenu *menu) { menu->addAction("置ON"); menu->addAction("置OFF"); menu->addAction("外观设置"); } void Button::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 requestReset(this); } if (action->text() == "置ON"){ emit requestSetON(this, true); } if (action->text() == "置OFF"){ emit requestSetON(this, false); } if (action->text() == "外观设置"){ QString fileName = QFileDialog::getOpenFileName(nullptr, "选择图片", "", "图片文件(*.png *.jpg *.bmp)"); if (!fileName.isEmpty()) { setCustomImage(fileName); } } } void Button::setCustomImage(const QString &imagePath) { imagePath_ = imagePath; loadImage(); update(); } void Button::loadImage() { if(!imagePath_.isEmpty()){ customPixmap_ = QPixmap(imagePath_); // if (!customPixmap_.isNull()){ // customPixmap_ = customPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); // } } } void Button::mousePressEvent(QGraphicsSceneMouseEvent *event) { // 检查是否点击了调整手柄 QRectF resizeHandle = QRectF(currentSize_.width() - resizeHandleSize_, currentSize_.height() - resizeHandleSize_, resizeHandleSize_, resizeHandleSize_); if (resizeHandle.contains(event->pos())) { resizing_ = true; resizeStartPos_ = event->scenePos(); originalSize_ = currentSize_; // 当前大小 event->accept(); } else { // 否则调用基类处理(移动等) QGraphicsObject::mousePressEvent(event); } } void Button::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (resizing_) { // 计算新的尺寸 QPointF delta = event->scenePos() - resizeStartPos_; qreal newWidth = originalSize_.width() + delta.x(); qreal newHeight = originalSize_.height() + delta.y(); // 限制最小尺寸 if (newWidth < 20) newWidth = 20; if (newHeight < 20) newHeight = 20; // 更新当前尺寸 currentSize_ = QSizeF(newWidth, newHeight); // 准备几何变化 prepareGeometryChange(); update(); // 更新显示 event->accept(); } else { QGraphicsObject::mouseMoveEvent(event); } } void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if (resizing_) { resizing_ = false; event->accept(); } else { QGraphicsObject::mouseReleaseEvent(event); } }