新增通用按钮地图控件

master
feiyangqingyun 2019-11-13 09:28:22 +08:00
parent 73422ddce9
commit 7dcb02fb68
51 changed files with 679 additions and 1 deletions

View File

@ -25,3 +25,4 @@
| 22 | lineeditnext | 文本框回车焦点下移 | | 22 | lineeditnext | 文本框回车焦点下移 |
| 23 | zhtopy | 汉字转拼音 | | 23 | zhtopy | 汉字转拼音 |
| 24 | qwtdemo | qwt的源码版本无需插件直接源码集成到你的项目即可 | | 24 | qwtdemo | qwt的源码版本无需插件直接源码集成到你的项目即可 |
| 25 | buttondefence | 通用按钮地图效果 |

View File

@ -0,0 +1,213 @@
#pragma execution_character_set("utf-8")
#include "buttondefence.h"
#include "qpainter.h"
#include "qevent.h"
#include "qtimer.h"
#include "qdebug.h"
ButtonDefence::ButtonDefence(QWidget *parent) : QWidget(parent)
{
canMove = false;
text = "1";
buttonStyle = ButtonStyle_Police;
buttonStatus = ButtonStatus_Arming;
type = "police";
imgName = QString(":/image/btn_defence_disarming_%1.png").arg(type);
isDark = false;
timer = new QTimer(this);
timer->setInterval(500);
connect(timer, SIGNAL(timeout()), this, SLOT(checkAlarm()));
this->installEventFilter(this);
}
ButtonDefence::~ButtonDefence()
{
if (timer->isActive()) {
timer->stop();
}
}
void ButtonDefence::paintEvent(QPaintEvent *)
{
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);
}
bool ButtonDefence::eventFilter(QObject *watched, QEvent *event)
{
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);
}
bool ButtonDefence::getCanMove() const
{
return this->canMove;
}
QString ButtonDefence::getText() const
{
return this->text;
}
ButtonDefence::ButtonStyle ButtonDefence::getButtonStyle() const
{
return this->buttonStyle;
}
ButtonDefence::ButtonStatus ButtonDefence::getButtonStatus() const
{
return this->buttonStatus;
}
QSize ButtonDefence::sizeHint() const
{
return QSize(50, 50);
}
QSize ButtonDefence::minimumSizeHint() const
{
return QSize(10, 10);
}
void ButtonDefence::checkAlarm()
{
if (isDark) {
imgName = QString(":/image/btn_defence_error_%1.png").arg(type);
} else {
imgName = QString(":/image/btn_defence_alarm_%1.png").arg(type);
}
isDark = !isDark;
this->update();
}
void ButtonDefence::setCanMove(bool canMove)
{
this->canMove = canMove;
}
void ButtonDefence::setText(const QString &text)
{
if (this->text != text) {
this->text = text;
this->update();
}
}
void ButtonDefence::setButtonStyle(const ButtonDefence::ButtonStyle &buttonStyle)
{
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 {
type = "circle";
}
setButtonStatus(buttonStatus);
}
void ButtonDefence::setButtonStatus(const ButtonDefence::ButtonStatus &buttonStatus)
{
this->buttonStatus = buttonStatus;
isDark = false;
if (timer->isActive()) {
timer->stop();
}
if (buttonStatus == ButtonStatus_Arming) {
imgName = QString(":/image/btn_defence_arming_%1.png").arg(type);
} else if (buttonStatus == ButtonStatus_Disarming) {
imgName = QString(":/image/btn_defence_disarming_%1.png").arg(type);
} else if (buttonStatus == ButtonStatus_Bypass) {
imgName = QString(":/image/btn_defence_bypass_%1.png").arg(type);
} else if (buttonStatus == ButtonStatus_Error) {
imgName = QString(":/image/btn_defence_error_%1.png").arg(type);
} else if (buttonStatus == ButtonStatus_Alarm) {
checkAlarm();
if (!timer->isActive()) {
timer->start();
}
}
this->update();
}

View File

