]> Git trees. - libqmvoc.git/commitdiff
Add complete support for READING old kvtml usage labels.
authorFrederik Gladhorn <gladhorn@kde.org>
Tue, 28 Aug 2007 13:30:15 +0000 (13:30 +0000)
committerFrederik Gladhorn <gladhorn@kde.org>
Tue, 28 Aug 2007 13:30:15 +0000 (13:30 +0000)
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
keduvocdocument/keduvockvtmlcompability.cpp [new file with mode: 0644]
keduvocdocument/keduvockvtmlcompability.h [new file with mode: 0644]
keduvocdocument/keduvockvtmlreader.cpp
keduvocdocument/keduvockvtmlreader.h

index 9f816f422ae77c2d4bf80ec731d85a0329731d30..96a1af714d819699930dc8299bd458a267c41ff8 100644 (file)
@@ -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 (file)
index 0000000..0a2fc2a
--- /dev/null
@@ -0,0 +1,114 @@
+/***************************************************************************
+
+    C++ Implementation: keduvockvtml1compability_p
+
+    -----------------------------------------------------------------------
+
+    begin         : Di Aug 28 2007
+
+    copyright     : (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+
+    -----------------------------------------------------------------------
+
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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<QString> KEduVocKvtmlCompability::usageFromKvtml1(const QString & oldUsage) const
+{
+    QSet<QString> 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<QString>::fromList(m_usages.values());
+}
+
+
diff --git a/keduvocdocument/keduvockvtmlcompability.h b/keduvocdocument/keduvockvtmlcompability.h
new file mode 100644 (file)
index 0000000..4ef8c9c
--- /dev/null
@@ -0,0 +1,85 @@
+/***************************************************************************
+
+    C++ Interface: keduvockvtml1compability_p
+
+    -----------------------------------------------------------------------
+
+    begin         : Di Aug 28 2007
+
+    copyright     : (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+
+    -----------------------------------------------------------------------
+
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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 <QtCore/QString>
+#include <QtCore/QMap>
+#include <QtCore/QSet>
+
+#include <KLocalizedString>
+
+/**
+  * @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<QString> usageFromKvtml1( const QString& oldUsage ) const;
+
+    QSet<QString> 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
index a3fc37cf9c6a83e0d99267c4475a36953b83061f..28cde178f9865408c6ef046d7b1ceb9331faa15e 100644 (file)
@@ -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<QString> &usages,
                                                         QString &paraphrase)
 {
   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<QString>             usage;
   QString                   paraphrase;
   QList<KEduVocConjugation> 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() )
index 3ed7c3359a1d0788f27f05ca8f6e423131fd4071..3a7f67bb4ec0098472ee84b2ba0cede12ed76c74 100644 (file)
@@ -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<QString> &usage,
                                      QString &paraphrase);
   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