120 lines
3.2 KiB
C++
120 lines
3.2 KiB
C++
#include "messageDialog.h"
|
|
#include "ui_messageDialog.h"
|
|
#include <QtMath>
|
|
|
|
int getStringLength(const QString& str)
|
|
{
|
|
int chineseCharCount = 0;
|
|
int englishCharCount = 0;
|
|
for(QChar ch : str)
|
|
{
|
|
ushort unicode = ch.unicode();
|
|
if(unicode >= 0x4E00 && unicode <= 0x9FFF)
|
|
chineseCharCount++;
|
|
else if (ch.isLetter())
|
|
englishCharCount++;
|
|
}
|
|
|
|
return chineseCharCount + englishCharCount * 0.6; //一个英文字符大概是0.6个中文字符长度
|
|
}
|
|
int getIndexForLength(const QString& str, int maxLength)
|
|
{
|
|
float length = 0.0;
|
|
int index = 0;
|
|
for(QChar ch : str)
|
|
{
|
|
ushort unicode = ch.unicode();
|
|
if(unicode >= 0x4E00 && unicode <= 0x9FFF)
|
|
length = length + 1.0;
|
|
else if (ch.isLetter())
|
|
length = length + 0.6;
|
|
|
|
if(qCeil(length) >= maxLength)
|
|
break;
|
|
else
|
|
index++;
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
MessageDialog::MessageDialog(QWidget *parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::messageDialog)
|
|
{
|
|
ui->setupUi(this);
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
ui->btnClose->setVisible(false);
|
|
|
|
connect(ui->btnConfrim, SIGNAL(clicked()), this, SLOT(onBtnClicked_confirm()));
|
|
connect(ui->btnYes, SIGNAL(clicked()), this, SLOT(onBtnClicked_yes()));
|
|
connect(ui->btnNo, SIGNAL(clicked()), this, SLOT(onBtnClicked_no()));
|
|
}
|
|
|
|
MessageDialog::~MessageDialog()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MessageDialog::setType(MessageDialogType type)
|
|
{
|
|
if(type == type_information)
|
|
{
|
|
ui->typeLabel->setStyleSheet("border-image: url(:/images/icon_information.png);");
|
|
ui->typeColor->setStyleSheet("background-color:rgb(64,182,113);");
|
|
ui->stackedWidget->setCurrentIndex(0);
|
|
}
|
|
else if(type == type_question)
|
|
{
|
|
ui->typeLabel->setStyleSheet("border-image: url(:/images/icon_exclamation.png);");
|
|
ui->typeColor->setStyleSheet("background-color:rgb(245,166,88);");
|
|
ui->stackedWidget->setCurrentIndex(1);
|
|
}
|
|
else if(type == type_warning)
|
|
{
|
|
ui->typeLabel->setStyleSheet("border-image: url(:/images/icon_no.png);");
|
|
ui->typeColor->setStyleSheet("background-color:rgb(200,68,56);");
|
|
ui->stackedWidget->setCurrentIndex(0);
|
|
}
|
|
}
|
|
|
|
void MessageDialog::setMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
|
|
{
|
|
setType(type);
|
|
int maxLength = 25;
|
|
ui->labelTitle->setText(strTitle);
|
|
//qInfo() << getStringLength(strContent);
|
|
if(getStringLength(strContent) > maxLength)
|
|
{
|
|
QString strText(strContent);
|
|
int index = getIndexForLength(strContent, maxLength);
|
|
//qInfo() << index;
|
|
strText.insert(index, "\n"); //实现简单自动换行
|
|
ui->labelContent->setText(strText);
|
|
}
|
|
else
|
|
ui->labelContent->setText(strContent);
|
|
}
|
|
|
|
void MessageDialog::onBtnClicked_confirm()
|
|
{
|
|
hide();
|
|
emit sgl_hide();
|
|
}
|
|
|
|
void MessageDialog::onBtnClicked_yes()
|
|
{
|
|
g_msgDlgBtn = btn_Yes;
|
|
hide();
|
|
emit sgl_hide();
|
|
}
|
|
|
|
void MessageDialog::onBtnClicked_no()
|
|
{
|
|
g_msgDlgBtn = btn_No;
|
|
hide();
|
|
emit sgl_hide();
|
|
}
|
|
|