@ -0,0 +1,105 @@
#ifndef BUTTONDEFENCE_H
#define BUTTONDEFENCE_H
/**
* :feiyangqingyun(QQ:517216493) 2018-7-2
* 1: 22
* 2:
* 3:
* 4:
* 5:
* 6:
*/
#include <QWidget>
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT ButtonDefence : public QWidget
#else
class ButtonDefence : public QWidget
#endif
{
Q_OBJECT
Q_ENUMS(ButtonStyle)
Q_ENUMS(ButtonStatus)
Q_PROPERTY(bool canMove READ getCanMove WRITE setCanMove)
Q_PROPERTY(QString text READ getText WRITE setText)
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
Q_PROPERTY(ButtonStatus buttonStatus READ getButtonStatus WRITE setButtonStatus)
public:
//防区样式 圆形、警察、气泡、气泡2、消息、消息2
enum ButtonStyle {
ButtonStyle_Circle = 0,
ButtonStyle_Police = 1,
ButtonStyle_Bubble = 2,
ButtonStyle_Bubble2 = 3,
ButtonStyle_Msg = 4,
ButtonStyle_Msg2 = 5
};
//防区状态 布防、撤防、报警、旁路、故障
enum ButtonStatus {
ButtonStatus_Arming = 0,
ButtonStatus_Disarming = 1,
ButtonStatus_Alarm = 2,
ButtonStatus_Bypass = 3,
ButtonStatus_Error = 4
};
explicit ButtonDefence(QWidget *parent = 0);
~ButtonDefence();
protected:
void paintEvent(QPaintEvent *);
bool eventFilter(QObject *watched, QEvent *event);
private:
bool canMove; //是否可移动
QString text; //显示文字
ButtonStyle buttonStyle; //防区样式
ButtonStatus buttonStatus; //防区状态
QString type; //图片末尾类型
QString imgName; //背景图片名称
bool isDark; //是否加深报警
QTimer *timer; //报警闪烁定时器
private slots:
void checkAlarm();
public:
bool getCanMove() const;
QString getText() const;
ButtonStyle getButtonStyle() const;
ButtonStatus getButtonStatus() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置可移动
void setCanMove(bool canMove);
//设置显示文字
void setText(const QString &text);
//设置防区样式
void setButtonStyle(const ButtonStyle &buttonStyle);
//设置防区状态
void setButtonStatus(const ButtonStatus &buttonStatus);
Q_SIGNALS:
void clicked();
void doubleClicked();
};
#endif //BUTTONDEFENCE_H

View File

@ -0,0 +1,25 @@
#-------------------------------------------------
#
# Project created by QtCreator 2018-07-04T09:39:27
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = buttondefence
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += frmbuttondefence.cpp
SOURCES += buttondefence.cpp
HEADERS += frmbuttondefence.h
HEADERS += buttondefence.h
FORMS += frmbuttondefence.ui
RESOURCES += main.qrc

View File

@ -0,0 +1,71 @@
#include "frmbuttondefence.h"
#include "ui_frmbuttondefence.h"
#include "buttondefence.h"
#include "qdebug.h"
frmButtonDefence::frmButtonDefence(QWidget *parent) : QWidget(parent), ui(new Ui::frmButtonDefence)
{
ui->setupUi(this);
this->initForm();
}
frmButtonDefence::~frmButtonDefence()
{
delete ui;
}
void frmButtonDefence::initForm()
{
//设置背景地图
ui->labMap->setStyleSheet("border-image:url(:/image/bg_call.jpg);");
btn1 = new ButtonDefence(ui->labMap);
btn1->setText("#1");
btn1->setGeometry(5, 5, 35, 35);
btn2 = new ButtonDefence(ui->labMap);
btn2->setText("#2");
btn2->setGeometry(45, 5, 35, 35);
btn3 = new ButtonDefence(ui->labMap);
btn3->setText("#3");
btn3->setGeometry(85, 5, 35, 35);
btnStyle << ui->btnCircle << ui->btnPolice << ui->btnBubble << ui->btnBubble2 << ui->btnMsg << ui->btnMsg2;
foreach (QPushButton *btn, btnStyle) {
connect(btn, SIGNAL(clicked(bool)), this, SLOT(changeStyle()));
}
btnStatus << ui->btnArming << ui->btnDisarming << ui->btnAlarm << ui->btnBypass << ui->btnError;
foreach (QPushButton *btn, btnStatus) {
connect(btn, SIGNAL(clicked(bool)), this, SLOT(changeStatus()));
}
}
void frmButtonDefence::changeStyle()
{
QPushButton *btn = (QPushButton *)sender();
int index = btnStyle.indexOf(btn);
ButtonDefence::ButtonStyle style = (ButtonDefence::ButtonStyle)index;
btn1->setButtonStyle(style);
btn2->setButtonStyle(style);
btn3->setButtonStyle(style);
}
void frmButtonDefence::changeStatus()
{
QPushButton *btn = (QPushButton *)sender();
int index = btnStatus.indexOf(btn);
ButtonDefence::ButtonStatus style = (ButtonDefence::ButtonStatus)index;
btn1->setButtonStatus(style);
btn2->setButtonStatus(style);
btn3->setButtonStatus(style);
}
void frmButtonDefence::on_ckCanMove_stateChanged(int arg1)
{
bool canMove = (arg1 != 0);
btn1->setCanMove(canMove);
btn2->setCanMove(canMove);
btn3->setCanMove(canMove);
}

