timewidget.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef TIMEWIDGET_H
  2. #define TIMEWIDGET_H
  3. /**
  4. * 使用说明
  5. * 1、这个时间选择器支持两种方式
  6. * * 提升QTimeEdit,作为编辑栏修改时间
  7. * * 以弹窗的方式出现
  8. * 2、时间选择区域m_wdgTimeArea和时间编辑栏(在UI中)不是统一的,时间选择区域可以点击时间编辑栏
  9. * 创建出来,也可以在弹窗中直接显示出来,所以moveEvent事件就是用来移动m_wdgTimeArea的。
  10. * 3、在原来的基础上新添加了两个信号,在关闭的时候发送
  11. * 4、使用Dialog模式的时候,点击空白处隐藏就会close掉,然后发送新的时间信号
  12. * 5、使用Dialog模式,执行execShow()函数,会阻塞运行,直到关闭
  13. */
  14. #include <QFrame>
  15. #include <QTime>
  16. class TimePartWidget;
  17. class QListWidgetItem;
  18. class ShadowWidget;
  19. namespace Ui {
  20. class TimeWidget;
  21. }
  22. /* 添加ShowType类型,判断这个控件是编辑栏还是弹窗 */
  23. class TimeWidget : public QFrame
  24. {
  25. Q_OBJECT
  26. public:
  27. enum ShowType{
  28. EditLine = 0, /* 时间编辑栏 */
  29. Dialog = 1, /* 以弹窗的形式出现 */
  30. };
  31. explicit TimeWidget(QWidget *parent = nullptr , ShowType type = EditLine);
  32. explicit TimeWidget(ShowType type = EditLine);
  33. ~TimeWidget();
  34. void CreateTimeVector(const QVector<int>& types);
  35. void ClearVector(QVector<TimePartWidget*>& vec);
  36. // 在父窗口无法容纳控件时,这是必要的
  37. void SetMainWindow(QWidget* pWidget);
  38. QTime getTime();
  39. QString getTimeStr();
  40. QTime getFormTime() const;
  41. void setTime(const QString& t);
  42. void setTime(const QTime& t);
  43. void clearTime();
  44. QString tipText() const;
  45. /* 新增一个设置时间条宽度的函数 */
  46. void setTimeAreaWidth(int w);
  47. /***** 2024-05-25 添加两个信号 ******/
  48. void showTimeEditArea();
  49. /* 以弹窗的模式模态显示 */
  50. void execShow();
  51. /* 设置时间图标 */
  52. void setIcon(const QString& icon);
  53. /* 设置图标显示 */
  54. void setIconShow(bool isShow);
  55. /* 设置图标大小 */
  56. void setIconSize(int w, int h);
  57. /* 设置默认的样式 */
  58. void setDefaultStyle();
  59. /* 设置编辑栏大小 */
  60. void setEditLine(int w, int h);
  61. /* 设置禁止使用滚轮修改时间 */
  62. void setWheelDisabled(bool disabled);
  63. /* 禁止点击时间图标清空时间信息 */
  64. void setDisableClear(bool disabled) { m_isDisableClear = disabled; }
  65. /* 设置QSS */
  66. void setQSS();
  67. signals:
  68. void signal_nowTime(const QTime& time);
  69. void signal_formerTimer(const QTime& time);
  70. void signal_close();
  71. protected:
  72. bool eventFilter(QObject* obj, QEvent* e) override;
  73. void moveEvent(QMoveEvent *event) override;
  74. private slots:
  75. void onBtnTipClicked();
  76. void onListItemClicked(QListWidgetItem* item);
  77. void onDateTimeChanged(const QDateTime& dt);
  78. private:
  79. void UpdateProperty(QObject* obj, const char* name, bool flag);
  80. void UpdatePopupTime(const QDateTime& dt);
  81. void ShowTimeArea(bool bShow);
  82. void CreatePopupWidget();
  83. /* 初始化函数 */
  84. void Init();
  85. private:
  86. const int TIME_AREA_WIDTH = 56;
  87. const int TIME_AREA_HEIGHT = 32;
  88. const int SHADOW_MARGIN = 9; // 对应BlurRadius模糊半径16px
  89. Ui::TimeWidget *ui;
  90. QVector<TimePartWidget*> m_vecTimePart;
  91. QVector<int> m_vecTimeSections;
  92. bool m_bTimeFlag{false}; // 时间更新标志
  93. QScopedPointer<ShadowWidget> m_wdgTimeArea; // 时间选择窗口
  94. QWidget* m_pMainWindow; // 外层祖辈窗口,能容纳时间控件高度即可(默认父窗口)
  95. ShowType m_type; /* 显示类型 */
  96. int m_width = 0; /* TimeArea宽度 */
  97. bool m_isDisableWheel = false; // 禁用滚轮修改时间
  98. bool m_isDisableClear = false; // 禁用点击时间图标清空时间信息
  99. };
  100. #endif // TIMEWIDGET_H