0504合并前提交
This commit is contained in:
+5
-81
@@ -2,6 +2,7 @@
|
||||
#include <QDebug>
|
||||
#include <QRegExp>
|
||||
#include "LogInfo.h"
|
||||
#include "ItemCommon.h"
|
||||
|
||||
PicDateItem::PicDateItem(QGraphicsItem *parent, QGraphicsScene *scene, const SourceType type)
|
||||
:PicTextItem(parent, scene, type)
|
||||
@@ -25,7 +26,7 @@ void PicDateItem::setSource(QVariant var)
|
||||
if(m_model.datetimePattern.isEmpty())
|
||||
return;
|
||||
|
||||
m_currDateTimeStr = generateDatetimeStr(QDateTime::currentDateTime());
|
||||
m_currDateTimeStr = generateTxtFromDateItemModel(m_model, QDateTime::currentDateTime());
|
||||
applyModify();
|
||||
}
|
||||
|
||||
@@ -46,13 +47,13 @@ bool PicDateItem::updateDatetimeStr()
|
||||
return false;
|
||||
|
||||
if(m_currDateTimeStr.isEmpty()) {
|
||||
m_currDateTimeStr = generateDatetimeStr(QDateTime::currentDateTime());
|
||||
m_currDateTimeStr = generateTxtFromDateItemModel(m_model, QDateTime::currentDateTime());
|
||||
applyModify();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto now = QDateTime::currentDateTime();
|
||||
auto str = generateDatetimeStr(now);
|
||||
auto str = generateTxtFromDateItemModel(m_model, now);
|
||||
if(str == m_currDateTimeStr) {
|
||||
return false;
|
||||
}
|
||||
@@ -120,81 +121,4 @@ QImage PicDateItem::itemImage()
|
||||
|
||||
auto ret = strToImage(m_currDateTimeStr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
QString PicDateItem::generateDatetimeStr(const QDateTime &datetime)
|
||||
{
|
||||
QString result = m_model.datetimePattern;
|
||||
|
||||
// 应用偏移量
|
||||
QDateTime applyDateTime = datetime;
|
||||
if(m_model.monthsOffset != 0) {
|
||||
applyDateTime = datetime.addMonths(m_model.monthsOffset);
|
||||
}
|
||||
else if(m_model.daysOffset != 0) {
|
||||
applyDateTime = datetime.addDays(m_model.daysOffset);
|
||||
}
|
||||
|
||||
// 使用正则表达式匹配并替换各种格式化模式
|
||||
|
||||
// 匹配四位数年份 yyyy
|
||||
int i = result.indexOf("yyyy");
|
||||
if(i != -1) {
|
||||
int year = applyDateTime.date().year();
|
||||
QString yearStr = m_model.yearsMap.contains(year) ? m_model.yearsMap[year] : QString::number(year);
|
||||
result.replace(i, 4, yearStr);
|
||||
}
|
||||
|
||||
// 匹配两位数年份 yy
|
||||
i = result.indexOf("YY");
|
||||
if(i != -1) {
|
||||
QString yearStr = QString::number(applyDateTime.date().year()).right(2);
|
||||
result.replace(i, 2, yearStr);
|
||||
}
|
||||
|
||||
// 匹配两位数月份 MM
|
||||
i = result.indexOf("MM");
|
||||
if(i != -1) {
|
||||
result.replace(i, 2, m_model.monthsMap[applyDateTime.date().month() - 1]);
|
||||
}
|
||||
|
||||
// 匹配两位数日期 dd
|
||||
i = result.indexOf("dd");
|
||||
if(i != -1) {
|
||||
result.replace(i, 2, m_model.daysMap[applyDateTime.date().day() - 1]);
|
||||
}
|
||||
|
||||
// 匹配两位数小时 HH
|
||||
i = result.indexOf("HH");
|
||||
if(i != -1) {
|
||||
result.replace(i, 2, m_model.hoursMap[applyDateTime.time().hour()]);
|
||||
}
|
||||
|
||||
// 匹配两位数分钟 mm
|
||||
QRegExp minutePattern("mm");
|
||||
if (result.contains(minutePattern)) {
|
||||
int minute = applyDateTime.time().minute();
|
||||
result.replace(minutePattern, QString::number(minute).rightJustified(2, '0'));
|
||||
}
|
||||
|
||||
// 匹配两位数秒钟 ss
|
||||
QRegExp secondPattern("ss");
|
||||
if (result.contains(secondPattern)) {
|
||||
int second = applyDateTime.time().second();
|
||||
result.replace(secondPattern, QString::number(second).rightJustified(2, '0'));
|
||||
}
|
||||
|
||||
// 格式化周几 w
|
||||
i = result.indexOf("w");
|
||||
if (i != -1) {
|
||||
result.replace(i, 1, m_model.weeksMap[applyDateTime.date().dayOfWeek() - 1]);
|
||||
}
|
||||
|
||||
// 格式化年中周数 c
|
||||
QRegExp weekNumberPattern("c");
|
||||
if (result.contains(weekNumberPattern)) {
|
||||
result.replace(weekNumberPattern, QString::number(applyDateTime.date().weekNumber()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user