View File

@ -0,0 +1,37 @@
#ifndef FRMBUTTONDEFENCE_H
#define FRMBUTTONDEFENCE_H
#include <QWidget>
class ButtonDefence;
class QPushButton;
namespace Ui
{
class frmButtonDefence;
}
class frmButtonDefence : public QWidget
{
Q_OBJECT
public:
explicit frmButtonDefence(QWidget *parent = 0);
~frmButtonDefence();
private slots:
void initForm();
void changeStyle();
void changeStatus();
void on_ckCanMove_stateChanged(int arg1);
private:
Ui::frmButtonDefence *ui;
ButtonDefence *btn1;
ButtonDefence *btn2;
ButtonDefence *btn3;
QList<QPushButton *> btnStyle;
QList<QPushButton *> btnStatus;
};
#endif // FRMBUTTONDEFENCE_H

View File

@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmButtonDefence</class>
<widget class="QWidget" name="frmButtonDefence">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>500</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="labMap">
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="btnCircle">
<property name="text">
<string>圆形</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnPolice">
<property name="text">
<string>警察</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBubble">
<property name="text">
<string>气泡</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBubble2">
<property name="text">
<string>气泡2</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMsg">
<property name="text">
<string>消息</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMsg2">
<property name="text">
<string>消息2</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnArming">
<property name="text">
<string>布防</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnDisarming">
<property name="text">
<string>撤防</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnAlarm">
<property name="text">
<string>报警</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnBypass">
<property name="text">
<string>旁路</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnError">
<property name="text">
<string>故障</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckCanMove">
<property name="text">
<string>可移动</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 953 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 848 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 919 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 416 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

31
buttondefence/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmbuttondefence.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
frmButtonDefence w;
w.setWindowTitle("防区按钮控件");
w.show();
return a.exec();
}

35
buttondefence/main.qrc Normal file
View File

@ -0,0 +1,35 @@
<RCC>
<qresource prefix="/">
<file>image/btn_defence_alarm_bubble.png</file>
<file>image/btn_defence_alarm_bubble2.png</file>
<file>image/btn_defence_alarm_circle.png</file>
<file>image/btn_defence_alarm_msg.png</file>
<file>image/btn_defence_alarm_msg2.png</file>
<file>image/btn_defence_alarm_police.png</file>
<file>image/btn_defence_arming_bubble.png</file>
<file>image/btn_defence_arming_bubble2.png</file>
<file>image/btn_defence_arming_circle.png</file>
<file>image/btn_defence_arming_msg.png</file>
<file>image/btn_defence_arming_msg2.png</file>
<file>image/btn_defence_arming_police.png</file>
<file>image/btn_defence_bypass_bubble.png</file>
<file>image/btn_defence_bypass_bubble2.png</file>
<file>image/btn_defence_bypass_circle.png</file>
<file>image/btn_defence_bypass_msg.png</file>
<file>image/btn_defence_bypass_msg2.png</file>
<file>image/btn_defence_bypass_police.png</file>
<file>image/btn_defence_disarming_bubble.png</file>
<file>image/btn_defence_disarming_bubble2.png</file>
<file>image/btn_defence_disarming_circle.png</file>
<file>image/btn_defence_disarming_msg.png</file>
<file>image/btn_defence_disarming_msg2.png</file>
<file>image/btn_defence_disarming_police.png</file>
<file>image/btn_defence_error_bubble.png</file>
<file>image/btn_defence_error_bubble2.png</file>
<file>image/btn_defence_error_circle.png</file>
<file>image/btn_defence_error_msg.png</file>
<file>image/btn_defence_error_msg2.png</file>
<file>image/btn_defence_error_police.png</file>
<file>image/bg_call.jpg</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB