/
LTMChartParser.cpp
226 lines (188 loc) · 5.92 KB
/
LTMChartParser.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) 2010 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "LTMChartParser.h"
#include "LTMSettings.h"
#include "LTMTool.h"
#include "Athlete.h"
#include <QDate>
#include <QDebug>
bool LTMChartParser::startDocument()
{
buffer.clear();
return true;
}
// to see the format of the charts.xml file, look at the serialize()
// function at the bottom of this source file.
bool LTMChartParser::endElement( const QString&, const QString&, const QString &qName )
{
//
// Single Attribute elements
//
if(qName == "chart") {
QString string = buffer.mid(1,buffer.length()-2);
QByteArray base64(string.toLatin1());
QByteArray unmarshall = QByteArray::fromBase64(base64);
QDataStream s(&unmarshall, QIODevice::ReadOnly);
LTMSettings x;
s >> x;
settings << x;
}
return true;
}
bool LTMChartParser::startElement( const QString&, const QString&, const QString &, const QXmlAttributes &)
{
buffer.clear();
return true;
}
bool LTMChartParser::characters( const QString& str )
{
buffer += str;
return true;
}
QList<LTMSettings>
LTMChartParser::getSettings()
{
return settings;
}
bool LTMChartParser::endDocument()
{
return true;
}
// static helper to protect special xml characters
// ideally we would use XMLwriter to do this but
// the file format is trivial and this implementation
// is easier to follow and modify... for now.
static QString xmlprotect(QString string)
{
QTextEdit trademark("™"); // process html encoding of(TM)
QString tm = trademark.toPlainText();
QString s = string;
s.replace( tm, "™" );
s.replace( "&", "&" );
s.replace( ">", ">" );
s.replace( "<", "<" );
s.replace( "\"", """ );
s.replace( "\'", "'" );
return s;
}
//
// Write out the charts.xml file
//
// Most of the heavy lifting is done by the LTMSettings
// << and >> operators. We just put them into the character
// data for a chart.
//
void
LTMChartParser::serialize(QString filename, QList<LTMSettings> charts)
{
// open file - truncate contents
QFile file(filename);
if (!file.open(QFile::WriteOnly)) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(QObject::tr("Problem Saving Charts Configuration"));
msgBox.setInformativeText(QObject::tr("File: %1 cannot be opened for 'Writing'. Please check file properties.").arg(filename));
msgBox.exec();
return;
};
file.resize(0);
QTextStream out(&file);
out.setCodec("UTF-8");
// begin document
out << QString("<charts version=\"%1\">\n").arg(LTM_VERSION_NUMBER);
// write out to file
foreach (LTMSettings chart, charts) {
out <<"\t<chart name=\"" << xmlprotect(chart.name) <<"\">\"";
// chart name
QByteArray marshall;
QDataStream s(&marshall, QIODevice::WriteOnly);
s << chart;
out<<marshall.toBase64();
out<<"\"</chart>\n";
}
// end document
out << "</charts>\n";
// close file
file.close();
}
ChartTreeView::ChartTreeView(Context *context) : context(context)
{
setDragDropMode(QAbstractItemView::InternalMove);
setDragDropOverwriteMode(true);
#ifdef Q_OS_MAC
setAttribute(Qt::WA_MacShowFocusRect, 0);
#endif
}
void
ChartTreeView::dropEvent(QDropEvent* event)
{
QTreeWidgetItem* target = (QTreeWidgetItem *)itemAt(event->pos());
int idxTo = indexFromItem(target).row();
// when dragging to past the last one, we get -1, so lets
// set the target row to the very last one
if (idxTo < 0) idxTo = invisibleRootItem()->childCount()-1;
int offsetFrom = 0;
int offsetTo = 0;
QList<int> idxToList;
foreach (QTreeWidgetItem *p, selectedItems()) {
int idxFrom = indexFromItem(p).row();
context->athlete->presets.move(idxFrom+offsetFrom, idxTo+offsetTo);
idxToList << idxTo+(idxFrom>idxTo?offsetTo:offsetFrom);
if (idxFrom<idxTo)
offsetFrom--;
else
offsetTo++;
}
// the default implementation takes care of the actual move inside the tree
//QTreeWidget::dropEvent(event);
// reset
context->notifyPresetsChanged();
clearSelection();
// select it!
foreach (int idx, idxToList) {
invisibleRootItem()->child(idx)->setSelected(true);
}
}
QStringList
ChartTreeView::mimeTypes() const
{
QStringList returning;
returning << "application/x-gc-charts";
return returning;
}
QMimeData *
ChartTreeView::mimeData (const QList<QTreeWidgetItem *> items) const
{
QMimeData *returning = new QMimeData;
// we need to pack into a byte array
QByteArray rawData;
QDataStream stream(&rawData, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_4_6);
// pack data
stream << (quint64)(context); // where did this come from?
stream << items.count();
foreach (QTreeWidgetItem *p, items) {
// get the season this is for
int index = p->treeWidget()->invisibleRootItem()->indexOfChild(p);
// season[index] ...
stream << context->athlete->presets[index]; // name
}
// and return as mime data
returning->setData("application/x-gc-charts", rawData);
return returning;
}