|
- #include "light.h"
- #include <QPainter>
- #include <QStyleOptionGraphicsItem>
- #include <QMenu>
- #include <QFileDialog>
- #include <QGraphicsSceneMouseEvent>
- #include <QCursor>
-
- Light::Light(const QString &type) : Item(type)
- {
-
- }
-
- QRectF Light::boundingRect() const
- {
- return QRectF(0, 0, currentSize_.width(), currentSize_.height());
- }
-
- void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
- {
- painter->setRenderHint(QPainter::Antialiasing);
-
- if (type_ == "指示灯") {
- if (state() && !onPixmap_.isNull()) {
- painter->drawPixmap(0, 0, currentSize_.width(), currentSize_.height(), onPixmap_);
- } else if (!state() && !offPixmap_.isNull()) {
- 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, currentSize_.width(), currentSize_.height());
- }
- }
- if (!registerId_.isEmpty()) {
- painter->save();
- painter->setFont(QFont("Arial", 8));
- painter->setPen(Qt::black);
- QString text = QString("%1").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());
- }
- }
-
- bool Light::state() const
- {
- return registerValue_ > 0;
- }
-
- void Light::addMenuActions(QMenu *menu)
- {
- menu->addAction("ON外观设置");
- menu->addAction("OFF外观设置");
- }
-
- void Light::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外观设置"){
- QString fileName = QFileDialog::getOpenFileName(nullptr,
- "选择图片",
- "",
- "图片文件(*.png *.jpg *.bmp)");
- if (!fileName.isEmpty())
- {
- setOnImage(fileName);
- }
- }
- if (action->text() == "OFF外观设置"){
- QString fileName = QFileDialog::getOpenFileName(nullptr,
- "选择图片",
- "",
- "图片文件(*.png *.jpg *.bmp)");
- if (!fileName.isEmpty())
- {
- setOffImage(fileName);
- }
- }
- }
-
- void Light::setOnImage(const QString& imagePath)
- {
- onImagePath_ = imagePath;
- loadImage();
- update();
- }
-
- void Light::setOffImage(const QString &imagePath)
- {
- offImagePath_ = imagePath;
- loadImage();
- update();
- }
-
- void Light::loadImage()
- {
- if (!onImagePath_.isEmpty()) {
- onPixmap_ = QPixmap(onImagePath_);
- }
- if (!offImagePath_.isEmpty()) {
- offPixmap_ = QPixmap(offImagePath_);
- }
- }
-
- 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);
- }
- }
|