1 #include "xmlparser.h"
\r
2 #include "datahandling.h"
\r
4 #include <QFileDialog>
\r
5 #include <QMessageBox>
\r
7 #include <QCoreApplication>
\r
13 xmlparser::xmlparser()
\r
15 fileName = (QCoreApplication::applicationDirPath() + "/xml/players/playerdata.xml");
\r
16 fileNameCourses = (QCoreApplication::applicationDirPath() + "/xml/courses/coursedata.xml");
\r
19 fileName = "/opt/GolfScorecard/xml/players/playerdata.xml";
\r
20 fileNameCourses = "/opt/GolfScorecard/xml/courses/coursedata.xml";
\r
24 void xmlparser::loadPlayerXMLFile()
\r
26 datahandling *playerdata = datahandling::instance();
\r
28 if (fileName.isEmpty()){
\r
29 QMessageBox::warning(this, tr("Player file"),
\r
30 tr("EMPTY! Cannot read file."));
\r
34 QFile file(fileName);
\r
36 if (!file.open(QFile::ReadOnly | QFile::Text)) {
\r
37 QMessageBox::warning(this, tr("Player file"),
\r
38 tr("Cannot read file %1:\n%2.")
\r
40 .arg(file.errorString()));
\r
44 QDomDocument doc("Players");
\r
46 if( !doc.setContent( &file ) )
\r
49 QMessageBox::warning(this, tr("Parsing failed"), tr("Parsing failed totally"));
\r
53 QDomElement root = doc.documentElement();
\r
54 if( root.tagName() != "players" )
\r
55 QMessageBox::warning(this, tr("No root found!"), tr("There was no root in XML found!"));
\r
57 QDomElement child = root.firstChildElement("player");
\r
58 while (!child.isNull())
\r
60 playerdata->addPlayer(child.attribute("name",""),child.attribute("hcp",""),child.attribute("gender", ""));
\r
61 child = child.nextSiblingElement("player");
\r
65 void xmlparser::loadCourseXMLFile()
\r
67 datahandling *coursedata = datahandling::instance();
\r
69 if (fileNameCourses.isEmpty()){
\r
70 QMessageBox::warning(this, tr("Course file"),
\r
71 tr("EMPTY! Cannot read file."));
\r
75 QFile file(fileNameCourses);
\r
77 if (!file.open(QFile::ReadOnly | QFile::Text)) {
\r
78 QMessageBox::warning(this, tr("Course file"),
\r
79 tr("Cannot read file %1:\n%2.")
\r
80 .arg(fileNameCourses)
\r
81 .arg(file.errorString()));
\r
85 QDomDocument doc("Courses");
\r
87 if( !doc.setContent( &file ) )
\r
90 QMessageBox::warning(this, tr("Parsing failed"), tr("Parsing failed totally"));
\r
94 QDomElement root = doc.documentElement();
\r
95 if( root.tagName() != "courses" )
\r
96 QMessageBox::warning(this, tr("No root found!"), tr("There was no root in XML found!"));
\r
98 QDomElement child = root.firstChildElement("course");
\r
99 while (!child.isNull())
\r
101 coursedata->addCourses(child.attribute("name", ""), child.attribute("course_file", ""));
\r
102 child = child.nextSiblingElement("course");
\r
106 void xmlparser::parseSelectedCourse()
\r
108 datahandling *coursedata = datahandling::instance();
\r
109 QString selectedCourse = (QCoreApplication::applicationDirPath() + "/xml/courses/" + coursedata->selectedCourse);
\r
112 selectedCourse = ("/opt/GolfScorecard/xml/courses/" + coursedata->selectedCourse);
\r
115 if (selectedCourse.isEmpty()){
\r
116 QMessageBox::warning(this, tr("Course file"),
\r
117 tr("EMPTY! Cannot read file."));
\r
121 QFile file(selectedCourse);
\r
123 if (!file.open(QFile::ReadOnly | QFile::Text)) {
\r
124 QMessageBox::warning(this, tr("Course file"),
\r
125 tr("Cannot read file %1:\n%2.")
\r
126 .arg(selectedCourse)
\r
127 .arg(file.errorString()));
\r
130 QDomDocument doc("Courses");
\r
132 if( !doc.setContent( &file ) )
\r
135 QMessageBox::warning(this, tr("Parsing failed"), tr("Parsing failed totally"));
\r
139 QDomElement root = doc.documentElement();
\r
140 if( root.tagName() != "courses" )
\r
141 QMessageBox::warning(this, tr("No root found!"), tr("There was no root in XML found!"));
\r
143 QDomElement course = root.firstChildElement("course");
\r
144 QDomElement cr = course.firstChildElement("cr");
\r
145 QDomElement male = cr.firstChildElement("male");
\r
146 QDomElement female = cr.firstChildElement("female");
\r
147 QDomElement slope = course.firstChildElement("slope");
\r
148 QDomElement hole = course.firstChildElement("hole");
\r
150 QDomElement white = hole.firstChildElement("white");
\r
151 QDomElement yellow = hole.firstChildElement("yellow");
\r
152 QDomElement blue = hole.firstChildElement("blue");
\r
153 QDomElement red = hole.firstChildElement("red");
\r
154 QDomElement green = hole.firstChildElement("green");
\r
158 while (!course.isNull())
\r
160 coursedata->addCourseData(course.attribute("name", ""), course.attribute("par", ""),
\r
161 course.attribute("latitude", "").toDouble(), course.attribute("longitude", "").toDouble(),
\r
162 course.attribute("country", ""), course.attribute("city", ""));
\r
163 while (!cr.isNull())
\r
165 while (!male.isNull())
\r
167 coursedata->addCourseCrMale(male.attribute("white", "").toDouble(), male.attribute("yellow", "").toDouble(),
\r
168 male.attribute("blue", "").toDouble(), male.attribute("red", "").toDouble());
\r
169 male = male.nextSiblingElement("male");
\r
172 while (!female.isNull())
\r
174 coursedata->addCourseCrFemale(female.attribute("yellow", "").toDouble(),
\r
175 female.attribute("blue", "").toDouble(), female.attribute("red", "").toDouble());
\r
176 female = female.nextSiblingElement("female");
\r
178 cr = cr.nextSiblingElement("cr");
\r
181 male = slope.firstChildElement("male");
\r
182 female = slope.firstChildElement("female");
\r
184 while (!slope.isNull())
\r
186 while (!male.isNull())
\r
188 coursedata->addCourseSlopeMale(male.attribute("white", "").toDouble(), male.attribute("yellow", "").toDouble(),
\r
189 male.attribute("blue", "").toDouble(), male.attribute("red", "").toDouble());
\r
190 male = male.nextSiblingElement("male");
\r
193 while (!female.isNull())
\r
195 coursedata->addCourseSlopeFemale(female.attribute("yellow", "").toDouble(),
\r
196 female.attribute("blue", "").toDouble(), female.attribute("red", "").toDouble());
\r
197 female = female.nextSiblingElement("female");
\r
199 slope = slope.nextSiblingElement("slope");
\r
202 while (!hole.isNull())
\r
204 coursedata->addHoleData(hole.attribute("id", ""), hole.attribute("hcp", ""),
\r
205 hole.attribute("par", ""), hole.attribute("map_uri", ""));
\r
206 while (!white.isNull())
\r
208 coursedata->addWhiteTee(white.attribute("length", ""),
\r
209 white.attribute("latitude", "").toDouble(), white.attribute("longitude", "").toDouble());
\r
210 white = white.nextSiblingElement("white");
\r
212 while (!yellow.isNull())
\r
214 coursedata->addYellowTee(yellow.attribute("length", ""),
\r
215 yellow.attribute("latitude", "").toDouble(), yellow.attribute("longitude", "").toDouble());
\r
216 yellow = yellow.nextSiblingElement("yellow");
\r
218 while (!blue.isNull())
\r
220 coursedata->addBlueTee(blue.attribute("length", ""),
\r
221 blue.attribute("latitude", "").toDouble(), blue.attribute("longitude", "").toDouble());
\r
222 blue = blue.nextSiblingElement("blue");
\r
224 while (!red.isNull())
\r
226 coursedata->addRedTee(red.attribute("length", ""),
\r
227 red.attribute("latitude", "").toDouble(), red.attribute("longitude", "").toDouble());
\r
228 red = red.nextSiblingElement("red");
\r
230 while (!green.isNull())
\r
232 coursedata->addGreenInfo(green.attribute("latitude", "").toDouble(), green.attribute("longitude", "").toDouble());
\r
233 green = green.nextSiblingElement("green");
\r
236 hole = hole.nextSiblingElement("hole");
\r
237 white = hole.firstChildElement("white");
\r
238 yellow = hole.firstChildElement("yellow");
\r
239 blue = hole.firstChildElement("blue");
\r
240 red = hole.firstChildElement("red");
\r
241 green = hole.firstChildElement("green");
\r
243 course = course.nextSiblingElement("course");
\r