/
todolistmodel.cpp
119 lines (88 loc) · 3.17 KB
/
todolistmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <QFile>
#include <QXmlStreamWriter>
#include "todolistmodel.h"
ToDoListModel::ToDoListModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles[ItemTextRole] = "itemtext";
roles[ItemDueDateRole] = "itemduedate";
setRoleNames(roles);
}
void ToDoListModel::addItem(const ToDoItem &item)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_items << item;
endInsertRows();
}
int ToDoListModel::rowCount(const QModelIndex & parent) const {
return m_items.count();
}
QVariant ToDoListModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() > m_items.count())
return QVariant();
const ToDoItem &item = m_items[index.row()];
if (role == ItemTextRole)
return item.text();
else if (role == ItemDueDateRole)
return item.dueDate().toString();
return QVariant();
}
void ToDoListModel::writeXML(QString fileName) {
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("Spice");
ToDoItem item;
foreach(item, m_items) {
item.writeXML(xmlWriter);
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
file.close();
}
void ToDoListModel::readXML(QString fileName) {
QFile file(fileName);
file.open(QFile::ReadOnly | QFile::Text);
QXmlStreamReader xml(&file);
while(!xml.atEnd() && !xml.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument) continue;
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
/* If it's named persons, we'll go to the next.*/
if(xml.name() == "Spice") continue;
/* If it's named person, we'll dig the information from there.*/
if(xml.name() == "Item") this->addItem(this->parseItem(xml));
}
}
file.close();
}
ToDoItem& ToDoListModel::parseItem(QXmlStreamReader& xml) {
ToDoItem* item = new ToDoItem();
/* Next element... */
xml.readNext();
/*
* We're going to loop over the things because the order might change.
* We'll continue the loop until we hit an EndElement named person.
*/
while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
xml.name() == "Item")) {
xml.readNext();
if(xml.name() == "Text" && xml.tokenType() == QXmlStreamReader::StartElement) {
xml.readNext();
QString txt = xml.text().toString();
item->setText(txt);
}
if(xml.name() == "DueDate" && xml.tokenType() == QXmlStreamReader::StartElement) {
xml.readNext();
QDate dueDate = QDate::fromString(xml.text().toString());
item->setDueDate(dueDate);
}
}
return (*item);
}