]> Git trees. - libqmvoc.git/commitdiff
Adapted the old import/export classes of the WQL format for working with the new...
authorEric Pignet <eric@erixpage.com>
Sun, 10 Jul 2005 19:11:34 +0000 (19:11 +0000)
committerEric Pignet <eric@erixpage.com>
Sun, 10 Jul 2005 19:11:34 +0000 (19:11 +0000)
Yet to do: handle special characters, selected cells, row heights (these datas don't exist in the new document
class).

Fixed the undo mechanism.

svn path=/branches/work/kwordquiz/src/keduvocwqlwriter.h; revision=433409

kwordquiz/keduvocdocument.cpp
kwordquiz/keduvocdocument.h
kwordquiz/keduvockvtmlreader.cpp
kwordquiz/keduvocwqlreader.cpp [new file with mode: 0644]
kwordquiz/keduvocwqlreader.h [new file with mode: 0644]
kwordquiz/keduvocwqlwriter.cpp [new file with mode: 0644]
kwordquiz/keduvocwqlwriter.h [new file with mode: 0644]
kwordquiz/prefleitner.cpp

index ebd681a12afc2e3258a2edb7bdb87e313b5a349e..c2798a68ca14917de8ff0fe6d0ca946df389653a 100644 (file)
@@ -37,6 +37,8 @@ using namespace std;
 
 #include "keduvockvtmlwriter.h"
 #include "keduvockvtmlreader.h"
+#include "keduvocwqlwriter.h"
+#include "keduvocwqlreader.h"
 #include "leitnersystem.h"
 //#include "prefs.h"
 
@@ -52,6 +54,7 @@ KEduVocDocument::KEduVocDocument(QObject *parent)
 
 KEduVocDocument::~KEduVocDocument()
 {
+  delete font;
 }
 
 
@@ -87,9 +90,12 @@ void KEduVocDocument::Init ()
   doc_url.setFileName(i18n("Untitled"));
   doctitle = "";
   author = "";
+  doc_remark = "";
+  doc_version = "";
+  font = NULL;
 
   activeLeitnerSystem = false;
-  leitnerSystem = 0;
+  leitnerSystem = NULL;
 }
 
 
@@ -125,6 +131,13 @@ bool KEduVocDocument::open(const KURL& url, bool append)
         }
         break;
 
