timewidget.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* 设置QSS */
  62. void setQSS();
  63. signals:
  64. void signal_nowTime(const QTime& time);
  65. void signal_formerTimer(const QTime& time);
  66. void signal_close();
  67. protected:
  68. bool eventFilter(QObject* obj, QEvent* e) override;
  69. void moveEvent(QMoveEvent *event) override;
  70. private slots:
  71. void onBtnTipClicked();
  72. void onListItemClicked(QListWidgetItem* item);
  73. void onDateTimeChanged(const QDateTime& dt);
  74. private:
  75. void UpdateProperty(QObject* obj, const char* name, bool flag);
  76. void UpdatePopupTime(const QDateTime& dt);
  77. void ShowTimeArea(bool bShow);
  78. void CreatePopupWidget();
  79. /* 初始化函数 */
  80. void Init();
  81. private:
  82. const int TIME_AREA_WIDTH = 56;
  83. const int TIME_AREA_HEIGHT = 32;
  84. const int SHADOW_MARGIN = 9; // 对应BlurRadius模糊半径16px
  85. Ui::TimeWidget *ui;
  86. QVector<TimePartWidget*> m_vecTimePart;
  87. QVector<int> m_vecTimeSections;
  88. bool m_bTimeFlag{false}; // 时间更新标志
  89. QScopedPointer<ShadowWidget> m_wdgTimeArea; // 时间选择窗口
  90. QWidget* m_pMainWindow; // 外层祖辈窗口,能容纳时间控件高度即可(默认父窗口)
  91. ShowType m_type; /* 显示类型 */
  92. int m_width = 0; /* TimeArea宽度 */
  93. };
  94. #endif // TIMEWIDGET_H