DialogBase.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef __DIALOGBASE_H__
  2. #define __DIALOGBASE_H__
  3. #include <QDialog>
  4. #include "spdlog/spdlog.h"
  5. #include "UIStyleManager.h"
  6. class QMouseEvent;
  7. class QVBoxLayout;
  8. class QPushButton;
  9. class QWidget;
  10. class QLabel;
  11. class QPoint;
  12. /**
  13. 这个基础类主要为了实现弹窗通用的拖动和阴影效果
  14. 1、父类的QSS通过信号来主动设置,避免和子类冲突
  15. 使用方法:
  16. 1、正常创建UI文件及其.h/.cpp文件
  17. 2、继承DialogBase类
  18. 3、创建一个新的QWidget初始化UI,然后通过setContentWidget()设置内容区域
  19. QWidget* contentWidget = new QWidget(this);
  20. ui->setupUi(contentWidget);
  21. // 设置内容
  22. this->setContentWidget(contentWidget);
  23. 4、默认带有两个按钮的底部区域,保存条件可以通过isOKClicked()函数重载来实现
  24. 如果不需要底部区域,可以通过removeBottomWidget()移除,如果底部区域需要自定义,
  25. 建议直接搞在内容区域里,比较方便
  26. */
  27. class DialogBase : public QDialog
  28. {
  29. Q_OBJECT
  30. public:
  31. explicit DialogBase(QWidget *parent = nullptr);
  32. explicit DialogBase(bool isFullScreen, QWidget *parent = nullptr);
  33. virtual ~DialogBase();
  34. /* 设置标题 */
  35. void setTitle(const QString &title, QSize size = QSize(120, 18));
  36. /* 获取标题 */
  37. QString getTitle() const;
  38. /* 获取是否点击了确定按钮 */
  39. virtual bool isOK() const { return m_isOK; }
  40. protected:
  41. /* --------------------------------------------------
  42. * 给子类使用的内部接口
  43. * --------------------------------------------------*/
  44. /* 设置内容容器 */
  45. bool setContentWidget(QWidget *widget);
  46. /* 设置底栏容器 */
  47. bool setBottomWidget(QWidget *widget);
  48. /* 移除底栏 */
  49. bool removeBottomWidget();
  50. /* 获取内容指针 */
  51. QWidget* getContentWidget() const { return m_widgetContent; }
  52. /* 获取底部容器指针 */
  53. QWidget* getBottomWidget() const { return m_widgetBottom; }
  54. /* 点击确定按钮之后执行的操作 */
  55. virtual bool isOKClicked();
  56. /* 点击关闭按钮之后执行的操作 */
  57. virtual bool isCloseClicked();
  58. /* 设置一个控件报警,边框显示红色
  59. * 注意:这个功能需要在qss里设置属性[Warn=true],并实现相应的报警样式 */
  60. void setWarn(QWidget* widget, bool isWarn = true);
  61. protected:
  62. /* 初始化UI */
  63. virtual void initUI();
  64. /* 初始化其他设置 */
  65. virtual void initSettings();
  66. /* 加载QSS */
  67. virtual void setParentQSS();
  68. /* 设置top栏的位置布局 */
  69. void layoutTop();
  70. protected slots:
  71. /* 关闭按钮点击事件 */
  72. void do_pBtn_Close_Clicked();
  73. /* 确认按钮点击事件 */
  74. void do_pBtn_OK_Clicked();
  75. /* 取消按钮点击事件 */
  76. void do_pBtnCancel_clicked() { this->reject(); }
  77. /* 设置QSS */
  78. void do_setQSS(EUIStyle style);
  79. protected:
  80. /* 重写鼠标按下事件 */
  81. void mousePressEvent(QMouseEvent *event) override;
  82. /* 重写鼠标移动事件 */
  83. void mouseMoveEvent(QMouseEvent *event) override;
  84. /* 重写鼠标释放事件 */
  85. void mouseReleaseEvent(QMouseEvent *event) override;
  86. /* 显示事件 */
  87. void showEvent(QShowEvent *event) override;
  88. /* 重新设置大小 */
  89. void resizeEvent(QResizeEvent *event) override;
  90. /* 事件过滤器 */
  91. bool eventFilter(QObject *watched, QEvent *event) override;
  92. protected:
  93. std::shared_ptr<spdlog::logger> m_logger = nullptr;
  94. bool m_isOK = false; /* 是否点击了确认按钮 */
  95. private:
  96. bool m_isFullScreen = false; /* 是否全屏显示,给时间选择和日期选择弹窗使用的,设置背后透明背景全屏 */
  97. QPoint m_lastPos; /* 鼠标点击的位置 */
  98. QWidget* m_widgetTransparent = nullptr; /* 透明背景容器,在m_widgetBackground下面,全屏时,
  99. m_widgetBackground在这个透明容器中移动 */
  100. QWidget* m_widgetBackground = nullptr; /* 背景容器,这个才是真正容纳所有内容的容器 */
  101. QWidget* m_widgetTop = nullptr; /* 顶部标题栏 */
  102. QWidget* m_widgetContent = nullptr; /* 内容区域 */
  103. QWidget* m_widgetBottom = nullptr; /* 底部按钮区域 */
  104. QVBoxLayout* m_layoutBackground = nullptr; /* 背景容器的布局 */
  105. QLabel* m_labelTitle = nullptr; /* 标题标签 */
  106. QPushButton* m_pBtn_Close = nullptr; /* 关闭按钮 */
  107. QPushButton* m_pBtn_OK = nullptr; /* 确认按钮 */
  108. QPushButton* m_pBtn_Cancel = nullptr; /* 取消按钮 */
  109. };
  110. #endif // __DIALOGBASE_H__