123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #include "warning.h"
- #include "ui_warning.h"
- #include <QPainter>
- #include <QLayout>
- #include <QDebug>
- #include <QFile>
- #include <QStyle>
- #include <QMouseEvent>
- #include "OneShadowEffect.h"
- // #include "lhstylemanager.h"
- #include "LHQLogAPI.h"
- Warning::Warning(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Warning)
- {
- ui->setupUi(this);
- /* 设置无边框和背景透明 */
- this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
- this->setAttribute(Qt::WA_TranslucentBackground);
- /* 加载QSS */
- // QFile file(":/QSS/QSS/warning_light.qss");
- // if(file.open(QFile::ReadOnly))
- // {
- // QString styleSheet = file.readAll();
- // this->setStyleSheet(styleSheet);
- // file.close();
- // }
- /* 设置文字自动换行 */
- ui->label_Warn->setWordWrap(true);
- /* 设置文本居中 */
- // ui->label_Warn->setAlignment(Qt::AlignCenter);
- ui->label_Warn->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- /* 加载警告图标 */
- // ui->label_warnIcon->setStyleSheet(R"(border-image: url(:/ESM-8C_ICON/Tip/Tips2x.png);)");
- /* 阴影宽度是16 */
- this->layout()->setMargin(SHADOW_W);
- // m_shadow = new OneShadow(QSize(width() - SHADOW_W*2, height() - SHADOW_W*2),SHADOW_W);
- auto pShadow = new OneShadowEffect(this);
- this->setGraphicsEffect(pShadow);
- connect(ui->pBtn_close,SIGNAL(clicked()),this,SLOT(close()));
- connect(ui->pBtn_cancel,SIGNAL(clicked()),this,SLOT(close()));
- connect(ui->pBtn_ok,SIGNAL(clicked()),this,SLOT(do_ok()));
- /* 注册事件过滤器 */
- ui->pBtn_close->installEventFilter(this);
- }
- Warning::~Warning()
- {
- delete ui;
- }
- void Warning::setText(const QString &text)
- {
- /* 根据文本大小设置高度 */
- ui->label_Warn->setText(text);
- /* 重新设置位置 */
- resetLabelSize();
- moveWarnICON();
- }
- /* 只有一个确定按钮 */
- void Warning::setTextWithOneButton(const QString &text)
- {
- ui->pBtn_cancel->hide();
- // ui->pBtn_ok->move(ui->pBtn_cancel->x(),ui->pBtn_cancel->y());
- ui->label_Warn->setText(text);
- /* 重新设置位置 */
- resetLabelSize();
- moveWarnICON();
- }
- /* 设置QSS */
- void Warning::setQSS(const QString& qssPath)
- {
- QString qssFile = qssPath + "/warning.qss";
- QFile file(qssFile);
- if(file.open(QIODevice::ReadOnly))
- {
- QString stylesheet = file.readAll();
- this->setStyleSheet(stylesheet);
- file.close();
- } else
- {
- LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
- }
- }
- // void Warning::paintEvent(QPaintEvent *event)
- // {
- // QPainter painter(this);
- // painter.setRenderHint(QPainter::Antialiasing);
- // /* 绘制阴影 */
- // painter.drawImage(QPoint(0,0),m_shadow->image());
- // }
- /* 事件过滤器 */
- bool Warning::eventFilter(QObject *watched, QEvent *event)
- {
- if(watched == ui->pBtn_close)
- {
- if(event->type() == QEvent::Enter)
- {
- ui->pBtn_close->setProperty("Hover", true);
- ui->pBtn_close->style()->unpolish(ui->pBtn_close);
- ui->pBtn_close->style()->polish(ui->pBtn_close);
- return true;
- }else if(event->type() == QEvent::Leave)
- {
- ui->pBtn_close->setProperty("Hover", false);
- ui->pBtn_close->style()->unpolish(ui->pBtn_close);
- ui->pBtn_close->style()->polish(ui->pBtn_close);
- return true;
- }
- }
- return QWidget::eventFilter(watched,event);
- }
- /* 鼠标点击事件 */
- void Warning::mousePressEvent(QMouseEvent *event)
- {
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 鼠标移动事件 */
- void Warning::mouseMoveEvent(QMouseEvent *event)
- {
- // QRect rect = this->geometry();
- // rect.setBottom(rect.top()+50);
- auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
- QRect rect(point, ui->widget_Top->size());
- if(!rect.contains(m_lastPos))
- {
- event->accept();
- return;
- }
- int dx = event->globalX() - m_lastPos.x();
- int dy = event->globalY() - m_lastPos.y();
- move(x()+dx, y()+dy);
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 鼠标释放事件 */
- void Warning::mouseReleaseEvent(QMouseEvent *event)
- {
- event->accept();
- }
- void Warning::do_ok()
- {
- emit signal_ok();
- m_isOk = true;
- this->close();
- }
- /**
- * @brief 重新设置显示文字的区域大小,主要是设置宽度,为计算出左侧的图标位置
- * 字号是18,每个汉字的宽度、高度都是18,行高是27。
- * 文字显示区域最大宽度是306,显示17个汉字。
- * 阴影宽度是16,需要加上阴影宽度的坐标。
- *
- */
- void Warning::resetLabelSize()
- {
- int TextCount = ui->label_Warn->text().count();
- int width = TextCount * 18;
- if(width > 306)
- {
- width = 306;
- }
- // ui->label_Warn->setFixedWidth(width);
- ui->label_Warn->resize(width, ui->label_Warn->height());
- /* ui->widget是布局确定的大小,获取它的大小不准确 */
- int widgetWidth = this->width();
- int x = (widgetWidth - width) / 2;
- // int x = (widgetWidth - width - ui->label_warnIcon->width() - 12) / 2;
- // x = x + ui->label_warnIcon->width() + 12; /* 加上图标的宽度,和图标之间的间距 */
- ui->label_Warn->move(x, ui->label_Warn->y());
- }
- /* 移动警告图标 */
- void Warning::moveWarnICON()
- {
- int x = ui->label_Warn->x() - ui->label_warnIcon->width() - 12;
- int y = ui->label_Warn->y() + ui->label_Warn->height() / 2 - ui->label_warnIcon->height() / 2;
- ui->label_warnIcon->move(x,y);
- }
|