110 lines
3.8 KiB
C++
110 lines
3.8 KiB
C++
#include "ViewModels.h"
|
|
#include <QDebug>
|
|
|
|
DateItemModel DateItemModel::createDefaultReplaceModel()
|
|
{
|
|
DateItemModel model;
|
|
|
|
model.dataMap << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7"
|
|
<< "8" << "9" << "A" << "B" << "C" << "D" << "E" << "F";
|
|
|
|
for (int i = 1; i <= 31; ++i) {
|
|
model.daysMap.append(QString("%1").arg(i, 2, 10, QChar('0')));
|
|
}
|
|
|
|
for (int i = 1; i <= 12; ++i) {
|
|
model.monthsMap.append(QString("%1").arg(i, 2, 10, QChar('0')));
|
|
}
|
|
|
|
int startYear = QDate::currentDate().year();
|
|
for (int i = 0; i < 16; ++i) {
|
|
model.yearsMap.insert(startYear + i, QString::number(startYear + i));
|
|
}
|
|
|
|
model.weeksMap << "MON" << "TUE" << "WED" << "THU" << "FRI" << "SAT" << "SUN";
|
|
|
|
for (int i = 0; i <= 23; ++i) {
|
|
model.hoursMap.append(QString("%1").arg(i, 2, 10, QChar('0')));
|
|
}
|
|
|
|
return model;
|
|
}
|
|
|
|
void DateItemModel::loadFromXml(DateItemModel &vm, const QXmlStreamAttributes &attrs)
|
|
{
|
|
auto& m = vm;
|
|
m.dataMap = attrs.value("dataMap").toString().split("<br>");
|
|
m.daysMap = attrs.value("daysMap").toString().split("<br>");
|
|
m.hoursMap = attrs.value("hoursMap").toString().split("<br>");
|
|
m.monthsMap = attrs.value("monthsMap").toString().split("<br>");
|
|
m.weeksMap = attrs.value("weeksMap").toString().split("<br>");
|
|
|
|
auto yearsMap = attrs.value("yearsMap").toString().split("<br>");
|
|
for(auto it = yearsMap.begin(); it != yearsMap.end(); ++it) {
|
|
auto yearPair = it->split("<subbr>");
|
|
if(yearPair.size() == 2) {
|
|
m.yearsMap[yearPair[0].toInt()] = yearPair[1];
|
|
}
|
|
}
|
|
|
|
m.monthsOffset = attrs.value("monthsOffset").toInt();
|
|
m.daysOffset = attrs.value("daysOffset").toInt();
|
|
m.datetimePattern = attrs.value("datetimePattern").toString();
|
|
}
|
|
|
|
void DateItemModel::saveToXml(const DateItemModel &vm, QXmlStreamAttributes &attrs)
|
|
{
|
|
auto& m = vm;
|
|
auto dataMap = m.dataMap.join("<br>");
|
|
auto daysMap = m.daysMap.join("<br>");
|
|
auto hoursMap = m.hoursMap.join("<br>");
|
|
auto monthsMap = m.monthsMap.join("<br>");
|
|
auto weeksMap = m.weeksMap.join("<br>");
|
|
|
|
QString yearsMap;
|
|
for(auto it = m.yearsMap.begin(); it != m.yearsMap.end(); ++it) {
|
|
yearsMap.append(QString("%1<subbr>%2<br>").arg(it.key()).arg(it.value()));
|
|
}
|
|
|
|
attrs.append("dataMap", dataMap);
|
|
attrs.append("daysMap", daysMap);
|
|
attrs.append("hoursMap", hoursMap);
|
|
attrs.append("monthsMap", monthsMap);
|
|
attrs.append("weeksMap", weeksMap);
|
|
attrs.append("yearsMap", yearsMap);
|
|
attrs.append("datetimePattern", m.datetimePattern);
|
|
attrs.append("monthsOffset", QString::number(m.monthsOffset));
|
|
attrs.append("daysOffset", QString::number(m.daysOffset));
|
|
}
|
|
|
|
void CounterViewModel::loadFromXml(CounterViewModel &vm, const QXmlStreamAttributes &attrs)
|
|
{
|
|
auto typeRef = attrs.value("counter_type");
|
|
auto digitalNumRef = attrs.value("digital_num");
|
|
|
|
bool ok = false;
|
|
int counterType = typeRef.toInt(&ok);
|
|
if(!ok || (counterType != COUNTER_TYPE_1 && COUNTER_TYPE_2 != counterType)) {
|
|
qWarning() << "Invalied counter type: " << counterType;
|
|
return;
|
|
}
|
|
|
|
ok = false;
|
|
int digitalNum = digitalNumRef.toInt(&ok);
|
|
if(!ok || digitalNum <= 0) {
|
|
qWarning() << "Invalied counter digital number: " << digitalNum;
|
|
return;
|
|
}
|
|
|
|
vm.counterType = static_cast<COUNTER_TYPE>(counterType);
|
|
vm.digitalNum = digitalNum;
|
|
qInfo() << "Loaded attrs from xml: " << " counter type: " << counterType << ", digitalNum: " << digitalNum;
|
|
}
|
|
|
|
void CounterViewModel::saveToXml(CounterViewModel &vm, QXmlStreamAttributes &attrs)
|
|
{
|
|
attrs.append("counter_type", QString::number(vm.counterType));
|
|
attrs.append("digital_num", QString::number(vm.digitalNum));
|
|
qInfo() << "Set attrs to xml: " << " counter type: " << vm.counterType << ", digitalNum: " << vm.digitalNum;
|
|
}
|