]> Git trees. - libqmvoc.git/commitdiff
changed multiple choice class to support any number of choices, fleshed out kvtml2rea...
authorJeremy Paul Whiting <jpwhiting@kde.org>
Thu, 19 Jul 2007 05:02:28 +0000 (05:02 +0000)
committerJeremy Paul Whiting <jpwhiting@kde.org>
Thu, 19 Jul 2007 05:02:28 +0000 (05:02 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=689767

kdeeducore/keduvockvtml2reader.cpp
kdeeducore/keduvockvtml2reader.h
kdeeducore/keduvockvtmlreader.cpp
kdeeducore/keduvockvtmlwriter.cpp
kdeeducore/keduvocmultiplechoice.cpp
kdeeducore/keduvocmultiplechoice.h

index 7e943ab1e9f8e651a38e34f2fbca62b0d2a8f5e0..01024269c8a99ca9d5adc4abce7a6699f4849954 100644 (file)
@@ -921,64 +921,31 @@ bool KEduVocKvtml2Reader::readComparison(QDomElement &domElementParent, KEduVocC
 }
 
 
-bool KEduVocKvtml2Reader::readMultipleChoice(QDomElement &domElementParent, KEduVocMultipleChoice &mc)
+bool KEduVocKvtml2Reader::readMultipleChoice(QDomElement &multipleChoiceElement, KEduVocMultipleChoice &mc)
 /*
  <multiplechoice>
-   <mc1>good</mc1>
-   <mc2>better</mc2>
-   <mc3>best</mc3>
-   <mc4>best 2</mc4>
-   <mc5>best 3</mc5>
+   <choice>good</choice>
+   <choice>better</choice>
+   <choice>best</choice>
+   <choice>best 2</choice>
+   <choice>best 3</choice>
  </multiplechoice>
 */
 
 {
+  QDomElement currentElement;
   QString s;
   mc.clear();
 
-  QDomElement currentElement;
-
-  currentElement = domElementParent.firstChildElement(KV_MC_1);
-  if (!currentElement.isNull()) {
-    s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC1(s);
-  }
-
-  currentElement = domElementParent.firstChildElement(KV_MC_2);
-  if (!currentElement.isNull()) {
-    s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC2(s);
-  }
-
-  currentElement = domElementParent.firstChildElement(KV_MC_3);
-  if (!currentElement.isNull()) {
-    s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC3(s);
-  }
-
-  currentElement = domElementParent.firstChildElement(KV_MC_4);
-  if (!currentElement.isNull()) {
-    s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC4(s);
-  }
-
-  currentElement = domElementParent.firstChildElement(KV_MC_5);
-  if (!currentElement.isNull()) {
-    s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC5(s);
+  QDomNodeList choiceNodes = multipleChoiceElement.elementsByTagName(KVTML_CHOICE);
+  for (int i = 0; i < choiceNodes.count(); ++i)
+  {
+    currentElement = choiceNodes.item(i).toElement();
+    if (currentElement.parentNode() == multipleChoiceElement)
+    {
+      mc.appendChoice(currentElement.text());
+    }
   }
-
-  mc.normalize();
   return true;
 }
 
index d00034e385c4313650ed37d563ef9345719e81cb..6e243b0a6dbc1f215f6719eee369e348c14e2c8d 100644 (file)
 class QIODevice;
 class KEduVocDocument;
 
-// internal types, indented are subtypes
-
-#define QM_VERB           "v"    // go
-#define   QM_VERB_IRR     "ir"
-#define   QM_VERB_REG     "re"
-#define QM_NOUN           "n"    // table, coffee
-#define   QM_NOUN_F       "f"
-#define   QM_NOUN_M       "m"
-#define   QM_NOUN_S       "s"
-#define QM_NAME           "nm"
-#define QM_ART            "ar"   // article
-#define   QM_ART_DEF      "def"  // definite    a/an
-#define   QM_ART_IND      "ind"  // indefinite  the
-#define QM_ADJ            "aj"   // adjective   expensive, good
-#define QM_ADV            "av"   // adverb      today, strongly
-#define QM_PRON           "pr"   // pronoun     you, she
-#define   QM_PRON_POS     "pos"  // possessive  my, your
-#define   QM_PRON_PER     "per"  // personal
-#define QM_PHRASE         "ph"
-#define QM_NUM            "num"  // numeral
-#define   QM_NUM_ORD      "ord"  // ordinal     first, second
-#define   QM_NUM_CARD     "crd"  // cardinal    one, two
-#define QM_INFORMAL       "ifm"
-#define QM_FIG            "fig"
-#define QM_CON            "con"  // conjuncton  and, but
-#define QM_PREP           "pre"  // preposition behind, between
-#define QM_QUEST          "qu"   // question    who, what
-
-// type delimiters
-
-#define QM_USER_TYPE  "#"   // designates number of user type
-#define QM_TYPE_DIV   ":"   // divide main from subtype
-
-// usage delimiters (also declared in UsageManager.h)
-
-#define UL_USER_USAGE  "#"   // designates number of user type
-
 /**
 * @brief class to read kvtml2 data files into keduvocdocument
-@author Jeremy Whiting
+@author Jeremy Whiting
 */
 class KEduVocKvtml2Reader : public QObject
 {
index c59ceb3c62876704df1adf9d93bf4a15b8ac5c6d..7a161f2e5ff8776fd31deb419c96c394e5f11b1f 100644 (file)
@@ -777,44 +777,33 @@ bool KEduVocKvtmlReader::readMultipleChoice(QDomElement &domElementParent, KEduV
   currentElement = domElementParent.firstChildElement(KV_MC_1);
   if (!currentElement.isNull()) {
     s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC1(s);
+    mc.appendChoice(s);
   }
 
   currentElement = domElementParent.firstChildElement(KV_MC_2);
   if (!currentElement.isNull()) {
     s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC2(s);
+    mc.appendChoice(s);
   }
 
   currentElement = domElementParent.firstChildElement(KV_MC_3);
   if (!currentElement.isNull()) {
     s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC3(s);
+    mc.appendChoice(s);
   }
 
   currentElement = domElementParent.firstChildElement(KV_MC_4);
   if (!currentElement.isNull()) {
     s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC4(s);
+    mc.appendChoice(s);
   }
 
   currentElement = domElementParent.firstChildElement(KV_MC_5);
   if (!currentElement.isNull()) {
     s = currentElement.text();
-    if (s.isNull())
-      s = "";
-    mc.setMC5(s);
+    mc.appendChoice(s);
   }
 
-  mc.normalize();
   return true;
 }
 
index 8ec658112512cb301f3b5c55e8cea7dbf7c04b0f..7b751b78531301553de64d7eeb3d32fa47bbc4c7 100644 (file)
@@ -591,46 +591,46 @@ bool KEduVocKvtmlWriter::writeMultipleChoice(QDomDocument &domDoc, QDomElement &
 
   QDomElement domElementMC = domDoc.createElement(KV_MULTIPLECHOICE_GRP);
 
-  if (!mc.mc1().isEmpty() )
+  if (!mc.choice(1).isEmpty() )
   {
     QDomElement domElementMC1 = domDoc.createElement(KV_MC_1);
-    QDomText domTextMC1 = domDoc.createTextNode(mc.mc1());
+    QDomText domTextMC1 = domDoc.createTextNode(mc.choice(1));
 
     domElementMC1.appendChild(domTextMC1);
     domElementMC.appendChild(domElementMC1);
   }
 
-  if (!mc.mc2().isEmpty() )
+  if (!mc.choice(2).isEmpty() )
   {
     QDomElement domElementMC2 = domDoc.createElement(KV_MC_2);
-    QDomText domTextMC2 = domDoc.createTextNode(mc.mc2());
+    QDomText domTextMC2 = domDoc.createTextNode(mc.choice(2));
 
     domElementMC2.appendChild(domTextMC2);
     domElementMC.appendChild(domElementMC2);
   }
 
-  if (!mc.mc3().isEmpty() )
+  if (!mc.choice(3).isEmpty() )
   {
     QDomElement domElementMC3 = domDoc.createElement(KV_MC_3);
-    QDomText domTextMC3 = domDoc.createTextNode(mc.mc3());
+    QDomText domTextMC3 = domDoc.createTextNode(mc.choice(3));
 
     domElementMC3.appendChild(domTextMC3);
     domElementMC.appendChild(domElementMC3);
   }
 
-  if (!mc.mc4().isEmpty() )
+  if (!mc.choice(4).isEmpty() )
   {
     QDomElement domElementMC4 = domDoc.createElement(KV_MC_4);
-    QDomText domTextMC4 = domDoc.createTextNode(mc.mc4());
+    QDomText domTextMC4 = domDoc.createTextNode(mc.choice(4));
 
     domElementMC4.appendChild(domTextMC4);
     domElementMC.appendChild(domElementMC4);
   }
 
-  if (!mc.mc5().isEmpty() )
+  if (!mc.choice(5).isEmpty() )
   {
     QDomElement domElementMC5 = domDoc.createElement(KV_MC_5);
-    QDomText domTextMC5 = domDoc.createTextNode(mc.mc5());
+    QDomText domTextMC5 = domDoc.createTextNode(mc.choice(5));
 
     domElementMC5.appendChild(domTextMC5);
     domElementMC.appendChild(domElementMC5);
index 13611d993d1e89ab768f36fa745f3fbca67b7449..75a79e41d0aff0fa8130bbbfede709ecc2c58fef 100644 (file)
 #include "keduvocmultiplechoice.h"
 
 KEduVocMultipleChoice::KEduVocMultipleChoice
-  (const QString &mc1, const QString &mc2, const QString &mc3, const QString &mc4,const QString &mc5)
+  (const QStringList &choices)
+  : m_choices(choices)
 {
-   setMC1 (mc1);
-   setMC2 (mc2);
-   setMC3 (mc3);
-   setMC4 (mc4);
-   setMC5 (mc5);
 }
 
 
 bool KEduVocMultipleChoice::isEmpty() const
 {
-  return muc1.simplified().isEmpty() &&
-         muc2.simplified().isEmpty() &&
-         muc3.simplified().isEmpty() &&
-         muc4.simplified().isEmpty() &&
-         muc5.simplified().isEmpty();
+  return m_choices.isEmpty();
 }
 
 
 void KEduVocMultipleChoice::clear()
 {
-   muc1 = "";
-   muc2 = "";
-   muc3 = "";
-   muc4 = "";
-   muc5 = "";
+  m_choices.clear();
 }
 
 
-QString KEduVocMultipleChoice::mc (unsigned idx) const
+QString KEduVocMultipleChoice::choice (int index) const
 {
-   switch (idx) {
-     case 0: return muc1;
-     case 1: return muc2;
-     case 2: return muc3;
-     case 3: return muc4;
-     case 4: return muc5;
-   }
-   return "";
+  QString choice;
+  if (m_choices.size() >= index)
+  {
+    choice = m_choices[index];
+  }
+  return choice;
 }
 
-
-unsigned KEduVocMultipleChoice::size()
+void KEduVocMultipleChoice::setChoice(int index, const QString &s)
 {
-   normalize();
-   unsigned num = 0;
-   if (!muc1.isEmpty())
-     ++num;
-   if (!muc2.isEmpty())
-     ++num;
-   if (!muc3.isEmpty())
-     ++num;
-   if (!muc4.isEmpty())
-     ++num;
-   if (!muc5.isEmpty())
-     ++num;
-   return num;
+  while (m_choices.size() < index)
+  {
+    m_choices.append(QString());
+  }
+  m_choices[index] = s;
 }
 
-
-void KEduVocMultipleChoice::normalize()
+unsigned KEduVocMultipleChoice::size()
 {
-  // fill from first to last
-
-  if (muc1.isEmpty()) {
-    muc1 = muc2;
-    muc2 = "";
-  }
-
-  if (muc2.isEmpty()) {
-    muc2 = muc3;
-    muc3 = "";
-  }
-
-  if (muc3.isEmpty()) {
-    muc3 = muc4;
-    muc4 = "";
-  }
-
-  if (muc4.isEmpty()) {
-    muc4 = muc5;
-    muc5 = "";
-  }
-
+  return m_choices.size();
 }
 
-
 bool KEduVocMultipleChoice::operator==(const KEduVocMultipleChoice &choice) const
 {
-  return muc1 == choice.muc1 &&
-    muc2 == choice.muc2 &&
-    muc3 == choice.muc3 &&
-    muc4 == choice.muc4 &&
-    muc5 == choice.muc5;
-}
-
-void KEduVocMultipleChoice::setMC1(const QString &s) 
-{ 
-       muc1 = s; 
-}
-
-void KEduVocMultipleChoice::setMC2(const QString &s) 
-{ 
-       muc2 = s; 
+  return m_choices == choice.m_choices;
 }
 
-void KEduVocMultipleChoice::setMC3(const QString &s) 
+void KEduVocMultipleChoice::appendChoice(const QString &s) 
 { 
-       muc3 = s; 
+  m_choices.append(s);
 }
-
-void KEduVocMultipleChoice::setMC4(const QString &s) 
-{ 
-    muc4 = s; 
-}
-
-void KEduVocMultipleChoice::setMC5(const QString &s) 
-{ 
-    muc5 = s; 
-}
-
-QString KEduVocMultipleChoice::mc1() const 
-{ 
-    return muc1; 
-}
-
-QString KEduVocMultipleChoice::mc2() const 
-{ 
-    return muc2; 
-}
-
-QString KEduVocMultipleChoice::mc3() const 
-{ 
-    return muc3; 
-}
-
-QString KEduVocMultipleChoice::mc4() const 
-{ 
-    return muc4; 
-}
-
-QString KEduVocMultipleChoice::mc5() const 
-{ 
-       return muc5; 
-}
-
index 6263ed3203b656741c24a4326e0530760cd53c5f..bce9622219139e8bab3a5a1ce4ee281d8e3f38c6 100644 (file)
@@ -30,6 +30,7 @@
 #include "libkdeedu_core_export.h"
 
 #include <QtCore/QString>
+#include <QtCore/QStringList>
 
 #define MAX_MULTIPLE_CHOICE  5  // select one out of x
 
@@ -40,24 +41,17 @@ public:
 
    KEduVocMultipleChoice() {}
 
-   KEduVocMultipleChoice (const QString &mc1, const QString &mc2, const QString &mc3, const QString &mc4, const QString &mc5);
+   KEduVocMultipleChoice (const QStringList &choices);
+   
+   void setChoices (const QStringList &choices);
+   QStringList choices() const;
 
-   void setMC1 (const QString &s);
-   void setMC2 (const QString &s);
-   void setMC3 (const QString &s);
-   void setMC4 (const QString &s);
-   void setMC5 (const QString &s);
+   void appendChoice (const QString &s);
 
-   QString mc1 () const;
-   QString mc2 () const;
-   QString mc3 () const;
-   QString mc4 () const;
-   QString mc5 () const;
-
-   QString mc (unsigned idx) const;
+   void setChoice(int index, const QString &s);
+   QString choice (int index) const;
 
    bool isEmpty() const;
-   void normalize();
    void clear();
    unsigned size();
 
@@ -65,7 +59,7 @@ public:
 
 protected:
 
-   QString  muc1, muc2, muc3, muc4, muc5;
+   QStringList m_choices;
 };