DatePicker.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "DatePicker.h"
  2. #include "Widgets/CycleListWidget.h"
  3. /**
  4. * @brief DatePicker::DatePicker
  5. * @param panel
  6. * @param parent
  7. * @param format: the format of date, could be `DatePicker.MM_DD_YYYY` or `DatePicker.YYYY_MM_DD`
  8. * @param monthTight: is the month column tight
  9. */
  10. DatePicker::DatePicker(QWidget *parent, DatePicker::Format format, bool monthTight)
  11. : DatePickerBase(parent), m_isMonthTight(monthTight)
  12. {
  13. MONTH = tr("month");
  14. YEAR = tr("year");
  15. DAY = tr("day");
  16. setDateFormat(format);
  17. }
  18. void DatePicker::setDateFormat(DatePicker::Format format)
  19. {
  20. this->clearColumns();
  21. int y = QDate::currentDate().year();
  22. m_dateFormat = format;
  23. QVariantList range;
  24. int w = 80;
  25. if (format == MM_DD_YYYY) {
  26. m_monthIndex = 0;
  27. m_dayIndex = 1;
  28. m_yearIndex = 2;
  29. range.clear();
  30. for (int i = 1; i < 13; ++i) {
  31. range << i;
  32. }
  33. addColumn(MONTH, range, w, Qt::AlignLeft, monthFormatter());
  34. range.clear();
  35. for (int i = 1; i < 32; ++i) {
  36. range << i;
  37. }
  38. addColumn(DAY, range, w, Qt::AlignLeft, dayFormatter());
  39. range.clear();
  40. for (int i = y - 100; i < y + 101; ++i) {
  41. range << i;
  42. }
  43. addColumn(YEAR, range, w, Qt::AlignLeft, yearFormatter());
  44. } else if (format == YYYY_MM_DD) {
  45. m_monthIndex = 1;
  46. m_dayIndex = 2;
  47. m_yearIndex = 0;
  48. range.clear();
  49. for (int i = y - 100; i < y + 101; ++i) {
  50. range << i;
  51. }
  52. addColumn(YEAR, range, w, Qt::AlignLeft, yearFormatter());
  53. range.clear();
  54. for (int i = 1; i < 13; ++i) {
  55. range << i;
  56. }
  57. addColumn(MONTH, range, w, Qt::AlignLeft, monthFormatter());
  58. range.clear();
  59. for (int i = 1; i < 32; ++i) {
  60. range << i;
  61. }
  62. addColumn(DAY, range, w, Qt::AlignLeft, dayFormatter());
  63. }
  64. setColumnWidth(m_monthIndex, monthColumnWidth());
  65. }
  66. QStringList DatePicker::panelInitialValue()
  67. {
  68. bool any = false;
  69. for (auto v : this->value()) {
  70. if (!v.isEmpty()) {
  71. any = true;
  72. }
  73. }
  74. if (any) {
  75. return this->value();
  76. }
  77. QDate date = QDate::currentDate();
  78. QString y = this->encodeValue(0, date.year()).toString();
  79. QString m = this->encodeValue(1, date.month()).toString();
  80. QString d = this->encodeValue(2, date.day()).toString();
  81. if (m_dateFormat == YYYY_MM_DD) {
  82. return QStringList{ y, m, d };
  83. } else {
  84. return QStringList{ m, d, y };
  85. }
  86. }
  87. void DatePicker::setMonthTight(bool monthTight)
  88. {
  89. if (m_isMonthTight == monthTight) {
  90. return;
  91. }
  92. m_isMonthTight = monthTight;
  93. setColumnWidth(m_monthIndex, monthColumnWidth());
  94. }
  95. void DatePicker::setDate(const QDate &date)
  96. {
  97. if (!date.isValid() || date.isNull()) {
  98. return;
  99. }
  100. this->date = date;
  101. setColumnValue(m_monthIndex, QString::number(date.month()));
  102. setColumnValue(m_dayIndex, QString::number(date.day()));
  103. setColumnValue(m_yearIndex, QString::number(date.year()));
  104. QVariantList range;
  105. for (int i = 1; i < date.daysInMonth() + 1; ++i) {
  106. range << i;
  107. }
  108. setColumnItems(m_dayIndex, range);
  109. }
  110. void DatePicker::onConfirmed(const QStringList &value)
  111. {
  112. int year = this->decodeValue(m_yearIndex, value[m_yearIndex]).toInt();
  113. int month = this->decodeValue(m_monthIndex, value[m_monthIndex]).toInt();
  114. int day = this->decodeValue(m_dayIndex, value[m_dayIndex]).toInt();
  115. QDate date(year, month, day);
  116. QDate od = this->date;
  117. setDate(date);
  118. if (od != date) {
  119. emit dateChanged(date);
  120. }
  121. }
  122. void DatePicker::onColumnValueChanged(PickerPanel *panel, int index, const QString & /*value*/)
  123. {
  124. if (index == m_dayIndex) {
  125. return;
  126. }
  127. // get days number in month
  128. QVariant m = this->decodeValue(m_monthIndex, panel->columnValue(m_monthIndex));
  129. int month = this->decodeValue(m_monthIndex, panel->columnValue(m_monthIndex)).toInt();
  130. int year = this->decodeValue(m_yearIndex, panel->columnValue(m_yearIndex)).toInt();
  131. QDate tmp(year, month, 1);
  132. int days = tmp.daysInMonth();
  133. // update days
  134. CycleListWidget *c = panel->column(m_dayIndex);
  135. QString day = c->currentItem()->text();
  136. QVariantList range;
  137. for (int i = 1; i < days + 1; ++i) {
  138. range << i;
  139. }
  140. setColumnItems(m_dayIndex, range);
  141. c->setItems(this->columns[m_dayIndex]->items());
  142. c->setSelectedItem(day);
  143. }
  144. int DatePicker::monthColumnWidth()
  145. {
  146. QFontMetrics fm = this->fontMetrics();
  147. int wm = -1;
  148. for (auto i : columns[m_monthIndex]->items()) {
  149. if (wm < fm.width(i.toString())) {
  150. wm = fm.width(i.toString());
  151. }
  152. }
  153. wm += 20;
  154. // don't use tight layout for english
  155. if (MONTH == "month") {
  156. return wm + 49;
  157. }
  158. if (m_isMonthTight) {
  159. return qMax(80, wm);
  160. } else {
  161. return wm + 49;
  162. }
  163. }