From 10251fd6f576d3e29e6885b1b83e8febaa629127 Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Tue, 28 Aug 2007 13:30:15 +0000 Subject: [PATCH] Add complete support for READING old kvtml usage labels. New class: KEduVocCompablility to contain all constants needed for the above conversions. The type and conjugation stuff should also move there, to finall clean up with all scattered defines. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=705745 --- keduvocdocument/CMakeLists.txt | 1 + keduvocdocument/keduvockvtmlcompability.cpp | 114 ++++++++++++++++++++ keduvocdocument/keduvockvtmlcompability.h | 85 +++++++++++++++ keduvocdocument/keduvockvtmlreader.cpp | 43 ++++---- keduvocdocument/keduvockvtmlreader.h | 5 +- 5 files changed, 224 insertions(+), 24 deletions(-) create mode 100644 keduvocdocument/keduvockvtmlcompability.cpp create mode 100644 keduvocdocument/keduvockvtmlcompability.h diff --git a/keduvocdocument/CMakeLists.txt b/keduvocdocument/CMakeLists.txt index 9f816f4..96a1af7 100644 --- a/keduvocdocument/CMakeLists.txt +++ b/keduvocdocument/CMakeLists.txt @@ -16,6 +16,7 @@ set(keduvocdocument_LIB_SRCS keduvockvtml2reader.cpp keduvockvtmlwriter.cpp keduvockvtml2writer.cpp + keduvockvtmlcompability.cpp keduvocmultiplechoice.cpp keduvocpaukerreader.cpp keduvocvokabelnreader.cpp diff --git a/keduvocdocument/keduvockvtmlcompability.cpp b/keduvocdocument/keduvockvtmlcompability.cpp new file mode 100644 index 0000000..0a2fc2a --- /dev/null +++ b/keduvocdocument/keduvockvtmlcompability.cpp @@ -0,0 +1,114 @@ +/*************************************************************************** + + C++ Implementation: keduvockvtml1compability_p + + ----------------------------------------------------------------------- + + begin : Di Aug 28 2007 + + copyright : (C) 2007 Frederik Gladhorn + + ----------------------------------------------------------------------- + + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "keduvockvtmlcompability.h" + +const QString KEduVocKvtmlCompability::USAGE_USER_DEFINED = QString("#"); +const QString KEduVocKvtmlCompability::USAGE_SEPERATOR = QString(":"); + + +KEduVocKvtmlCompability::KEduVocKvtmlCompability() +{ + m_usages = usageMap(); + m_userdefinedUsageCounter = 0; +} + + +QSet KEduVocKvtmlCompability::usageFromKvtml1(const QString & oldUsage) const +{ + QSet usages; + foreach ( QString usage , oldUsage.split(USAGE_SEPERATOR, QString::SkipEmptyParts) ) { + usages.insert( m_usages[usage] ); + } + return usages; +} + + +QMap< QString, QString > KEduVocKvtmlCompability::usageMap() +{ + QMap< QString, QString > usages; + + usages["Am" ] = i18n("Americanism"); + usages["abbr" ] = i18n("abbreviation"); + usages["anat" ] = i18n("anatomy"); + usages["astr" ] = i18n("astronomy"); + usages["biol" ] = i18n("biology"); + usages["bs" ] = i18n("bad sense"); + usages["contp" ] = i18n("contemptuously"); + usages["eccl" ] = i18n("ecclesiastical"); + usages["fig" ] = i18n("figuratively"); + usages["geol" ] = i18n("geology"); + usages["hist" ] = i18n("historical"); + usages["icht" ] = i18n("ichthyology"); + usages["ifm" ] = i18n("informal"); + usages["iro" ] = i18n("ironic"); + usages["irr" ] = i18n("irregular"); + usages["lit" ] = i18n("literary"); + usages["metal" ] = i18n("metallurgy"); + usages["meteo" ] = i18n("meteorology"); + usages["miner" ] = i18n("mineralogy"); + usages["mot" ] = i18n("motoring"); + usages["mount" ] = i18n("mountaineering"); + usages["myth" ] = i18n("mythology"); + usages["npr" ] = i18n("proper name"); + usages["opt" ] = i18n("optics"); + usages["orn" ] = i18n("ornithology"); + usages["os" ] = i18n("oneself"); + usages["p" ] = i18n("person"); + usages["parl" ] = i18n("parliamentary"); + usages["pharm" ] = i18n("pharmacy"); + usages["phls" ] = i18n("philosophy"); + usages["phot" ] = i18n("photography"); + usages["phys" ] = i18n("physics"); + usages["physio"] = i18n("physiology"); + usages["pl" ] = i18n("plural"); + usages["poet" ] = i18n("poetry"); + usages["pol" ] = i18n("politics"); + usages["prov" ] = i18n("provincialism"); + usages["psych" ] = i18n("psychology"); + usages["rhet" ] = i18n("rhetoric"); + usages["surv" ] = i18n("surveying"); + usages["telg" ] = i18n("telegraphy"); + usages["telph" ] = i18n("telephony"); + usages["thea" ] = i18n("theater"); + usages["typo" ] = i18n("typography"); + usages["univ" ] = i18n("university"); + usages["vet" ] = i18n("veterinary medicine"); + usages["zoo" ] = i18n("zoology"); + + return usages; +} + +void KEduVocKvtmlCompability::addUserdefinedUsage(const QString & usage) +{ + // start counting at 1 !!! + m_userdefinedUsageCounter++; + m_usages[USAGE_USER_DEFINED + QString::number(m_userdefinedUsageCounter)] = usage; +} + +QSet< QString > KEduVocKvtmlCompability::documentUsages() const +{ + return QSet::fromList(m_usages.values()); +} + + diff --git a/keduvocdocument/keduvockvtmlcompability.h b/keduvocdocument/keduvockvtmlcompability.h new file mode 100644 index 0000000..4ef8c9c --- /dev/null +++ b/keduvocdocument/keduvockvtmlcompability.h @@ -0,0 +1,85 @@ +/*************************************************************************** + + C++ Interface: keduvockvtml1compability_p + + ----------------------------------------------------------------------- + + begin : Di Aug 28 2007 + + copyright : (C) 2007 Frederik Gladhorn + + ----------------------------------------------------------------------- + + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef KEDUVOCKVTML1COMPABILITY_P_H +#define KEDUVOCKVTML1COMPABILITY_P_H + +#include +#include +#include + +#include + +/** + * @file contains defines and constants necessary for reading kvtml files prior to KDE4. kvtml version 1. + */ +class KEduVocKvtmlCompability { + +public: + /** + * Constructor, initialize the map. + */ + KEduVocKvtmlCompability(); + + /** + * In old kvtml documents usages could be added. When parsing the doc, + * we need those too. They map "#1" to something meaningful. + * Add them in order! + * @param usage the old user defined usage. + */ + void addUserdefinedUsage( const QString& usage ); + + /** + * This "translates" the old usage string found in the files to the + * real word/meaning. It will also consider the user defined usages. + * Use this method for usages. + * @param oldUsage string e.g. "biol:anat:#1" + * @return full set e.g. {"biology", "anatomy", "user defined 1"} + */ + QSet usageFromKvtml1( const QString& oldUsage ) const; + + QSet documentUsages() const; + +private: + /** + * This gives a map of old abbreviations used in the files and their meaning. + * [biol] == biology and so on... + * @return the map + */ + static QMap< QString, QString > usageMap(); + + /// Map to store usages. Initialized with preset values by the constructor. Later user defined usages can be added. + QMap< QString, QString > m_usages; + + /// # defines user defined usage + static const QString USAGE_USER_DEFINED; + /// : seperates usages + static const QString USAGE_SEPERATOR; + + /// only order was decisive, we have to keep count. + int m_userdefinedUsageCounter; +}; + + + +#endif // KEDUVOCKVTML1COMPABILITY_P_H diff --git a/keduvocdocument/keduvockvtmlreader.cpp b/keduvocdocument/keduvockvtmlreader.cpp index a3fc37c..28cde17 100644 --- a/keduvocdocument/keduvockvtmlreader.cpp +++ b/keduvocdocument/keduvockvtmlreader.cpp @@ -682,31 +682,27 @@ bool KEduVocKvtmlReader::readTense(QDomElement &domElementParent) bool KEduVocKvtmlReader::readUsage(QDomElement &domElementParent) { - QString s; + // get user defined usages + QDomElement currentElement; - QStringList descriptions; QDomNodeList entryList = domElementParent.elementsByTagName(KV_USAGE_DESC); - if (entryList.length() <= 0) + if (entryList.length() <= 0) { return false; + } for (int i = 0; i < entryList.count(); ++i) { currentElement = entryList.item(i).toElement(); if (currentElement.parentNode() == domElementParent) { - int no = 0; - - QDomAttr attribute = currentElement.attributeNode(KV_USAGE_NO); - if (!attribute.isNull()) - no = attribute.value().toInt(); - - s = currentElement.text(); - if (s.isNull()) - s = ""; - m_doc->addUsage(s); + m_compability.addUserdefinedUsage(currentElement.text()); } } + foreach ( QString usage, m_compability.documentUsages() ) { + m_doc->addUsage(usage); + } + return true; } @@ -820,7 +816,7 @@ bool KEduVocKvtmlReader::readExpressionChildAttributes( QDomElement &domElementE QString &synonym, QString &example, QString &antonym, - QString &usage, + QSet &usages, QString ¶phrase) { int pos; @@ -928,12 +924,16 @@ bool KEduVocKvtmlReader::readExpressionChildAttributes( QDomElement &domElementE example = attribute.value(); ///@todo usages -/* - usage = ""; + attribute = domElementExpressionChild.attributeNode(KV_USAGE); if (!attribute.isNull()) { + kDebug() << "Read usages: " << attribute.value(); + usages = m_compability.usageFromKvtml1(attribute.value()); + + /* usage = attribute.value(); + if (usage.length() != 0 && usage.left(1) == UL_USER_USAGE) { int num = qMin(usage.mid (1, 40).toInt(), 1000); // paranioa check @@ -950,8 +950,8 @@ bool KEduVocKvtmlReader::readExpressionChildAttributes( QDomElement &domElementE } m_doc->setUsageDescriptions(sl); } - } - }*/ + }*/ + } paraphrase = ""; attribute = domElementExpressionChild.attributeNode(KV_PARAPHRASE); @@ -1037,7 +1037,7 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent) QString synonym; QString example; QString antonym; - QString usage; + QSet usage; QString paraphrase; QList conjug; KEduVocComparison comparison; @@ -1257,11 +1257,8 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent) expr.translation(i).setSynonym (synonym); if (!example.isEmpty() ) expr.translation(i).setExample (example); -///@todo enable reading of usages into a qstringlist -/* if (!usage.isEmpty() ) - expr.translation(i).setUsageLabel (usage); -*/ + expr.translation(i).setUsages (usage); if (!paraphrase.isEmpty() ) expr.translation(i).setParaphrase (paraphrase); if (!antonym.isEmpty() ) diff --git a/keduvocdocument/keduvockvtmlreader.h b/keduvocdocument/keduvockvtmlreader.h index 3ed7c33..3a7f67b 100644 --- a/keduvocdocument/keduvockvtmlreader.h +++ b/keduvocdocument/keduvockvtmlreader.h @@ -28,6 +28,7 @@ #include "keduvocgrade.h" #include "keduvocgrammar.h" #include "keduvocmultiplechoice.h" +#include "keduvockvtmlcompability.h" class QIODevice; class KEduVocDocument; @@ -70,7 +71,7 @@ public: QString &synonym, QString &example, QString &antonym, - QString &usage, + QSet &usage, QString ¶phrase); bool readExpression(QDomElement &domElementParent); bool readBody(QDomElement &domElementParent); @@ -84,6 +85,8 @@ private: int m_cols; int m_lines; QStringList m_oldSelfDefinedTypes; + + KEduVocKvtmlCompability m_compability; }; #endif -- 2.47.3