123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #include "TimePicker.h"
- TimePicker::TimePicker(QWidget *parent, bool showSeconds) : TimePickerBase(parent, showSeconds)
- {
- // add hour column
- int w = showSeconds ? 80 : 120;
- QVariantList range;
- for (int i = 0; i < 24; ++i) {
- range << i;
- }
- addColumn(tr("hour"), range, w, Qt::AlignLeft, new DigitFormatter(this));
- range.clear();
- for (int i = 0; i < 60; ++i) {
- range << i;
- }
- // add minute column;
- addColumn(tr("minute"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
- // add seconds column;
- addColumn(tr("second"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
- setColumnVisible(2, showSeconds);
- }
- void TimePicker::setTime(const QTime &time)
- {
- if (time.isNull() || !time.isValid()) {
- return;
- }
- this->time = time;
- setColumnValue(0, QString::number(time.hour()));
- setColumnValue(1, QString::number(time.minute()));
- setColumnValue(2, QString::number(time.second()));
- }
- void TimePicker::setSecondVisible(bool visible)
- {
- setColumnVisible(2, visible);
- int w = visible ? 80 : 120;
- for (auto button : columns) {
- button->setFixedWidth(w);
- }
- }
- QStringList TimePicker::panelInitialValue()
- {
- bool any = false;
- for (auto v : this->value()) {
- if (!v.isEmpty()) {
- any = true;
- }
- }
- if (any) {
- return this->value();
- }
- QTime time = QTime::currentTime();
- QString h = this->encodeValue(0, time.hour()).toString();
- QString m = this->encodeValue(1, time.minute()).toString();
- QString s = this->encodeValue(2, time.second()).toString();
- if (showSeconds) {
- return QStringList{ h, m, s };
- } else {
- return QStringList{ h, m };
- }
- }
- void TimePicker::onConfirmed(const QStringList &value)
- {
- TimePickerBase::onConfirmed(value);
- int h = this->decodeValue(0, value[0]).toInt();
- int m = this->decodeValue(1, value[1]).toInt();
- int s = 0;
- if (value.count() > 2) {
- s = this->decodeValue(2, value[2]).toInt();
- }
- QTime time(h, m, s);
- QTime ot = this->time;
- setTime(time);
- if (ot != time) {
- emit timeChanged(time);
- }
- }
- AMTimePicker::AMTimePicker(QWidget *parent, bool showSeconds) : TimePickerBase(parent, showSeconds)
- {
- AM = tr("AM");
- PM = tr("PM");
- QVariantList range;
- for (int i = 1; i < 13; ++i) {
- range << i;
- }
- int w = 80;
- addColumn(tr("hour"), range, w, Qt::AlignLeft, new AMHourFormatter(this));
- range.clear();
- for (int i = 0; i < 60; ++i) {
- range << i;
- }
- // add minute column;
- addColumn(tr("minute"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
- // add seconds column;
- addColumn(tr("second"), range, w, Qt::AlignLeft, new MiniuteFormatter(this));
- setColumnVisible(2, showSeconds);
- // add AM/PM column
- range.clear();
- range << AM << PM;
- addColumn(AM, range, w, Qt::AlignLeft, new AMPMFormatter(this));
- }
- void AMTimePicker::setTime(const QTime &time)
- {
- if (time.isNull() || !time.isValid()) {
- return;
- }
- this->time = time;
- setColumnValue(0, QString::number(time.hour()));
- setColumnValue(1, QString::number(time.minute()));
- setColumnValue(2, QString::number(time.second()));
- setColumnValue(3, QString::number(time.hour()));
- }
- void AMTimePicker::setSecondVisible(bool visible)
- {
- setColumnVisible(2, visible);
- }
- QStringList AMTimePicker::panelInitialValue()
- {
- bool any = false;
- for (auto v : this->value()) {
- if (!v.isEmpty()) {
- any = true;
- }
- }
- if (any) {
- return this->value();
- }
- QTime time = QTime::currentTime();
- QString h = this->encodeValue(0, time.hour()).toString();
- QString m = this->encodeValue(1, time.minute()).toString();
- QString s = this->encodeValue(2, time.second()).toString();
- QString p = this->encodeValue(3, time.hour()).toString();
- if (showSeconds) {
- return QStringList{ h, m, s, p };
- } else {
- return QStringList{ h, m, p };
- }
- }
- void AMTimePicker::onConfirmed(const QStringList &value)
- {
- TimePickerBase::onConfirmed(value);
- int h, m, s;
- QString p;
- h = this->decodeValue(0, value[0]).toInt();
- m = this->decodeValue(1, value[1]).toInt();
- if (value.count() == 3) {
- p = this->decodeValue(3, value[2]).toString();
- s = 0;
- } else {
- s = this->decodeValue(2, value[2]).toInt();
- p = this->decodeValue(3, value[3]).toString();
- }
- if (p == AM) {
- h = (h == 12) ? 0 : h;
- } else {
- h = (h == 12) ? h : h + 12;
- }
- QTime time(h, m, s);
- QTime ot = this->time;
- setTime(time);
- if (ot != time) {
- emit timeChanged(time);
- }
- }
|