qt_demoe/devicebutton/devicebutton.cpp

214 lines
5.7 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";
buttonStyle = ButtonStyle_Police;
2021-05-17 06:43:33 +00:00
buttonColor = ButtonColor_Green;
2019-11-13 01:28:22 +00:00
type = "police";
2021-05-17 06:43:33 +00:00
imgName = QString(":/image/devicebutton/devicebutton_green_%1.png").arg(type);
2019-11-13 01:28:22 +00:00
isDark = false;
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
{
if (canMove) {
static QPoint lastPoint;
static bool isPressed = false;
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *e = static_cast<QMouseEvent *>(event);
if (this->rect().contains(e->pos()) && (e->button() == Qt::LeftButton)) {
lastPoint = e->pos();
isPressed = true;
}
} else if (event->type() == QEvent::MouseMove && isPressed) {
QMouseEvent *e = static_cast<QMouseEvent *>(event);
int dx = e->pos().x() - lastPoint.x();
int dy = e->pos().y() - lastPoint.y();
this->move(this->x() + dx, this->y() + dy);
return true;
} else if (event->type() == QEvent::MouseButtonRelease && isPressed) {
isPressed = false;
}
}
if (event->type() == QEvent::MouseButtonPress) {
emit clicked();
} else if (event->type() == QEvent::MouseButtonDblClick) {
emit doubleClicked();
}
return QWidget::eventFilter(watched, event);
}
2021-05-17 06:43:33 +00:00
bool DeviceButton::getCanMove() const
2019-11-13 01:28:22 +00:00
{
return this->canMove;
}
2021-05-17 06:43:33 +00:00
QString DeviceButton::getText() const
2019-11-13 01:28:22 +00:00
{
return this->text;
}
2021-05-17 06:43:33 +00:00
DeviceButton::ButtonStyle DeviceButton::getButtonStyle() const
2019-11-13 01:28:22 +00:00
{
return this->buttonStyle;
}
2021-05-17 06:43:33 +00:00
DeviceButton::ButtonColor DeviceButton::getButtonColor() const
2019-11-13 01:28:22 +00:00
{
2021-05-17 06:43:33 +00:00
return this->buttonColor;
2019-11-13 01:28:22 +00:00
}
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) {
2021-05-17 06:43:33 +00:00
imgName = QString(":/image/devicebutton/devicebutton_black_%1.png").arg(type);
2019-11-13 01:28:22 +00:00
} else {
2021-05-17 06:43:33 +00:00
imgName = QString(":/image/devicebutton/devicebutton_red_%1.png").arg(type);
2019-11-13 01:28:22 +00:00
}
isDark = !isDark;
this->update();
}
2021-05-17 06:43:33 +00:00
void DeviceButton::setCanMove(bool canMove)
2019-11-13 01:28:22 +00:00
{
this->canMove = canMove;
}
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();
}
}
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
}
2021-05-17 06:43:33 +00:00
void DeviceButton::setButtonColor(const DeviceButton::ButtonColor &buttonColor)
2019-11-13 01:28:22 +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();
}
2021-05-17 06:43:33 +00:00
if (buttonColor == ButtonColor_Green) {
imgName = QString(":/image/devicebutton/devicebutton_green_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Blue) {
imgName = QString(":/image/devicebutton/devicebutton_blue_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Gray) {
imgName = QString(":/image/devicebutton/devicebutton_gray_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Black) {
imgName = QString(":/image/devicebutton/devicebutton_black_%1.png").arg(type);
} else if (buttonColor == ButtonColor_Red) {
2019-11-13 01:28:22 +00:00
checkAlarm();
if (!timer->isActive()) {
timer->start();
}
}
this->update();
}