moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / ktouch / src / ktouchleveldata.cpp
blobc31386cc8e16c0e394d05047b71faf43befaa4c7
1 /***************************************************************************
2 * ktouchleveldata.cpp *
3 * ----------------- *
4 * Copyright (C) 2000 by Håvard Frøiland, 2003 by Andreas Nicolai *
5 * ghorwin@users.sourceforge.net *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 ***************************************************************************/
13 #include "ktouchleveldata.h"
14 #include <qiodevice.h>
15 #include <qdom.h>
16 #include <klocale.h>
17 #include <kdebug.h>
19 const QString& KTouchLevelData::line(unsigned int lineNumber) const {
20 if (lineNumber>=m_lines.size())
21 lineNumber=0;
22 return m_lines[lineNumber];
25 void KTouchLevelData::createDefault() {
26 m_newChars = i18n("some");
27 m_lines.clear();
28 m_lines.push_back( i18n("No training lecture loaded.") );
31 bool KTouchLevelData::readLevel(QTextStream& in) {
32 QString line;
33 m_comment = m_newChars = QString::null;
34 m_lines.clear();
35 // now read all the lines of the level, until EOF or an empty line
36 line = in.readLine();
37 while (!line.isNull()) {
38 if (!line.isEmpty()) {
39 // Do we have a comment here? And if yes, is a keyword in the line?
40 if (line.find("# Comment:")==0)
41 m_comment = line.right(line.length() - 10).stripWhiteSpace();
42 else if (line[0]!='#') {
43 // the first line is the new characters line
44 if (m_newChars.isNull()) m_newChars = line;
45 else m_lines.push_back(line);
48 line = in.readLine();
50 if (m_lines.empty()) {
51 createDefault();
52 return false;
54 return true;
57 bool KTouchLevelData::readLevel(QDomNode in) {
58 QDomNode newChars = in.namedItem("NewCharacters");
59 if (newChars.isNull()) m_newChars = i18n("basically all characters on the keyboard","abcdefghijklmnopqrstuvwxyz");
60 else m_newChars = newChars.firstChild().nodeValue();
61 QDomNode levelComment = in.namedItem("LevelComment");
62 if (!levelComment.isNull()) m_comment = levelComment.firstChild().nodeValue();
63 m_lines.clear();
64 QDomNode line = in.namedItem("Line");
65 while ( !line.isNull() ) {
66 m_lines.push_back( line.firstChild().nodeValue() );
67 line = line.nextSibling();
69 if (m_lines.empty()) {
70 createDefault();
71 return false;
73 return true;
76 void KTouchLevelData::writeLevel(QTextStream& out) const {
77 out << "# Comment: " << m_comment << endl; // comment line
78 out << m_newChars << endl; // new characters line
79 for (QValueVector<QString>::const_iterator it=m_lines.begin(); it!=m_lines.end(); ++it)
80 out << *it << endl;
81 out << endl;
84 void KTouchLevelData::writeLevel(QDomDocument& doc, QDomElement& root) const {
85 QDomElement level = doc.createElement("Level");
86 root.appendChild(level);
87 // comment (optional)
88 if (!m_comment.isEmpty()) {
89 QDomElement comment = doc.createElement("LevelComment");
90 QDomText commentText = doc.createTextNode(m_comment);
91 comment.appendChild(commentText);
92 level.appendChild(comment);
94 // new characters (must have that)
95 QDomElement newchars = doc.createElement("NewCharacters");
96 QDomText newcharsText;
97 if (m_newChars.isEmpty())
98 newcharsText = doc.createTextNode(i18n("basically all characters on the keyboard","abcdefghijklmnopqrstuvwxyz"));
99 else
100 newcharsText = doc.createTextNode(m_newChars);
101 newchars.appendChild(newcharsText);
102 level.appendChild(newchars);
103 // the lines
104 for (QValueVector<QString>::const_iterator it=m_lines.begin(); it!=m_lines.end(); ++it) {
105 QDomElement line = doc.createElement("Line");
106 QDomText lineText = doc.createTextNode(*it);
107 line.appendChild(lineText);
108 level.appendChild(line);