@@ -1,8 +1,10 @@ | |||
#include "button.h" | |||
#include <QPainter> | |||
#include <QStyleOptionGraphicsItem> | |||
#include <QGraphicsSceneMouseEvent> | |||
#include <QMenu> | |||
#include <QFileDialog> | |||
#include <QCursor> | |||
Button::Button(const QString &type) : Item(type) | |||
{ | |||
@@ -11,7 +13,7 @@ Button::Button(const QString &type) : Item(type) | |||
QRectF Button::boundingRect() const | |||
{ | |||
return QRectF(0, 0, 50, 50); | |||
return QRectF(0, 0, currentSize_.width(), currentSize_.height()); | |||
} | |||
void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) | |||
@@ -21,10 +23,10 @@ void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QW | |||
if (type_ == "按钮") { | |||
if (!imagePath_.isEmpty() && !customPixmap_.isNull()) | |||
{ | |||
painter->drawPixmap(0,0,50,50,customPixmap_); | |||
painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), customPixmap_); | |||
} | |||
else{ | |||
painter->drawRect(0,0,50,50); | |||
painter->drawRect(0, 0, currentSize_.width(), currentSize_.height()); | |||
} | |||
} | |||
if (!registerId_.isEmpty()) { | |||
@@ -40,7 +42,7 @@ void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QW | |||
{ | |||
text = QString("%1\nOFF").arg(registerId()); | |||
} | |||
painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text); | |||
painter->drawText(QRectF(0, 0, currentSize_.width(), currentSize_.height()), Qt::AlignCenter, text); | |||
painter->restore(); | |||
} | |||
if (option->state & QStyle::State_Selected) { | |||
@@ -103,8 +105,61 @@ void Button::loadImage() | |||
{ | |||
if(!imagePath_.isEmpty()){ | |||
customPixmap_ = QPixmap(imagePath_); | |||
if (!customPixmap_.isNull()){ | |||
customPixmap_ = customPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); | |||
} | |||
// 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); | |||
} | |||
} |
@@ -15,9 +15,20 @@ public: | |||
void setCustomImage(const QString& imagePath); | |||
void loadImage(); | |||
// 鼠标事件处理 | |||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override; | |||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; | |||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; | |||
private: | |||
QString imagePath_; | |||
QPixmap customPixmap_; | |||
bool resizing_ = false; // 是否正在调整大小 | |||
QPointF resizeStartPos_; // 调整大小开始位置 | |||
QSizeF originalSize_; // 调整前的原始大小 | |||
QSizeF currentSize_ = QSizeF(50, 50); // 当前实际尺寸 | |||
int resizeHandleSize_ = 8; // 调整手柄大小 | |||
int resizeMargin_ = 5; // 调整边距 | |||
}; | |||
#endif // BUTTON_H |
@@ -3,6 +3,8 @@ | |||
#include <QStyleOptionGraphicsItem> | |||
#include <QMenu> | |||
#include <QFileDialog> | |||
#include <QGraphicsSceneMouseEvent> | |||
#include <QCursor> | |||
Light::Light(const QString &type) : Item(type) | |||
{ | |||
@@ -11,7 +13,7 @@ Light::Light(const QString &type) : Item(type) | |||
QRectF Light::boundingRect() const | |||
{ | |||
return QRectF(0, 0, 50, 50); | |||
return QRectF(0, 0, currentSize_.width(), currentSize_.height()); | |||
} | |||
void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) | |||
@@ -20,16 +22,16 @@ void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi | |||
if (type_ == "指示灯") { | |||
if (state() && !onPixmap_.isNull()) { | |||
painter->drawPixmap(0, 0, 50, 50, onPixmap_); | |||
painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), onPixmap_); | |||
} else if (!state() && !offPixmap_.isNull()) { | |||
painter->drawPixmap(0, 0, 50, 50, offPixmap_); | |||
painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), offPixmap_); | |||
} else { | |||
if (state()) { | |||
painter->setBrush(Qt::green); // 激活状态 | |||
} else { | |||
painter->setBrush(Qt::white); // 未激活状态 | |||
} | |||
painter->drawEllipse(0,0,50,50); | |||
painter->drawEllipse(0, 0, currentSize_.width(), currentSize_.height()); | |||
} | |||
} | |||
if (!registerId_.isEmpty()) { | |||
@@ -37,7 +39,7 @@ void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi | |||
painter->setFont(QFont("Arial", 8)); | |||
painter->setPen(Qt::black); | |||
QString text = QString("%1").arg(registerId()); | |||
painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text); | |||
painter->drawText(QRectF(0, 0, currentSize_.width(), currentSize_.height()), Qt::AlignCenter, text); | |||
painter->restore(); | |||
} | |||
if (option->state & QStyle::State_Selected) { | |||
@@ -115,14 +117,60 @@ void Light::loadImage() | |||
{ | |||
if (!onImagePath_.isEmpty()) { | |||
onPixmap_ = QPixmap(onImagePath_); | |||
if (!onPixmap_.isNull()) { | |||
onPixmap_ = onPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); | |||
} | |||
} | |||
if (!offImagePath_.isEmpty()) { | |||
offPixmap_ = QPixmap(offImagePath_); | |||
if (!offPixmap_.isNull()) { | |||
offPixmap_ = offPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); | |||
} | |||
} | |||
} | |||
void Light::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 Light::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 Light::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | |||
{ | |||
if (resizing_) { | |||
resizing_ = false; | |||
event->accept(); | |||
} else { | |||
QGraphicsObject::mouseReleaseEvent(event); | |||
} | |||
} |
@@ -18,11 +18,21 @@ public: | |||
void setOffImage(const QString& imagePath); | |||
void loadImage(); | |||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override; | |||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; | |||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; | |||
private: | |||
QString onImagePath_; // ON状态图片路径 | |||
QString offImagePath_; // OFF状态图片路径 | |||
QPixmap onPixmap_; // ON状态图片 | |||
QPixmap offPixmap_; // OFF状态图片 | |||
bool resizing_ = false; // 是否正在调整大小 | |||
QPointF resizeStartPos_; // 调整大小开始位置 | |||
QSizeF originalSize_; // 调整前的原始大小 | |||
QSizeF currentSize_ = QSizeF(50, 50); // 当前实际尺寸 | |||
int resizeHandleSize_ = 8; // 调整手柄大小 | |||
int resizeMargin_ = 5; // 调整边距 | |||
}; | |||
#endif // LIGHT_H |