1 /***************************************************************************
2 * ktouchleveldata.cpp *
4 * Copyright (C) 2000 by Håvard Frøiland, 2003 by Andreas Nicolai *
5 * ghorwin@users.sourceforge.net *
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>
19 const QString
& KTouchLevelData::line(unsigned int lineNumber
) const {
20 if (lineNumber
>=m_lines
.size())
22 return m_lines
[lineNumber
];
25 void KTouchLevelData::createDefault() {
26 m_newChars
= i18n("some");
28 m_lines
.push_back( i18n("No training lecture loaded.") );
31 bool KTouchLevelData::readLevel(QTextStream
& in
) {
33 m_comment
= m_newChars
= QString::null
;
35 // now read all the lines of the level, until EOF or an empty line
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
);
50 if (m_lines
.empty()) {
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();
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()) {
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
)
84 void KTouchLevelData::writeLevel(QDomDocument
& doc
, QDomElement
& root
) const {
85 QDomElement level
= doc
.createElement("Level");
86 root
.appendChild(level
);
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"));
100 newcharsText
= doc
.createTextNode(m_newChars
);
101 newchars
.appendChild(newcharsText
);
102 level
.appendChild(newchars
);
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
);