qt_demoe/control/devicebutton/devicebutton.cpp

259 lines
6.5 KiB
C++
Raw Normal View History

2019-11-13 01:28:22 +00:00
#pragma execution_character_set("utf-8")
2021-05-17 06:43:33 +00:00
#include "devicebutton.h"
2019-11-13 01:28:22 +00:00
#include "qpainter.h"
#include "qevent.h"
#include "qtimer.h"
#include "qdebug.h"
2021-05-17 06:43:33 +00:00
DeviceButton::DeviceButton(QWidget *parent) : QWidget(parent)
2019-11-13 01:28:22 +00:00
{
canMove = false;
text = "1";
2022-05-06 08:37:44 +00:00
colorNormal = "black";
colorAlarm = "red";
2019-11-13 01:28:22 +00:00
buttonStyle = ButtonStyle_Police;
2021-05-17 06:43:33 +00:00
buttonColor = ButtonColor_Green;
2019-11-13 01:28:22 +00:00
2022-04-30 10:17:02 +00:00
isPressed = false;
lastPoint = QPoint();
2019-11-13 01:28:22 +00:00
type = "police";
2022-05-06 08:37:44 +00:00
imgPath = ":/image/devicebutton/devicebutton";
imgName = QString("%1_green_%2.png").arg(imgPath).arg(type);
2019-11-13 01:28:22 +00:00
2022-05-06 08:37:44 +00:00
isDark = false;
2019-11-13 01:28:22 +00:00
timer = new QTimer(this);
timer->setInterval(500);
connect(timer, SIGNAL(timeout()), this, SLOT(checkAlarm()));
this->installEventFilter(this);
}
2021-05-17 06:43:33 +00:00
DeviceButton::~DeviceButton()
2019-11-13 01:28:22 +00:00
{
if (timer->isActive()) {
timer->stop();
}
}
2021-05-17 06:43:33 +00:00
void DeviceButton::paintEvent(QPaintEvent *)
2019-11-13 01:28:22 +00:00
{
double width = this->width();
double height = this->height();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//绘制背景图
QImage img(imgName);
if (!img.isNull()) {
img = img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
painter.drawImage(0, 0, img);
}
//计算字体
QFont font;
font.setPixelSize(height * 0.37);
font.setBold(true);
//自动计算文字绘制区域,绘制防区号
QRectF rect = this->rect();
if (buttonStyle == ButtonStyle_Police) {
double y = (30 * height / 60);
rect = QRectF(0, y, width, height - y);
} else if (buttonStyle == ButtonStyle_Bubble) {
double y = (8 * height / 60);
rect = QRectF(0, 0, width, height - y);
} else if (buttonStyle == ButtonStyle_Bubble2) {
double y = (13 * height / 60);
rect = QRectF(0, 0, width, height - y);
font.setPixelSize(width * 0.33);
} else if (buttonStyle == ButtonStyle_Msg) {
double y = (17 * height / 60);
rect = QRectF(0, 0, width, height - y);
} else if (buttonStyle == ButtonStyle_Msg2) {
double y = (17 * height / 60);
rect = QRectF(0, 0, width, height - y);
}
//绘制文字标识
painter.setFont(font);
painter.setPen(Qt::white);
painter.drawText(rect, Qt::AlignCenter, text);
}
2021-05-17 06:43:33 +00:00
bool DeviceButton::eventFilter(QObject *watched, QEvent *event)
2019-11-13 01:28:22 +00:00
{
2022-04-30 10:17:02 +00:00
//识别鼠标 按下+移动+松开+双击 等事件
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (event->type() == QEvent::MouseButtonPress) {
//限定鼠标左键
if (mouseEvent->button() == Qt::LeftButton) {
lastPoint = mouseEvent->pos();
isPressed = true;
emit clicked();
return true;
}
} else if (event->type() == QEvent::MouseMove) {
//允许拖动并且鼠标按下准备拖动
if (canMove && isPressed) {
int dx = mouseEvent->pos().x() - lastPoint.x();
int dy = mouseEvent->pos().y() - lastPoint.y();
2019-11-13 01:28:22 +00:00
this->move(this->x() + dx, this->y() + dy);
return true;
}
2022-04-30 10:17:02 +00:00
} else if (event->type() == QEvent::MouseButtonRelease) {
isPressed = false;
2019-11-13 01:28:22 +00:00
} else if (event->type() == QEvent::MouseButtonDblClick) {
emit doubleClicked();
}
return QWidget::eventFilter(watched, event);
}
2021-05-17 06:43:33 +00:00
QSize DeviceButton::sizeHint() const
2019-11-13 01:28:22 +00:00
{
return QSize(50, 50);
}
2021-05-17 06:43:33 +00:00
QSize DeviceButton::minimumSizeHint() const
2019-11-13 01:28:22 +00:00
{
return QSize(10, 10);
}
2021-05-17 06:43:33 +00:00
void DeviceButton::checkAlarm()
2019-11-13 01:28:22 +00:00
{
if (isDark) {
2022-05-06 08:37:44 +00:00
imgName = QString("%1_%2_%3.png").arg(imgPath).arg(colorNormal).arg(type);
2019-11-13 01:28:22 +00:00
} else {
2022-05-06 08:37:44 +00:00
imgName = QString("%1_%2_%3.png").arg(imgPath).arg(colorAlarm).arg(type);
2019-11-13 01:28:22 +00:00
}
isDark = !isDark;
this->update();
}
2023-09-08 05:51:39 +00:00
bool DeviceButton::getCanMove() const
{
return this->canMove;
}
2021-05-17 06:43:33 +00:00
void DeviceButton::setCanMove(bool canMove)
2019-11-13 01:28:22 +00:00
{
this->canMove = canMove;
}
2023-09-08 05:51:39 +00:00
QString DeviceButton::getText() const
{
return this->text;
}
2021-05-17 06:43:33 +00:00
void DeviceButton::setText(const QString &text)
2019-11-13 01:28:22 +00:00
{
if (this->text != text) {
this->text = text;
this->update();
}
}
2023-09-08 05:51:39 +00:00
QString DeviceButton::getColorNormal() const
{
return this->colorNormal;
}
2022-05-06 08:37:44 +00:00
void DeviceButton::setColorNormal(const QString &colorNormal)
{
if (this->colorNormal != colorNormal) {
this->colorNormal = colorNormal;
this->update();
}
}
2023-09-08 05:51:39 +00:00
QString DeviceButton::getColorAlarm() const
{
return this->colorAlarm;
}
2022-05-06 08:37:44 +00:00
void DeviceButton::setColorAlarm(const QString &colorAlarm)
{
if (this->colorAlarm != colorAlarm) {
this->colorAlarm = colorAlarm;
this->update();
}
}
2023-09-08 05:51:39 +00:00
DeviceButton::ButtonStyle DeviceButton::getButtonStyle() const
{
return this->buttonStyle;
}
2021-05-17 06:43:33 +00:00
void DeviceButton::setButtonStyle(const DeviceButton::ButtonStyle &buttonStyle)
2019-11-13 01:28:22 +00:00
{
this->buttonStyle = buttonStyle;
if (buttonStyle == ButtonStyle_Circle) {
type = "circle";
} else if (buttonStyle == ButtonStyle_Police) {
type = "police";
} else if (buttonStyle == ButtonStyle_Bubble) {
type = "bubble";
} else if (buttonStyle == ButtonStyle_Bubble2) {
type = "bubble2";
} else if (buttonStyle == ButtonStyle_Msg) {
type = "msg";
} else if (buttonStyle == ButtonStyle_Msg2) {
type = "msg2";
} else {
2020-12-24 10:00:09 +00:00
type = "circle";
2019-11-13 01:28:22 +00:00
}
2021-05-17 06:43:33 +00:00
setButtonColor(buttonColor);
2019-11-13 01:28:22 +00:00
}
2023-09-08 05:51:39 +00:00
DeviceButton::ButtonColor DeviceButton::getButtonColor() const
{
return this->buttonColor;
}
2021-05-17 06:43:33 +00:00
void DeviceButton::setButtonColor(const DeviceButton::ButtonColor &buttonColor)
2019-11-13 01:28:22 +00:00
{
2022-05-06 08:37:44 +00:00
//先停止定时器
2021-05-17 06:43:33 +00:00
this->buttonColor = buttonColor;
2019-11-13 01:28:22 +00:00
isDark = false;
if (timer->isActive()) {
timer->stop();
}
2022-05-06 08:37:44 +00:00
QString color;
2021-05-17 06:43:33 +00:00
if (buttonColor == ButtonColor_Green) {
2022-05-06 08:37:44 +00:00
color = "green";
2021-05-17 06:43:33 +00:00
} else if (buttonColor == ButtonColor_Blue) {
2022-05-06 08:37:44 +00:00
color = "blue";
2021-05-17 06:43:33 +00:00
} else if (buttonColor == ButtonColor_Gray) {
2022-05-06 08:37:44 +00:00
color = "gray";
2021-05-17 06:43:33 +00:00
} else if (buttonColor == ButtonColor_Black) {
2022-05-06 08:37:44 +00:00
color = "black";
2021-07-02 09:19:02 +00:00
} else if (buttonColor == ButtonColor_Purple) {
2022-05-06 08:37:44 +00:00
color = "purple";
2021-07-02 09:19:02 +00:00
} else if (buttonColor == ButtonColor_Yellow) {
2022-05-06 08:37:44 +00:00
color = "yellow";
2021-05-17 06:43:33 +00:00
} else if (buttonColor == ButtonColor_Red) {
2022-05-06 08:37:44 +00:00
color = "red";
} else {
color = "green";
}
//如果和报警颜色一致则主动启动定时器切换报警颜色
imgName = QString("%1_%2_%3.png").arg(imgPath).arg(color).arg(type);
if (color == colorAlarm) {
2019-11-13 01:28:22 +00:00
checkAlarm();
if (!timer->isActive()) {
timer->start();
}
}
this->update();
}