qt/LedOK/wProgramManager/wEditProgram/wElement/edclock.cpp
2022-01-20 10:08:17 +08:00

425 lines
17 KiB
C++

#include "edclock.h"
#include "loapptools.h"
#include "edclockattr.h"
#include "QThread"
#include <QSettings>
eDClock::eDClock(QRectF rectInit,InteractiveType type, QGraphicsItem *parent) :
eObject(rectInit, type, parent)
{
m_iType=eObject::DClock;
m_attr.timeZoneId = QTimeZone::systemTimeZoneId();
m_attr.year = true;
m_attr.month = true;
m_attr.day = true;
m_attr.hour = true;
m_attr.min = true;
m_attr.sec = true;
m_attr.weekly = true;
m_attr.fullYear = true;
m_attr.hour12 = true;
m_attr.AmPm = true;
m_attr.dateStyle = 0;
m_attr.timeStyle = 0;
m_attr.multiline = true;
m_attr.fontFamily = "Arial";
m_attr.fontSize = 9;
m_attr.fontBold = false;
m_attr.fontItalics = false;
m_attr.fontUnderline = false;
m_attr.textColor = Qt::red;
m_attr.playDuration = MACRO_DEFAULT_PLAYDURATION;
init();
}
eDClock::eDClock(const QJsonObject &json, InteractiveType type, QGraphicsItem *parent) :
eObject(json["geometry"].toObject(), type, parent)
{
m_iType=eObject::DClock;
setElement(json, m_attr);
init();
}
void eDClock::init()
{
connect(LoAppTools::getInstance(), SIGNAL(sTick()), this, SLOT(updateGeometry()));
}
void eDClock::setElement(const QJsonObject &json, Data &attr)
{
auto apptool = LoAppTools::getInstance();
attr.timeZoneId = QByteArray().append(json["widget"]["timeZone"].toString());
attr.year = json["widget"]["year"].toBool();
attr.month = json["widget"]["month"].toBool();
attr.day = json["widget"]["day"].toBool();
attr.hour = json["widget"]["hour"].toBool();
attr.min = json["widget"]["min"].toBool();
attr.sec = json["widget"]["sec"].toBool();
attr.weekly = json["widget"]["weekly"].toBool();
attr.fullYear = json["widget"]["fullYear"].toBool();
attr.hour12 = json["widget"]["12Hour"].toBool();
attr.AmPm = json["widget"]["AmPm"].toBool();
attr.dateStyle = json["widget"]["dataStyle"].toInt();
attr.timeStyle = json["widget"]["timeStyle"].toInt();
attr.multiline = json["widget"]["multiline"].toBool();
attr.fontFamily = json["widget"]["font"]["family"].toString();
attr.fontSize = json["widget"]["font"]["size"].toInt();
attr.fontBold = json["widget"]["font"]["bold"].toBool();
attr.fontItalics = json["widget"]["font"]["italics"].toBool();
attr.fontUnderline = json["widget"]["font"]["underline"].toBool();
attr.textColor = apptool->int2Color(json["widget"]["font"]["color"].toInt());
attr.playDuration = json["play"]["duration"].toInt();
}
void eDClock::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QString sDate;
QString sWeekly;
QString sTime;
QDateTime dt = QDateTime::currentDateTime().toTimeZone(QTimeZone(m_attr.timeZoneId));
QDate date = dt.date();
QTime time = dt.time();
QString spacer = m_attr.multiline ? "\n" : "\x20\x20\x20\x20";
auto tool = LoAppTools::getInstance();
QString yearStyle = tool->selectStr(m_attr.fullYear, "yyyy", "yy");
switch (m_attr.dateStyle) {
case 0:
sDate = tool->selectStr(m_attr.year, date.toString(yearStyle) + tr("Y_")) +
tool->selectStr(m_attr.month, date.toString("MM") + tr("M_")) +
tool->selectStr(m_attr.day, date.toString("dd") + tr("D_"));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 1:
sDate = tool->selectStr(m_attr.year, date.toString(yearStyle) + tr("Y_")) +
tool->selectStr(m_attr.month, date.toString("M") + tr("M_")) +
tool->selectStr(m_attr.day, date.toString("d") + tr("D_"));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 2:
sDate = tool->selectStr(m_attr.month, date.toString("M") + tr("/")) +
tool->selectStr(m_attr.day, date.toString("d")) +
tool->selectStr(m_attr.year, tr("/") + date.toString(yearStyle));
if(!m_attr.day) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 3:
sDate = tool->selectStr(m_attr.month, date.toString("MM") + tr("/")) +
tool->selectStr(m_attr.day, date.toString("dd")) +
tool->selectStr(m_attr.year, tr("/") + date.toString(yearStyle));
if(!m_attr.day) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 4:
sDate = tool->selectStr(m_attr.day, date.toString("d") + tr("/")) +
tool->selectStr(m_attr.month, date.toString("M")) +
tool->selectStr(m_attr.year, tr("/") + date.toString(yearStyle));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 5:
sDate = tool->selectStr(m_attr.day, date.toString("dd") + tr("/")) +
tool->selectStr(m_attr.month, date.toString("MM")) +
tool->selectStr(m_attr.year, tr("/") + date.toString(yearStyle));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 6:
sDate = tool->selectStr(m_attr.year, date.toString(yearStyle) + tr("/")) +
tool->selectStr(m_attr.month, date.toString("M")) +
tool->selectStr(m_attr.day, tr("/") + date.toString("d"));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 7:
sDate = tool->selectStr(m_attr.year, date.toString(yearStyle) + tr("/")) +
tool->selectStr(m_attr.month, date.toString("MM")) +
tool->selectStr(m_attr.day, tr("/") + date.toString("dd"));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 8:
sDate = tool->selectStr(m_attr.month, date.toString("M") + tr("-")) +
tool->selectStr(m_attr.day, date.toString("d")) +
tool->selectStr(m_attr.year, tr("-") + date.toString(yearStyle));
if(!m_attr.day) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 9:
sDate = tool->selectStr(m_attr.month, date.toString("MM") + tr("-")) +
tool->selectStr(m_attr.day, date.toString("dd")) +
tool->selectStr(m_attr.year, tr("-") + date.toString(yearStyle));
if(!m_attr.day) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 10:
sDate = tool->selectStr(m_attr.day, date.toString("d") + tr("-")) +
tool->selectStr(m_attr.month, date.toString("M")) +
tool->selectStr(m_attr.year, tr("-") + date.toString(yearStyle));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 11:
sDate = tool->selectStr(m_attr.day, date.toString("dd") + tr("-")) +
tool->selectStr(m_attr.month, date.toString("MM")) +
tool->selectStr(m_attr.year, tr("-") + date.toString(yearStyle));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 12:
sDate = tool->selectStr(m_attr.year, date.toString(yearStyle) + tr("-")) +
tool->selectStr(m_attr.month, date.toString("M")) +
tool->selectStr(m_attr.day, tr("-") + date.toString("d"));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
case 13:
sDate = tool->selectStr(m_attr.year, date.toString(yearStyle) + tr("-")) +
tool->selectStr(m_attr.month, date.toString("MM")) +
tool->selectStr(m_attr.day, tr("-") + date.toString("dd"));
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
break;
default:
break;
}
if(!m_attr.month) { int i = sDate.indexOf('/'); if(i < 0) i = sDate.indexOf('-'); if(i>=0) sDate.remove(i, 1); }
if(!sDate.isEmpty()) sDate += spacer;
sWeekly = tool->selectStr(m_attr.weekly, QDate::currentDate().toString("dddd") + spacer);
QString timeHead = tool->selectStr(m_attr.hour12, "A ");
switch (m_attr.timeStyle) {
case 0: sTime = time.toString(timeHead +
(m_attr.hour ? "hh:" : "") +
(m_attr.min ? "mm" : "") +
(m_attr.sec ? ":ss" : "")); break;
case 1: sTime = time.toString(timeHead +
(m_attr.hour ? "h:" : "") +
(m_attr.min ? "mm" : "") +
(m_attr.sec ? ":ss" : "")); break;
default: break;
}
if(m_attr.hour12 && !m_attr.AmPm) sTime = sTime.split(' ').at(1);
if(!m_attr.min) sTime.remove(sTime.indexOf(':'), 1);
QString res = sDate + sWeekly + sTime;
QFont font;
QTextOption opt;
font.setFamily(m_attr.fontFamily);
font.setPointSize(m_attr.fontSize);
font.setBold(m_attr.fontBold);
font.setItalic(m_attr.fontItalics);
font.setUnderline(m_attr.fontUnderline);
MACRO_FONT_FANG_JUCHI_2(font)
opt.setAlignment(Qt::AlignCenter);
painter->save();
MACRO_FAN_JUCHI(painter)
//painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(m_attr.textColor);
painter->setFont(font);
painter->drawText(rect(), res, opt);
painter->restore();
eObject::paint(painter, option, widget);
}
QWidget* eDClock::wAttr()
{
QWidget *wObj = eObject::wAttr();
QWidget *w = wAttrElement();
static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
return w;
}
QWidget* eDClock::wAttrElement()
{
eDClockAttr *w = new eDClockAttr(m_attr);
connect(w, SIGNAL(sAttrChanged(const eDClock::Data &)), this, SLOT(onAttrChanged(const eDClock::Data &)));
return w;
}
QJsonObject eDClock::elementJson() const
{
QJsonObject oRoot;
QJsonObject oWidget;
QJsonObject oFont;
QJsonObject oPlay;
auto apptool = LoAppTools::getInstance();
oRoot["elementType"] = "DClock";
oRoot["elementTypeId"] = type();
oRoot["geometry"] = eObject::elementJson();
oWidget["timeZone"] = QString(m_attr.timeZoneId);
oWidget["year"] = m_attr.year;
oWidget["month"] = m_attr.month;
oWidget["day"] = m_attr.day;
oWidget["hour"] = m_attr.hour;
oWidget["min"] = m_attr.min;
oWidget["sec"] = m_attr.sec;
oWidget["weekly"] = m_attr.weekly;
oWidget["fullYear"] = m_attr.fullYear;
oWidget["12Hour"] = m_attr.hour12;
oWidget["AmPm"] = m_attr.AmPm;
oWidget["dataStyle"] = m_attr.dateStyle;
oWidget["timeStyle"] = m_attr.timeStyle;
oWidget["multiline"] = m_attr.multiline;
oFont["family"] = m_attr.fontFamily;
oFont["size"] = m_attr.fontSize;
oFont["bold"] = m_attr.fontBold;
oFont["italics"] = m_attr.fontItalics;
oFont["underline"] = m_attr.fontUnderline;
oFont["color"] = apptool->color2Int(m_attr.textColor);
oWidget["font"] = oFont;
oWidget["html"] = m_attr.strHtml;
QRectF r1 = geometry();
QRect r=r1.toRect();
QString strDir=QString("%1%2%3%4%5").arg(zValue()).arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
oWidget.insert("idDir",strDir);
oRoot["widget"] = oWidget;
oPlay["duration"] = m_attr.playDuration;
oRoot["play"] = oPlay;
return oRoot;
}
void eDClock::onAttrChanged(const eDClock::Data &data)
{
m_attr = data;
updateGeometry();
}
void eDClock::CreateAllWordPic(QString strPath)
{
QStringList strWordList;
strWordList.append(tr("0"));
strWordList.append(tr("1"));
strWordList.append(tr("2"));
strWordList.append(tr("3"));
strWordList.append(tr("4"));
strWordList.append(tr("5"));
strWordList.append(tr("6"));
strWordList.append(tr("7"));
strWordList.append(tr("8"));
strWordList.append(tr("9"));
strWordList.append(tr("MON"));
strWordList.append(tr("TUE"));
strWordList.append(tr("WED"));
strWordList.append(tr("THU"));
strWordList.append(tr("FRI"));
strWordList.append(tr("SAT"));
strWordList.append(tr("SUN"));
strWordList.append(tr("AM"));
strWordList.append(tr("PM"));
strWordList.append(tr("YEAR"));
strWordList.append(tr("MONTH"));
strWordList.append(tr("DAY"));
strWordList.append(":");
strWordList.append("/");
strWordList.append("-");
QStringList strFileList;
strFileList.append("0");
strFileList.append("1");
strFileList.append("2");
strFileList.append("3");
strFileList.append("4");
strFileList.append("5");
strFileList.append("6");
strFileList.append("7");
strFileList.append("8");
strFileList.append("9");
strFileList.append("MON");
strFileList.append("TUE");
strFileList.append("WED");
strFileList.append("THU");
strFileList.append("FRI");
strFileList.append("SAT");
strFileList.append("SUN");
strFileList.append("AM");
strFileList.append("PM");
strFileList.append("YEAR");
strFileList.append("MONTH");
strFileList.append("DAY");
strFileList.append("maohao");
strFileList.append("xiegang");
strFileList.append("hengxian");
for(int i=0;i<strWordList.count();i++)
CreateWordPic(strWordList.at(i),strFileList.at(i),strPath);
}
void eDClock::CreateWordPic(QString strWord,QString strFileName,QString strPath)
{
//设置字体
QFont font;
font.setFamily(m_attr.fontFamily);
font.setPointSize(m_attr.fontSize);
// font.setPixelSize(m_attr.fontSize);
font.setBold(m_attr.fontBold);
font.setUnderline(m_attr.fontUnderline);
font.setItalic(m_attr.fontItalics);
MACRO_FONT_FANG_JUCHI_2(font)
QFontMetrics fm(font);
QRect rec = fm.boundingRect(strWord);
int iFontHeight=rec.height();
// int iFontHeight=font.pointSize();
// int iFontHeight=m_attr.fontSize;
//字符串所占的像素宽度,高度
//int textWidth = rec.width();
//int leading=fm.leading();
int linespacing=fm.lineSpacing();
//int textHeight = rec.height()-leading-linespacing;
QPixmap *tempPixamp=new QPixmap(rec.width()+1,iFontHeight);
tempPixamp->fill(Qt::transparent);
QPainter *painter=new QPainter();
MACRO_FAN_JUCHI(painter)
painter->begin(tempPixamp);
QTextOption opt;
opt.setAlignment(Qt::AlignVCenter|Qt::AlignLeft);
opt.setWrapMode(QTextOption::NoWrap);
//opt.setAlignment(Qt::AlignCenter|Qt::AlignVCenter);
painter->setPen(m_attr.textColor);
painter->setFont(font);
//QRectF aa(0,(iFontHeight-linespacing)/2,rect().width(),rec.height());
QRectF aa(0,0,rect().width(),rec.height());
painter->drawText(aa, strWord, opt);
painter->end();
delete painter;
QString strSaveFile=strPath+MACRO_FENGEDANFU+strFileName+".png";
strSaveFile.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
tempPixamp->save(strSaveFile,"PNG");
delete tempPixamp;
}
bool eDClock::save(const QString &pRoot){
QRectF r1 = geometry();
QRect r=r1.toRect();
QString strDir=QString("%1%2%3%4%5").arg(zValue()).arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
//保存图片元素到相应目录
QDir dirTemp(pRoot+MACRO_FENGEFU+strDir);
if(dirTemp.exists())
{
if(dirTemp.removeRecursively())
{
int iReTryCount=0;
QDir dRoot(pRoot);
while(!dRoot.mkdir(strDir))
{
QThread::sleep(1);
iReTryCount++;
if(iReTryCount>10)
break;
}
}
}
else {
int iReTryCount=0;
QDir dRoot(pRoot);
while(!dRoot.mkdir(strDir))
{
QThread::sleep(1);
iReTryCount++;
if(iReTryCount>10)
break;
}
}
CreateAllWordPic(pRoot+MACRO_FENGEFU+strDir);
return true;
}