DialogBase.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @brief 这个基础类主要为了实现弹窗通用的拖动和阴影效果
  14. * 1、父类的QSS通过信号来主动设置,避免和子类冲突
  15. *
  16. */
  17. class DialogBase : public QDialog
  18. {
  19. Q_OBJECT
  20. public:
  21. explicit DialogBase(QWidget *parent = nullptr);
  22. virtual ~DialogBase();
  23. /* 设置标题 */
  24. void setTitle(const QString &title, QSize size = QSize(120, 18));
  25. /* 获取标题 */
  26. QString getTitle() const;
  27. /* 获取是否点击了确定按钮 */
  28. virtual bool isOK() const { return m_isOK; }
  29. protected:
  30. /* --------------------------------------------------
  31. * 给子类使用的内部接口
  32. * --------------------------------------------------*/
  33. /* 设置内容容器 */
  34. bool setContentWidget(QWidget *widget);
  35. /* 设置底栏容器 */
  36. bool setBottomWidget(QWidget *widget);
  37. /* 移除底栏 */
  38. bool removeBottomWidget();
  39. /* 获取内容指针 */
  40. QWidget* getContentWidget() const { return m_widgetContent; }
  41. /* 获取底部容器指针 */
  42. QWidget* getBottomWidget() const { return m_widgetBottom; }
  43. /* 点击确定按钮之前执行的操作 */
  44. virtual bool isOKClicked();
  45. /* 设置一个控件报警,边框显示红色
  46. * 注意:这个功能需要在qss里设置属性[Warn=true],并实现相应的报警样式 */
  47. void setWarn(QWidget* widget, bool isWarn = true);
  48. protected:
  49. /* 初始化UI */
  50. virtual void initUI();
  51. /* 初始化其他设置 */
  52. virtual void initSettings();
  53. /* 加载QSS */
  54. virtual void setParentQSS();
  55. /* 设置top栏的位置布局 */
  56. void layoutTop();
  57. protected slots:
  58. /* 关闭按钮点击事件 */
  59. void do_pBtn_Close_Clicked() { this->close(); }
  60. /* 确认按钮点击事件 */
  61. void do_pBtn_OK_Clicked();
  62. /* 取消按钮点击事件 */
  63. void do_pBtnCancel_clicked() { this->reject(); }
  64. /* 设置QSS */
  65. void do_setQSS(EUIStyle style);
  66. protected:
  67. /* 重写鼠标按下事件 */
  68. void mousePressEvent(QMouseEvent *event) override;
  69. /* 重写鼠标移动事件 */
  70. void mouseMoveEvent(QMouseEvent *event) override;
  71. /* 重写鼠标释放事件 */
  72. void mouseReleaseEvent(QMouseEvent *event) override;
  73. /* 显示事件 */
  74. void showEvent(QShowEvent *event) override;
  75. /* 重新设置大小 */
  76. void resizeEvent(QResizeEvent *event) override;
  77. /* 事件过滤器 */
  78. bool eventFilter(QObject *watched, QEvent *event) override;
  79. protected:
  80. std::shared_ptr<spdlog::logger> m_logger = nullptr;
  81. bool m_isOK = false; /* 是否点击了确认按钮 */
  82. private:
  83. QPoint m_lastPos; /* 鼠标点击的位置 */
  84. QWidget* m_widgetBackground = nullptr; /* 背景容器,这个才是真正容纳所有内容的容器 */
  85. QWidget* m_widgetTop = nullptr; /* 顶部标题栏 */
  86. QWidget* m_widgetContent = nullptr; /* 内容区域 */
  87. QWidget* m_widgetBottom = nullptr; /* 底部按钮区域 */
  88. QVBoxLayout* m_layoutBackground = nullptr; /* 背景容器的布局 */
  89. QLabel* m_labelTitle = nullptr; /* 标题标签 */
  90. QPushButton* m_pBtn_Close = nullptr; /* 关闭按钮 */
  91. QPushButton* m_pBtn_OK = nullptr; /* 确认按钮 */
  92. QPushButton* m_pBtn_Cancel = nullptr; /* 取消按钮 */
  93. };
  94. #endif // __DIALOGBASE_H__