123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- #include "DialogBase.h"
- #include <QMouseEvent>
- #include <QVBoxLayout>
- #include <QLabel>
- #include <QPushButton>
- #include <QBoxLayout>
- #include <QStyle>
- #include <qsize.h>
- #include "OneShadowEffect.h"
- DialogBase::DialogBase(QWidget *parent)
- : QDialog(parent)
- {
- m_logger = spdlog::get("ACASetting");
- if(m_logger == nullptr)
- {
- fmt::print("DialogBase: 未发现ACASetting日志记录器,请检查日志记录器是否已初始化。\n");
- return;
- }
- initUI();
- initSettings();
- /* 设置qss */
- connect(&UIStyle, &UIStyleManager::signal_qssChanged, this, &DialogBase::do_setQSS);
- /* 设置默认的UI */
- setParentQSS();
- }
- DialogBase::~DialogBase()
- {
- }
- /* 设置标题 */
- void DialogBase::setTitle(const QString &title, QSize size)
- {
- m_labelTitle->setText(title);
- m_labelTitle->setFixedSize(size);
- }
- /* 获取标题 */
- QString DialogBase::getTitle() const
- {
- return m_labelTitle ? m_labelTitle->text() : QString();
- }
- /* 设置内容容器 */
- bool DialogBase::setContentWidget(QWidget *widget)
- {
- if(widget == nullptr)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "设置内容容器失败,widget为nullptr");
- return false;
- }
- if(m_widgetContent == widget)
- {
- return true; // 如果已经是这个widget了,就不需要重新设置
- }
- if(m_widgetContent != nullptr)
- {
- m_layoutBackground->removeWidget(m_widgetContent);
- delete m_widgetContent;
- m_widgetContent = nullptr;
- }
- m_widgetContent = widget;
- m_layoutBackground->insertWidget(1, m_widgetContent);
- return true;
- }
- /* 设置低栏容器 */
- bool DialogBase::setBottomWidget(QWidget *widget)
- {
- if(widget == nullptr)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "设置底部容器失败,widget为nullptr");
- return false;
- }
- if(m_widgetBottom != nullptr)
- {
- m_layoutBackground->removeWidget(m_widgetBottom);
- delete m_widgetBottom;
- m_widgetBottom = nullptr;
- }
- m_widgetBottom = widget;
- m_layoutBackground->insertWidget(2, m_widgetBottom);
- return true;
- }
- /* 移除底栏 */
- bool DialogBase::removeBottomWidget()
- {
- if(m_widgetBottom == nullptr)
- {
- return true;
- }
- m_layoutBackground->removeWidget(m_widgetBottom);
- delete m_widgetBottom;
- m_widgetBottom = nullptr;
- return true;
- }
- /* 点击确定按钮之前执行的操作 */
- bool DialogBase::isOKClicked()
- {
- /* 默认返回true,子类可以重载这个函数来实现自定义的逻辑 */
- return true;
- }
- /* 设置一个控件报警,边框显示红色
- * 注意:这个功能需要在qss里设置属性[Warn=true],并实现相应的报警样式 */
- void DialogBase::setWarn(QWidget* widget, bool isWarn)
- {
- if(widget == nullptr)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "设置控件报警失败,widget为nullptr");
- return;
- }
- widget->setProperty("Warn", isWarn);
- widget->style()->unpolish(widget);
- widget->style()->polish(widget);
- widget->update();
- }
- /* 初始化UI */
- void DialogBase::initUI()
- {
- /*--------------------- 设置对话框的属性 -----------------------*/
- this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
- this->setAttribute(Qt::WA_TranslucentBackground, true);
- // this->setObjectName("DialogBase");
- /*--------------------- 初始化背景容器 -----------------------*/
- m_widgetBackground = new QWidget(this);
- m_widgetBackground->setObjectName("widget_content__");
- QVBoxLayout* layout = new QVBoxLayout(this);
- /* 设置边距,就是阴影的宽度 */
- layout->setContentsMargins(20, 20, 20, 20);
- layout->setSpacing(0);
- this->setLayout(layout);
- layout->addWidget(m_widgetBackground);
- /* 创建背景容器内部的布局 */
- m_layoutBackground = new QVBoxLayout(m_widgetBackground);
- m_layoutBackground->setContentsMargins(0, 0, 0, 0);
- m_layoutBackground->setSpacing(0);
- m_widgetBackground->setLayout(m_layoutBackground);
- /*--------------------- 初始顶栏 -----------------------*/
- /* 初始化顶部标题栏,高度56,下面分割线高度1 */
- m_widgetTop = new QWidget(this);
- m_widgetTop->setObjectName("widget_top");
- m_widgetTop->setFixedHeight(56);
- m_layoutBackground->addWidget(m_widgetTop);
- QHBoxLayout* topHBoxLayout = new QHBoxLayout(m_widgetTop);
- m_widgetTop->setLayout(topHBoxLayout);
- /* 初始化标题栏 */
- m_labelTitle = new QLabel(m_widgetTop);
- m_labelTitle->setObjectName("label_title");
- m_labelTitle->setText("Dialog Title");
- m_labelTitle->setFixedSize(120, 18);
- m_labelTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- /* 初始化关闭按钮 */
- m_pBtn_Close = new QPushButton(m_widgetTop);
- m_pBtn_Close->setObjectName("pBtn_Close");
- m_pBtn_Close->resize(32, 32);
- /* 安装事件过滤器,主要是处理鼠标悬停事件 */
- m_pBtn_Close->installEventFilter(this);
- /* 重新布局顶栏 */
- layoutTop();
- /*--------------------- 初始化内容栏 -----------------------*/
- // m_widgetContent = new QWidget(this);
- // m_widgetContent->resize(400, 300);
- // m_layoutBackground->addWidget(m_widgetContent);
- /*--------------------- 初始底栏 -----------------------*/
- m_widgetBottom = new QWidget(this);
- m_widgetBottom->setObjectName("widget_bottom");
- m_widgetBottom->setFixedHeight(88);
- m_layoutBackground->addWidget(m_widgetBottom);
- QHBoxLayout* bottomHBoxLayout = new QHBoxLayout(m_widgetBottom);
- m_widgetBottom->setLayout(bottomHBoxLayout);
- /* 初始化取消按钮 */
- m_pBtn_Cancel = new QPushButton(m_widgetBottom);
- m_pBtn_Cancel->setObjectName("pBtn_Cancel");
- m_pBtn_Cancel->setText("取消");
- m_pBtn_Cancel->setFixedSize(60, 32);
- bottomHBoxLayout->addWidget(m_pBtn_Cancel, 0, Qt::AlignRight | Qt::AlignVCenter);
- /* 初始化确认按钮 */
- m_pBtn_OK = new QPushButton(m_widgetBottom);
- m_pBtn_OK->setObjectName("pBtn_OK");
- m_pBtn_OK->setText("确定");
- m_pBtn_OK->setFixedSize(60, 32);
- bottomHBoxLayout->addWidget(m_pBtn_OK, 0, Qt::AlignRight | Qt::AlignVCenter);
- /* 添加一个底部横向弹簧 */
- bottomHBoxLayout->insertStretch(0, 1);
- /* 设置底部按钮的间距 */
- bottomHBoxLayout->setContentsMargins(32, 24, 32, 32);
- bottomHBoxLayout->setSpacing(16);
- /*--------------------- 设置整体背景 -----------------------*/
- /* 这个背景需要在初始化所有的widget控件之后再设置 */
- // auto screenRect = QGuiApplication::screenAt(QCursor::pos())->geometry();
- // this->resize(screenRect.width(), screenRect.height());
- // /* 设置设置区域居中显示 */
- // m_widgetBackground->move(screenRect.width() / 2 - m_widgetBackground->width() / 2,
- // screenRect.height() / 2 - m_widgetBackground->height() / 2);
-
- /*--------------------- 创建阴影 -----------------------*/
- auto pShadow = new OneShadowEffect(this);
- m_widgetBackground->setGraphicsEffect(pShadow);
- }
- /* 初始化其他设置 */
- void DialogBase::initSettings()
- {
- /* 初始化变量 */
- m_lastPos = QPoint(0, 0);
- /* 连接信号和槽 */
- connect(m_pBtn_Close, &QPushButton::clicked, this, &DialogBase::do_pBtn_Close_Clicked);
- connect(m_pBtn_OK, &QPushButton::clicked, this, &DialogBase::do_pBtn_OK_Clicked);
- connect(m_pBtn_Cancel, &QPushButton::clicked, this, &DialogBase::do_pBtnCancel_clicked);
- /* 设置样式表 */
- setParentQSS();
- }
- /* 加载QSS */
- void DialogBase::setParentQSS()
- {
- /* 获取样式表路径 */
- QString qssPath = UIStyle.getQSSPath() + "/dialogbase.qss";
- QFile file(qssPath);
- if(file.open(QFile::ReadOnly))
- {
- QString qss = file.readAll();
- file.close();
- m_widgetBackground->setStyleSheet(qss);
- } else
- {
- SPDLOG_LOGGER_ERROR(m_logger, "打开QSS文件失败: {}", qssPath.toStdString());
- SPDLOG_LOGGER_ERROR(m_logger, "错误信息: {}", file.errorString().toStdString());
- }
- }
- /* 设置top栏的位置布局,标题和关闭按钮的高度起始位置不一样,不方便使用布局,所有手动修改坐标
- * 标题位置32,18,高度18
- * 关闭按钮右对齐16,y12,大小32x32 */
- void DialogBase::layoutTop()
- {
- m_labelTitle->move(32, 18);
- int x = m_widgetTop->width() - m_pBtn_Close->width() - 16;
- m_pBtn_Close->move(x, 12);
- }
- /* 确认按钮点击事件 */
- void DialogBase::do_pBtn_OK_Clicked()
- {
- if(!isOKClicked())
- {
- /* 如果子类没有重载这个函数,默认返回true */
- return;
- }
- m_isOK = true;
- this->accept();
- }
- /* 设置QSS */
- void DialogBase::do_setQSS(EUIStyle style)
- {
- setParentQSS();
- }
- /* 重写鼠标按下事件 */
- void DialogBase::mousePressEvent(QMouseEvent *event)
- {
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 重写鼠标移动事件 */
- void DialogBase::mouseMoveEvent(QMouseEvent *event)
- {
- auto point = m_widgetTop->mapToGlobal(QPoint(0, 0));
- QRect rect(point, m_widgetTop->size());
- if(!rect.contains(m_lastPos))
- {
- event->accept();
- return;
- }
- int dx = event->globalX() - m_lastPos.x();
- int dy = event->globalY() - m_lastPos.y();
- // m_widgetBackground->move(m_widgetBackground->x() + dx, m_widgetBackground->y() + dy);
- move(x() + dx, y() + dy);
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 重写鼠标释放事件 */
- void DialogBase::mouseReleaseEvent(QMouseEvent *event)
- {
- event->accept();
- }
- /* 显示事件 */
- void DialogBase::showEvent(QShowEvent *event)
- {
- QDialog::showEvent(event);
- /* 重新布局顶栏 */
- layoutTop();
-
- }
- /* 重新设置大小 */
- void DialogBase::resizeEvent(QResizeEvent *event)
- {
- QDialog::resizeEvent(event);
- /* 重新布局顶栏 */
- layoutTop();
- }
- /* 事件过滤器 */
- bool DialogBase::eventFilter(QObject *watched, QEvent *event)
- {
- if(watched == m_pBtn_Close && event->type() == QEvent::HoverEnter)
- {
- m_pBtn_Close->setProperty("Hover", true);
- m_pBtn_Close->style()->unpolish(m_pBtn_Close);
- m_pBtn_Close->style()->polish(m_pBtn_Close);
- m_pBtn_Close->update();
- return true;
- }
- else if(watched == m_pBtn_Close && event->type() == QEvent::HoverLeave)
- {
- m_pBtn_Close->setProperty("Hover", false);
- m_pBtn_Close->style()->unpolish(m_pBtn_Close);
- m_pBtn_Close->style()->polish(m_pBtn_Close);
- m_pBtn_Close->update();
- return true;
- }
- return QDialog::eventFilter(watched, event);
- }
|