2022-08-25 18:37:24 +08:00
|
|
|
#include "etimer.h"
|
|
|
|
#include "tools.h"
|
2024-08-07 18:18:37 +08:00
|
|
|
#include "main.h"
|
2022-08-25 18:37:24 +08:00
|
|
|
#include "base/lodateselector.h"
|
|
|
|
#include "base/locolorselector.h"
|
2023-04-27 15:06:24 +08:00
|
|
|
#include "gutil/qgui.h"
|
2022-08-25 18:37:24 +08:00
|
|
|
#include <QRadioButton>
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QButtonGroup>
|
|
|
|
#include <QTextEdit>
|
|
|
|
#include <QDateTimeEdit>
|
|
|
|
#include <QFontComboBox>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QTextOption>
|
|
|
|
#include <QPainter>
|
2023-04-18 14:14:46 +08:00
|
|
|
#include <QToolButton>
|
2022-08-25 18:37:24 +08:00
|
|
|
|
|
|
|
ETimer::ETimer(EBase *multiWin) : EBase(multiWin) {
|
|
|
|
mType = EBase::Timer;
|
2024-08-07 18:18:37 +08:00
|
|
|
isDown = true;
|
|
|
|
targetTime = QDateTime::currentDateTime();
|
|
|
|
hasDay = true;
|
|
|
|
hasHour = true;
|
|
|
|
hasMin = true;
|
|
|
|
hasSec = true;
|
|
|
|
isMultiline = true;
|
|
|
|
font = qfont("Arial", 12);
|
|
|
|
textColor = Qt::red;
|
|
|
|
backColor = Qt::transparent;
|
2022-08-25 18:37:24 +08:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:58:29 +08:00
|
|
|
ETimer::ETimer(const JObj &json, EBase *multiWin) : EBase(multiWin){
|
2022-08-25 18:37:24 +08:00
|
|
|
mType = EBase::Timer;
|
|
|
|
setBaseAttr(json);
|
2024-08-07 18:18:37 +08:00
|
|
|
isDown = json["isDown"].toBool();
|
|
|
|
targetTime = QDateTime::fromString(json["targetTime"].toString(), "yyyy-MM-dd HH:mm:ss");
|
|
|
|
hasDay = json["hasDay"].toBool();
|
|
|
|
hasHour = json["hasHour"].toBool();
|
|
|
|
hasMin = json["hasMin"].toBool();
|
|
|
|
hasSec = json["hasSec"].toBool();
|
|
|
|
text = json["text"].toString();
|
|
|
|
isMultiline = json["isMultiline"].toBool();
|
|
|
|
font = qfont(json["font"].toString(), json["fontSize"].toInt(), json["fontBold"].toBool(), json["fontItalic"].toBool());
|
|
|
|
font.setUnderline(json["fontUnderline"].toBool());
|
|
|
|
textColor = json["textColor"].toString();
|
2022-08-25 18:37:24 +08:00
|
|
|
auto color = json["backColor"].toString();
|
2024-08-07 18:18:37 +08:00
|
|
|
backColor = color.isEmpty() ? QColor(0,0,0,0) : color;
|
2022-08-25 18:37:24 +08:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETimer::init() {
|
2024-08-07 18:18:37 +08:00
|
|
|
connect(gTick, &Tick::secChanged, this, [this](const QDateTime &cur) {
|
|
|
|
auto sss = isDown ? cur.secsTo(targetTime) : targetTime.secsTo(cur);
|
|
|
|
if(sss < 0) sss = 0;
|
|
|
|
if(secs==sss) return;
|
|
|
|
secs = sss;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETimer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
|
|
|
|
QString text;
|
2024-08-07 18:18:37 +08:00
|
|
|
if(!this->text.isEmpty()) {
|
|
|
|
text += this->text;
|
|
|
|
if(this->isMultiline) text += '\n';
|
2022-08-25 18:37:24 +08:00
|
|
|
else text += " ";
|
|
|
|
}
|
|
|
|
int secs = this->secs;
|
2024-08-07 18:18:37 +08:00
|
|
|
if(this->hasDay) {
|
2022-08-25 18:37:24 +08:00
|
|
|
text.append(QString::number(secs/86400)).append(" ").append(tr("day")).append(" ");
|
|
|
|
secs %= 86400;
|
|
|
|
}
|
2024-08-07 18:18:37 +08:00
|
|
|
if(this->hasHour) {
|
2022-08-25 18:37:24 +08:00
|
|
|
text.append(QString::asprintf("%02d ", secs/3600)).append(tr("hour")).append(" ");
|
|
|
|
secs %= 3600;
|
|
|
|
}
|
2024-08-07 18:18:37 +08:00
|
|
|
if(this->hasMin) {
|
2022-08-25 18:37:24 +08:00
|
|
|
text.append(QString::asprintf("%02d ", secs/60)).append(tr("min")).append(" ");
|
|
|
|
secs %= 60;
|
|
|
|
}
|
2024-08-07 18:18:37 +08:00
|
|
|
if(this->hasSec) text.append(QString::asprintf("%02d ", secs)).append(tr("sec")).append(" ");
|
2022-08-25 18:37:24 +08:00
|
|
|
text = text.trimmed();
|
2024-08-07 18:18:37 +08:00
|
|
|
this->font.setStyleStrategy(gTextAntialiasing ? QFont::PreferAntialias : QFont::NoAntialias);
|
|
|
|
if(this->backColor.alpha() != 0) painter->fillRect(0, 0, mWidth, mHeight, this->backColor);
|
|
|
|
painter->setFont(this->font);
|
|
|
|
painter->setPen(this->textColor);
|
2022-08-25 18:37:24 +08:00
|
|
|
painter->drawText(innerRect(), text, QTextOption(Qt::AlignCenter));
|
|
|
|
EBase::paint(painter, option, widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* ETimer::attrWgt() {
|
|
|
|
auto wgtAttr = new QWidget();
|
|
|
|
auto vBox = new QVBoxLayout(wgtAttr);
|
|
|
|
vBox->setContentsMargins(6, 0, 6, 0);
|
|
|
|
if(mMultiWin!=nullptr) vBox->setSpacing(3);
|
|
|
|
|
|
|
|
addBaseAttrWgt(vBox);
|
|
|
|
|
|
|
|
auto hBox = new QHBoxLayout();
|
|
|
|
hBox->addWidget(new QLabel(tr("Basic Properties")));
|
|
|
|
|
|
|
|
auto line = new QFrame();
|
|
|
|
line->setFrameShape(QFrame::HLine);
|
|
|
|
line->setFrameShadow(QFrame::Sunken);
|
|
|
|
hBox->addWidget(line, 1);
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addStretch();
|
|
|
|
auto fdCntDown = new QRadioButton(tr("Count Down"));
|
|
|
|
auto fdCntUp = new QRadioButton(tr("Count Up"));
|
2024-08-07 18:18:37 +08:00
|
|
|
if(this->isDown) fdCntDown->setChecked(true);
|
2022-08-25 18:37:24 +08:00
|
|
|
else fdCntUp->setChecked(true);
|
|
|
|
auto cntGroup = new QButtonGroup(wgtAttr);
|
|
|
|
cntGroup->addButton(fdCntDown);
|
|
|
|
cntGroup->addButton(fdCntUp);
|
|
|
|
connect(fdCntDown, &QRadioButton::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->isDown = checked;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdCntDown);
|
|
|
|
hBox->addStretch();
|
|
|
|
hBox->addWidget(fdCntUp);
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
hBox->addWidget(new QLabel(tr("Time")));
|
|
|
|
|
2024-08-07 18:18:37 +08:00
|
|
|
auto fdTime = new QDateTimeEdit(this->targetTime);
|
2022-08-25 18:37:24 +08:00
|
|
|
fdTime->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
connect(fdTime, &QTimeEdit::dateTimeChanged, this, [this](const QDateTime &dateTime) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->targetTime = dateTime;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdTime);
|
|
|
|
|
|
|
|
auto wDateSelector = new LoDateSelector();
|
|
|
|
connect(wDateSelector, &LoDateSelector::sDateSelected, fdTime, &QDateTimeEdit::setDate);
|
|
|
|
hBox->addWidget(wDateSelector);
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
|
|
|
|
auto fdHasDay = new QCheckBox(tr("Day"));
|
2024-08-07 18:18:37 +08:00
|
|
|
fdHasDay->setChecked(this->hasDay);
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdHasDay, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->hasDay = checked;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdHasDay);
|
|
|
|
|
|
|
|
auto fdHasHour = new QCheckBox(tr("Hour"));
|
2024-08-07 18:18:37 +08:00
|
|
|
fdHasHour->setChecked(this->hasHour);
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdHasHour, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->hasHour = checked;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdHasHour);
|
|
|
|
|
|
|
|
auto fdHasMin = new QCheckBox(tr("Min"));
|
2024-08-07 18:18:37 +08:00
|
|
|
fdHasMin->setChecked(this->hasMin);
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdHasMin, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->hasMin = checked;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdHasMin);
|
|
|
|
|
|
|
|
auto fdHasSec = new QCheckBox(tr("Sec"));
|
2024-08-07 18:18:37 +08:00
|
|
|
fdHasSec->setChecked(this->hasSec);
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdHasSec, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->hasSec = checked;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdHasSec);
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
auto fdIsMultiline = new QCheckBox(tr("Multiline"));
|
2024-08-07 18:18:37 +08:00
|
|
|
fdIsMultiline->setChecked(this->isMultiline);
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdIsMultiline, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->isMultiline = checked;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdIsMultiline);
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
hBox->addWidget(new QLabel(tr("Text")));
|
|
|
|
|
2024-08-07 18:18:37 +08:00
|
|
|
auto fdText = new QTextEdit(this->text);
|
2022-08-25 18:37:24 +08:00
|
|
|
fdText->setMaximumHeight(50);
|
|
|
|
connect(fdText, &QTextEdit::textChanged, this, [this, fdText]() {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->text = fdText->toPlainText();
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdText);
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
|
|
|
|
auto fdFont = new QFontComboBox();
|
2024-08-07 18:18:37 +08:00
|
|
|
fdFont->setCurrentText(this->font.family());
|
2022-08-25 18:37:24 +08:00
|
|
|
fdFont->setEditable(false);
|
|
|
|
connect(fdFont, &QFontComboBox::currentFontChanged, this, [this](const QFont &f) {
|
2023-04-23 10:04:49 +08:00
|
|
|
QFont font(f.family());
|
2024-08-07 18:18:37 +08:00
|
|
|
font.setPixelSize(this->font.pixelSize());
|
|
|
|
font.setBold(this->font.bold());
|
|
|
|
font.setItalic(this->font.italic());
|
|
|
|
font.setUnderline(this->font.underline());
|
|
|
|
this->font = font;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdFont);
|
|
|
|
|
|
|
|
auto fdFontSize = new QSpinBox();
|
|
|
|
fdFontSize->setRange(4, 9999);
|
2024-08-07 18:18:37 +08:00
|
|
|
fdFontSize->setValue(this->font.pixelSize());
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdFontSize, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this](int value) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->font.setPixelSize(value);
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdFontSize);
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
|
2023-04-18 14:14:46 +08:00
|
|
|
auto fdFontBold = new QPushButton("B");
|
|
|
|
fdFontBold->setStyleSheet("QPushButton{background: #bbb; color: #888; font-size: 20px; font-weight: bold;} QPushButton:checked{background: #29c; color: #fff;}");
|
2022-08-25 18:37:24 +08:00
|
|
|
fdFontBold->setFixedSize(30, 30);
|
|
|
|
fdFontBold->setCheckable(true);
|
2024-08-07 18:18:37 +08:00
|
|
|
fdFontBold->setChecked(this->font.bold());
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdFontBold, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->font.setBold(checked);
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdFontBold);
|
|
|
|
|
2023-04-18 14:14:46 +08:00
|
|
|
auto fdFontItalic = new QPushButton("I");
|
|
|
|
fdFontItalic->setStyleSheet("QPushButton{background: #bbb; color: #888; font-size: 20px; font-style: italic;} QPushButton:checked{background: #29c; color: #fff;}");
|
2022-08-25 18:37:24 +08:00
|
|
|
fdFontItalic->setFixedSize(30, 30);
|
|
|
|
fdFontItalic->setCheckable(true);
|
2024-08-07 18:18:37 +08:00
|
|
|
fdFontItalic->setChecked(this->font.italic());
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdFontItalic, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->font.setItalic(checked);
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdFontItalic);
|
|
|
|
|
2023-04-18 14:14:46 +08:00
|
|
|
auto fdFontUnderline = new QPushButton("U");
|
|
|
|
fdFontUnderline->setStyleSheet("QPushButton{background: #bbb; color: #888; font-size: 20px; text-decoration: underline;} QPushButton:checked{background: #29c; color: #fff;}");
|
2022-08-25 18:37:24 +08:00
|
|
|
fdFontUnderline->setFixedSize(30, 30);
|
|
|
|
fdFontUnderline->setCheckable(true);
|
2024-08-07 18:18:37 +08:00
|
|
|
fdFontUnderline->setChecked(this->font.underline());
|
2022-08-25 18:37:24 +08:00
|
|
|
connect(fdFontUnderline, &QCheckBox::toggled, this, [this](bool checked) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->font.setUnderline(checked);
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdFontUnderline);
|
|
|
|
hBox->addStretch();
|
|
|
|
|
2024-08-07 18:18:37 +08:00
|
|
|
auto fdTextColor = new LoColorSelector("T", this->textColor);
|
2022-08-25 18:37:24 +08:00
|
|
|
fdTextColor->setFixedSize(30, 30);
|
|
|
|
fdTextColor->setFlat(true);
|
|
|
|
connect(fdTextColor, &LoColorSelector::sColorChanged, this, [this](const QColor &color) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->textColor = color;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdTextColor);
|
|
|
|
|
2024-08-07 18:18:37 +08:00
|
|
|
auto fdBackColor = new LoColorSelector("B", this->backColor);
|
2022-08-25 18:37:24 +08:00
|
|
|
fdBackColor->setFixedSize(30, 30);
|
|
|
|
fdBackColor->setFlat(true);
|
|
|
|
connect(fdBackColor, &LoColorSelector::sColorChanged, this, [this](const QColor &color) {
|
2024-08-07 18:18:37 +08:00
|
|
|
this->backColor = color;
|
2022-08-25 18:37:24 +08:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdBackColor);
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
vBox->addStretch();
|
|
|
|
return wgtAttr;
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:58:29 +08:00
|
|
|
JObj ETimer::attrJson() const {
|
|
|
|
JObj obj;
|
2022-08-25 18:37:24 +08:00
|
|
|
addBaseAttr(obj);
|
|
|
|
obj["elementType"] = "Timer";
|
2024-08-07 18:18:37 +08:00
|
|
|
obj["isDown"] = this->isDown;
|
|
|
|
obj["targetTime"] = this->targetTime.toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
obj["hasDay"] = this->hasDay;
|
|
|
|
obj["hasHour"] = this->hasHour;
|
|
|
|
obj["hasMin"] = this->hasMin;
|
|
|
|
obj["hasSec"] = this->hasSec;
|
|
|
|
obj["text"] = this->text;
|
|
|
|
obj["isMultiline"] = this->isMultiline;
|
|
|
|
obj["font"] = this->font.family();
|
|
|
|
obj["fontSize"] = this->font.pixelSize();
|
|
|
|
obj["fontBold"] = this->font.bold();
|
|
|
|
obj["fontItalic"] = this->font.italic();
|
|
|
|
obj["fontUnderline"] = this->font.underline();
|
|
|
|
obj["textColor"] = this->textColor.name();
|
|
|
|
obj["backColor"] = this->backColor.alpha()==0 ? "" : this->backColor.name();
|
2022-08-25 18:37:24 +08:00
|
|
|
return obj;
|
|
|
|
}
|