TimePicker.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef TIMEPICKER_H
  2. #define TIMEPICKER_H
  3. #include "PickerBase.h"
  4. #include <QTime>
  5. class TimePickerBase : public PickerBase
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit TimePickerBase(QWidget *parent = nullptr, bool showSeconds = false) : PickerBase(parent)
  10. {
  11. this->showSeconds = showSeconds;
  12. }
  13. bool showSeconds;
  14. QTime time;
  15. virtual void setTime(const QTime &time) = 0;
  16. virtual void setSecondVisible(bool visible) = 0;
  17. signals:
  18. void timeChanged(const QTime &);
  19. };
  20. class MiniuteFormatter : public DigitFormatter
  21. {
  22. Q_OBJECT
  23. public:
  24. explicit MiniuteFormatter(QObject *parent = nullptr) : DigitFormatter(parent) { }
  25. QString encode(const QVariant &minute) override
  26. {
  27. QString min = minute.toString();
  28. min = min.rightJustified(2, '0');
  29. return min;
  30. }
  31. };
  32. class AMHourFormatter : public DigitFormatter
  33. {
  34. Q_OBJECT
  35. public:
  36. explicit AMHourFormatter(QObject *parent = nullptr) : DigitFormatter(parent) { }
  37. QString encode(const QVariant &hour) override
  38. {
  39. int h = hour.toInt();
  40. if (h == 0 || h == 12) {
  41. return QString::number(12);
  42. }
  43. return QString::number(h % 12);
  44. }
  45. };
  46. class AMPMFormatter : public PickerColumnFormatter
  47. {
  48. Q_OBJECT
  49. public:
  50. explicit AMPMFormatter(QObject *parent = nullptr) : PickerColumnFormatter(parent)
  51. {
  52. AM = tr("AM");
  53. PM = tr("PM");
  54. }
  55. QString encode(const QVariant &hour) override
  56. {
  57. bool ok;
  58. int h = hour.toInt(&ok);
  59. if (!ok) {
  60. return hour.toString();
  61. }
  62. if (h < 12) {
  63. return AM;
  64. }
  65. return PM;
  66. }
  67. QString AM;
  68. QString PM;
  69. };
  70. /**
  71. * @brief 24 hours time picker
  72. */
  73. class TimePicker : public TimePickerBase
  74. {
  75. Q_OBJECT
  76. public:
  77. explicit TimePicker(QWidget *parent = nullptr, bool showSeconds = false);
  78. void setTime(const QTime &time) override;
  79. void setSecondVisible(bool visible) override;
  80. QStringList panelInitialValue();
  81. private slots:
  82. void onConfirmed(const QStringList &value) override;
  83. };
  84. /**
  85. * @brief AM/PM time picker
  86. */
  87. class AMTimePicker : public TimePickerBase
  88. {
  89. Q_OBJECT
  90. public:
  91. explicit AMTimePicker(QWidget *parent = nullptr, bool showSeconds = false);
  92. void setTime(const QTime &time) override;
  93. void setSecondVisible(bool visible) override;
  94. QStringList panelInitialValue();
  95. QString AM;
  96. QString PM;
  97. private slots:
  98. void onConfirmed(const QStringList &value) override;
  99. };
  100. #endif // TIMEPICKER_H