TimePicker.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "TimePicker.h"
  2. TimePicker::TimePicker(QWidget *parent, bool showSeconds) : TimePickerBase(parent, showSeconds)
  3. {
  4. // add hour column
  5. int w = showSeconds ? 80 : 120;
  6. QVariantList range;
  7. for (int i = 0; i < 24; ++i) {
  8. range << i;
  9. }
  10. addColumn(tr("hour"), range, w, Qt::AlignLeft, new DigitFormatter(this));
  11. range.clear();
  12. for (int i = 0; i < 60; ++i) {
  13. range << i;
  14. }
  15. // add minute column;
  16. addColumn(tr("minute"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
  17. // add seconds column;
  18. addColumn(tr("second"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
  19. setColumnVisible(2, showSeconds);
  20. }
  21. void TimePicker::setTime(const QTime &time)
  22. {
  23. if (time.isNull() || !time.isValid()) {
  24. return;
  25. }
  26. this->time = time;
  27. setColumnValue(0, QString::number(time.hour()));
  28. setColumnValue(1, QString::number(time.minute()));
  29. setColumnValue(2, QString::number(time.second()));
  30. }
  31. void TimePicker::setSecondVisible(bool visible)
  32. {
  33. setColumnVisible(2, visible);
  34. int w = visible ? 80 : 120;
  35. for (auto button : columns) {
  36. button->setFixedWidth(w);
  37. }
  38. }
  39. QStringList TimePicker::panelInitialValue()
  40. {
  41. bool any = false;
  42. for (auto v : this->value()) {
  43. if (!v.isEmpty()) {
  44. any = true;
  45. }
  46. }
  47. if (any) {
  48. return this->value();
  49. }
  50. QTime time = QTime::currentTime();
  51. QString h = this->encodeValue(0, time.hour()).toString();
  52. QString m = this->encodeValue(1, time.minute()).toString();
  53. QString s = this->encodeValue(2, time.second()).toString();
  54. if (showSeconds) {
  55. return QStringList{ h, m, s };
  56. } else {
  57. return QStringList{ h, m };
  58. }
  59. }
  60. void TimePicker::onConfirmed(const QStringList &value)
  61. {
  62. TimePickerBase::onConfirmed(value);
  63. int h = this->decodeValue(0, value[0]).toInt();
  64. int m = this->decodeValue(1, value[1]).toInt();
  65. int s = 0;
  66. if (value.count() > 2) {
  67. s = this->decodeValue(2, value[2]).toInt();
  68. }
  69. QTime time(h, m, s);
  70. QTime ot = this->time;
  71. setTime(time);
  72. if (ot != time) {
  73. emit timeChanged(time);
  74. }
  75. }
  76. AMTimePicker::AMTimePicker(QWidget *parent, bool showSeconds) : TimePickerBase(parent, showSeconds)
  77. {
  78. AM = tr("AM");
  79. PM = tr("PM");
  80. QVariantList range;
  81. for (int i = 1; i < 13; ++i) {
  82. range << i;
  83. }
  84. int w = 80;
  85. addColumn(tr("hour"), range, w, Qt::AlignLeft, new AMHourFormatter(this));
  86. range.clear();
  87. for (int i = 0; i < 60; ++i) {
  88. range << i;
  89. }
  90. // add minute column;
  91. addColumn(tr("minute"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
  92. // add seconds column;
  93. addColumn(tr("second"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
  94. setColumnVisible(2, showSeconds);
  95. // add AM/PM column
  96. range.clear();
  97. range << AM << PM;
  98. addColumn(AM, range, w, Qt::AlignLeft, new AMPMFormatter(this));
  99. }
  100. void AMTimePicker::setTime(const QTime &time)
  101. {
  102. if (time.isNull() || !time.isValid()) {
  103. return;
  104. }
  105. this->time = time;
  106. setColumnValue(0, QString::number(time.hour()));
  107. setColumnValue(1, QString::number(time.minute()));
  108. setColumnValue(2, QString::number(time.second()));
  109. setColumnValue(3, QString::number(time.hour()));
  110. }
  111. void AMTimePicker::setSecondVisible(bool visible)
  112. {
  113. setColumnVisible(2, visible);
  114. }
  115. QStringList AMTimePicker::panelInitialValue()
  116. {
  117. bool any = false;
  118. for (auto v : this->value()) {
  119. if (!v.isEmpty()) {
  120. any = true;
  121. }
  122. }
  123. if (any) {
  124. return this->value();
  125. }
  126. QTime time = QTime::currentTime();
  127. QString h = this->encodeValue(0, time.hour()).toString();
  128. QString m = this->encodeValue(1, time.minute()).toString();
  129. QString s = this->encodeValue(2, time.second()).toString();
  130. QString p = this->encodeValue(3, time.hour()).toString();
  131. if (showSeconds) {
  132. return QStringList{ h, m, s, p };
  133. } else {
  134. return QStringList{ h, m, p };
  135. }
  136. }
  137. void AMTimePicker::onConfirmed(const QStringList &value)
  138. {
  139. TimePickerBase::onConfirmed(value);
  140. int h, m, s;
  141. QString p;
  142. h = this->decodeValue(0, value[0]).toInt();
  143. m = this->decodeValue(1, value[1]).toInt();
  144. if (value.count() == 3) {
  145. p = this->decodeValue(3, value[2]).toString();
  146. s = 0;
  147. } else {
  148. s = this->decodeValue(2, value[2]).toInt();
  149. p = this->decodeValue(3, value[3]).toString();
  150. }
  151. if (p == AM) {
  152. h = (h == 12) ? 0 : h;
  153. } else {
  154. h = (h == 12) ? h : h + 12;
  155. }
  156. QTime time(h, m, s);
  157. QTime ot = this->time;
  158. setTime(time);
  159. if (ot != time) {
  160. emit timeChanged(time);
  161. }
  162. }