DatePicker.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef DATEPICKER_H
  2. #define DATEPICKER_H
  3. #include "PickerBase.h"
  4. #include <QDate>
  5. #if (QT_VERSION > QT_VERSION_CHECK(5, 14, 0))
  6. #include <QCalendar>
  7. #endif
  8. class MonthFormatter : public PickerColumnFormatter
  9. {
  10. Q_OBJECT
  11. public:
  12. MonthFormatter(QObject *parent = nullptr) : PickerColumnFormatter(parent)
  13. {
  14. m_months = QStringList{ tr("January"), tr("February"), tr("March"), tr("April"),
  15. tr("May"), tr("June"), tr("July"), tr("August"),
  16. tr("September"), tr("October"), tr("November"), tr("December") };
  17. }
  18. private:
  19. QStringList m_months;
  20. // PickerColumnFormatter interface
  21. public:
  22. QString encode(const QVariant &month) override { return m_months[month.toInt() - 1]; }
  23. QString decode(const QVariant &value) override { return QString::number(m_months.indexOf(value.toString()) + 1); }
  24. };
  25. class DatePickerBase : public PickerBase
  26. {
  27. Q_OBJECT
  28. public:
  29. explicit DatePickerBase(QWidget *parent = nullptr) : PickerBase(parent)
  30. {
  31. date = QDate();
  32. #if (QT_VERSION > QT_VERSION_CHECK(5, 14, 0))
  33. calendar = QCalendar();
  34. #endif
  35. m_yearFormatter = nullptr;
  36. m_monthFormatter = nullptr;
  37. m_dayFormatter = nullptr;
  38. }
  39. virtual void setDate(const QDate &date) = 0;
  40. void setYearFormatter(PickerColumnFormatter *formatter) { m_yearFormatter = formatter; }
  41. void setMonthFormatter(PickerColumnFormatter *formatter) { m_monthFormatter = formatter; }
  42. void setDayFormatter(PickerColumnFormatter *formatter) { m_dayFormatter = formatter; }
  43. PickerColumnFormatter *yearFormatter()
  44. {
  45. if (!m_yearFormatter) {
  46. return new DigitFormatter(this);
  47. }
  48. return m_yearFormatter;
  49. }
  50. PickerColumnFormatter *monthFormatter()
  51. {
  52. if (!m_monthFormatter) {
  53. return new MonthFormatter(this);
  54. }
  55. return m_monthFormatter;
  56. }
  57. PickerColumnFormatter *dayFormatter()
  58. {
  59. if (!m_dayFormatter) {
  60. return new DigitFormatter(this);
  61. }
  62. return m_dayFormatter;
  63. }
  64. QDate date;
  65. #if (QT_VERSION > QT_VERSION_CHECK(5, 14, 0))
  66. QCalendar calendar;
  67. #endif
  68. signals:
  69. void dateChanged(const QDate &);
  70. private:
  71. PickerColumnFormatter *m_yearFormatter;
  72. PickerColumnFormatter *m_monthFormatter;
  73. PickerColumnFormatter *m_dayFormatter;
  74. };
  75. class DatePicker : public DatePickerBase
  76. {
  77. Q_OBJECT
  78. public:
  79. enum Format
  80. {
  81. MM_DD_YYYY = 0,
  82. YYYY_MM_DD = 1
  83. };
  84. explicit DatePicker(QWidget *parent = nullptr, Format format = MM_DD_YYYY, bool monthTight = true);
  85. void setDateFormat(Format format);
  86. QStringList panelInitialValue();
  87. void setMonthTight(bool monthTight);
  88. void setDate(const QDate &date) override;
  89. QString YEAR;
  90. QString MONTH;
  91. QString DAY;
  92. public slots:
  93. void onConfirmed(const QStringList &value) override;
  94. void onColumnValueChanged(PickerPanel *panel, int index, const QString &value) override;
  95. private:
  96. int monthColumnWidth();
  97. private:
  98. bool m_isMonthTight;
  99. Format m_dateFormat;
  100. int m_monthIndex;
  101. int m_dayIndex;
  102. int m_yearIndex;
  103. };
  104. /**
  105. * @brief Chinese date formatter
  106. */
  107. class ZhFormatter : public PickerColumnFormatter
  108. {
  109. Q_OBJECT
  110. public:
  111. ZhFormatter(QObject *parent = nullptr) : PickerColumnFormatter(parent), suffix("") { }
  112. QString suffix;
  113. // PickerColumnFormatter interface
  114. public:
  115. QString encode(const QVariant &value) override { return value.toString() + suffix; }
  116. QString decode(const QVariant &value) override
  117. {
  118. QString date = value.toString();
  119. if (date.isEmpty()) {
  120. return "0";
  121. } else {
  122. return date.left(date.count() - suffix.count());
  123. }
  124. }
  125. };
  126. class ZhYearFormatter : public ZhFormatter
  127. {
  128. Q_OBJECT
  129. public:
  130. ZhYearFormatter(QObject *parent = nullptr) : ZhFormatter(parent) { suffix = "年"; }
  131. };
  132. class ZhMonthFormatter : public ZhFormatter
  133. {
  134. Q_OBJECT
  135. public:
  136. ZhMonthFormatter(QObject *parent = nullptr) : ZhFormatter(parent) { suffix = "月"; }
  137. };
  138. class ZhDayFormatter : public ZhFormatter
  139. {
  140. Q_OBJECT
  141. public:
  142. ZhDayFormatter(QObject *parent = nullptr) : ZhFormatter(parent) { suffix = "日"; }
  143. };
  144. class ZhDatePicker : public DatePicker
  145. {
  146. Q_OBJECT
  147. public:
  148. explicit ZhDatePicker(QWidget *parent = nullptr) : DatePicker(parent)
  149. {
  150. MONTH = "月";
  151. YEAR = "年";
  152. DAY = "日";
  153. setDayFormatter(new ZhDayFormatter(this));
  154. setYearFormatter(new ZhYearFormatter(this));
  155. setMonthFormatter(new ZhMonthFormatter(this));
  156. setDateFormat(YYYY_MM_DD);
  157. }
  158. };
  159. #endif // DATEPICKER_H