138 lines
6.2 KiB
C++
138 lines
6.2 KiB
C++
#include "confirmdialog.h"
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QTimer>
|
|
#include "global.h"
|
|
|
|
ConfirmDialog::ConfirmDialog(QWidget* parent)
|
|
: QDialog(parent)
|
|
{
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
setModal(true);
|
|
if (parent) setGeometry(parent->rect());
|
|
setStyleSheet("background-color: rgba(0,0,0,102);");
|
|
|
|
m_panel = new QWidget(this);
|
|
m_panel->setObjectName("ConfirmPanel");
|
|
m_panel->setFixedSize(700, 420);
|
|
m_panel->setStyleSheet(QString(
|
|
"QWidget#ConfirmPanel{background-color:%1;border:2px solid %2;border-radius:16px;}")
|
|
.arg(C::PANEL, C::BORDER));
|
|
|
|
QVBoxLayout* lay = new QVBoxLayout(m_panel);
|
|
lay->setContentsMargins(28, 24, 28, 24);
|
|
lay->setSpacing(0); // 标题与正文不留间隙(与“操作说明”弹窗一致)
|
|
|
|
m_title = new QLabel("", m_panel);
|
|
m_title->setStyleSheet(QString("background:transparent;color:%1;font-size:50px;font-weight:700;").arg(C::TEXT));
|
|
m_title->setAlignment(Qt::AlignCenter);
|
|
lay->addWidget(m_title);
|
|
lay->addSpacing(60); // 标题与正文之间空一行
|
|
|
|
m_body = new QLabel("", m_panel);
|
|
m_body->setStyleSheet(QString("background:transparent;color:%1;font-size:50px;").arg(C::TEXT));
|
|
m_body->setAlignment(Qt::AlignCenter);
|
|
m_body->setWordWrap(true);
|
|
lay->addWidget(m_body); // 不再拉伸:标题与正文紧挨着
|
|
lay->addStretch(1); // 剩余空间放在正文与按钮之间(标题/正文保持自然高度,标题顶到最上面)
|
|
|
|
QHBoxLayout* btnLay = new QHBoxLayout();
|
|
btnLay->addStretch(1);
|
|
QPushButton* ok = new QPushButton("确认", m_panel);
|
|
// 常态灰底、选中(悬停/焦点)蓝底白字。
|
|
// 注意:该表设了 background-color,会压住全局的 variant/焦点规则,所以状态必须写在这里。
|
|
ok->setFixedSize(180, 80);
|
|
ok->setStyleSheet(QString(
|
|
"QPushButton{background-color:%1;color:%2;border:1px solid %3;border-radius:12px;font-size:60px;}"
|
|
"QPushButton:hover,QPushButton:focus{background-color:%4;color:#FFFFFF;border:2px solid #8ABFFF;}")
|
|
.arg(C::PANEL_HI, C::TEXT, C::BORDER, C::PRIMARY));
|
|
connect(ok, &QPushButton::clicked, this, &ConfirmDialog::accept);
|
|
m_cancel = new QPushButton("取消", m_panel);
|
|
m_cancel->setFixedSize(180, 80); // 与“确认”同尺寸
|
|
m_cancel->setStyleSheet(cancelButtonStyle(60)); // 与“确认”同字号;选中时红底白字
|
|
connect(m_cancel, &QPushButton::clicked, this, &ConfirmDialog::reject);
|
|
btnLay->addWidget(ok);
|
|
btnLay->addSpacing(80); // “确认”与“取消”之间留 40px
|
|
btnLay->addWidget(m_cancel);
|
|
btnLay->addStretch(1);
|
|
lay->addSpacing(30); // 正文与按钮之间留 30px
|
|
lay->addLayout(btnLay);
|
|
|
|
QVBoxLayout* root = new QVBoxLayout(this);
|
|
root->setContentsMargins(0, 0, 0, 0);
|
|
root->addStretch(1);
|
|
root->addWidget(m_panel, 0, Qt::AlignHCenter);
|
|
root->addStretch(1);
|
|
}
|
|
|
|
void ConfirmDialog::setTitle(const QString& t) { m_title->setText(t); }
|
|
void ConfirmDialog::setText(const QString& t) { m_body->setText(t); }
|
|
|
|
bool ConfirmDialog::confirm(QWidget* parent, const QString& title, const QString& text)
|
|
{
|
|
ConfirmDialog dlg(parent);
|
|
dlg.setTitle(title);
|
|
dlg.setText(text);
|
|
return dlg.exec() == QDialog::Accepted;
|
|
}
|
|
|
|
void ConfirmDialog::message(QWidget* parent, const QString& title, const QString& text,
|
|
int width, int fontPx)
|
|
{
|
|
ConfirmDialog dlg(parent);
|
|
dlg.m_cancel->hide(); // 只保留“确认”
|
|
dlg.m_title->setStyleSheet(QString("background:transparent;color:%1;font-size:50px;font-weight:700;").arg(C::TEXT));
|
|
// 正文单独给字号:文案长的调用点可以调小它,让固定的字数在更窄的面板里排成预期行数
|
|
dlg.m_body->setStyleSheet(QString("background:transparent;color:%1;font-size:%2px;")
|
|
.arg(C::TEXT).arg(fontPx));
|
|
dlg.m_title->setWordWrap(true);
|
|
dlg.m_title->setContentsMargins(0, 20, 0, 0); // 标题文字距该行顶部 10px
|
|
dlg.m_body->setContentsMargins(0, 0, 0, 0);
|
|
dlg.m_body->setAlignment(Qt::AlignCenter); // 正文居中(水平+垂直)
|
|
if (QBoxLayout* lay = qobject_cast<QBoxLayout*>(dlg.m_panel->layout())) {
|
|
lay->setSpacing(0); // 统一间隔置 0,各段间距单独控制
|
|
lay->setContentsMargins(28, 28, 28, 28); // 内容距面板四边 28px
|
|
//lay->insertSpacing(lay->count() - 1, 20); // “确认”按钮上方留 20px
|
|
}
|
|
dlg.setTitle(title);
|
|
dlg.setText(text);
|
|
|
|
// 面板宽度由调用点给定,高度按内容算:标题/正文都开了 wordWrap,宽度先定下来,
|
|
// layout 才能用 heightForWidth 量出准确高度(QLabel 的 sizeHint 对换行文本不准,
|
|
// 所以不能靠 adjustSize)。宽度以前是写死的常量、三个调用点共用,一处的长文案
|
|
// 会把别处的弹窗一起撑宽,故改成逐调用点传入。
|
|
const int kW = width;
|
|
dlg.m_panel->ensurePolished(); // 先让 QSS 的 50px 字号落到控件上,量出来的高度才对
|
|
dlg.m_panel->setFixedSize(kW, dlg.m_panel->heightForWidth(kW));
|
|
dlg.exec();
|
|
}
|
|
|
|
void ConfirmDialog::toast(QWidget* parent, const QString& text, int ms)
|
|
{
|
|
QDialog* d = new QDialog(parent);
|
|
d->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
|
|
d->setAttribute(Qt::WA_TranslucentBackground);
|
|
d->setModal(true);
|
|
if (parent) d->setGeometry(parent->rect());
|
|
d->setStyleSheet("background-color: rgba(0,0,0,80);");
|
|
|
|
QLabel* lbl = new QLabel(text, d);
|
|
lbl->setAlignment(Qt::AlignCenter);
|
|
lbl->setStyleSheet(QString(
|
|
"background-color:%1;color:%2;font-size:50px;font-weight:700;border:2px solid %3;border-radius:16px;padding:56px 88px;")
|
|
.arg(C::PANEL, C::SUCCESS, C::BORDER));
|
|
lbl->adjustSize();
|
|
|
|
QVBoxLayout* root = new QVBoxLayout(d);
|
|
root->addStretch(1);
|
|
root->addWidget(lbl, 0, Qt::AlignHCenter);
|
|
root->addStretch(1);
|
|
|
|
QTimer::singleShot(ms, d, &QDialog::accept);
|
|
d->exec();
|
|
d->deleteLater();
|
|
}
|