13#include <QXmlStreamReader>
20 p.replace(QLatin1Char(
'&'), QLatin1String(
"&"));
21 p.replace(QLatin1Char(
'\"'), QLatin1String(
"""));
22 p.replace(QLatin1Char(
'>'), QLatin1String(
">"));
23 p.replace(QLatin1Char(
'<'), QLatin1String(
"<"));
24 p.replace(QLatin1Char(
'\''), QLatin1String(
"'"));
29 : shrtc(-1), m_phraseBook(0)
33Phrase::
Phrase(
const QString &source,
const QString &target,
const QString &definition,
40 const QString &definition,
PhraseBook *phraseBook)
42 m_phraseBook(phraseBook)
52 m_phraseBook->phraseChanged(
this);
61 m_phraseBook->phraseChanged(
this);
70 m_phraseBook->phraseChanged(
this);
75 return p.source() == q.source() && p.target() == q.target() &&
83 :
XmlParser(reader), pb(phraseBook), ferrorCount(0)
93 QStringView qName,
const QXmlStreamAttributes &atts)
override;
94 bool endElement(QStringView namespaceURI, QStringView localName,
95 QStringView qName)
override;
97 bool fatalError(qint64 line, qint64 column,
const QString &message)
override;
104 QString m_sourceLanguage;
111 QStringView qName,
const QXmlStreamAttributes &atts)
113 Q_UNUSED(namespaceURI);
116 if (qName == QLatin1String(
"QPH")) {
117 m_language = atts.value(QLatin1String(
"language")).toString();
118 m_sourceLanguage = atts.value(QLatin1String(
"sourcelanguage")).toString();
119 }
else if (qName == QLatin1String(
"phrase")) {
122 definition.truncate(0);
131 Q_UNUSED(namespaceURI);
134 if (qName == QLatin1String(
"source"))
136 else if (qName == QLatin1String(
"target"))
138 else if (qName == QLatin1String(
"definition"))
140 else if (qName == QLatin1String(
"phrase"))
141 pb->m_phrases.append(
new Phrase(source, target, definition, pb));
153 if (ferrorCount++ == 0) {
154 QString msg = PhraseBook::tr(
"Parse error at line %1, column %2 (%3).")
158 QMessageBox::information(
nullptr, QObject::tr(
"Qt Linguist"), msg);
165 m_language(QLocale::C),
166 m_sourceLanguage(QLocale::C),
167 m_territory(QLocale::AnyTerritory),
168 m_sourceTerritory(QLocale::AnyTerritory)
174 qDeleteAll(m_phrases);
177void PhraseBook::setLanguageAndTerritory(QLocale::Language lang, QLocale::Territory territory)
179 if (m_language == lang && m_territory == territory)
182 m_territory = territory;
186void PhraseBook::setSourceLanguageAndTerritory(QLocale::Language lang, QLocale::Territory territory)
188 if (m_sourceLanguage == lang && m_sourceTerritory == territory)
190 m_sourceLanguage = lang;
191 m_sourceTerritory = territory;
198 if (!f.open(QIODevice::ReadOnly))
201 m_fileName = fileName;
203 QXmlStreamReader reader(&f);
205 reader.setNamespaceProcessing(
false);
208 Translator::languageAndTerritory(hand->language(), &m_language, &m_territory);
209 *langGuessed =
false;
210 if (m_language == QLocale::C) {
212 m_language = sys.language();
213 m_territory = sys.territory();
217 QString lang = hand->sourceLanguage();
218 if (lang.isEmpty()) {
219 m_sourceLanguage = QLocale::C;
220 m_sourceTerritory = QLocale::AnyTerritory;
222 Translator::languageAndTerritory(lang, &m_sourceLanguage, &m_sourceTerritory);
228 qDeleteAll(m_phrases);
240 if (!f.open(QIODevice::WriteOnly))
243 m_fileName = fileName;
247 t <<
"<!DOCTYPE QPH>\n<QPH";
248 if (sourceLanguage() != QLocale::C)
249 t <<
" sourcelanguage=\""
250 << Translator::makeLanguageCode(sourceLanguage(), sourceTerritory()) <<
'"';
251 if (language() != QLocale::C)
252 t <<
" language=\"" << Translator::makeLanguageCode(language(), territory()) <<
'"';
254 for (Phrase *p : std::as_const(m_phrases)) {
256 t <<
" <source>" << xmlProtect( p->source() ) <<
"</source>\n";
257 t <<
" <target>" << xmlProtect( p->target() ) <<
"</target>\n";
258 if (!p->definition().isEmpty())
259 t <<
" <definition>" << xmlProtect( p->definition() )
260 <<
"</definition>\n";
271 m_phrases.append(phrase);
279 m_phrases.removeOne(phrase);
287 if (m_changed != modified) {
288 emit modifiedChanged(modified);
289 m_changed = modified;
302 if (!m_fileName.isEmpty())
303 return QFileInfo(m_fileName).fileName();
void append(Phrase *phrase)
QString friendlyPhraseBookName() const
bool load(const QString &fileName, bool *langGuessed)
bool save(const QString &fileName)
void remove(Phrase *phrase)
void setPhraseBook(PhraseBook *book)
PhraseBook * phraseBook() const
void setDefinition(const QString &nd)
void setSource(const QString &ns)
Phrase(const QString &source, const QString &target, const QString &definition, PhraseBook *phraseBook)
Phrase(const QString &source, const QString &target, const QString &definition, const Candidate &candidate, int sc=-1)
void setTarget(const QString &nt)
bool fatalError(qint64 line, qint64 column, const QString &message) override
QphHandler(PhraseBook *phraseBook, QXmlStreamReader &reader)
QString sourceLanguage() const
bool endElement(QStringView namespaceURI, QStringView localName, QStringView qName) override
~QphHandler() override=default
bool startElement(QStringView namespaceURI, QStringView localName, QStringView qName, const QXmlStreamAttributes &atts) override
bool characters(QStringView ch) override
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QString xmlProtect(const QString &str)
bool operator==(const Phrase &p, const Phrase &q)