LibKEduVocDocument

keduvocwqlreader.cpp
1/*
2 * read a KEduVocDocument from a WQL file
3 * SPDX-FileCopyrightText: 2004, 2007, 2008 Peter Hedlund <peter.hedlund@kdemail.net>
4 * SPDX-FileCopyrightText: 2005 Eric Pignet
5 * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "keduvocwqlreader.h"
10
11#include <KLocalizedString>
12#include <QIODevice>
13#include <QTextStream>
14
15#include "keduvocexpression.h"
16
18 : m_inputFile(&file)
19{
20 m_errorMessage = QLatin1String("");
21}
22
24{
25 QTextStream ts(m_inputFile);
26 QString line1(ts.readLine());
27
28 m_inputFile->seek(0);
29 return (line1 == QLatin1String("WordQuiz"));
30}
31
36
38{
39 m_doc = &doc;
40
41 QTextStream inputStream(m_inputFile);
42 qWarning() << " Need to fix inputStream.setCodec( Windows-1252 );";
43 inputStream.setAutoDetectUnicode(false);
44 inputStream.seek(0);
45
46 QString s = QLatin1String("");
47 s = inputStream.readLine();
48 if (s != QLatin1String("WordQuiz")) {
49 m_errorMessage = i18n("This does not appear to be a (K)WordQuiz file: Missing First line \"WordQuiz\"");
51 }
52 s = inputStream.readLine();
53
54 if (!s.isEmpty()) {
55 s = s.at(0);
56 }
57 int iFV = s.toInt(nullptr);
58 if (iFV != 5) {
59 m_errorMessage = i18n("Only files created by WordQuiz 5.x or later can be opened: Missing Second Line \"5\"");
61 }
62
63 m_errorMessage = i18n("Error while reading file");
64
65 while (!inputStream.atEnd() && inputStream.readLine() != QLatin1String("[Font Info]"))
66 ;
67 if (inputStream.atEnd()) {
68 m_errorMessage = i18n("Error while reading file: Missing [Font Info]");
70 }
71 s = inputStream.readLine();
72 int p = s.indexOf(QLatin1Char('='), 0);
73 QString fam = s.right(s.length() - (p + 1));
74 fam = fam.mid(1, fam.length() - 2);
75
76 s = inputStream.readLine();
77 p = s.indexOf(QLatin1Char('='), 0);
78 s = s.right(s.length() - (p + 1));
79 // int ps = s.toInt(0);
80
81 s = inputStream.readLine();
82 p = s.indexOf(QLatin1Char('='), 0);
83 s = s.right(s.length() - (p + 1));
84 // int b = 0;
85 // if ( s == "1" ) {
86 // b = QFont::Bold;
87 // }
88
89 s = inputStream.readLine();
90 p = s.indexOf(QLatin1Char('='), 0);
91 s = s.right(s.length() - (p + 1));
92 // bool it = (s == "1");
93
94 // m_doc->setFont(new QFont(fam, ps, b, it));
95
96 /* TODO
97 while (inputStream.readLine() != "[Character Info]");
98 s = inputStream.readLine();
99 p = s.find("=", 0);
100 m_specialCharacters = s.right(s.length() - (p + 1));
101 */
102 while (!inputStream.atEnd() && inputStream.readLine() != QLatin1String("[Grid Info]"))
103 ;
104 if (inputStream.atEnd()) {
105 m_errorMessage = i18n("Error while reading file: Missing [Grid Info]");
107 }
108 inputStream.readLine(); // skip value for width of row headers
109
110 s = inputStream.readLine();
111 p = s.indexOf(QLatin1Char('='), 0);
112 s = s.right(s.length() - (p + 1));
113 // m_doc->setSizeHint( 0, s.toInt() );
114
115 s = inputStream.readLine();
116 p = s.indexOf(QLatin1Char('='), 0);
117 s = s.right(s.length() - (p + 1));
118 // m_doc->setSizeHint( 1, s.toInt() );
119
120 /* TODO
121 s = inputStream.readLine();
122 p = s.find("=", 0);
123 s = s.right(s.length() - (p + 1));
124 m_numRows = (s.toInt() - 1); //We need to reduce by one since the header is not included
125 // Selection
126 s = inputStream.readLine();
127 p = s.find("=", 0);
128 s = s.right(s.length() - (p + 1));
129 m_topLeft =s.toInt(0, 10) - 1;
130
131 s = inputStream.readLine();
132 p = s.find("=", 0);
133 s = s.right(s.length() - (p + 1));
134 m_topRight =s.toInt(0, 10) - 1;
135
136 s = inputStream.readLine();
137 p = s.find("=", 0);
138 s = s.right(s.length() - (p + 1));
139 m_bottomLeft =s.toInt(0, 10) - 1;
140
141 s = inputStream.readLine();
142 p = s.find("=", 0);
143 s = s.right(s.length() - (p + 1));
144 m_bottomRight =s.toInt(0, 10) - 1 ;
145 */
146 while (!inputStream.atEnd() && inputStream.readLine() != QLatin1String("[Vocabulary]"))
147 ;
148 if (inputStream.atEnd()) {
149 m_errorMessage = i18n("Error while reading file: Missing [Vocabulary]");
151 }
152
153 KEduVocLesson *lesson = new KEduVocLesson(i18n("Vocabulary"), m_doc->lesson());
154 m_doc->lesson()->appendChildContainer(lesson);
155
156 s = inputStream.readLine();
157 p = s.indexOf(QLatin1String(" ["), 0);
158 s = s.left(p);
159 s = s.simplified();
160 m_doc->appendIdentifier();
161 m_doc->identifier(0).setLocale(s);
162 m_doc->identifier(0).setName(s);
163 s = inputStream.readLine();
164 m_doc->appendIdentifier();
165 m_doc->identifier(1).setLocale(s);
166 m_doc->identifier(1).setName(s);
167
168 while (!s.isNull()) {
169 s = inputStream.readLine();
170 p = s.indexOf(QLatin1String("["), 0);
171 // QString r = s.mid(p + 1, 10);
172 // int h = r.toInt();
173 s = s.left(p);
174 s = s.simplified();
175 QString b;
176 b = inputStream.readLine();
177
179 expr->setTranslation(1, b);
180 lesson->appendEntry(expr);
181 }
183}
The primary entry point to the hierarchy of objects describing vocabularies.
int appendIdentifier(const KEduVocIdentifier &identifier=KEduVocIdentifier())
Appends a new identifier (usually a language)
KEduVocLesson * lesson()
Get the lesson root object.
ErrorCode
the return code when opening/saving
@ FileTypeUnknown
unknown file type
@ FileReaderFailed
file reader failed
KEduVocIdentifier & identifier(int index)
Returns the identifier of translation index.
FileType
known vocabulary file types
@ Wql
Word Quiz format.
This class contains one vocabulary expression as an original with one or more translations.
void setTranslation(int index, const QString &expression)
Add a translation to this expression.
void setLocale(const QString &name)
Set the locale.
void setName(const QString &name)
Set the name.
class to store information about a lesson
void appendEntry(KEduVocExpression *entry)
append an entry to the lesson
bool isParsable() override
Can this reader parse this file.
KEduVocDocument::ErrorCode read(KEduVocDocument &doc) override
Parse file and write into doc.
KEduVocWqlReader(QIODevice &file)
constructor
KEduVocDocument::FileType fileTypeHandled() override
returns the KEduVocDocument::FileType that this reader handles
QString i18n(const char *text, const TYPE &arg...)
virtual bool seek(qint64 pos)
const QChar at(qsizetype position) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool isNull() const const
QString left(qsizetype n) const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString right(qsizetype n) const const
QString simplified() const const
int toInt(bool *ok, int base) const const
bool atEnd() const const
QString readLine(qint64 maxlen)
bool seek(qint64 pos)
void setAutoDetectUnicode(bool enabled)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:55:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.