FaceAccess/Linguist/utility/UiTools.cpp
2024-07-11 11:27:12 +08:00

3147 lines
101 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "UiTools.h"
#include <QHBoxLayout>
#include <QDebug>
#include <QFontDatabase>
#include <QPainter>
#include <QTime>
#include <QBitmap>
#include <QMouseEvent>
#include <QTranslator>
#include <QTextCodec>
#include <QGraphicsDropShadowEffect>
#include <QScrollBar>
#include <QScroller>
#include <QCalendarWidget>
#include <QScreen>
#include <QTimer>
#include <stdio.h>
#include "UiConfig.h"
#include "rw_zlog.h"
#include "ScopeGuard.h"
WidgetWithBackstageInterface::WidgetWithBackstageInterface(QWidget *parent) : QWidget(parent), m_backstageIf(nullptr)
{
}
WidgetWithBackstageInterface::~WidgetWithBackstageInterface()
{
}
void WidgetWithBackstageInterface::setBackstageUiinterface(BackstageInterfaceForUi* interface)
{
m_backstageIf = interface;
}
CustomDialog::CustomDialog(const QString& okBtnText, QObject *bgWidget, const QString& cancelBtnText, int timeout) : QDialog(), m_timeout(timeout)
{
dialogWidth = static_cast<int>(UiConfig::GetInstance()->getUiWidth() * 0.8);
dialogHeight = static_cast<int>(UiConfig::GetInstance()->getUiHeight() * 0.2);
resize(dialogWidth, dialogHeight);
QPalette palette(this->palette());
hbtmLayout = new QHBoxLayout();
if("" != cancelBtnText){
m_btn_cancle = new QPushButton(this);
m_btn_cancle->setFixedSize(dialogWidth / 2, dialogHeight / 4);
m_btn_cancle->setText(cancelBtnText);
palette.setColor(QPalette::ButtonText,QColor(92, 89, 93, 255));
m_btn_cancle->setPalette(palette);
m_btn_cancle->setStyleSheet("background-color: transparent;border-top: 1px solid #DFDDEB;border-right: 1px solid #DFDDEB;");
connect(m_btn_cancle, SIGNAL(clicked(bool)), this, SLOT(slotCancleBtnClicked()));
hbtmLayout->addWidget(m_btn_cancle);
}
if("" != okBtnText){
m_btn_ok = new QPushButton(this);
m_btn_ok->setDefault(true);
if("" != cancelBtnText){
m_btn_ok->setFixedSize(dialogWidth / 2, dialogHeight / 4);
m_btn_ok->setStyleSheet("background-color: transparent;border-top: 1px solid #DFDDEB;border-left: 1px solid #DFDDEB;");
}
else {
m_btn_ok->setFixedSize(dialogWidth, dialogHeight / 4);
m_btn_ok->setStyleSheet("background-color: transparent;border-top: 1px solid #DFDDEB;");
}
m_btn_ok->setText(okBtnText);
palette.setColor(QPalette::ButtonText,QColor(15, 116, 248, 0xFF));
m_btn_ok->setPalette(palette);
connect(m_btn_ok, SIGNAL(clicked(bool)), this, SLOT(slotConfirmBtnClicked()));
hbtmLayout->addWidget(m_btn_ok);
}
hbtmLayout->setSpacing(0);
hbtmLayout->setMargin(0);
if(nullptr != bgWidget){
connect(this, SIGNAL(signalShowShadowPage(bool)), bgWidget, SLOT(slotShowShadowPage(bool)));
emit signalShowShadowPage(true);
}
if(m_timeout){
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotCancleBtnClicked()) );
m_timer->start(m_timeout);
}
}
CustomDialog::~CustomDialog()
{
emit signalShowShadowPage(false);
}
void CustomDialog::slotCancleBtnClicked()
{
done(Rejected);
}
void CustomDialog::slotConfirmBtnClicked()
{
done(Accepted);
}
void CustomDialog::paintEvent(QPaintEvent *event)
{
/*
//圆角
QBitmap bmp(this->size());
bmp.fill();
QPainter p(&bmp);
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
p.drawRoundedRect(bmp.rect(),10,10);
setMask(bmp);
//绘制边框
QStyleOption opt;
opt.initFrom(this);
QPainter pr(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &pr, this);//绘制样式
*/
QPainter painter(this);
if(UiConfig::GetInstance()->isRkDevice()){
painter.fillRect(this->rect(), QColor(UiConfig::SUB_STYLE_COLOR_RED, UiConfig::SUB_STYLE_COLOR_GREEN, UiConfig::SUB_STYLE_COLOR_BLUE, UiConfig::SUB_STYLE_COLOR_TRAN));
}
else{
painter.fillRect(this->rect(), QColor(UiConfig::MAIN_STYLE_COLOR_RED, UiConfig::MAIN_STYLE_COLOR_GREEN, UiConfig::MAIN_STYLE_COLOR_BLUE, UiConfig::MAIN_STYLE_COLOR_TRAN));
}
return QWidget::paintEvent(event);
}
bool CustomDialog::eventFilter(QObject *watched, QEvent *event)
{
if( watched == this){
if(event->type() == QEvent::MouseButtonPress){
qDebug() << "CustomDialog mouse clicked";
if(m_timeout){
m_timer->start(m_timeout);
}
}
}
else{
return QWidget::eventFilter(watched, event);
}
return false;
}
bool CustomDialog::event(QEvent *event)
{
switch( event->type() )
{
case QEvent::TouchBegin:
qDebug() << "CustomDialog touch begin";
event->accept();
if(m_timeout){
m_timer->start(m_timeout);
}
//return true;
break;
case QEvent::TouchUpdate:
qDebug() << "CustomDialog touch update";
event->accept();
//return true;
break;
case QEvent::TouchEnd:
qDebug() << "CustomDialog touch end";
event->accept();
if(m_timeout){
m_timer->start(m_timeout);
}
//return true;
break;
default:
break;
}
return QWidget::event(event);
}
#if 1
PasswordDialog::PasswordDialog(const QString& title, const QString& tip1, int timeout, QObject *bgWidget, const QString& pwd, const QString& tip0,
const QString& okBtn, const QString& cancelBtn) : CustomDialog (okBtn, bgWidget, cancelBtn, timeout), m_bgWidget(bgWidget)
{
setAttribute(Qt::WA_AcceptTouchEvents);
QVBoxLayout* vbLayout = new QVBoxLayout();
const int fontSize = UiConfig::GetInstance()->getGlobalFontSize();
QFont ft;
QPalette palette(this->palette());
palette.setColor(QPalette::WindowText,QColor(92, 89, 93, 255));
if(!tip0.isNull()){
QLabel *labelTip0 = new QLabel(this);
labelTip0->setText(tip0);
ft.setPointSize(static_cast<int>(fontSize * 0.7));
labelTip0->setFont(ft);
labelTip0->setPalette(palette);
vbLayout->addWidget(labelTip0, 1, Qt::AlignLeft);
}
QLabel *m_label_msg = new QLabel(this);
m_label_msg->setText(title);
ft.setPointSize(fontSize);
m_label_msg->setFont(ft);
vbLayout->addWidget(m_label_msg, 2, Qt::AlignCenter);
QLabel *labelTip1 = new QLabel(this);
labelTip1->setText(tip1);
labelTip1->setPalette(palette);
ft.setPointSize(static_cast<int>(fontSize * 0.7));
labelTip1->setFont(ft);
vbLayout->addWidget(labelTip1, 1, Qt::AlignCenter);
m_lineEdit_pwd = new LineEditWithKeyboard(this);
keyboard::GetInstance(m_lineEdit_pwd);
if(!pwd.isEmpty()){
m_lineEdit_pwd->setText(pwd);
}
m_lineEdit_pwd->setEchoMode(QLineEdit::Password);
m_lineEdit_pwd->setFixedSize(dialogWidth / 2, dialogHeight / 4);
palette.setColor(QPalette::Text,QColor(92, 89, 93, 255)); //0x94, 0x9e, 0xba, 0xFF
m_lineEdit_pwd->setPalette(palette);
m_lineEdit_pwd->setStyleSheet("background:transparent;border-width:0;border-style:outset");
//m_lineEdit_pwd->setStyleSheet("m_lineEdit_pwd{border-bottom:1px solid #648EC9;}");//background-color: transparent;
m_btn_visable = new QPushButton(this);
setButtonBackImage(m_btn_visable, ":res/image/eye-close.png", 30, 30);
#if 0
m_btn_factory = new QPushButton(this);
m_btn_factory->setText(tr("恢复出厂"));
palette.setColor(QPalette::ButtonText,QColor(0x94, 0x9e, 0xba, 0xFF));
m_btn_factory->setPalette(palette);
m_btn_factory->setStyleSheet("QPushButton{background-color: transparent ;"
"border:0px;border-left:1px ;border-right:1px ;border-top:1px }");
#endif
QHBoxLayout* hbLayout_pwd = new QHBoxLayout();
hbLayout_pwd->addWidget(m_lineEdit_pwd);
hbLayout_pwd->addWidget(m_btn_visable);
hbLayout_pwd->setContentsMargins(dialogWidth / 4, 0, 0, 0);
vbLayout->addLayout(hbLayout_pwd, 1);
#if 0
vbLayout->addWidget(m_btn_factory, 1, Qt::AlignLeft | Qt::AlignTop);
#endif
vbLayout->addLayout(hbtmLayout, 1);
vbLayout->setSpacing(10);
vbLayout->setMargin(0);
setLayout(vbLayout);
setWindowTitle(tr("工程密码"));
connect(m_btn_visable, SIGNAL(clicked(bool)), this, SLOT(slotPwdVisableBtnClicked()));
#if 0
connect(m_btn_factory, SIGNAL(clicked(bool)), this, SLOT(slotRestoreBtnClicked()));
#endif
}
PasswordDialog::~PasswordDialog()
{
}
void PasswordDialog::slotPwdVisableBtnClicked()
{
if(m_mode){
m_lineEdit_pwd->setEchoMode(QLineEdit::Password);
setButtonBackImage(m_btn_visable, ":res/image/eye-close.png", 30, 30);
}else{
m_lineEdit_pwd->setEchoMode(QLineEdit::Normal);
setButtonBackImage(m_btn_visable, ":res/image/eye-open.png", 30, 30);
}
m_mode = !m_mode;
}
void PasswordDialog::slotRestoreBtnClicked()
{
QuestionDialog dlg(tr("您确定恢复出厂设置吗?"), m_bgWidget, tr("恢复"));
if(QDialog::Accepted == dlg.exec()){
qDebug() << "factory reset!";
emit signalRestoreFactory();
}
}
void PasswordDialog::slotConfirmBtnClicked()
{
m_pwd = m_lineEdit_pwd->text();
CustomDialog::slotConfirmBtnClicked();
}
const QString& PasswordDialog::getPwd() const
{
return m_pwd;
}
RadioBtnChooseDialog::RadioBtnChooseDialog(const QVector<QString>& labels, QObject *bgWidget, const int choose, const QVector<bool> *config, const QString& title) : CustomDialog(tr("确定"), bgWidget), m_chooseIndex(choose)
{
qDebug() << "RadioBtnChooseDialog::RadioBtnChooseDialog()";
//setWindowFlag(Qt::WindowCloseButtonHint);
//QPalette palette(this->palette());
//palette.setColor(QPalette::WindowText,QColor(0x0e, 0xbc, 0xf8, 0xFF));
vLayout = new QVBoxLayout();
if(title != ""){
m_label_title = new QLabel(this);
m_label_title->setText(title);
//m_label_title->setPalette(palette);
QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addWidget(m_label_title, 1, Qt::AlignLeft);
vLayout->addLayout(hLayout);
}
m_option_num = labels.size();
m_rdo = new QRadioButton[m_option_num];
for(int i = 0; i < m_option_num; i ++)
{
QWidget* widget = new QWidget(this);
if(labels[i] != ""){
QLabel* label = new QLabel(this);
label->setText(labels[i]);
//label->setPalette(palette);
widget = label;
}
m_rdo[i].setStyleSheet("QRadioButton::indicator:unchecked{image: url(:/res/image/select_1.png);}"
"QRadioButton::indicator:checked{image: url(:/res/image/select_2.png);}");
m_rdo[i].setFocusPolicy(Qt::NoFocus);
// "QRadioButton::indicator:checked{image: url(:/res/image/select_2.png);width: 30px;height: 30px;}");
connect(m_rdo + i, SIGNAL(pressed()), this, SLOT(slotRadioButtonPressed()));
if(nullptr != config){
if(false == (*config)[i]){
widget->hide();
m_rdo[i].hide();
continue;
}
}
QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addWidget(widget, 1, Qt::AlignLeft);
hLayout->addWidget(m_rdo + i, 1, Qt::AlignRight);
hLayout->setContentsMargins(dialogWidth / 10, dialogHeight / 10, dialogWidth / 10, dialogHeight / 10);
vLayout->addLayout(hLayout);
vLayout->setSpacing(0);
}
if(choose < m_option_num)
m_rdo[choose].setChecked(true);
vLayout->addLayout(hbtmLayout, 1);
vLayout->setMargin(0);
vLayout->setSpacing(5);
setLayout(vLayout);
}
RadioBtnChooseDialog::~RadioBtnChooseDialog()
{
qDebug() << "RadioBtnChooseDialog::~RadioBtnChooseDialog()";
delete [] m_rdo;
}
void RadioBtnChooseDialog::slotRadioButtonPressed()
{
for(int i = 0; i < m_option_num; i++)
{
if(sender() == static_cast<QObject*>(m_rdo + i))
{
m_chooseIndex = i;
break;
}
}
}
int RadioBtnChooseDialog::getChoosedIndex() const
{
return m_chooseIndex;
}
QuestionDialog::QuestionDialog(const QString& question, QObject *bgWidget, const QString& btnText) : CustomDialog(btnText, bgWidget)
{
QLabel* labelQues = new QLabel(this);
labelQues->setWordWrap(true);
labelQues->setText(question);
QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(labelQues, 90, Qt::AlignCenter);
vLayout->addLayout(hbtmLayout);
vLayout->setSpacing(dialogHeight / 5);
vLayout->setContentsMargins(0, dialogHeight / 10, 0, 0);
setLayout(vLayout);
}
InfoDialog::InfoDialog(const QString& info, QObject *bgWidget, const QString& btnText, int timeout) : CustomDialog(btnText, bgWidget, "", timeout)
{
QLabel* labelInfo = new QLabel(this);
labelInfo->setWordWrap(true);
labelInfo->setText(info);
QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(labelInfo, 90, Qt::AlignCenter);
vLayout->addLayout(hbtmLayout);
vLayout->setSpacing(dialogHeight / 5);
vLayout->setContentsMargins(0, dialogHeight / 10, 0, 0);
setLayout(vLayout);
}
SliderDialog::SliderDialog(int current, int min, int max, const QString& tip, QObject *bgWidget) : CustomDialog (tr("确定"), bgWidget)
{
m_labelCurrent = new QLabel(this);
m_labelCurrent->setText(QString::number(current));
QLabel* labelMin = new QLabel(this);
labelMin->setText(QString::number(min));
QLabel* labelMax = new QLabel(this);
labelMax->setText(QString::number(max));
m_slider = new QSlider(this);
m_slider->setOrientation(Qt::Horizontal);
m_slider->setMinimumHeight(UiConfig::GetInstance()->isRkDevice() ? 44 :60);
m_slider->setMinimum(min);
m_slider->setMaximum(max);
m_slider->setValue(current);
//m_slider->setSingleStep(10);
//m_slider->setGeometry(30,this->height()-28,200,25);
m_slider->setStyleSheet("QSlider::handle:horizontal{width:48px;background-color:rgb(255,255,255);margin:-22px 0px -22px 0px;border-radius:24px;}"
"QSlider::groove:horizontal{height:4px;background-color:rgb(219,219,219);}"
"QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
"QSlider::sub-page:horizontal{background-color:rgb(26,217,110);}");
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setOffset(4,4);
effect->setColor(QColor(0,0,0,50));
effect->setBlurRadius(10);
m_slider->setGraphicsEffect(effect);
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addWidget(labelMin, 1);
hLayout->addWidget(m_slider, 30);
hLayout->addWidget(labelMax, 1);
hLayout->setMargin(20);
hLayout->setSpacing(10);
QLabel* m_labelTip = new QLabel(this);
m_labelTip->setText(tip);
QPalette palette(this->palette());
palette.setColor(QPalette::WindowText,QColor(92, 89, 93, 255));
m_labelTip->setPalette(palette);
#if 0
QFont ft;
ft.setPointSize(static_cast<int>(15));
m_labelTip->setFont(ft);
#endif
QVBoxLayout* vbLayout = new QVBoxLayout();
vbLayout->addWidget(m_labelCurrent, 3, Qt::AlignCenter);
vbLayout->addLayout(hLayout, 7);
vbLayout->addWidget(m_labelTip, 1, Qt::AlignCenter);
vbLayout->addLayout(hbtmLayout, 2);
vbLayout->setSpacing(10);
vbLayout->setMargin(0);
setLayout(vbLayout);
//setWindowTitle(tr("音量设置"));
m_iCurrent = current;
};
SliderDialog::~SliderDialog()
{
}
void SliderDialog::slotValueChanged(int value)
{
m_iCurrent = value;
m_labelCurrent->setText(QString::number(value));
}
int SliderDialog::getValueSet() const
{
return m_iCurrent;
}
DateTimeDialog::DateTimeDialog(const QDateTime& dateTime, QObject *bgWidget, bool needDate, bool needTime) : CustomDialog (tr("确定"), bgWidget)
{
m_dateTimeSet = new QDateTimeEdit(this);
m_dateTimeSet->setDateTime(QDateTime::currentDateTime());
//m_dateTimeSet->setCalendarPopup(true);
QString format;
if(needDate){
format += "yyyy-MM-dd";
}
if(needTime){
format += needDate ? " " : "";
format += "HH:mm";
}
m_dateTimeSet->setDisplayFormat(format);
m_dateTimeSet->setFixedSize(static_cast<int>(dialogWidth * 0.8), static_cast<int>(dialogHeight * 0.5));
m_dateTimeSet->setFrame(false);
m_dateTimeSet->setAlignment(Qt::AlignCenter);
QFont ft;
//ft.setPointSize(static_cast<int>(dialogWidth * 0.05));
ft.setPointSize(static_cast<int>(m_dateTimeSet->font().pointSize() * 1.4));
m_dateTimeSet->setFont(ft);
m_dateTimeSet->setDateTime(dateTime);
/*
m_dateTimeSet->setStyleSheet("QDateTimeEdit::up-button,QTimeEdit::up-button,QDoubleSpinBox::up-button,QSpinBox::up-button"
"{subcontrol-origin:border;subcontrol-position:right;image: url(:res/image/up.png);};"
"QDateTimeEdit::down-button,QTimeEdit::down-button,QDoubleSpinBox::down-button,QSpinBox::down-button"
"{subcontrol-origin:border;subcontrol-position:left;image: url(:res/image/down.png); }");
m_dateTimeSet->setStyleSheet("QDateTimeEdit::up-button{background-image:url(:res/image/up.png);width: 55px;height: 40px;}"
"QDateTimeEdit::down-button{background-image:url(:res/image/down.png);width: 55px;height: 40px;}");
*/
m_dateTimeSet->setStyleSheet("background-color:transparent;");
QLabel* m_labelTip = new QLabel(this);
m_labelTip->setText(tr("点击对应时间项设置"));
QPalette palette(this->palette());
palette.setColor(QPalette::WindowText,QColor(92, 89, 93, 255));
m_labelTip->setPalette(palette);
QVBoxLayout* vbLayout = new QVBoxLayout();
vbLayout->addWidget(m_dateTimeSet, 3, Qt::AlignCenter);
vbLayout->addWidget(m_labelTip, 1, Qt::AlignCenter);
vbLayout->addLayout(hbtmLayout, 2);
vbLayout->setSpacing(10);
vbLayout->setMargin(0);
setLayout(vbLayout);
m_dateTimeSet->setCurrentSectionIndex(0);
}
QDateTime DateTimeDialog::getValueSet() const
{
return m_dataTimeVal;
}
void DateTimeDialog::slotConfirmBtnClicked()
{
m_dataTimeVal = m_dateTimeSet->dateTime();
CustomDialog::slotConfirmBtnClicked();
}
void LineEditInputDialog::init(const QStringList& labels)
{
//setWindowFlag(Qt::WindowCloseButtonHint);
//QPalette palette(this->palette());
//palette.setColor(QPalette::WindowText,QColor(0x0e, 0xbc, 0xf8, 0xFF));
vLayout = new QVBoxLayout();
formLayout = new QFormLayout();
m_rowNum = labels.size();
qDebug() << "row num:" << m_rowNum;
m_editRow = new LineEditWithKeyboard[m_rowNum];
m_stringRowInput = new QString[m_rowNum];
for(int i = 0; i < m_rowNum; i ++){
QLabel* rowLabel = new QLabel(this);
rowLabel->setText(labels[i]);
//rowLabel->setPalette(palette);
m_editRow[i].setStyleSheet("QLineEdit{border-bottom:1px solid #648EC9;}");
//palette.setColor(QPalette::Text,QColor(0x0e, 0xbc, 0xf8, 0xFF));
//m_editRow[i].setPalette(palette);
//m_editRow[i].setStyleSheet("QLineEdit{background-color: transparent ;border:2px solid #0ebcf8;}");
formLayout->addRow(rowLabel, m_editRow + i);
}
//formLayout->setSpacing(30);
formLayout->setContentsMargins(30, 50, 30, 30);
vLayout->addLayout(formLayout, 8);
vLayout->addLayout(hbtmLayout, 1);
vLayout->setMargin(0);
vLayout->setSpacing(10);
setLayout(vLayout);
move((UiConfig::GetInstance()->getUiWidth() - dialogWidth) / 2, UiConfig::GetInstance()->getUiHeight() / 3);
}
LineEditInputDialog::LineEditInputDialog(const QStringList& labels, QObject *bgWidget, const QString& confirBtm) :
CustomDialog(confirBtm, bgWidget), m_rowNum(0)
{
qDebug() << "LineEditInputDialog::LineEditInputDialog()";
init(labels);
}
LineEditInputDialog::LineEditInputDialog(const QString& label, QObject *bgWidget, const QString& confirBtm) :
CustomDialog(confirBtm, bgWidget), m_rowNum(0)
{
qDebug() << "LineEditInputDialog()";
QList<QString> labels = QList<QString>() << label;
init(labels);
}
LineEditInputDialog::~LineEditInputDialog()
{
qDebug() << "~LineEditInputDialog()";
delete []m_editRow;
delete []m_stringRowInput;
}
QString LineEditInputDialog::getRow(const int row) const
{
return (row < m_rowNum ? m_stringRowInput[row] : "");
}
void LineEditInputDialog::fillRow(const int row, const QString& str, bool readOnly)
{
if(row < m_rowNum){
m_editRow[row].setText(str);
if(readOnly){
m_editRow[row].setEnabled(false);
//m_editRow[row].setReadOnly(readOnly);
}
}
}
void LineEditInputDialog::slotConfirmBtnClicked()
{
for(int i = 0; i < m_rowNum; i ++)
{
m_stringRowInput[i] = m_editRow[i].text();
}
CustomDialog::slotConfirmBtnClicked();
}
ComSelectDialog::ComSelectDialog(const QStringList& titles, const QVector<QStringList>& sels,
QObject *bgWidget, const QString& confirBtm) :
CustomDialog(confirBtm, bgWidget), m_rowNum(0)
{
qDebug() << "ComSelectDialog()";
vLayout = new QVBoxLayout();
formLayout = new QFormLayout();
m_rowNum = titles.size();
qDebug() << "row num:" << m_rowNum;
for(int i = 0; i < m_rowNum; i ++){
QComboBox* cbSel = new QComboBox(this);
m_cbSels.push_back(cbSel);
formLayout->addRow(titles.at(i), cbSel);
if(i < sels.size()){
for(const auto& sel : sels.at(i)){
cbSel->addItem(sel);
}
}
}
//formLayout->setSpacing(30);
formLayout->setContentsMargins(30, 50, 30, 30);
vLayout->addLayout(formLayout, 8);
vLayout->addLayout(hbtmLayout, 1);
vLayout->setMargin(0);
vLayout->setSpacing(10);
setLayout(vLayout);
move((UiConfig::GetInstance()->getUiWidth() - dialogWidth) / 2, UiConfig::GetInstance()->getUiHeight() / 3);
}
ComSelectDialog::~ComSelectDialog()
{
qDebug() << "~ComSelectDialog()";
}
int ComSelectDialog::getRow(int row) const
{
return (row < m_rowNum ? m_cbSels.at(row)->currentIndex() : -1);
}
void ComSelectDialog::setRow(int row, int set, bool readOnly)
{
if(row < m_rowNum && set < m_cbSels.at(row)->count()){
m_cbSels[row]->setCurrentIndex(set);
if(readOnly){
m_cbSels[row]->setEnabled(false);
}
}
}
MsgDialog::MsgDialog(const QString& msg, QObject *bgWidget) : CustomDialog("", bgWidget, "")
{
QLabel* labelInfo = new QLabel(this);
labelInfo->setText(msg);
QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(labelInfo, 90, Qt::AlignCenter);
vLayout->setMargin(0);
setLayout(vLayout);
}
void IpInputDialog::slotTextChanged(const QString & t)
{
const QObject* send = sender();
for(int i=0; i<m_rowNum; i++){
if(send == m_editRow + i){
QStringList list = t.split(".");
for(int j=0; j<4; j++){
if(list.at(j).toInt() > 255){
InfoDialog(QObject::tr("请指定一个介于0和255之间的值")).exec();
list[j] = "255";
m_editRow[i].setText(list[0] + "." + list[1] + "." + list[2] + "." + list[3]);
QString posiStr;
for(int k=0; k<j + 1; k++){
posiStr += list[k];
if(k < 3){
posiStr += ".";
}
}
//qDebug() << "posiStr:" << posiStr;
m_editRow[i].setCursorPosition(posiStr.length());
break;
}
}
}
}
}
IpInputDialog::IpInputDialog(const QStringList& ips, QObject *bgWidget, const QString& confirBtm) :
LineEditInputDialog(ips, bgWidget, confirBtm)
{
qDebug() << "IpInputDialog()";
for(int i=0; i<m_rowNum; i++){
m_editRow[i].setInputMask("000.000.000.000");
connect(m_editRow + i, SIGNAL(textChanged(const QString &)), this, SLOT(slotTextChanged(const QString &)));
}
}
IpInputDialog::IpInputDialog(const QString& ips, QObject *bgWidget, const QString& confirBtm) :
LineEditInputDialog(ips, bgWidget, confirBtm)
{
qDebug() << "IpInputDialog()";
for(int i=0; i<m_rowNum; i++){
m_editRow[i].setInputMask("000.000.000.000");
}
}
IpInputDialog::~IpInputDialog()
{
qDebug() << "~IpInputDialog()";
}
void IpInputDialog::slotConfirmBtnClicked()
{
int i = 0;
for(i = 0; i < m_rowNum; i ++){
if(!checkip(m_editRow[i].text())){
InfoDialog(QObject::tr("设置有误,请重新输入!")).exec();
break;
}
}
if(i == m_rowNum){
LineEditInputDialog::slotConfirmBtnClicked();
}
}
ItemChoosePage::ItemChoosePage(const QVector<SetOpn>& items, int choose, QWidget *parent) : SettingUiPage(items, parent), m_chooseIndex(choose)
{
#if 0
const QString QSS_VerticalScrollBar( ""
"QScrollBar:vertical{" //垂直滑块整体
"min-width:55px;"
"background:rgba(255, 255, 255, 1);" //背景色 #FFFFFF
"padding-top:0px;" //上预留位置(放置向上箭头)
"padding-bottom:0px;" //下预留位置(放置向下箭头)
"padding-left:0px;" //左预留位置(美观)
"padding-right:0px;" //右预留位置(美观)
"border-left:0px solid grey;}"//左分割线 #d7d7d7
"QScrollBar::handle:vertical{"//滑块样式
"background:rgba(219, 219, 219, 1);" //滑块颜色
"border-radius:4px;" //边角圆润
"}" //滑块最大高度max-height:20px;
"QScrollBar::handle:vertical:hover{"//鼠标触及滑块样式
"background:rgba(219, 219, 219, 1);}" //滑块颜色
"QScrollBar::add-line:vertical{"//向下箭头样式
"background:url(:res/image/down.png) center no-repeat;}"
"QScrollBar::sub-line:vertical{"//向上箭头样式
"background:url(:res/image/up.png) center no-repeat;}"
"");
m_listWidget->verticalScrollBar()->setStyleSheet(QSS_VerticalScrollBar);
#else
m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
#endif
m_listWidget->setCurrentRow(m_chooseIndex);
QScroller::grabGesture(m_listWidget, QScroller::LeftMouseButtonGesture);
m_listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
QString style = styleSheet() + "background:rgb(";
style += QString::number(UiConfig::SUB_STYLE_COLOR_RED);
style += ", ";
style += QString::number(UiConfig::SUB_STYLE_COLOR_GREEN);
style += ", ";
style += QString::number(UiConfig::SUB_STYLE_COLOR_BLUE);
style += ");}";
m_listWidget->setStyleSheet(style);
}
void ItemChoosePage::slotItemClicked(QListWidgetItem* item)
{
myListWidget* wdtPre = dynamic_cast<myListWidget*>(m_listWidget->itemWidget(m_listWidget->item(m_chooseIndex)));
if(wdtPre == nullptr){
return;
}
wdtPre->updateIcon("");
myListWidget* wdt = dynamic_cast<myListWidget*>(m_listWidget->itemWidget(item));
if(wdt == nullptr){
return;
}
wdt->updateIcon(":/res/image/select_2.png");
m_chooseIndex = m_listWidget->currentRow();
}
int ItemChoosePage::getChoosedIndex() const
{
return m_chooseIndex;
}
ItemChooseDialog::ItemChooseDialog(const QVector<QString>& items, int choose, QObject *bgWidget) :
CustomDialog(tr("确定"), bgWidget)
{
QVector<myListWidget::SetOpnCfg> opns;
int i = 0;
for(auto &item : items){
opns.append(ItemChoosePage::SetOpn(item, "", i == choose ? myListWidget::enProperty::enPropertyTick : myListWidget::enProperty::enPropertyNull));
i++;
}
m_ItemsPage = new ItemChoosePage(opns, choose, this);
m_ItemsPage->setFixedSize(width(), static_cast<int>(UiConfig::GetInstance()->getUiHeight() * 0.8));
vLayout = new QVBoxLayout();
vLayout->addWidget(m_ItemsPage, 100, Qt::AlignTop);
vLayout->addLayout(hbtmLayout, 1);
vLayout->setMargin(0);
vLayout->setSpacing(0);
setLayout(vLayout);
resize(width(), static_cast<int>(UiConfig::GetInstance()->getUiHeight() * 0.8));
}
ItemChooseDialog::~ItemChooseDialog()
{
}
int ItemChooseDialog::getChoosedIndex() const
{
return m_ItemsPage->getChoosedIndex();
}
CalendarDialog::CalendarDialog(bool isEn, QObject *bgWidget) : CustomDialog (tr("确定"), bgWidget)
{
m_wgtCal = new QCalendarWidget(this);
m_wgtCal->setFixedWidth(dialogWidth);
m_wgtCal->adjustSize();
qDebug() << "isEn:" << isEn;
m_wgtCal->setLocale(isEn ? QLocale::English : QLocale::Chinese);
m_wgtCal->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
m_wgtCal->setSelectedDate(QDate::currentDate());
QVBoxLayout* vbLayout = new QVBoxLayout();
vbLayout->addWidget(m_wgtCal, 3, Qt::AlignCenter);
vbLayout->addLayout(hbtmLayout, 2);
vbLayout->setSpacing(10);
vbLayout->setMargin(0);
setLayout(vbLayout);
}
QDate CalendarDialog::getDateChoose() const
{
return m_date;
}
void CalendarDialog::slotConfirmBtnClicked()
{
m_date = m_wgtCal->selectedDate();
CustomDialog::slotConfirmBtnClicked();
}
LineEditWithDialog::LineEditWithDialog(int dialogType, QObject *bgWidget, QWidget *parent) :
QLineEdit(parent), m_dialogType(dialogType), m_bgWidget(bgWidget)
{
}
LineEditWithDialog::~LineEditWithDialog()
{qDebug() << "~LineEditWithDialog";}
void LineEditWithDialog::mousePressEvent(QMouseEvent *event)
{
qDebug() << "LineEditWithDialog::mousePressEvent";
QLineEdit::mousePressEvent(event);
switch(m_dialogType)
{
case 0:
{
CalendarDialog cal(0 != UiConfig::GetInstance()->getLanguageType(), m_bgWidget);
cal.setFocus();
cal.setFocusPolicy(Qt::StrongFocus);
if(cal.exec() == QDialog::Accepted)
{
setText(cal.getDateChoose().toString("yyyy-MM-dd"));
}
}
break;
default:
break;
}
}
#endif
MyMessageBox::MyMessageBox()
{
//设置按钮背景色和字体颜色
//setStyleSheet("QPushButton { background-color: rgb(15, 188, 248); color: rgb(85, 255, 0);}");
setStyleSheet("QPushButton { background-color: rgb(15, 188, 248);}");
}
void MyMessageBox::showEvent(QShowEvent* event)
{
QWidget *textField = findChild<QWidget*>("qt_msgBoxbox_label");
if(textField != nullptr)
{
textField -> setMinimumSize(200, 50);//可设置消息对话框大小,默认太小
}
QMessageBox::showEvent(event);
}
void MyMessageBox::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(this->rect(), QColor(0x18, 0x1f, 0x33,/*0x1f, 0x28, 0x41,*/ 255)); //QColor最后一个参数代表背景的透明度
return QWidget::paintEvent(event);
}
void setLineEditFontColor(QLineEdit* edit, const QColor& color)
{
QPalette p = edit->palette();
p.setColor(QPalette::Active, QPalette::Text, color);
p.setColor(QPalette::Inactive, QPalette::Text, color);
edit->setPalette(p);
}
void setLineEditStyle(QLineEdit* edit)
{
edit->setReadOnly(true);
edit->setFrame(false);
edit->setMaximumWidth(100);
QPalette p = edit->palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::lightGray);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::lightGray);
edit->setPalette(p);
//edit->setText("xxxx");
edit->setAlignment(Qt::AlignCenter);
}
void setButtonBackImage(QPushButton *button, const QString image, int sizeW, int sizeH)
{
QPixmap pixmap(image);
if(sizeW !=0 && sizeH != 0){
pixmap=pixmap.scaled(163,163).scaled(sizeW, sizeH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
button->setIconSize(QSize(sizeW, sizeH));
}
button->setIcon(QIcon(pixmap));
button->setFlat(true);
button->setStyleSheet("border: 0px");
button->setCursor(QCursor(Qt::PointingHandCursor));
button->setFocusPolicy(Qt::NoFocus);
}
QListWidgetItem* setListWidgetItem(QListWidgetItem* item, const QString text, const QString image)
{
if(text != "")
item->setText(text);
if(image != "")
item->setIcon(QIcon(image));
item->setSizeHint(QSize(120, 90));
item->setTextAlignment(Qt::AlignCenter);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
return item;
}
myListWidget::myListWidget(const SetOpnCfg& cfg, QWidget* parent) : QWidget(parent)
{
if(cfg.m_sOpn != "" || cfg.m_sVal != "" || cfg.m_prop != myListWidget::enPropertyNull)
{
m_hbLayout = new QHBoxLayout();
if(enProperty::enPropertyIndent1 & cfg.m_prop){
m_hbLayout->addSpacing(UiConfig::GetInstance()->getUiWidth() / 20);
m_layoutObjQty++;
m_hasIndent = true;
}
m_label_leftIcon = new QLabel(this);
m_layoutObjQty++;
m_label_left = new QLabel(this);
m_layoutObjQty++;
m_label_left->setText(cfg.m_sOpn);
if(enProperty::enPropertyRedWord & cfg.m_prop){
m_label_left->setStyleSheet("color:red;font-size:30px;");
}else {
m_label_left->setStyleSheet("color:black;font-size:30px;");//background-color: rgb(225, 225, 225);
}
m_hbLayout->addWidget(m_label_left, 20, Qt::AlignLeft);
m_label_right = new QLabel(this);
m_layoutObjQty++;
m_label_right->setText(cfg.m_sVal);
m_label_right->setStyleSheet("color:gray;font-size:30px;");
m_hbLayout->addWidget(m_label_right, 1, Qt::AlignRight);
m_label_icon = new QLabel(this);
m_layoutObjQty++;
QPixmap pm;
QString icon;
if(enPropertySwitch & cfg.m_prop){
icon = ":/res/image/off.png";
pm.load(icon);
}else if(enProperty::enPropertyMoreArrow & cfg.m_prop){
icon = ":/res/image/pageNext.png";
pm.load(icon);
//pm = pm.scaled(30, 40, Qt::KeepAspectRatio);
}else if(enProperty::enPropertyTick & cfg.m_prop){
icon = ":/res/image/select_2.png";
pm.load(icon);
}
m_label_icon->setPixmap(pm);
m_hbLayout->addWidget(m_label_icon, 1, Qt::AlignRight);
m_hbLayout->setContentsMargins(20, 20, 40, 20);
setLayout(m_hbLayout);
}
else{
setEnabled(false);
}
}
void myListWidget::updateLabel(const QString& val)
{
if(m_label_right)
m_label_right->setText(val);
}
void myListWidget::setLeftIcon(const QString& icon, int scale_w, int scale_h)
{
QPixmap pm(icon);
if(0!=scale_w && 0!=scale_h){
pm = pm.scaled(scale_w, scale_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
m_label_leftIcon->setPixmap(pm);
qDebug() << "before m_hbLayout->count():" << m_hbLayout->count();
if(m_hbLayout->count() < m_layoutObjQty){
m_hbLayout->insertWidget(m_hasIndent ? 1 : 0, m_label_leftIcon, 1, Qt::AlignLeft);
}
qDebug() << "after m_hbLayout->count():" << m_hbLayout->count();
}
void myListWidget::updateIcon(const QString& icon, int scale_w, int scale_h)
{
if(m_label_icon){
QPixmap pm(icon);
//pm = pm.scaled(40, 40, Qt::KeepAspectRatio);
if(0!=scale_w && 0!=scale_h){
pm = pm.scaled(scale_w, scale_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
m_label_icon->setPixmap(pm);
}
}
myListWidget::~myListWidget()
{
//qDebug() << "~myListWidget()";
}
const QString myListWidget::getLabelString() const
{
return m_label_right->text();
}
const QString myListWidget::getOpnLabelString() const
{
return m_label_left->text();
}
const QString myListWidget::getOptionLabel() const
{
return m_label_left->text();
}
void setLanguage(QApplication& app, const QString& languageFile)
{
static QTranslator m_translator;
do{
if(languageFile.contains(".qm")){
//m_translator->load(":/res/language/A038_TC.qm");
m_translator.load(languageFile);
}
else {
break;
}
if(!m_translator.isEmpty())
{
app.installTranslator(&m_translator);
}
else {
qDebug() << "language is empty!";
}
}while(0);
}
void setFont(QApplication& app, const QString& ttf, const int fontSize, const int nLanguageType)
{
QString fontFile = ttf;
if(6 == nLanguageType){//泰文
fontFile += "thai.ttf";
}
else {
fontFile += "DroidSansFallback.ttf";//中文
}
int id = QFontDatabase::addApplicationFont(fontFile);
QFontDatabase::addApplicationFont(ttf + "pingfang.ttf");
QString msyh = QFontDatabase::applicationFontFamilies (id).at(0);
QFont font(msyh, fontSize); //8
//qDebug()<<msyh<<endl;
//font.setPointSize(5);
app.setFont(font);
}
setPushButton::setPushButton(QPushButton *pPushButton, const QString &tip, const QString &icon, const QSize& size)
{
int btnWidth = size.width();
pPushButton->setMinimumSize(btnWidth, btnWidth);
//圆角
pPushButton->setStyleSheet("background-color: rgb(255, 255, 255);border-radius:10px;padding:2px 4px;focus{outline: none;}");
//"{background-color:#FF0000;border-radius:4px;text-align: bottom;color:#000000;}"
_pVBoxLayoutDevice = new QVBoxLayout(pPushButton);
_pLabelIcon = new QLabel(pPushButton);
QPixmap pm(icon);
if(UiConfig::GetInstance()->isRkDevice()){
//pm = pm.scaledToWidth(btnWidth / 3, Qt::SmoothTransformation);
pm = pm.scaled(btnWidth / 3.5, btnWidth / 3.5, Qt::KeepAspectRatio); //TODO, special for rk right now
}
else{
pm = pm.scaled(btnWidth / 3, btnWidth / 3, Qt::KeepAspectRatio);
}
_pLabelIcon->setPixmap(pm);
_pVBoxLayoutDevice->addWidget(_pLabelIcon, 2, Qt::AlignCenter);
QLabel *btmName = new QLabel(pPushButton);
btmName->setText(tip);
_pVBoxLayoutDevice->addWidget(btmName, 1, Qt::AlignCenter);
_pVBoxLayoutDevice->setAlignment(Qt::AlignCenter);
pPushButton->setFocusPolicy(Qt::NoFocus);
}
setPushButton::~setPushButton()
{
}
MyWidgetWithSubStyleColor::MyWidgetWithSubStyleColor(QWidget *parent) : WidgetWithBackstageInterface(parent)
{
#if 0
setCursor(QCursor(Qt::BlankCursor));
#endif
}
void MyWidgetWithSubStyleColor::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(this->rect(), QColor(UiConfig::SUB_STYLE_COLOR_RED, UiConfig::SUB_STYLE_COLOR_GREEN, UiConfig::SUB_STYLE_COLOR_BLUE, UiConfig::SUB_STYLE_COLOR_TRAN)); //QColor最后一个参数代表背景的透明度
return QWidget::paintEvent(event);
}
MyWidgetWithMainStyleColor::MyWidgetWithMainStyleColor(QWidget *parent) : WidgetWithBackstageInterface(parent)
{
#if 0
setCursor(QCursor(Qt::BlankCursor));
#endif
}
void MyWidgetWithMainStyleColor::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(this->rect(), QColor(UiConfig::MAIN_STYLE_COLOR_RED, UiConfig::MAIN_STYLE_COLOR_GREEN, UiConfig::MAIN_STYLE_COLOR_BLUE, UiConfig::MAIN_STYLE_COLOR_TRAN)); //QColor最后一个参数代表背景的透明度
return QWidget::paintEvent(event);
}
PageAcceptMouseAndTouch::PageAcceptMouseAndTouch(QWidget *parent) : WidgetWithBackstageInterface(parent)
{
setAttribute(Qt::WA_AcceptTouchEvents); //touch
installEventFilter(this); //mouse click
#if 0
setCursor(QCursor(Qt::BlankCursor));
#endif
}
bool PageAcceptMouseAndTouch::eventFilter(QObject *watched, QEvent *event)
{
if( watched == this){
if(event->type() == QEvent::MouseButtonPress){
qDebug() << "mouse clicked";
}
}
else{
return QWidget::eventFilter(watched, event);
}
return false;
}
bool PageAcceptMouseAndTouch::event(QEvent *event)
{
#if 0
switch( event->type() )
{
case QEvent::TouchBegin:
qDebug() << "touch begin";
event->accept();
return true;
case QEvent::TouchUpdate:
event->accept();
return true;
case QEvent::TouchEnd:
qDebug() << "touch end";
event->accept();
return true;
default:
break;
}
#endif
return QWidget::event(event);
}
TimerUpdate::TimerUpdate(QLabel *pLabel, QWidget *parent)
: QWidget(parent), _labelTime(pLabel), _labelDate(nullptr), m_timeType(0), m_dateType(0), m_weekType(0)
{
_timerID = startTimer(500);
}
TimerUpdate::TimerUpdate(QLabel* labelTime, QLabel* labelDate, QWidget *parent, int timeType, int dateType, int weekType) :
QWidget(parent), _labelTime(labelTime), _labelDate(labelDate), m_timeType(timeType), m_dateType(dateType), m_weekType(weekType)
{
_timerID = startTimer(500);
}
TimerUpdate::~TimerUpdate()
{
if(0 != _timerID){
killTimer(_timerID);
}
}
void TimerUpdate::timerEvent(QTimerEvent *event)
{
if(event->timerId() == _timerID)
{
const QDateTime dateTime = QDateTime::currentDateTime();
const QString time = dateTime.toString("hh:mm:ss");
if(time == "00:00:00"){
emit signalNewDay();
}
if(!m_enableUpdate){
//qDebug() << "dont update";
return;
}
if(_labelTime){
QString timeStr;
if(0 == m_timeType){
timeStr = time.left(5);
}else if(1 == m_timeType){
timeStr = time;
}
if(_labelTime->text() != timeStr){
_labelTime->setText(timeStr);
#if 1
if(UiConfig::GetInstance()->isRkDevice()){
if(parent()){
dynamic_cast<QWidget*>(parent())->update();
}
if(parent()->parent()){
dynamic_cast<QWidget*>(parent()->parent())->update();
}
}
#endif
}
}
if(_labelDate){
QString dateStr;
if(0 == m_dateType){
dateStr = dateTime.toString("yyyy/MM/dd ");
}else if(1 == m_dateType){
dateStr = dateTime.toString("yyyy-MM-dd ");
}
if(1 == m_weekType){
dateStr += dateTime.toString("ddd");
}else if(2 == m_weekType){
QLocale locale = QLocale::Chinese;
dateStr += locale.toString(dateTime, "ddd");
}
if(_labelDate->text() != dateStr){
_labelDate->setText(dateStr);
#if 1
if(UiConfig::GetInstance()->isRkDevice()){
if(parent()){
dynamic_cast<QWidget*>(parent())->update();
}
if(parent()->parent()){
dynamic_cast<QWidget*>(parent()->parent())->update();
}
}
#endif
}
}
}
}
void TimerUpdate::enableUpdate(bool enable)
{
m_enableUpdate = enable;
}
QPixmap PixmapToRound(QPixmap &src, int radius)
{
if (src.isNull()) {
return QPixmap();
}
QSize size(2*radius, 2*radius);
QBitmap mask(size);
QPainter painter(&mask);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
painter.setBrush(QColor(0, 0, 0));
painter.drawRoundedRect(0, 0, size.width(), size.height(), 99, 99);
QPixmap image = src.scaled(size);
image.setMask(mask);
return image;
}
MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
{
}
MyLineEdit::~MyLineEdit()
{
}
void MyLineEdit::mousePressEvent(QMouseEvent *event)
{
if( event->button() == Qt::LeftButton )
{
emit clicked();
}
}
QImage ScaleImage(const QImage& image, const QSize& size)
{
double dWidthRatio = 1.0 * image.size().width() / size.width();
double dHeightRatio = 1.0 * image.size().height() / size.height();
return dWidthRatio>dHeightRatio ? image.scaledToWidth(size.width(), Qt::SmoothTransformation) : image.scaledToHeight(size.height(), Qt::SmoothTransformation);
}
QImage ScaleImage2Label(const QImage& qImage, const QLabel& label)
{
return ScaleImage(qImage, label.size());
}
QPixmap ScalePixmap(const QPixmap& pixmap, const QSize& size)
{
#if 0
return QPixmap::fromImage(ScaleImage(QImage(pixmap.toImage()), size));
#else
double dWidthRatio = 1.0 * pixmap.size().width() / size.width();
double dHeightRatio = 1.0 * pixmap.size().height() / size.height();
return dWidthRatio>dHeightRatio ? pixmap.scaledToWidth(size.width(), Qt::SmoothTransformation) : pixmap.scaledToHeight(size.height(), Qt::SmoothTransformation);
#endif
}
QPixmap ScalePixmap2Label(const QPixmap& pixmap, const QLabel& label)
{
return ScalePixmap(pixmap, label.size());
}
FourListWidget::FourListWidget(const QString &left, const QString &right,
const QString &btnText, const QString &icon, QWidget *parent) : QWidget(parent)
{
QHBoxLayout* hbLayout = new QHBoxLayout();
m_label_left = new QLabel(this);
m_label_left->setText(left);
m_label_left ->setStyleSheet("color:white"); //background-color: rgb(250, 0, 0); font-size:60px;
hbLayout->addWidget(m_label_left, 20, Qt::AlignLeft);
m_label_right = new QLabel(this);
m_label_icon = new QLabel(this);
m_btn_mid = new QPushButton(this);
QPixmap pm(icon);
if(right != QString(""))
{
m_label_right->setText(right);
m_label_right ->setStyleSheet("color:white");
hbLayout->addWidget(m_label_right, 1, Qt::AlignRight);
pm = pm.scaled(20, 20, Qt::KeepAspectRatio);
}
else
{
pm = pm.scaled(40, 40, Qt::KeepAspectRatio);
}
m_label_icon->setPixmap(pm);
m_btn_mid->setObjectName("pushbutton_test");
m_btn_mid->setStyleSheet("#pushbutton_test{background-color: #0ebcf8;color: #FFFFFF}");
m_btn_mid->setText(btnText);
hbLayout->addWidget(m_btn_mid, 1, Qt::AlignRight);
hbLayout->addWidget(m_label_icon, 1, Qt::AlignRight);
hbLayout->setMargin(1);
setLayout(hbLayout);
}
FourListWidget::~FourListWidget()
{
if(nullptr != m_label_left)
{
delete m_label_left;
m_label_left = nullptr;
}
if(nullptr != m_label_right)
{
delete m_label_right;
m_label_right = nullptr;
}
if(nullptr != m_label_icon)
{
delete m_label_icon;
m_label_icon = nullptr;
}
if(nullptr != m_btn_mid)
{
delete m_btn_mid;
m_btn_mid = nullptr;
}
}
void FourListWidget::updateLabel(const QString &label)
{
if(m_label_right)
m_label_right->setText(label);
}
void FourListWidget::updateBtnText(const QString &text)
{
if(m_btn_mid)
{
m_btn_mid->setText(text);
}
}
QPushButton *FourListWidget::getPushButton()
{
return m_btn_mid;
}
const QString FourListWidget::getLabelString() const
{
return m_label_right->text();
}
SwitchListWidget::SwitchListWidget(const QString &left, const QString &right, const QString &icon, QWidget *parent)
{
Q_UNUSED(parent);
QHBoxLayout* hbLayout = new QHBoxLayout();
m_label_left = new QLabel(this);
m_label_left->setText(left);
m_label_left ->setStyleSheet("color:white"); //background-color: rgb(250, 0, 0); font-size:60px;
hbLayout->addWidget(m_label_left, 20, Qt::AlignLeft);
m_label_right = new QLabel(this);
m_label_icon = new QLabel(this);
QPixmap pm(icon);
if(right != QString(""))
{
m_label_right->setText(right);
m_label_right ->setStyleSheet("color:white");
hbLayout->addWidget(m_label_right, 1, Qt::AlignRight);
pm = pm.scaled(20, 20, Qt::KeepAspectRatio);
}
else
{
pm = pm.scaled(30, 30, Qt::KeepAspectRatio);
}
m_label_icon->setPixmap(pm);
hbLayout->addWidget(m_label_icon, 1, Qt::AlignRight);
hbLayout->setMargin(1);
setLayout(hbLayout);
}
SwitchListWidget::~SwitchListWidget()
{
}
void SwitchListWidget::updateLabel(const QString &label)
{
if(m_label_right)
m_label_right->setText(label);
}
void SwitchListWidget::updateIcon(const QString& icon)
{
if(m_label_icon)
{
QPixmap pm(icon);
pm = pm.scaled(30, 30, Qt::KeepAspectRatio);
m_label_icon->setPixmap(pm);
}
}
void SwitchListWidget::updateIcon(bool bFlag)
{
if(m_label_icon)
{
QPixmap pm(bFlag ? ":/res/image/on.png" : ":/res/image/off.png");
pm = pm.scaled(30, 30, Qt::KeepAspectRatio);
m_label_icon->setPixmap(pm);
m_Res = bFlag;
}
}
bool SwitchListWidget::getSwitchRes()
{
return m_Res;
}
const QString SwitchListWidget::getLabelString() const
{
return m_label_right->text();
}
LocalFourListWidget::LocalFourListWidget(const QString& left, const QString& mid,
const QString& right, const QString& icon, QWidget*parent) : QWidget(parent)
{
QHBoxLayout* hbLayout = new QHBoxLayout();
m_label_left = new QLabel(this);
m_label_left->setText(left);
m_label_left ->setStyleSheet("color:white"); //background-color: rgb(250, 0, 0); font-size:60px;
hbLayout->addWidget(m_label_left, 20, Qt::AlignLeft);
m_label_mid = new QLabel(this);
m_label_mid->setText(mid);
m_label_mid ->setStyleSheet("color:white");
hbLayout->addWidget(m_label_mid, 1, Qt::AlignRight);
m_label_right = new QLabel(this);
m_label_right->setText(right);
m_label_right ->setStyleSheet("color:white");
hbLayout->addWidget(m_label_right, 1, Qt::AlignRight);
m_label_icon = new QLabel(this);
if(!icon.isEmpty())
{
QPixmap pm(icon);
pm = pm.scaled(20, 20, Qt::KeepAspectRatio);
m_label_icon->setPixmap(pm);
hbLayout->addWidget(m_label_icon, 1, Qt::AlignRight);
}
hbLayout->setMargin(1);
setLayout(hbLayout);
}
LocalFourListWidget::~LocalFourListWidget()
{
if(nullptr != m_label_left)
{
delete m_label_left;
m_label_left = nullptr;
}
if(nullptr != m_label_mid)
{
delete m_label_mid;
m_label_mid = nullptr;
}
if(nullptr != m_label_right)
{
delete m_label_right;
m_label_right = nullptr;
}
if(nullptr != m_label_icon)
{
delete m_label_icon;
m_label_icon = nullptr;
}
}
void LocalFourListWidget::updateLabel(const QString &label)
{
if(m_label_mid)
m_label_mid->setText(label);
}
void LocalFourListWidget::updateRight(const QString &text)
{
if(m_label_right)
{
m_label_right->setText(text);
}
}
void LocalFourListWidget::updateIcon(const QString& icon)
{
if(m_label_icon)
{
QPixmap pm(icon);
pm = pm.scaled(30, 30, Qt::KeepAspectRatio);
m_label_icon->setPixmap(pm);
}
}
const QString LocalFourListWidget::getLabelString() const
{
return m_label_mid->text();
}
bool checkip(const QString& ip)
{
QRegExp rx2("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
//QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
//QRegExp rx2("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
if( !rx2.exactMatch(ip) )
{
return false;
}
return true;
}
bool checkMask(const QString& mask)
{
QRegExp rx2("^(254|252|248|240|224|192|128|0)\\.0\\.0\\.0|255\\.(254|252|248|240|224|192|128|0)\\.0\\.0|255\\.255\\.(254|252|248|240|224|192|128|0)\\.0|255\\.255\\.255\\.(254|252|248|240|224|192|128|0)$");
if( !rx2.exactMatch(mask) )
{
return false;
}
return true;
}
//判断是否在同一网段
bool isSameNetwork(const QString& ip, const QString& mask, const QString& gateway)
{
QStringList ipList = ip.split('.',QString::SkipEmptyParts);
QStringList maskList = mask.split('.',QString::SkipEmptyParts);
QStringList gatewayList = gateway.split('.',QString::SkipEmptyParts);
bool ok = true;
for(int i = 0;i < 4;i++) {
if ((ipList[i].toInt(&ok,10) & maskList[i].toInt(&ok,10)) !=
(gatewayList[i].toInt(&ok,10) & maskList[i].toInt(&ok,10))) {
return false;
}
}
return true;
}
static bool getIpFromDialogInput(const QString& lineEdit, const QString& initIp, QString& newIp, bool(*pCheckFunc)(const QString&), QObject *bgWidget)
{
bool ret = false;
bool settingFailed = true;
do
{
LineEditInputDialog dlg(lineEdit, bgWidget);
dlg.fillRow(0, initIp);
if(dlg.exec() == QDialog::Accepted)
{
do{
if(!((*pCheckFunc)(dlg.getRow(0))))
{
break;
}
newIp = dlg.getRow(0);
settingFailed = false;
ret = true;
}while(0);
}
else {
settingFailed = false;
}
if(settingFailed)
{
InfoDialog(QObject::tr("设置有误,请重新输入!"), bgWidget, QObject::tr("知道了")).exec();
}
}while(settingFailed);
return ret;
}
bool getIpFromUserInput(const QString& lineEdit, const QString& initIp, QString& newIp, QObject *bgWidget)
{
return getIpFromDialogInput(lineEdit, initIp, newIp, checkip, bgWidget);
}
bool getMaskFromUserInput(const QString& lineEdit, const QString& initIp, QString& newIp, QObject *bgWidget)
{
return getIpFromDialogInput(lineEdit, initIp, newIp, checkMask, bgWidget);
}
bool getNumFromUserInput(const QString& lineEdit, const QString& initNum, QString& newValue, const int min, const int max, QObject *bgWidget)
{
bool ret = false;
bool settingFailed = true;
do
{
LineEditInputDialog dlg(lineEdit, bgWidget);
if(!initNum.isEmpty())
{
dlg.fillRow(0, initNum);
}
if(dlg.exec() == QDialog::Accepted)
{
bool isDigi = false;
int valueSet = dlg.getRow(0).toInt(&isDigi);
if(isDigi){
do{
if(valueSet < min || valueSet > max){
break;
}
newValue = QString::number(valueSet); //转换数字回字符串,去掉无效字符
settingFailed = false;
ret = true;
}while(0);
}
}
else {
settingFailed = false;
}
if(settingFailed)
{
InfoDialog(QObject::tr("设置有误,请重新输入!"), bgWidget).exec();
}
}while(settingFailed);
return ret;
}
QString GBK2UTF8(const QString &inStr)
{
QTextCodec *gbk = QTextCodec::codecForName("GB18030");
//QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QString g2u = gbk->toUnicode(gbk->fromUnicode(inStr)); // gbk convert utf8
return g2u;
}
QString UTF82GBK(const QString &inStr)
{
QTextCodec *gbk = QTextCodec::codecForName("GB18030");
//QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QString utf2gbk = gbk->toUnicode(inStr.toLocal8Bit());
return utf2gbk;
}
std::string gbk2utf8(const QString &inStr)
{
return GBK2UTF8(inStr).toStdString();
}
QString utf82gbk(const std::string &inStr)
{
QString str = QString::fromStdString(inStr);
return UTF82GBK(str);
}
void gbkToUtf8(char *gbkstr)
{
QTextCodec *gbk = QTextCodec::codecForName("GB18030");
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
//创建str指针开辟另一控件储存转换后的UTF-8编码
char *str = utf8->fromUnicode(gbk->toUnicode(gbkstr)).data();
//将原字符数组清空
char *clear = gbkstr; //将原字符数组地址附给指针clear使gbkstr指向地址不作改变
while(*clear != 0x00) //0x00是“空”的意思也就是' '
{
*clear = 0x00;
clear++; //后移一位,继续判断
}
//复制(覆盖)到原字符数组
strcpy(gbkstr,str);
}
ProgressWidget::ProgressWidget(const QString& title)
{
m_label_title = new QLabel(this);
m_label_title->setText(title);
m_label_title->setAlignment(Qt::AlignCenter);
m_label_msg = new QLabel(this);
m_label_msg->setAlignment(Qt::AlignCenter);
m_slider_progess = new QSlider(this);
m_slider_progess->setOrientation(Qt::Horizontal);
m_slider_progess->setMaximumHeight(20);
m_slider_progess->setMinimum(0);
m_slider_progess->setMaximum(100);
m_slider_progess->setStyleSheet(" \
QSlider::add-page:Horizontal\
{ \
background-color: rgb(87, 97, 106);\
height:4px;\
}\
QSlider::sub-page:Horizontal \
{\
background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(231,80,229, 255), stop:1 rgba(7,208,255, 255));\
height:4px;\
}\
QSlider::groove:Horizontal \
{\
background:transparent;\
height:6px;\
}\
QSlider::handle:Horizontal \
{\
height: 30px;\
width:8px;\
border-image: url(:/images/ic_music_thumb.png);\
margin: -8 0px; \
} \
");
QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(m_label_title);
vLayout->addWidget(m_label_msg);
vLayout->addWidget(m_slider_progess);
setLayout(vLayout);
}
ProgressWidget::~ProgressWidget()
{
}
void ProgressWidget::setMsg(const unsigned current, const unsigned all)
{
m_label_msg->setText(QString::number(current) + "/" + QString::number(all));
m_slider_progess->setValue(static_cast<int>(static_cast<float>(current) / all * 100));
}
PureColorPage::PureColorPage(unsigned color, unsigned char tran, bool drawRect, QWidget* parent) : PageAcceptMouseAndTouch(parent)
{
m_color = color;
m_tran = tran;
m_drawRect = drawRect;
#if 1
QPalette pal = palette();
pal.setColor(QPalette::Background, QColor((m_color >> 16) & 0xFF, (m_color >> 8) & 0xFF, m_color & 0xFF, m_tran));
setPalette(pal);
#endif
resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
}
PureColorPage::~PureColorPage()
{
}
void PureColorPage::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
#if 0
painter.fillRect(this->rect(), QColor((m_color >> 16) & 0xFF, (m_color >> 8) & 0xFF, m_color & 0xFF, m_tran));
#endif
if(m_drawRect){
painter.setPen(Qt::red);
QRectF rectangle(UiConfig::GetInstance()->getUiWidth() / 24, UiConfig::GetInstance()->getUiWidth() / 24,
UiConfig::GetInstance()->getUiWidth() / 24, UiConfig::GetInstance()->getUiWidth() / 24);
painter.drawRect(rectangle);
}
return QWidget::paintEvent(event);
}
SettingUiPage::SettingUiPage(const QVector<SetOpn>& cfg, QWidget *parent) : MyWidgetWithMainStyleColor(parent), config(cfg)
{
//qDebug() << "SettingUiPage config size: " << config.size();
m_listWidget = new QListWidget(this);
m_listWidget->setFrameStyle(QFrame::NoFrame);
m_listWidget->setCursor(QCursor(Qt::PointingHandCursor));
m_listWidget->setFocusPolicy(Qt::NoFocus);
m_listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_listWidget->setSpacing(0);
m_listWidget->setStyleSheet("QListWidget{border-width:0;border-style:outset; background:rgb(255, 255, 255);border-top: 1px solid #DFDDEB;}"
"QListWidget::item{border-bottom: 1px solid #DFDDEB;}"
"QListWidget::Item:hover{background:transparent;}"
"QListWidget::item:selected{background:transparent; }"
/*"QListWidget::item:selected:!active{border-width:0px; }"*/);
for(int i=0; i<config.size()/* + 1*/; i++)
{
QListWidgetItem* pItem = new QListWidgetItem(m_listWidget);
myListWidget* mylistwidget = nullptr;
if(i < config.size()){
mylistwidget = new myListWidget(config[i], m_listWidget);
}
else {
mylistwidget = new myListWidget(SetOpn(), m_listWidget);
}
pItem->setSizeHint(QSize(UiConfig::GetInstance()->getUiHeight() / 12, UiConfig::GetInstance()->getUiHeight() / 12));
m_listWidget->setItemWidget(pItem, mylistwidget);
//m_listWidget->setStyleSheet("QListWidget::item{border-top: 1px solid #DFDDEB;}");
#if 0
m_listWidget->setStyleSheet("QListWidget{border-top: 1px solid #DFDDEB;}"
"QListWidget::item{border-bottom: 1px solid #DFDDEB;}");
#endif
}
connect(m_listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotItemClicked(QListWidgetItem*)));
QVBoxLayout* vbLayout = new QVBoxLayout();
vbLayout->addWidget(m_listWidget);
vbLayout->setMargin(0);
setLayout(vbLayout);
m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QScroller::grabGesture(m_listWidget, QScroller::LeftMouseButtonGesture);
m_listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
}
void SettingUiPage::controlListWidgetItemShow(const bool sw, int option)
{
QListWidgetItem* pItem = m_listWidget->item(option);
m_listWidget->setItemHidden(pItem, static_cast<bool>(!static_cast<int>(sw)));
}
MsgListWidgetItem::MsgListWidgetItem(QWidget* parent) : QWidget(parent)
{
labelMsg.resize(m_msgOpnQty);
labelPic = new QLabel(this);
labelPic->setFixedSize(static_cast<int>(UiConfig::GetInstance()->getUiWidth() / 4), static_cast<int>(UiConfig::GetInstance()->getUiHeight() / 10));
labelPic->setAlignment(Qt::AlignCenter);
QVBoxLayout* vLayout = new QVBoxLayout();
for(auto &la : labelMsg){
la = new QLabel(this);
la->setMinimumHeight(labelPic->height() / 4);
vLayout->addWidget(la, 1, Qt::AlignLeft);
}
vLayout->setMargin(5);
vLayout->setSpacing(static_cast<int>(labelPic->height() * 0.3));
QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addWidget(labelPic);
hLayout->addLayout(vLayout, 100);
hLayout->setSpacing(5);
hLayout->setMargin(5);
setLayout(hLayout);
}
MsgListWidgetItem::~MsgListWidgetItem()
{
}
void MsgListWidgetItem::showMsg(const QString& pic, const QVector<stMsgOpn>& msgs)
{
labelPic->clear();
for(auto &opn : labelMsg){
opn->clear();
}
if("" != pic){
QPixmap faceImg(pic);
labelPic->setPixmap(ScalePixmap2Label(faceImg, *labelPic));
}
for(int i=0; i<labelMsg.size() && i <msgs.size(); i++){
labelMsg[i]->setText(msgs[i].m_sMsg);
if(enPropertyBold & msgs[i].m_eProp){
QFont ft;
ft.setBold(true);
labelMsg[i]->setFont(ft);
}
if(enPropertyGreyWord & msgs[i].m_eProp){
labelMsg[i]->setStyleSheet("color:grey;font-size:30px;");
}
else if(enPropertyGreenWord & msgs[i].m_eProp){
labelMsg[i]->setStyleSheet("color:green;font-size:30px;");
}
else if(enPropertyRedWord & msgs[i].m_eProp){
labelMsg[i]->setStyleSheet("color:red;font-size:30px;");
}
}
}
PersonMsgUiPage::PersonMsgUiPage(QWidget *parent) : MyWidgetWithMainStyleColor(parent)
{
m_listWidget = new QListWidget(this);
m_listWidget->setFrameStyle(QFrame::NoFrame);
m_listWidget->setCursor(QCursor(Qt::PointingHandCursor));
m_listWidget->setFocusPolicy(Qt::NoFocus);
m_listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_listWidget->setSpacing(0);
m_listWidget->setStyleSheet("QListWidget{border-width:0;border-style:outset; background:rgb(255, 255, 255);border-top: 1px solid #DFDDEB;}"
"QListWidget::item{border-bottom: 1px solid #DFDDEB;}"
"QListWidget::Item:hover{background:transparent;}"
"QListWidget::item:selected{background:transparent; }"
/*"QListWidget::item:selected:!active{border-width:0px; }"*/);
connect(m_listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotItemClicked(QListWidgetItem*)));
const QString tips[SEARCH_OPN_QTY] = {tr("请输入人员姓名"), tr("请选择通行时间")};
const QString moreBtnIcon[SEARCH_OPN_QTY] = {":res/image/down.png", ":res/image/display.png"};
QGridLayout* glayout = new QGridLayout();
glayout->setColumnStretch(0, 10);
glayout->setColumnStretch(1, 1);
glayout->setContentsMargins(20, 5, 20, 5);
glayout->setSpacing(5);
for (int i=0; i<SEARCH_OPN_QTY; i++) {
if(enSearchOpnName == i){
m_editSearch[i] = new LineEditWithKeyboard(this);
}
else {
m_editSearch[i] = new LineEditWithDialog(0, parent->parent(), this);
}
m_editSearch[i]->setFixedHeight(static_cast<int>(UiConfig::GetInstance()->getUiHeight() / 20));
m_editSearch[i]->setStyleSheet("background-color: lightgrey;border-width:0;border-style:outset");
m_editSearch[i]->setTextMargins(40, 0, 40, 0);
//QPalette p = m_editSearch[i]->palette();
//p.setColor(QPalette::Active, QPalette::Base, palette().color(QPalette::Active, QPalette::Background));
//p.setColor(QPalette::Inactive, QPalette::Base, palette().color(QPalette::Inactive, QPalette::Background));
//m_editSearch[i]->setPalette(p);
connect(m_editSearch[i], SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
if(enSearchOpnName != i){
m_editSearch[i]->hide();
}
QLabel *searchIcon = new QLabel(this);
const int searchIconWidth = static_cast<int>(m_editSearch[i]->height() * 0.8);
searchIcon->setMaximumSize(searchIconWidth, searchIconWidth);
searchIcon->setCursor(QCursor(Qt::ArrowCursor));
QPixmap search(":res/image/search.png");
//searchIcon->setPixmap(search.scaled(searchIconWidth, searchIconWidth, Qt::KeepAspectRatio));
searchIcon->setPixmap(search);
m_labelTip[i] = new QLabel(this);
m_labelTip[i]->setText(tips[i]);
QPalette labelple = m_labelTip[i]->palette();
labelple.setColor(QPalette::Active, QPalette::Text, Qt::gray);
labelple.setColor(QPalette::Inactive, QPalette::Text, Qt::gray);
m_labelTip[i]->setPalette(labelple);
m_labelTip[i]->setStyleSheet("background-color:transparent;font-size:20px;");
m_btnClearSearch[i] = new QPushButton(this);
setButtonBackImage(m_btnClearSearch[i], ":res/image/close.png", 30, 30);
connect(m_btnClearSearch[i], SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
m_btnClearSearch[i]->hide();
QSpacerItem *spaceItem = new QSpacerItem(700, 40, QSizePolicy::Expanding);
QHBoxLayout *editLayout = new QHBoxLayout();
editLayout->setContentsMargins(5, 0, 5, 0);
editLayout->addWidget(searchIcon, 1, Qt::AlignLeft);
editLayout->addWidget(m_labelTip[i], 1, Qt::AlignLeft);
editLayout->addSpacerItem(spaceItem);
editLayout->addWidget(m_btnClearSearch[i], 100, Qt::AlignRight);
editLayout->setSpacing(10);
m_editSearch[i]->setLayout(editLayout);
if(enSearchOpnName != i){
m_editSearch[i]->setReadOnly(true);
}
glayout->addWidget(m_editSearch[i], i, 0);
if(enSearchOpnName == i){
m_btnMore = new QPushButton(this);
setButtonBackImage(m_btnMore, ":res/image/down.png", 55, 40);
connect(m_btnMore, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
m_btnMore->setEnabled(false);
glayout->addWidget(m_btnMore, i, 1);
}
}
m_stackWgt = new QStackedWidget(this);
m_stackWgt->addWidget(m_listWidget);
m_stackWgt->setFixedSize(UiConfig::GetInstance()->getUiWidth(), static_cast<int>(UiConfig::GetInstance()->getUiHeight() / 0.6));
m_labelPage = new QLabel(this);
m_labelMin = new QLabel(this);
m_labelMax = new QLabel(this);
m_btnPrePage = new QPushButton(this);
setButtonBackImage(m_btnPrePage, ":res/image/left.png", 37, 61);
connect(m_btnPrePage, SIGNAL(clicked(bool)), this, SLOT(slotBtnClicked()));
m_btnNextPage = new QPushButton(this);
setButtonBackImage(m_btnNextPage, ":res/image/right.png", 37, 61);
connect(m_btnNextPage, SIGNAL(clicked(bool)), this, SLOT(slotBtnClicked()));
m_sliderPage = new QSlider(this);
m_sliderPage->setOrientation(Qt::Horizontal);
m_sliderPage->setMinimumWidth(static_cast<int>(UiConfig::GetInstance()->getUiWidth() * 0.5));
//m_sliderPage->setMinimum(min);
//m_sliderPage->setMaximum(max);
//m_sliderPage->setValue(current);
//m_sliderPage->setSingleStep(10);
//m_sliderPage->setGeometry(30,this->height()-28,200,25);
if(UiConfig::GetInstance()->isRkDevice()){
m_sliderPage->setMaximumHeight(44);
m_sliderPage->setStyleSheet("QSlider::handle:horizontal{width:48px;background-color:rgb(255,255,255);margin:-22px 0px -22px 0px;border-radius:24px;}"
"QSlider::groove:horizontal{height:32px;background-color:rgb(219,219,219);}"
"QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
"QSlider::sub-page:horizontal{background-color:rgb(26,217,110);}");
}
else{
m_sliderPage->setMinimumHeight(60);
m_sliderPage->setStyleSheet("QSlider::handle:horizontal{width:48px;background-color:rgb(255,255,255);margin:-22px 0px -22px 0px;border-radius:24px;}"
"QSlider::groove:horizontal{height:48px;background-color:rgb(219,219,219);}"
"QSlider::add-page:horizontal{background-color:rgb(219,219,219);}"
"QSlider::sub-page:horizontal{background-color:rgb(26,217,110);}");
}
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setOffset(4,4);
effect->setColor(QColor(0,0,0,50));
effect->setBlurRadius(10);
m_sliderPage->setGraphicsEffect(effect);
connect(m_sliderPage, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
connect(m_sliderPage, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
QHBoxLayout* hlay = new QHBoxLayout();
hlay->addWidget(m_btnPrePage, 1, Qt::AlignLeft);
hlay->addWidget(m_labelMin, 15, Qt::AlignCenter);
hlay->addWidget(m_sliderPage, 70, Qt::AlignCenter);
hlay->addWidget(m_labelMax, 15, Qt::AlignCenter);
hlay->addWidget(m_btnNextPage, 1, Qt::AlignRight);
hlay->setSpacing(20);
hlay->setMargin(30);
QVBoxLayout* vbLay = new QVBoxLayout();
vbLay->addWidget(m_labelPage, 1, Qt::AlignCenter);
vbLay->addLayout(hlay, 1);
vbLay->setSpacing(0);
vbLay->setMargin(5);
QVBoxLayout* vbLayout = new QVBoxLayout();
vbLayout->addLayout(glayout, 1);
vbLayout->addWidget(m_stackWgt, 80, Qt::AlignCenter);
vbLayout->addLayout(vbLay, 1);
vbLayout->setSpacing(20);
vbLayout->setMargin(10);
setLayout(vbLayout);
for(unsigned i=0; i<m_itemQty/* + 1*/; i++)
{
QListWidgetItem* pItem = new QListWidgetItem(m_listWidget);
MsgListWidgetItem* listwidgetItem = new MsgListWidgetItem(m_listWidget);
pItem->setSizeHint(QSize(UiConfig::GetInstance()->getUiHeight() / 8, UiConfig::GetInstance()->getUiHeight() / 8));
m_listWidget->setItemWidget(pItem, listwidgetItem);
//m_listWidget->setStyleSheet("QListWidget::item{border-top: 1px solid #DFDDEB;}");
m_listWidget->setItemHidden(pItem, true);
}
setPageControlBarHide(true);
}
void PersonMsgUiPage::showMsg(const QVector<MsgSet>& msgs)
{
for(int i = 0; i < static_cast<int>(m_itemQty); i++){
if(i<msgs.size()){
dynamic_cast<MsgListWidgetItem*>(m_listWidget->itemWidget(m_listWidget->item(i)))->showMsg(msgs[i].m_sPic, msgs[i].m_vMsgs);
m_listWidget->setItemHidden(m_listWidget->item(i), false);
}
else {
m_listWidget->setItemHidden(m_listWidget->item(i), true);
}
}
}
void PersonMsgUiPage::clearMsg()
{
for(unsigned i = 0; i < m_itemQty/* + 1*/; i++){
m_listWidget->setItemHidden(m_listWidget->item(static_cast<int>(i)), true);
}
}
void PersonMsgUiPage::setPageControlBarHide(bool hide, int min, int max, int current)
{
if(hide){
m_labelPage->hide();
m_labelMin->hide();
m_labelMax->hide();
m_btnPrePage->hide();
m_btnNextPage->hide();
m_sliderPage->hide();
}
else {
m_sliderPage->setMinimum(min);
m_sliderPage->setMaximum(max);
m_sliderPage->setValue(current);
m_labelPage->setText(QString::number(current));
m_labelMin->setText(QString::number(min));
m_labelMax->setText(QString::number(max));
m_labelPage->show();
m_labelMin->show();
m_labelMax->show();
m_btnPrePage->show();
m_btnNextPage->show();
m_sliderPage->show();
m_btnPrePage->setEnabled(current - min);
m_btnNextPage->setEnabled(max - current);
}
}
void PersonMsgUiPage::showMoreSearchOpn()
{
m_btnMore->setEnabled(true);
}
void PersonMsgUiPage::slotValueChanged(int value)
{
m_labelPage->setText(QString::number(value));
}
void PersonMsgUiPage::slotBtnClicked()
{
for (int i=0; i<SEARCH_OPN_QTY; i++) {
if(sender() == m_btnClearSearch[i]){
m_editSearch[i]->clear();
return;
}
}
if(sender() == m_btnMore){
static bool sw = false;
if(sw){
m_editSearch[enSearchOpnTime]->hide();
setButtonBackImage(m_btnMore, ":res/image/down.png", 55, 40);
}
else {
m_editSearch[enSearchOpnTime]->show();
setButtonBackImage(m_btnMore, ":res/image/up.png", 55, 40);
}
m_editSearch[enSearchOpnTime]->clear();
m_sSearchtime = "";
sw = !sw;
return;
}
else if(sender() == m_btnPrePage){
qDebug() << "pre btn clicked";
m_toPageNum = m_labelPage->text().toInt() - 1;
}
else if(sender() == m_btnNextPage){
qDebug() << "next btn clicked";
m_toPageNum = m_labelPage->text().toInt() + 1;
}
m_btnPrePage->setEnabled(m_toPageNum > 1);
m_btnNextPage->setEnabled(m_toPageNum < m_sliderPage->maximum());
m_labelPage->setText(QString::number(m_toPageNum));
m_sliderPage->setValue(m_toPageNum);
}
void PersonMsgUiPage::slotItemClicked(QListWidgetItem *item)
{
return;
}
void PersonMsgUiPage::slotSliderReleased()
{
m_toPageNum = m_labelPage->text().toInt();
m_btnPrePage->setEnabled(m_toPageNum > 1);
m_btnNextPage->setEnabled(m_toPageNum < m_sliderPage->maximum());
}
WifiListWidget::WifiListWidget(const QString& ssid, bool connected, bool withLock, int sigLevel, bool isTargetNet, QWidget* parent) :
QWidget (parent), m_isLock(withLock), m_isTargetNet(isTargetNet), m_isConnected(connected)
{
QHBoxLayout* hLay = new QHBoxLayout();
m_label_conn = new QLabel(this);
if(connected){
QPixmap pm(":/res/image/wifi_connected.png");
m_label_conn->setPixmap(pm);
}
hLay->addWidget(m_label_conn, 1, Qt::AlignLeft);
m_label_ssid = new QLabel(this);
m_label_ssid->setText(ssid);
hLay->addWidget(m_label_ssid, 90, Qt::AlignLeft);
if(withLock){
QLabel* lock = new QLabel(this);
QPixmap pm(":/res/image/wifi_lock.png");
lock->setPixmap(pm);
hLay->addWidget(lock, 1, Qt::AlignRight);
}
QPixmap pm;
QLabel* labelSigLevel = new QLabel(this);
if(sigLevel >= 0){
QString wifiIcon;
if(sigLevel <= 34){
wifiIcon = ":/res/image/wifi1.png";
}
else if(sigLevel <= 49){
wifiIcon = ":/res/image/wifi2.png";
}
else if(sigLevel <= 69){
wifiIcon = ":/res/image/wifi3.png";
}
else{
wifiIcon = ":/res/image/wifi4.png";
}
pm.load(wifiIcon);
//pm = pm.scaled(44, 44);
labelSigLevel->setPixmap(pm);
}
hLay->addWidget(labelSigLevel, 1, Qt::AlignRight);
QLabel* labelNext = new QLabel(this);
pm.load(":/res/image/pageNext.png");
labelNext->setPixmap(pm);
hLay->addWidget(labelNext, 1, Qt::AlignRight);
hLay->setSpacing(10);
hLay->setContentsMargins(20, 20, 40, 20);
setLayout(hLay);
}
WifiListWidget::~WifiListWidget()
{
}
void WifiListWidget::updateStatus(bool connected)
{
if(connected){
QPixmap pm(":/res/image/wifi_connected.png");
m_label_conn->setPixmap(pm);
}
else {
m_label_conn->clear();
}
m_isConnected = connected;
if(m_isConnected){
m_isTargetNet = true;
}
}
QString WifiListWidget::getSsid() const
{
return m_label_ssid->text();
}
bool WifiListWidget::isTargetNet() const
{
return m_isTargetNet;
}
bool WifiListWidget::isConnected() const
{
return m_isConnected;
}
bool WifiListWidget::getLockStatus() const
{
return m_isLock;
}
void WifiListWidget::clearStatus()
{
m_isTargetNet = false;
updateStatus(false);
}
WifiAccPointDialog::WifiAccPointDialog(QObject *bgWidget) : LineEditInputDialog(QList<QString>{tr("名称"), tr("密码")}, bgWidget, tr("加入"))
{
qDebug() << "WifiAccPointDialog";
QLabel* label = new QLabel(this);
label->setText(tr("加密方式"));
m_comBox_encType = new QComboBox(this);
QStringList strList;
strList<<"none"<<"wep"<<"WPA-PSK/WPA2-PSK";
m_comBox_encType->addItems(strList);
formLayout->insertRow(1, label, m_comBox_encType);
}
WifiAccPointDialog::~WifiAccPointDialog()
{
qDebug() << "~WifiAccPointDialog";
}
void WifiAccPointDialog::slotConfirmBtnClicked()
{
m_sEncType = m_comBox_encType->currentText();
LineEditInputDialog::slotConfirmBtnClicked();
}
QString WifiAccPointDialog::getEncType() const
{
return m_sEncType;
}
int GrabFullScreen(const QString& picName, int rotate)
{
QMatrix matrix;
matrix.rotate(rotate);
return QGuiApplication::primaryScreen()->grabWindow(0).transformed(matrix, Qt::SmoothTransformation).save(picName, "jpg");
}
QPasswordLineEdit::QPasswordLineEdit(QWidget *parent, const QString& ch, int timeout) : QLineEdit(parent), m_ch(ch), m_Timeout(timeout)
{
connect(this,SIGNAL(cursorPositionChanged(int,int)),this,SLOT(DisplayPasswordAfterEditSlot(int,int)));
//connect(this,SIGNAL(textEdited(const QString&)),this,SLOT(GetRealTextSlot(const QString&)));
if(UiConfig::GetInstance()->isRkDevice()){
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(DisplayPasswordSlot()));
}
}
QPasswordLineEdit::~QPasswordLineEdit()
{
}
void QPasswordLineEdit::DisplayPasswordAfterEditSlot(int oldPos,int newPos)
{
if(oldPos>=0 && newPos>=0 ){
if(newPos>oldPos){
if(UiConfig::GetInstance()->isRkDevice()){
m_timer->start(m_Timeout);
}
else{
QTimer::singleShot(m_Timeout,this,SLOT(DisplayPasswordSlot()));
}
}else{
setCursorPosition(oldPos);
}
}
}
void QPasswordLineEdit::DisplayPasswordSlot()
{
setText(GetMaskString());
if(UiConfig::GetInstance()->isRkDevice()){
m_timer->stop();
}
}
void QPasswordLineEdit::GetRealTextSlot(const QString& text)
{
if(text.count()>m_LastCharCount){
m_LineEditText.append(text.right(1));
}else if(text.count()){
m_LineEditText.remove(m_LineEditText.count()-1,1);
}
m_LastCharCount = m_LineEditText.count();
}
QString QPasswordLineEdit::GetPassword() const
{
return m_LineEditText;
}
void QPasswordLineEdit::SetTimeout(int msec)
{
m_Timeout = msec;
}
int QPasswordLineEdit::GetTimeout() const
{
return m_Timeout;
}
QString QPasswordLineEdit::GetMaskString()
{
QString ret;
for(int i=0; i<text().length(); i++){
ret += m_ch;
}
return ret;
}
DrawPage::DrawPage(int w, int h, QWidget *parent) : QWidget(parent)
{
setFixedSize(w, h);
m_btnClear = new QPushButton(this);
m_btnClear->setFixedWidth(w / 4);
m_btnClear->move(w / 8 * 1, 50);
m_btnClear->setText("reset");
m_btnClear->setStyleSheet("QPushButton{border:none;background:transparent;}");
//m_btnClear->setFlat(true);
m_btnExit = new QPushButton(this);
m_btnExit->setFixedWidth(w / 4);
m_btnExit->move(w / 8 * 5, 50);
m_btnExit->setText("exit");
m_btnExit->setStyleSheet("QPushButton{border:none;background:transparent;}");
QPalette pal = palette();
pal.setColor(QPalette::Background, QColor(255, 255, 255, 255));
setPalette(pal);
setAutoFillBackground(true);
m_current.points.clear();
connect(m_btnClear, SIGNAL(clicked()), this, SLOT(slotClearBtnClicked()));
connect(m_btnExit, SIGNAL(clicked()), this, SLOT(slotExitBtnClicked()));
}
void DrawPage::mousePressEvent(QMouseEvent *evt)
{
m_current.points.append(evt->pos());
}
void DrawPage::mouseMoveEvent(QMouseEvent *evt)
{
append(evt->pos());
update();
}
void DrawPage::mouseReleaseEvent(QMouseEvent *evt)
{
append(evt->pos());
m_drawList.append(m_current);
m_current.points.clear();
update();
}
void DrawPage::append(QPoint p)
{
m_current.points.append(p);
}
void DrawPage::paintEvent(QPaintEvent *)
{
QPainter painter(this);
for(int i=0; i<m_drawList.count(); i++)
{
draw(painter, m_drawList[i]);
}
draw(painter, m_current);
}
void DrawPage::slotClearBtnClicked()
{
m_drawList.clear();
update();
}
void DrawPage::slotExitBtnClicked()
{
close();
emit signalClosed();
}
void DrawPage::draw(QPainter& painter, DrawPara& param)
{
if( param.points.count() >= 2)
{
painter.setPen(QPen(Qt::black));
painter.setBrush(QBrush(Qt::black));
for(int i=0; i<param.points.count()-1; i++)
{
painter.drawLine(param.points[i], param.points[i+1]);
}
}
}
DrawPage::~DrawPage()
{
}
UserPwdDialog::UserPwdDialog()
{
m_editPwd = new QLineEdit(this);
m_editPwd->setPlaceholderText(tr("请输入您的密码"));
m_editPwd->setReadOnly(true);
m_btnConfirm = new QPushButton(this);
m_btnConfirm->setText(tr("确认"));
connect(m_btnConfirm, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
m_btnBackspace = new QPushButton(this);
m_btnBackspace->setText(tr("删除"));
connect(m_btnBackspace, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
m_btnClear = new QPushButton(this);
m_btnClear->setText(tr("清空"));
connect(m_btnClear, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
m_btnBack = new QPushButton(this);
m_btnBack->setText(tr("返回"));
connect(m_btnBack, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
QHBoxLayout* hlayTop = new QHBoxLayout();
hlayTop->addWidget(m_btnBack);
hlayTop->addWidget(m_editPwd);
QGridLayout* glayMid = new QGridLayout();
for(int i=0; i<NUM_BTNS_QTY; i++){
m_btnsNum[i] = new QPushButton(this);
m_btnsNum[i]->setText(QString::number(i));
connect(m_btnsNum[i], SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
if(i > 0){
const int pos = i - 1;
glayMid->addWidget(m_btnsNum[i], pos / 3, pos % 3);
}
}
glayMid->addWidget(m_btnClear, 3, 0);
glayMid->addWidget(m_btnsNum[0], 3, 1);
glayMid->addWidget(m_btnBackspace, 3, 2);
QHBoxLayout* hlayBtm = new QHBoxLayout();
hlayBtm->addWidget(m_btnConfirm);
QVBoxLayout* vlay = new QVBoxLayout();
vlay->addLayout(hlayTop);
vlay->addLayout(glayMid);
vlay->addLayout(hlayBtm);
setLayout(vlay);
}
void UserPwdDialog::slotBtnClicked()
{
const QObject* send = sender();
if (send == m_btnConfirm) {
m_pwd = m_editPwd->text();
//m_editPwd->clear();
//hide();
done(Accepted);
}
else if (send == m_btnBackspace) {
QString text = m_editPwd->text();
if(text.size()){
m_editPwd->setText(text.left(text.size() - 1));
}
}
else if (send == m_btnClear) {
m_editPwd->clear();
}
else if (send == m_btnBack) {
m_pwd = m_editPwd->text();
//m_editPwd->clear();
//hide();
done(Rejected);
}
else{
for(int i=0; i<NUM_BTNS_QTY; i++){
if(send == m_btnsNum[i]){
m_editPwd->setText(m_editPwd->text() + m_btnsNum[i]->text());
break;
}
}
}
}
RoundedWidget::RoundedWidget(QWidget *parent) : QWidget(parent)
{
}
void RoundedWidget::paintEvent(QPaintEvent *event)
{
//圆角
QBitmap bmp(this->size());
bmp.fill();
QPainter p(&bmp);
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
p.drawRoundedRect(bmp.rect(),20,20);
setMask(bmp);
//绘制边框
QStyleOption opt;
opt.initFrom(this);
QPainter pr(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &pr, this);//绘制样式
return QWidget::paintEvent(event);
}
StyleItemChooseDialog::StyleItemChooseDialog(const QString& title, const QStringList& items, BackstageInterfaceForUi* backstageIf)
: CustomDialog (tr("确定"), nullptr, tr("取消")), m_backstageIf(backstageIf)
{
dialogWidth = static_cast<int>(UiConfig::GetInstance()->getUiWidth() * 0.7);
double scale = 0.1 * (2 + items.size());
scale = scale > 0.8 ? 0.8 : scale;
dialogHeight = static_cast<int>(UiConfig::GetInstance()->getUiHeight() * scale);
resize(dialogWidth, dialogHeight);
QLabel* tle = new QLabel(title, this);
m_listWidget = new QListWidget(this);
m_listWidget->setFrameStyle(QFrame::NoFrame);
m_listWidget->setCursor(QCursor(Qt::PointingHandCursor));
m_listWidget->setFocusPolicy(Qt::NoFocus);
m_listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_listWidget->setSpacing(static_cast<int>(dialogHeight * 0.03));
m_listWidget->setStyleSheet("QListWidget{border-width:0;border-style:outset; background:transparent;}"
"QListWidget::Item{background:rgb(182, 182, 182);border-radius:10px;padding:2px 4px;}"
"QListWidget::Item:hover{background:rgb(255, 255, 255);border-radius:10px;padding:2px 4px;}"
"QListWidget::item:selected{background:rgb(70, 132, 198);border-radius:10px;padding:2px 4px;}"
"QListWidget::item:selected:!active{background:rgb(70, 132, 198);border-width:0px;border-radius:10px;padding:2px 4px;}");
m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QScroller::grabGesture(m_listWidget, QScroller::LeftMouseButtonGesture);
m_listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
m_listWidget->setFixedSize(dialogWidth, static_cast<int>(dialogHeight * 0.7));
m_listWidget->addItems(items);
//QFont ft;
for (int i=0; i<m_listWidget->count(); i++) {
//ft.setPointSize(static_cast<int>(m_listWidget->item(i)->font().pointSize() * 2));
//m_listWidget->item(i)->setFont(ft);
m_listWidget->item(i)->setTextAlignment(0x0084);
m_listWidget->item(i)->setSizeHint(QSize(dialogWidth, static_cast<int>(UiConfig::GetInstance()->getUiHeight() * 0.05)));
}
m_listWidget->setCurrentRow(0);
connect(m_listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotItemClicked(QListWidgetItem*)));
QVBoxLayout* vLay = new QVBoxLayout();
vLay->addWidget(tle, 1, Qt::AlignCenter);
vLay->addWidget(m_listWidget, 90, Qt::AlignCenter);
//vLay->setMargin(0);
vLay->setContentsMargins(0, static_cast<int>(dialogHeight * 0.1), 0, 0);
vLay->setSpacing(static_cast<int>(dialogHeight * 0.1));
vLay->addLayout(hbtmLayout);
setLayout(vLay);
//支持圆角边框
setStyleSheet("background-color:rgb(255, 255, 255);border-radius:10px;padding:2px 4px;focus{outline: none;}");
//setStyleSheet("QWidget{background-color:gray;border-top-left-radius:15px;border-top-right-radius:5px;}");
setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
setWindowFlags(Qt::FramelessWindowHint); //设置无边框窗口
if(m_timer == nullptr){
m_timer = new QTimer(this);
}
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
m_timer->start(TIMEOUT);
}
StyleItemChooseDialog::~StyleItemChooseDialog()
{
}
void StyleItemChooseDialog::paintEvent(QPaintEvent *event)
{
//支持圆角边框
QStyleOption opt;
opt.initFrom(this);
QPainter pr(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &pr, this);//绘制样式
return QWidget::paintEvent(event);
}
void StyleItemChooseDialog::slotItemClicked(QListWidgetItem* item)
{
Q_UNUSED(item);
m_chooseIndex = m_listWidget->currentRow();
m_timeCnt = 0;
m_timer->start(TIMEOUT);
}
int StyleItemChooseDialog::getChoosedIndex() const
{
return m_chooseIndex;
}
void StyleItemChooseDialog::slotTimeout()
{
m_timeCnt++;
qDebug() << "slotTimeout:" << m_timeCnt;
if(m_timeCnt <= (PROC_OVERTIME - BACKSTAGE_PROC_OVERTIME + 1) ){//后台运行时间增加1s确保界面选择了的情况下后台不会发送超时
m_backstageIf->resetDecisionCenterTimer();
}
else if(m_timeCnt >= PROC_OVERTIME){
m_timer->stop();
done(Rejected);
}
}
SelectionDialog::SelectionDialog(const QString& tips, const QString& sel0, const QString& sel1, QObject *bgWidget)
: CustomDialog(sel1, bgWidget, sel0)
{
QLabel* labelQues = new QLabel(this);
labelQues->setWordWrap(true);
labelQues->setText(tips);
QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(labelQues, 90, Qt::AlignCenter);
vLayout->addLayout(hbtmLayout);
vLayout->setSpacing(dialogHeight / 5);
vLayout->setContentsMargins(0, dialogHeight / 10, 0, 0);
setLayout(vLayout);
if(QDialog::Accepted == exec()){
m_sel = 1;
}
}
int SelectionDialog::selecion() const
{
return m_sel;
}
TimeWidget::TimeWidget(bool enType, int timeSize, int dateSize, QWidget *parent) : QWidget(parent)
{
QLabel* time = new QLabel(this);
QFont ft;
ft.setPointSize(timeSize);
time->setFont(ft);
QLabel* date = new QLabel(this);
ft.setPointSize(dateSize);
date->setFont(ft);
m_timerUpdate = new TimerUpdate(time, date, this, 1, 0, enType ? 1 : 2);
//Q_UNUSED(timerUpdate);
QPalette palette(this->palette());
palette.setColor(QPalette::WindowText,Qt::black);
setPalette(palette);
QVBoxLayout* vlay = new QVBoxLayout();
vlay->addStretch(10);
vlay->addWidget(time, 1, Qt::AlignCenter);
vlay->addWidget(date, 1, Qt::AlignCenter);
vlay->addStretch(10);
setLayout(vlay);
}
void TimeWidget::enableUpdate(bool enable)
{
m_timerUpdate->enableUpdate(enable);
}
DateTimeWidgetClassical::DateTimeWidgetClassical(int fontSize, QWidget *parent) : QWidget(parent)
{
QFont ft;
ft.setPointSize(fontSize);
QLabel* date = new QLabel(this);
date->setFont(ft);
QLabel* time = new QLabel(this);
time->setFont(ft);
TimerUpdate* timerUpdate = new TimerUpdate(time, date, this, 1, 1);
connect(timerUpdate, SIGNAL(signalNewDay()), this, SIGNAL(signalNewDay()));
QHBoxLayout* hlay = new QHBoxLayout();
hlay->addWidget(date, 1, Qt::AlignLeft);
hlay->addWidget(time, 1, Qt::AlignRight);
hlay->setMargin(0);
setLayout(hlay);
}
//BtListWidget::BtListWidget(const QString &devId, const QString &devName, bool isPaired, int devStat)
BtListWidget::BtListWidget(BluetoothDev_t &rBtDev/*, bool isPaired*/)
{
m_btDev = rBtDev;
//m_isPaired = isPaired;
m_label_dev = new QLabel(this);
m_label_btIcon= new QLabel(this);
m_pushButton_set = new QPushButton(this);
connect(m_pushButton_set, SIGNAL(clicked()), this, SLOT(slotBtnSetClick()));
QHBoxLayout* hLay = new QHBoxLayout();
hLay->addWidget(m_label_btIcon, 1, Qt::AlignLeft);
hLay->addWidget(m_label_dev, 90, Qt::AlignLeft);
hLay->addWidget(m_pushButton_set, 1, Qt::AlignRight);
hLay->setSpacing(10);
hLay->setContentsMargins(20, 20, 40, 20);
setLayout(hLay);
refreshWidget();
}
BtListWidget::~BtListWidget()
{
}
void BtListWidget::refreshWidget()
{
QPixmap pm;
if(m_btDev.devStat == BlueToothDevStat_connected)
{
pm.load(":/res/image/bt_blue_large.png");
}else
{
pm.load(":/res/image/bt_black_large.png");
}
m_label_btIcon->setPixmap(pm.scaled(45, 45, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
QString showTextStr;
switch (m_btDev.devStat) {
case BlueToothDevStat_idle:
case BlueToothDevStat_paired:
showTextStr = QString("<p style='line-height:100%;'><font size='+0'>%1</font></p>").arg(QString::fromStdString(m_btDev.name));
break;
case BlueToothDevStat_pairing://匹配中
showTextStr = QString("<p style='line-height:100%;'><font size='+0'>%1</font><br><font size='-2'>正在匹配中...</font></p>").arg(QString::fromStdString(m_btDev.name));
break;
case BlueToothDevStat_connecting://连接中
showTextStr = QString("<p style='line-height:100%;'><font size='+0' color='#42A2FF'>%1</font><br><font size='-2'>正在连接中...</font></p>").arg(QString::fromStdString(m_btDev.name));
break;
case BlueToothDevStat_connected://已连接
showTextStr = QString("<p style='line-height:100%;'><font size='+0' color='#42A2FF'>%1</font><br><font size='-2'>已连接</font></p>").arg(QString::fromStdString(m_btDev.name));
break;
default:
showTextStr = QString("%1").arg(QString::fromStdString(m_btDev.name));
break;
}
m_label_dev->setText(showTextStr);
//m_label_dev->setStyleSheet("background-color:yellow");
m_label_dev->setAttribute(Qt::WA_TransparentForMouseEvents, true);//观察到设置html文本时属性发生变化
setButtonBackImage(m_pushButton_set, ":/res/image/gear_large.png");
if(/*m_isPaired*/m_btDev.devStat >= BlueToothDevStat_paired){
m_pushButton_set->setVisible(true);
}else{
m_pushButton_set->setVisible(false);
}
}
void BtListWidget::getDevInfo(BluetoothDev_t &dev)
{
dev=m_btDev;
}
void BtListWidget::updateDev(BluetoothDev_t &rBtDev)
{
m_btDev = rBtDev;
refreshWidget();
}
void BtListWidget::slotBtnSetClick()
{
LOGD("slotBtnSetClick: %s", m_btDev.name.c_str());
emit signalClickSet(m_btDev);
}
IpInputBar::IpInputBar(bool isMask, QWidget *parent) : QWidget(parent), m_isMask(isMask) {
QHBoxLayout *hlay = new QHBoxLayout();
hlay->setMargin(5);
hlay->setSpacing(5);
for (int i = 0; i < 4; i++) {
LineEditWithKeyboard *edit = new LineEditWithKeyboard(this);
edit->setFixedWidth(60);
edit->setStyleSheet("QLineEdit { background: transparent; border: none; border-bottom: 1px solid black; }");
connect(edit, SIGNAL(textChanged(const QString &)), this, SLOT(slotTextChanged(const QString &)));
connect(edit, SIGNAL(signalBackspacePressed()), this, SLOT(slotBackspacePressed()));
m_edits.push_back(edit);
hlay->addWidget(edit);
if (i < 3) {
hlay->addWidget(new QLabel(".", this));
}
}
setLayout(hlay);
}
void IpInputBar::slotBackspacePressed() {
LineEditWithKeyboard *edit = dynamic_cast<LineEditWithKeyboard *>(sender());
int editIndex = 0;
for (int i = 0; i < 4; i++) {
if (edit == m_edits.at(i)) {
editIndex = i;
break;
}
}
if (editIndex != 0 && edit->cursorPosition() == 0) {
keyboard::GetInstance(m_edits.at(editIndex - 1));
}
}
void IpInputBar::slotTextChanged(const QString &t) {
qDebug() << "text:" << t;
if (t.isEmpty()) {
return;
}
bool ok = false;
int value = t.toInt(&ok);
if (!ok) {
value = -1;
}
LineEditWithKeyboard *edit = dynamic_cast<LineEditWithKeyboard *>(sender());
int editIndex = 0;
for (int i = 0; i < 4; i++) {
if (edit == m_edits.at(i)) {
editIndex = i;
break;
}
}
do {
bool inputOk = false;
ON_SCOPE_EXIT([&] {
if (inputOk) {
if (t.size() >= 3 && editIndex != 3) {
keyboard::GetInstance(m_edits.at(editIndex + 1));
}
}
});
QString lastChar{t.at(t.size() - 1)};
if (value < 0 && editIndex != 3 && (lastChar == "." || lastChar == "·")) {
keyboard::GetInstance(m_edits.at(editIndex + 1));
break;
}
QString errorTip;
if (!m_isMask && editIndex == 0) {
if (value >= 1 && value <= 223) {
inputOk = true;
return;
}
errorTip = tr("请指定一个介于1和223之间的值");
} else {
if (value >= 0 && value <= 255) {
inputOk = true;
return;
}
errorTip = tr("请指定一个介于0和255之间的值");
}
InfoDialog(errorTip).exec();
} while (0);
edit->setText(edit->text().remove(edit->cursorPosition() - 1, 1));
}
bool IpInputBar::isEmpty() const {
for (const auto &e : m_edits) {
if (!e->text().isEmpty()) {
return false;
}
}
return true;
}
QString IpInputBar::getIp() const {
return m_edits.at(0)->text() + "." + m_edits.at(1)->text() + "." + m_edits.at(2)->text() + "." +
m_edits.at(3)->text();
}
IpInputBarDialog::IpInputBarDialog(const QStringList &options, const QVector<bool> &required,
const QVector<bool> &isMask, QObject *bgWidget)
: CustomDialog(tr("确定"), bgWidget), m_required(required) {
QVBoxLayout *vLayout = new QVBoxLayout();
int i = 0;
for (const auto &s : options) {
QHBoxLayout *hLayBar = new QHBoxLayout();
hLayBar->setSpacing(10);
hLayBar->addWidget(new QLabel(s, this), 10, Qt::AlignLeft);
IpInputBar *bar = new IpInputBar(isMask.size() > i ? isMask.at(i) : false, this);
bar->setFixedHeight(dialogHeight / 4);
hLayBar->addWidget(bar, 1, Qt::AlignRight);
m_bars.push_back(bar);
vLayout->addLayout(hLayBar);
i++;
}
vLayout->addStretch();
vLayout->addLayout(hbtmLayout, 1);
vLayout->setMargin(20);
vLayout->setSpacing(10);
setLayout(vLayout);
setFixedSize(dialogWidth, static_cast<int>(dialogHeight * 1.5));
move((UiConfig::GetInstance()->getUiWidth() - dialogWidth) / 2, UiConfig::GetInstance()->getUiHeight() / 3);
}
void IpInputBarDialog::slotConfirmBtnClicked() {
for (int i = 0; i < m_bars.size(); i++) {
const bool needCheck = !m_bars.at(i)->isEmpty() || i >= m_required.size() || m_required.at(i);
if (needCheck && !checkip(m_bars.at(i)->getIp())) {
InfoDialog(tr("设置有误,请重新输入!")).exec();
return;
}
}
CustomDialog::slotConfirmBtnClicked();
}
QString IpInputBarDialog::getRow(int row) const {
if (row >= m_bars.size()) {
return "";
}
return m_bars.at(row)->isEmpty() ? "" : m_bars.at(row)->getIp();
}
QString SearchBarCandidate::getCurrentChooseText()
{
return m_listWidget->currentItem()->text();
}
void SearchBarCandidate::slotItemClicked(QListWidgetItem* item)
{
ItemChoosePage::slotItemClicked(item);
emit signalItemChoosed(m_listWidget->currentRow());
}
ImgPlayer::ImgPlayer(int width, int height, QWidget *parent) : QWidget(parent), m_width(width), m_height(height)
{
setFixedSize(m_width, m_height);
m_targetRct = QRectF(0.0, 0.0, m_width, m_height);
}
void ImgPlayer::paintEvent(QPaintEvent * event)
{
QPainter painter(this);
painter.drawImage(m_targetRct, m_img);
}
void ImgPlayer::slotImgLoad(QImage img)
{
m_img = img.scaled(m_width, m_height);
update();
}