#include "ItemCommon.h" QString generateTxtFromDateItemModel(const DateItemModel &model, const QDateTime &time) { QString result = model.datetimePattern; // 应用偏移量 QDateTime applyDateTime = time; if(model.monthsOffset != 0) { applyDateTime = time.addMonths(model.monthsOffset); } else if(model.daysOffset != 0) { applyDateTime = time.addDays(model.daysOffset); } // 使用正则表达式匹配并替换各种格式化模式 // 匹配四位数年份 yyyy int i = result.indexOf("yyyy"); if(i != -1) { int year = applyDateTime.date().year(); QString yearStr = model.yearsMap.contains(year) ? 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, model.monthsMap[applyDateTime.date().month() - 1]); } // 匹配两位数日期 dd i = result.indexOf("dd"); if(i != -1) { result.replace(i, 2, model.daysMap[applyDateTime.date().day() - 1]); } // 匹配两位数小时 HH i = result.indexOf("HH"); if(i != -1) { result.replace(i, 2, 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, model.weeksMap[applyDateTime.date().dayOfWeek() - 1]); } // 格式化年中周数 c QRegExp weekNumberPattern("c"); if (result.contains(weekNumberPattern)) { result.replace(weekNumberPattern, QString::number(applyDateTime.date().weekNumber())); } return result; } QString generateTxtFromBarcodeViewModel(const BarcodeViewModel &vm, const std::array counterValues, const QDateTime &time) { QString txt = vm.contentPattern; auto pos = vm.contentPattern.indexOf("{t}"); if(pos != -1) { txt.replace(pos, 3, generateTxtFromDateItemModel(vm.dateViewModel, time)); } pos = vm.contentPattern.indexOf("{c}"); if(pos != 1) { auto cntValue = vm.counterViewModel.counterType == COUNTER_TYPE_1 ? counterValues[0] : counterValues[1]; auto valueStr = QString("%1").arg(cntValue, 10, QChar('0'));; valueStr.right(vm.counterViewModel.digitalNum); txt.replace(pos, 3, valueStr); } return txt; }