+        case wql:
+        {
+          KEduVocWqlReader wqlReader(&f);
+          read = wqlReader.readDoc(this);
+        }
+        break;
+
         case vt_lex:
         {
           QTextStream is (&f);
@@ -190,6 +203,8 @@ bool KEduVocDocument::saveAs(QObject *parent, const KURL & url, FileType ft, con
   {
     if (tmp.path().right(strlen("." KVTML_EXT)) == "." KVTML_EXT)
       ft = kvtml;
+    else if (tmp.path().right(strlen("." WQL_EXT)) == "." WQL_EXT)
+      ft = wql;
     else if (tmp.path().right(strlen("." VT5_LEX_EXT)) == "." VT5_LEX_EXT)
       ft = vt_lex;
     else if (tmp.path().right(strlen("." VCB_EXT)) == "." VCB_EXT)
@@ -223,6 +238,12 @@ bool KEduVocDocument::saveAs(QObject *parent, const KURL & url, FileType ft, con
       }
       break;
 
+      case wql: {
+        KEduVocWqlWriter wqlWriter(&f);
+        saved = wqlWriter.writeDoc(this);
+      }
+      break;
+
       case vt_lex: {
         QTextStream os( &f );                       // serialize using f
         //TODO saved = saveToLex(os, title);
@@ -857,6 +878,12 @@ QString KEduVocDocument::getDocRemark() const
 }
 
 
+QFont* KEduVocDocument::getFont() const
+{
+  return font;
+}
+
+
 void KEduVocDocument::setTitle(const QString & title)
 {
   doctitle = title.stripWhiteSpace();
@@ -881,6 +908,13 @@ void KEduVocDocument::setDocRemark(const QString & s)
 }
 
 
+void KEduVocDocument::setFont(QFont* font)
+{
+  delete this->font;
+  this->font = font;
+}
+
+
 int KEduVocDocument::search(QString substr, int id,
                          int first, int last,
                          bool word_start,
@@ -995,6 +1029,9 @@ KEduVocDocument::FileType KEduVocDocument::detectFT(const QString &filename)
    if (c1 == '<' && c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l')
      return kvtml;
 
+   if (line == WQL_IDENT)
+     return wql;
+
    if (line.find (VCB_SEPARATOR) >= 0)
      return vt_vcb;
 
index af0aac36b985fdf736cd166b88651ac0b0c19c52..5e91379646d8981552174f3fdee0f7a121d82e01 100644 (file)
@@ -24,7 +24,7 @@
 #endif
 
 #include <qobject.h>
-
+#include <qfont.h>
 #include <kurl.h>
 
 #include "keduvocexpression.h"
 #define KV_CONJ_COMMON "common"      // female contains common for all three
 
 #define LEX_IDENT_50   "Vocabulary Trainer V5.0"
+#define WQL_IDENT      "WordQuiz"
 
 #define KVTML_EXT        "kvtml"
 #define VT5_LEX_EXT      "lex"
 #define KVL_EXT          "vl"
 #define CSV_EXT          "csv"
 #define TXT_EXT          "txt"
+#define WQL_EXT          "wql"
 
 #define VCB_SEPARATOR    "__"
 
@@ -254,11 +256,13 @@ class KEduVocDocument : public QObject
   Q_OBJECT
   friend class KEduVocKvtmlWriter;
   friend class KEduVocKvtmlReader;
+  friend class KEduVocWqlReader;
  
  public:
 
   enum FileType { kvd_none, automatic,
                   kvtml,
+                  wql,
                   kvtbin,
                   vt_lex, vt_vcb, csv /*, kvoclearn, qvocab*/ };
 
@@ -298,6 +302,11 @@ class KEduVocDocument : public QObject
    */
   void removeEntry (int index);
 
+  /** removes all entries from doc (clears vocabulary)
+   */
+  inline void removeAllEntries ()
+    { vocabulary.clear(); dirty = true; }
+
   /** sorts vocabulary alphabetically
    *
    * @param  index            index expression
@@ -500,6 +509,9 @@ class KEduVocDocument : public QObject
   /** returns remark of file  */
   QString getDocRemark() const;
 
+  /** returns font  */
+  QFont* getFont() const;
+
   inline void getQueryLang(QString &org, QString &trans) const
     { org = queryorg; trans = querytrans; }
 
@@ -518,6 +530,9 @@ class KEduVocDocument : public QObject
   /** sets remark of file  */
   void setDocRemark(const QString & rem);
 
+  /** sets font  */
+  void setFont(QFont *font);
+
   /** gets version of loaded file  */
   void getVersion(int &major, int &minor, int &patch);
 
@@ -661,6 +676,8 @@ protected:
   int                    current_lesson;
   vector<int>            extraSizehints;
   vector<int>            sizehints;
+  QFont*                 font;
+
   QString                generator;
   QString                queryorg,
                          querytrans;
@@ -679,8 +696,8 @@ protected:
   vector<Article>        articles;
   vector<Conjugation>    conjugations;
 
-  LeitnerSystem*        leitnerSystem;
-  bool                  activeLeitnerSystem;
+  LeitnerSystem*            leitnerSystem;
+  bool                          activeLeitnerSystem;
 };
 
 
index a6567c8b6993b29d310b627543e3bd5053237502..bade16ff0c32f756c29043dce93405950e586ac7 100644 (file)
@@ -1068,24 +1068,24 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent)
   //-----------
   // Attributes
 
-    if (!readExpressionChildAttributes( domElementExpressionChild,
-                                        lang,
-                                        grade,  r_grade,
-                                        qcount, r_qcount,
-                                        qdate,  r_qdate,
-                                        remark,
-                                        bcount, r_bcount,
-                                        query_id,
-                                        pronunce,
-                                        width,
-                                        type,
-                                        faux_ami_t,
-                                        faux_ami_f,
-                                        synonym,
-                                        example,
-                                        antonym,
-                                        usage,
-                                        paraphrase))
+  if (!readExpressionChildAttributes( domElementExpressionChild,
+                                      lang,
+                                      grade,  r_grade,
+                                      qcount, r_qcount,
+                                      qdate,  r_qdate,
+                                      remark,
+                                      bcount, r_bcount,
+                                      query_id,
+                                      pronunce,
+                                      width,
+                                      type,
+                                      faux_ami_t,
+                                      faux_ami_f,
+                                      synonym,
+                                      example,
+                                      antonym,
+                                      usage,
+                                      paraphrase))
     return false;
 
 
diff --git a/kwordquiz/keduvocwqlreader.cpp b/kwordquiz/keduvocwqlreader.cpp
new file mode 100644 (file)
index 0000000..62fc3eb
--- /dev/null
@@ -0,0 +1,161 @@
+/***************************************************************************
+                     read a KEduVocDocument from a WQL file
+    -----------------------------------------------------------------------
+    copyright            : (C) 2004 Peter Hedlung
+                           (C) 2005 The KDE-EDU team
+    email                : peter@peterandlinda.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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 <kapplication.h>
+#include <kdebug.h>
+#include <klocale.h>
+#include <kmessagebox.h>
+
+#include <qdom.h>
+#include <qtextstream.h>
+
+#include "keduvocwqlreader.h"
+#include "keduvocdocument.h"
+
+KEduVocWqlReader::KEduVocWqlReader(QFile *file)
+{
+  // the file must be already open
+  m_inputFile = file;
+}
+
+KEduVocWqlReader::~KEduVocWqlReader()
+{
+}
+
+bool KEduVocWqlReader::readDoc(KEduVocDocument *doc)
+{
+  m_doc = doc;
+    
+  QTextStream inputStream(m_inputFile);
+  inputStream.setEncoding(QTextStream::Latin1);
+
+  QString s = "";
+  s=inputStream.readLine();
+  if (s != "WordQuiz")
+  {
+    KMessageBox::error(0, i18n("This does not appear to be a (K)WordQuiz file") + s);
+    return false;
+  }
+  s = inputStream.readLine();
+  s = s.left(1);
+  int iFV = s.toInt(0);
+  if (iFV != 5)
+  {
+    KMessageBox::error(0, i18n("KWordQuiz can only open files created by WordQuiz 5.x"));
+    return false;
+  }
+  // TODO these loops cause crashes if the input file is invalid !
+  while (inputStream.readLine() != "[Font Info]");
+  s = inputStream.readLine();
+  int p = s.find("=", 0);
+  QString fam = s.right(s.length() - (p + 1));
+  fam = fam.mid(1, fam.length() - 2);
+  //g->font().setFamily(s);
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  int ps = s.toInt(0);
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  int b = 0;
+  if (s == "1")
+  {
+    b = QFont::Bold;
+  }
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  bool it = (s == "1");
+
+  m_doc->setFont(new QFont(fam, ps, b, it));
+
+/* TODO
+  while (inputStream.readLine() != "[Character Info]");  
+  s = inputStream.readLine();    
+  p = s.find("=", 0);
+  m_specialCharacters = s.right(s.length() - (p + 1));
+*/
+  while (inputStream.readLine() != "[Grid Info]");
+  inputStream.readLine(); //skip value for width of row headers
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_doc->setSizeHint(0, s.toInt());
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_doc->setSizeHint(1, s.toInt());
+
+/* TODO
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_numRows = (s.toInt() - 1); //We need to reduce by one since the header is not included
+  // Selection
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_topLeft =s.toInt(0, 10) - 1;
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_topRight =s.toInt(0, 10) - 1;
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_bottomLeft =s.toInt(0, 10) - 1;
+
+  s = inputStream.readLine();
+  p = s.find("=", 0);
+  s = s.right(s.length() - (p + 1));
+  m_bottomRight =s.toInt(0, 10) - 1 ;
+*/
+  while ((inputStream.readLine() != "[Vocabulary]"));
+
+  s = inputStream.readLine();
+  p = s.find("   [", 0);
+  s = s.left(p);
+  s = s.stripWhiteSpace();
+  m_doc->langs.push_back(s);
+  m_doc->langs.push_back(inputStream.readLine());
+
+  while (!s.isNull())
+  {
+    s = inputStream.readLine();
+    p = s.find("[", 0);
+    QString r = s.mid(p + 1, 10);
+    int h = r.toInt();
+    s = s.left(p);
+    s = s.stripWhiteSpace();
+    
+    QString b;
+    b = inputStream.readLine();
+    
+    KEduVocExpression expr = KEduVocExpression(s);
+    expr.setTranslation(1, b);
+    m_doc->appendEntry(&expr);
+  }
+  return true;
+}
diff --git a/kwordquiz/keduvocwqlreader.h b/kwordquiz/keduvocwqlreader.h
new file mode 100644 (file)
index 0000000..bb6a805
--- /dev/null
@@ -0,0 +1,40 @@
+/***************************************************************************
+                     read a KEduVocDocument from a WQL file
+    -----------------------------------------------------------------------
+    copyright            : (C) 2004 Peter Hedlund
+                         : (c) 2005 The KDE-EDU team
+    email                : peter@peterandlinda.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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 KEDUVOCWQLREADER_H
+#define KEDUVOCWQLREADER_H
+
+#include <qfile.h>
+
+#include "keduvocdocument.h"
+
+class KEduVocDocument;
+
+class KEduVocWqlReader : public QObject
+{
+public:
+       KEduVocWqlReader(QFile *file);
+       ~KEduVocWqlReader();
+
+       bool readDoc(KEduVocDocument *doc);
+
+private:
+  QFile *m_inputFile;
+  KEduVocDocument *m_doc;
+};
+
+#endif
diff --git a/kwordquiz/keduvocwqlwriter.cpp b/kwordquiz/keduvocwqlwriter.cpp
new file mode 100644 (file)
index 0000000..f780d38
--- /dev/null
@@ -0,0 +1,127 @@
+/***************************************************************************
+                   export a KEduVocDocument to a WQL file
+    -----------------------------------------------------------------------
+    copyright            : (C) 2004 Peter Hedlung
+                           (C) 2005 The KDE-EDU team
+    email                : peter@peterandlinda.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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 <kdebug.h>
+#include <qdom.h>
+#include <qtextstream.h>
+#include <qfont.h>
+
+#include "keduvocwqlwriter.h"
+#include "keduvocdocument.h"
+
+#define winendl "\r\n"
+
+KEduVocWqlWriter::KEduVocWqlWriter(QFile *file)
+{
+  // the file must be already open
+  m_outputFile = file;
+}
+
+KEduVocWqlWriter::~KEduVocWqlWriter()
+{
+}
+
+bool KEduVocWqlWriter::writeDoc(KEduVocDocument *doc)
+{
+  m_doc = doc;
+
+  m_outputStream.setDevice(m_outputFile);
+  m_outputStream.setEncoding(QTextStream::Latin1);
+
+  m_outputStream << "WordQuiz" << winendl;
+  m_outputStream << "5.9.0" << winendl << winendl;      
+
+  if (doc->getFont() != NULL)
+    writeFont(*(doc->getFont()));
+  // TODO writeCharacters(Prefs::specialCharacters());
+  // TODO Find the vertical header width, handle the case where there are not exactly two languages
+  writeGridInfo(10, m_doc->getSizeHint(0), m_doc->getSizeHint(1), m_doc->numEntries());
+  /* TODO Find the selected cells
+  if (g->numSelections() > 0)
+  {
+    QTableSelection qts = g->selection(0);
+    writeSelection(qts.leftCol(), qts.topRow(), qts.rightCol(), qts.bottomRow());
+  }
+  else
+  {
+    writeSelection(g->currentColumn(), g->currentRow(), g->currentColumn(), g->currentRow());
+  }*/
+  writeFirstItem(m_doc->getOriginalIdent(), m_doc->getIdent(1));
+  int r = m_doc->numEntries();
+  for (int w=0; w < r; w++)
+  {
+    // TODO Find the row height (g->rowHeight(w))
+    writeItem(m_doc->getEntry(w)->getOriginal(), m_doc->getEntry(w)->getTranslation(1), 30);
+  }
+}
+
+void KEduVocWqlWriter::writeFont( const QFont & font )
+{
+  m_outputStream << "[Font Info]" << winendl;
+  m_outputStream << "FontName1=\"" << font.family() << "\"" << winendl;
+  m_outputStream << "FontSize1=" << QString::number(font.pointSize()) << winendl;
+  m_outputStream << QString("FontBold1=%1").arg(font.bold() ? "1" : "0") <<winendl;
+  m_outputStream << QString("FontItalic1=%1").arg(font.italic() ? "1" : "0") <<winendl;
+  m_outputStream << "FontColor1=0" << winendl;
+  m_outputStream << "CharSet1=0" << winendl;
+  m_outputStream << "Layout1=0" << winendl;
+  
+  m_outputStream << "FontName2=\"" << font.family() << "\"" << winendl;
+  m_outputStream << "FontSize2=" << QString::number(font.pointSize()) << winendl;
+  m_outputStream << QString("FontBold2=%1").arg(font.bold() ? "1" : "0") <<winendl;
+  m_outputStream << QString("FontItalic2=%1").arg(font.italic() ? "1" : "0") <<winendl;
+  m_outputStream << "FontColor2=0" << winendl;
+  m_outputStream << "CharSet2=0" << winendl;
+  m_outputStream << "Layout2=0" << winendl << winendl;
+}
+
+void KEduVocWqlWriter::writeCharacters( const QString & s )
+{
+  m_outputStream << "[Character Info]" << winendl;
+  m_outputStream << "Characters1=" << s << winendl;
+  m_outputStream << "Characters2=" << s << winendl << winendl;
+}
+
+void KEduVocWqlWriter::writeGridInfo( int col0, int col1, int col2, int numRows )
+{
+  m_outputStream << "[Grid Info]" << winendl;
+  m_outputStream << "ColWidth0=" << QString::number(col0) << winendl;
+  m_outputStream << "ColWidth1=" << QString::number(col1) << winendl;
+  m_outputStream << "ColWidth2=" << QString::number(col2) << winendl;
+  m_outputStream << "RowCount=" << QString::number(numRows + 1) << winendl; //Add one for the header
+}
+
+void KEduVocWqlWriter::writeSelection( int lc, int tr, int rc, int br ) //part of [Grid Info]
+{
+  m_outputStream << "SelLeft=" << QString::number(lc + 1) << winendl;
+  m_outputStream << "SelTop=" << QString::number(tr + 1) << winendl;
+  m_outputStream << "SelRight=" << QString::number(rc + 1) << winendl;
+  m_outputStream << "SelBottom=" << QString::number(br + 1) << winendl << winendl;
+}
+
+void KEduVocWqlWriter::writeFirstItem( const QString & ll, const QString & rl )
+{
+  m_outputStream << "[Vocabulary]" << winendl;
+  m_outputStream << ll  << "   [0000000300]" << winendl;
+  m_outputStream << rl  << winendl;
+}
+
+void KEduVocWqlWriter::writeItem( const QString & left, const QString & right, int rh )
+{
+  m_outputStream << left  << QString( "   [%1]").arg(rh * 15, 10, 10 ) << winendl ;
+  m_outputStream << right << winendl;
+}
\ No newline at end of file
diff --git a/kwordquiz/keduvocwqlwriter.h b/kwordquiz/keduvocwqlwriter.h
new file mode 100644 (file)
index 0000000..ea3b10c
--- /dev/null
@@ -0,0 +1,47 @@
+/***************************************************************************
+                   export a KEduVocDocument to a WQL file
+    -----------------------------------------------------------------------
+    copyright            : (C) 2004 Peter Hedlung
+                           (C) 2005 The KDE-EDU team
+    email                : peter@peterandlinda.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   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 KEDUVOCWQLWRITER_H
+#define KEDUVOCWQLWRITER_H
+
+#include <qfile.h>
+#include <qfont.h>
+
+class KEduVocDocument;
+
+class KEduVocWqlWriter
+{
+public:
+  KEduVocWqlWriter(QFile *file);
+  ~KEduVocWqlWriter();
+
+  bool writeDoc(KEduVocDocument *doc);
+
+  void writeFont(const QFont & font);
+  void writeCharacters(const QString & s);
+  void writeGridInfo(int col0, int col1, int col2, int numRows);
+  void writeSelection(int lc, int tr, int rc, int br);
+  void writeFirstItem(const QString &ll, const QString &rl);
+  void writeItem(const QString &left, const QString &right, int rh);
+
+private:
+  QFile *m_outputFile;
+  QTextStream m_outputStream;
+  KEduVocDocument *m_doc;
+};
+
+#endif
index efe024db3e0a9f9395bd48284b5acf64ea89f9fa..6efd7a93900d6852df7ce4441b1ed7c32093aa1f 100644 (file)
@@ -145,4 +145,4 @@ LeitnerSystem* PrefLeitner::getSystem()
        return m_selectedSystem;
 }
 
-#include "prefleitner.moc"
+//#include "prefleitner.moc"