Merge branch 'master' of ssh://drop.maemo.org/git/mdictionary
authorBartosz Szatkowski <bulislaw@linux.com>
Fri, 20 Aug 2010 10:03:53 +0000 (12:03 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Fri, 20 Aug 2010 10:03:53 +0000 (12:03 +0200)
Conflicts:
trunk/src/base/base.pro

14 files changed:
data/dicts/eng-thai.xdxf
data/dicts/eng-us.xdxf
trunk/src/base/base.pro
trunk/src/base/gui/MainWindow.cpp
trunk/src/base/gui/MainWindow.h
trunk/src/base/gui/TranslationWidget.cpp
trunk/src/base/gui/TranslationWidget.h
trunk/src/base/xsl.xsl [new file with mode: 0644]
trunk/src/base/xslt2.qrc [new file with mode: 0644]
trunk/src/plugins/xdxf/src/TranslationXdxf.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/tests/dict.xdxf
trunk/src/plugins/xdxf/tests/test.cpp
trunk/src/plugins/xdxf/tests/test.h

index d519da3..353129f 100644 (file)
@@ -19,6 +19,8 @@ sib</ar>
 nueng-muen</ar>
 <ar><k>100</k>
 nueng-roy</ar>
+<ar><k>Aluminum</k>
+Aluminium to kolejne tlumaczenie z innego slownika</ar>
 <ar><k>100,000</k>
 nueng-saen</ar>
 <ar><k>101</k>
index 220c3e3..5b1a879 100644 (file)
@@ -10,7 +10,7 @@ Medium Wave</ar>
 <ar><k>Airplane</k>
 Aeroplane</ar>
 <ar><k>Aluminum</k>
-Aluminium</ar>
+Aluminium <tr>wymowa</tr> <ex>przyklad</ex> <c c="#FF00FF"> <b> test koloru i pogrobienia </b> </c> </ar>
 <ar><k>Apartment</k>
 Flatlane</ar>
 <ar><k>Appetizer</k>
@@ -302,7 +302,7 @@ Tartlane</ar>
 <ar><k>airplane</k>
 Aeroplane</ar>
 <ar><k>aluminum</k>
-Aluminium</ar>
+Aluminium to ala ma kota a kot ma ale Aluminium to ala ma kota a kot ma alAluminium to ala ma kota a kot ma alAluminium to ala ma kota a kot ma al</ar>
 <ar><k>amrtment</k>
 Medium Wave</ar>
 <ar><k>anesthesia</k>
index 1272aef..48ff83e 100644 (file)
@@ -1,10 +1,16 @@
 # -------------------------------------------------
 # Project created by QtCreator 2010-08-03T08:54:27
-# -------------------------------------------------
+#-------------------------------------------------
+
 QT += core \
     gui \
-    sql
+    sql \
+    xml \
+    xmlpatterns
+
 maemo5:QT += maemo5
+
+
 TARGET = mdictionary
 TEMPLATE = app
 MDICT_BINDIR = $$[MDICT_BINDIR]
@@ -27,8 +33,11 @@ SOURCES += gui/main.cpp \
     gui/SettingsWidget.cpp \
     gui/BookmarksWidget.cpp \
     gui/WelcomeScreenWidget.cpp \
-    gui/AboutWidget.cpp
+    gui/AboutWidget.cpp \
+    gui/TranslationTextEdit.cpp
+
 HEADERS += gui/MainWindow.h \
+
     gui/AboutWidget.h \
     gui/SearchBarWidget.h \
     gui/WordListWidget.h \
@@ -51,8 +60,11 @@ HEADERS += gui/MainWindow.h \
     gui/SettingsWidget.h \
     gui/BookmarksWidget.h \
     gui/WelcomeScreenWidget.h \
-    ../includes/Notify.h
+    ../includes/Notify.h \
+    gui/TranslationTextEdit.h
+
 FORMS += gui/MainWindow.ui
+
 RESOURCES += gui/gui.qrc
 unix { 
     # VARIABLES
@@ -81,3 +93,5 @@ unix {
     desktop.files += ../../../data/other/$${TARGET}.desktop
     icon64.files += ../../../data/icons/64x64/$${TARGET}.png
 }
+
+RESOURCES += xslt2.qrc
index a9a70d9..fac9d25 100644 (file)
@@ -249,6 +249,12 @@ QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
     return backbone->getDictionaries();
 }
 
+
+void MainWindow::search(QString word) {
+    setExactSearch(false);
+    searchBarWidget->search(word);
+}
+
 void MainWindow::searchExact(QString word) {
     setExactSearch(true);
     //searching with searchBar, not directly by emiting searchWordList(),
@@ -448,6 +454,15 @@ void MainWindow::connectTranslationWidget() {
     connect(this, SIGNAL(showTranslation(QStringList)),
             translationWidget, SLOT(show(QStringList)));
 
+     #ifdef Q_WS_MAEMO_5
+        connect(translationWidget, SIGNAL(search(QString)),
+                this, SLOT(search(QString)));
+    #else
+        connect(translationWidget, SIGNAL(search(QString)),
+                this, SLOT(searchExact(QString)));
+    #endif
+
+
 }
 
 void MainWindow::connectDictManager() {
index 7f570b0..d475ad6 100644 (file)
@@ -124,6 +124,14 @@ public:
     void searchExact(QString);
 
 
+    //! Search for given word
+    /*!
+      \param word which will be searched in dictionaries
+      */
+    void search(QString);
+
+
+
     //! Gets word list from backbone and prepares received list to display
     /*!
       Checks if received list is empty, in that case displays suitable
index 04c7565..8b2e564 100644 (file)
 #include "TranslationWidget.h"
 #include "TranslationWidgetAutoResizer.h"
 #include <QDebug>
+#ifdef Q_WS_MAEMO_5
+    #include <QtGui/QX11Info>
+    #include <X11/Xlib.h>
+    #include <X11/Xatom.h>
+#endif
 
 TranslationWidget::TranslationWidget(QWidget *parent):
     QScrollArea(parent) {
@@ -34,8 +39,12 @@ TranslationWidget::TranslationWidget(QWidget *parent):
     setWindowFlags(windowFlags() | Qt::Window);
 
     initializeUI();
-
+    //Q_INIT_RESOURCE(xslt);
     setWindowTitle(tr("Translation"));
+
+    connect(textEdit, SIGNAL(search()),
+            this, SLOT(searchSelected()));
+
 }
 
 
@@ -45,7 +54,12 @@ void TranslationWidget::show() {
 
 void TranslationWidget::show(QStringList translations) {
 
-    show();
+    showMaximized();
+
+    #ifdef Q_WS_MAEMO_5
+        if(!buttonsInitialized)
+            initButtons();
+    #endif
 
     textEdit->clear();
 
@@ -55,7 +69,12 @@ void TranslationWidget::show(QStringList translations) {
         trans += t + "\n";
     }
 
-    textEdit->setPlainText(trans);
+  //  qDebug()<<trans;
+    trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
+
+    trans=XslConversion(trans);
+    textEdit->insertHtml(trans);
+  //  textEdit->setPlainText(trans);
 
     textEdit->repaint(this->rect());
 
@@ -64,6 +83,24 @@ void TranslationWidget::show(QStringList translations) {
     emit updateSize();
 }
 
+QString TranslationWidget::XslConversion(QString translation)
+{
+    QXmlQuery myQuery(QXmlQuery::XSLT20);
+    myQuery.setFocus(translation);
+//    qDebug()<<translation;
+    QFile file(":/xsl/xsl.xsl");
+    if(!file.open(QFile::ReadOnly)){
+        qDebug()<<"can't open a xslt file";
+        return translation;
+    }
+    QString xslt;
+    xslt=file.readAll();
+    myQuery.setQuery(xslt);
+    QString result("");
+    myQuery.evaluateTo(&result);
+    return result;
+}
+
 void TranslationWidget::initializeUI() {
 
     zoomInToolButton = new QToolButton;
@@ -71,12 +108,98 @@ void TranslationWidget::initializeUI() {
 
     zoomOutToolButton = new QToolButton;
     zoomOutToolButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
+#ifdef Q_WS_MAEMO_5
+void TranslationWidget::initButtons() {
+
+        int x = width() - showButtonsButton->sizeHint().width();
+        int y = height() - showButtonsButton->sizeHint().height();
+
+        showButtonsButton->move(QPoint(x,y));
+        showButtonsButton->show();
+        showButtonsButton->setCheckable(true);
+//==================================================================
+
+        x = width() - zoomOutButton->sizeHint().width();
+        y = height() - 2*zoomOutButton->sizeHint().height();
+        zoomOutButton->move(QPoint(x, height()));
+
+        zoomOutButtonAnimation =
+                new QPropertyAnimation(zoomOutButton, "pos", this);
+
+        zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
+        zoomOutButtonAnimation->setEndValue(QPoint(x,y));
+        zoomOutButtonAnimation->setDuration(200);
+        zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
+//==================================================================
+        x = width() - zoomInButton->sizeHint().width();
+        y = height() - 3*zoomInButton->sizeHint().height();
+        zoomInButton->move(QPoint(x, height()));
+
+        zoomInButtonAnimation =
+                new QPropertyAnimation(zoomInButton, "pos", this);
+
+        zoomInButtonAnimation->setStartValue(QPoint(x, height()));
+        zoomInButtonAnimation->setEndValue(QPoint(x,y));
+        zoomInButtonAnimation->setDuration(400);
+        zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
+//==================================================================
+        x = 0;
+        y = height() - copyButton->sizeHint().height();
+
+        copyButton->move(QPoint(x, height()));
+
+        copyButtonAnimation =
+                new QPropertyAnimation(copyButton, "pos", this);
+
+        copyButtonAnimation->setStartValue(QPoint(x, height()));
+        copyButtonAnimation->setEndValue(QPoint(x,y));
+        copyButtonAnimation->setDuration(200);
+        copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
+//==================================================================
+        x = 0;
+        y = height() - 2*copyButton->sizeHint().height();
+
+        selectAllButton->move(QPoint(x, height()));
+
+        selectAllButtonAnimation =
+                new QPropertyAnimation(selectAllButton, "pos", this);
+
+        selectAllButtonAnimation->setStartValue(QPoint(x, height()));
+        selectAllButtonAnimation->setEndValue(QPoint(x,y));
+        selectAllButtonAnimation->setDuration(400);
+        selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
+//==================================================================
+        x = 0;
+        y = height() - 3*copyButton->sizeHint().height();
+
+        searchButton->move(QPoint(x, height()));
+
+        searchButtonAnimation =
+                new QPropertyAnimation(searchButton, "pos", this);
+
+        searchButtonAnimation->setStartValue(QPoint(x, height()));
+        searchButtonAnimation->setEndValue(QPoint(x,y));
+        searchButtonAnimation->setDuration(600);
+        searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
+//==================================================================
+
+        buttonsAnimation = new QParallelAnimationGroup(this);
+        buttonsAnimation->addAnimation(zoomInButtonAnimation);
+        buttonsAnimation->addAnimation(zoomOutButtonAnimation);
+        buttonsAnimation->addAnimation(selectAllButtonAnimation);
+        buttonsAnimation->addAnimation(copyButtonAnimation);
+        buttonsAnimation->addAnimation(searchButtonAnimation);
+        buttonsInitialized = true;
+        buttonsVisible = false;
+
+        connect(showButtonsButton, SIGNAL(toggled(bool)),
+                this, SLOT(showButtons(bool)));
+}
+#endif
 
-   // horizontalLayout = new QHBoxLayout;
-    //horizontalLayout->addWidget(zoomInToolButton);
-   // horizontalLayout->addWidget(zoomOutToolButton);
+void TranslationWidget::initializeUI() {
 
-    textEdit = new QTextEdit;
+    textEdit = new TranslationTextEdit;
     textEdit->setReadOnly(true);
 
     resizer = new TranslationWidgetAutoResizer(textEdit);
@@ -94,43 +217,122 @@ void TranslationWidget::initializeUI() {
     this->setWidget(w);
     this->setWidgetResizable(true);
 
-    connect(zoomInToolButton, SIGNAL(clicked()),
-            textEdit, SLOT(zoomIn()));
-
-    connect(zoomInToolButton, SIGNAL(clicked()),
-            this, SIGNAL(updateSize()));
-
-    connect(zoomOutToolButton, SIGNAL(clicked()),
-            textEdit, SLOT(zoomOut()));
-
-    connect(zoomInToolButton, SIGNAL(clicked()),
-            this, SIGNAL(updateSize()));
-
-   /* #ifdef Q_WS_MAEMO_5
-        fullScreenButton = new QToolButton(this);
-        fullScreenButton->setIcon(QIcon::fromTheme("general_fullsize"));
-        fullScreenButton->setMinimumSize(fullScreenButton->sizeHint());
-        int x = QApplication::desktop()->screenGeometry(this).width()  -
-                fullScreenButton->sizeHint().width();
-        int y = QApplication::desktop()->screenGeometry(this).height() -
-                fullScreenButton->sizeHint().height();
-        fullScreenButton->move(QPoint(x,y));
-        fullScreenButton->show();
-        fullScreenButton->setWindowOpacity(0.5);
-
-
-        backButton = new QToolButton(this);
-        backButton->setIcon(QIcon::fromTheme("general_overlay_back"));
-        backButton->setMinimumSize(fullScreenButton->sizeHint());
-        x = QApplication::desktop()->screenGeometry(this).width()  -
-                backButton->sizeHint().width();
-        y = 0;
-        backButton->move(QPoint(x,y));
-        backButton->show();
-        backButton->setWindowOpacity(0.5);
-
-        connect(backButton, SIGNAL(clicked()),
-                this, SLOT(hide()));
-    #endif*/
+
+    #ifdef Q_WS_MAEMO_5
+        zoomInButton = new QToolButton(this);
+        zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
+        zoomInButton->setMinimumSize(zoomInButton->sizeHint());
+
+        zoomOutButton = new QToolButton(this);
+        zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
+        zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
+
+        selectAllButton = new QToolButton(this);
+        selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
+        selectAllButton->setMinimumSize(selectAllButton->sizeHint());
+
+        copyButton = new QToolButton(this);
+        copyButton->setIcon(QIcon::fromTheme("general_notes"));
+        copyButton->setMinimumSize(copyButton->sizeHint());
+        copyButton->setEnabled(false);
+
+        searchButton = new QToolButton(this);
+        searchButton->setIcon(QIcon::fromTheme("general_search"));
+        searchButton->setMinimumSize(searchButton->sizeHint());
+        searchButton->setEnabled(false);
+
+        showButtonsButton = new QToolButton(this);
+        showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
+        showButtonsButton->setMinimumSize(searchButton->sizeHint());
+
+        connect(zoomInButton, SIGNAL(clicked()),
+                textEdit, SLOT(zoomIn()));
+
+        connect(zoomOutButton, SIGNAL(clicked()),
+                textEdit, SLOT(zoomOut()));
+
+
+        connect(searchButton, SIGNAL(clicked()),
+                this, SLOT(searchSelected()));
+
+        connect(copyButton, SIGNAL(clicked()),
+                textEdit, SLOT(copy()));
+
+        connect(textEdit, SIGNAL(copyAvailable(bool)),
+                searchButton, SLOT(setEnabled(bool)));
+
+        connect(textEdit, SIGNAL(copyAvailable(bool)),
+                copyButton, SLOT(setEnabled(bool)));
+
+        connect(selectAllButton, SIGNAL(clicked()),
+                textEdit, SLOT(selectAll()));
+
+        buttonsInitialized = false;
+
+
+
+        grabZoomKeys(true);
+    #endif
 }
 
+void TranslationWidget::searchSelected() {
+    #ifdef Q_WS_MAEMO_5
+        hide();
+    #endif
+    emit search(textEdit->textCursor().selectedText());
+}
+
+#ifdef Q_WS_MAEMO_5
+void TranslationWidget::showButtons(bool show) {
+    if(show && !buttonsVisible) {
+        buttonsAnimation->setDirection(QAbstractAnimation::Forward);
+        buttonsAnimation->start();
+        buttonsVisible = true;
+    }
+    else if(!show && buttonsVisible) {
+        buttonsAnimation->setDirection(QAbstractAnimation::Backward);
+        buttonsAnimation->start();
+        buttonsVisible = false;
+    }
+}
+
+void TranslationWidget::grabZoomKeys(bool grab) {
+     if (!winId()) {
+         return;
+     }
+
+    unsigned long val = (grab) ? 1 : 0;
+    Atom atom = XInternAtom(QX11Info::display(),
+                            "_HILDON_ZOOM_KEY_ATOM", False);
+    if (!atom) {
+        return;
+    }
+
+    XChangeProperty (QX11Info::display(),
+         winId(),
+         atom,
+         XA_INTEGER,
+         32,
+         PropModeReplace,
+         reinterpret_cast<unsigned char *>(&val),
+         1);
+}
+
+void TranslationWidget::keyPressEvent(QKeyEvent* event) {
+    switch (event->key()) {
+        case Qt::Key_F7:
+        textEdit->zoomIn();
+        event->accept();
+        break;
+
+        case Qt::Key_F8:
+        textEdit->zoomOut();
+        event->accept();
+        break;
+    }
+    QWidget::keyPressEvent(event);
+}
+#endif
+
+
+
index 957f243..05f61ec 100644 (file)
 #include <QtGui>
 #include "../backbone/backbone.h"
 #include "TranslationWidgetAutoResizer.h"
+#include <QtXml>
+#include <QXmlQuery>
+#include "TranslationTextEdit.h"
+#include <QPropertyAnimation>
 
 //! Displays translation of word found in dictionaries
 /*!
@@ -43,6 +47,8 @@ public:
 Q_SIGNALS:
     void updateSize();
 
+    void search(QString);
+
 public Q_SLOTS:
     //! Show widget.
     void show();
@@ -50,21 +56,48 @@ public Q_SLOTS:
     //! Request to show translation of word list passed as parameter.
     void show(QStringList);
 
+protected:
+    #ifdef Q_WS_MAEMO_5
+        void keyPressEvent(QKeyEvent *);
+    #endif
+
+private Q_SLOTS:
+    void searchSelected();
+    #ifdef Q_WS_MAEMO_5
+        void showButtons(bool);
+    #endif
 
 private:
-    QTextEdit *textEdit;
+    QString XslConversion(QString translation);
     QToolButton* zoomInToolButton;
     QToolButton* zoomOutToolButton;
+    TranslationTextEdit *textEdit;
     #ifdef Q_WS_MAEMO_5
-        QToolButton* fullScreenButton;
-        QToolButton* backButton;
+        QToolButton* zoomInButton;
+        QToolButton* zoomOutButton;
+        QToolButton* selectAllButton;
+        QToolButton* copyButton;
+        QToolButton* searchButton;
+        QToolButton* showButtonsButton;
+
+        QPropertyAnimation* zoomInButtonAnimation;
+        QPropertyAnimation* zoomOutButtonAnimation;
+        QPropertyAnimation* copyButtonAnimation;
+        QPropertyAnimation* selectAllButtonAnimation;
+        QPropertyAnimation* searchButtonAnimation;
+        QAnimationGroup* buttonsAnimation;
+
+        void initButtons();
+        bool buttonsInitialized;
+        bool buttonsVisible;
+
+        void grabZoomKeys(bool);
     #endif
     QVBoxLayout *verticalLayout;
     QHBoxLayout* horizontalLayout;
     TranslationWidgetAutoResizer* resizer;
 
     void initializeUI();
-
 };
 
 #endif // TRANSLATIONWIDGET_H
diff --git a/trunk/src/base/xsl.xsl b/trunk/src/base/xsl.xsl
new file mode 100644 (file)
index 0000000..ce50c46
--- /dev/null
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+<xsl:template match="/">
+       <html>
+       <body>
+       <table border="1" WIDTH="100%">
+       <tr bgcolor="#99CCFF">
+               <th><xsl:value-of select="ar/dict[1]/key"/></th>
+       </tr>
+       <xsl:for-each select="ar/dict"> 
+               <!-- <xsl:variable name="path" select="info/@path"/>-->
+               <tr bgcolor="#99FF99">
+                       <th><xsl:value-of select="info"/> <!-- <img src="{$path}"/> --></th>
+               </tr>
+               <xsl:for-each select="t">
+                       <tr>
+                       <td><xsl:apply-templates/></td>
+                       </tr>
+               </xsl:for-each> 
+       </xsl:for-each>
+       </table>
+       </body>
+       </html>
+</xsl:template> 
+
+<xsl:template match="tr">
+       [<i><xsl:apply-templates/></i>]
+</xsl:template> 
+
+<xsl:template match="pot">
+       ,<i><xsl:apply-templates/></i>
+</xsl:template>
+
+<xsl:template match="tence">
+       ,<i><xsl:apply-templates/></i>
+</xsl:template>
+
+<xsl:template match="def">
+       ,(<xsl:apply-templates/>)
+</xsl:template>
+
+<xsl:template match="ex">
+       ,"<xsl:apply-templates/>"
+</xsl:template>
+
+<xsl:template match="c">
+       <xsl:variable name="atr" select="@c"/>
+       <font color="{$atr}"> <xsl:apply-templates/> </font>
+</xsl:template>
+
+</xsl:stylesheet> 
diff --git a/trunk/src/base/xslt2.qrc b/trunk/src/base/xslt2.qrc
new file mode 100644 (file)
index 0000000..94c710a
--- /dev/null
@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/xsl">
+        <file>xsl.xsl</file>
+    </qresource>
+</RCC>
index 1c9fa34..c0ae5d9 100644 (file)
@@ -38,7 +38,11 @@ QString TranslationXdxf::dictionaryInfo() const {
 }
 
 QString TranslationXdxf::toHtml() const {
-    return xdxfPlugin->search(_key);
+    QString result("");
+//    qDebug()<<xdxfPlugin->search(_key);
+    result+="<dict>" + _dictionaryInfo + "<key>" + _key + "</key>"  +xdxfPlugin->search(_key) + "</dict>";
+//    qDebug()<<result;
+    return result;
 }
 
 void TranslationXdxf::setKey(QString _key) {
index 807937e..b4fdb5a 100644 (file)
@@ -87,28 +87,37 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
-            word+="%";
         word = word.toLower();
         word = word.replace("*", "%");
         word = word.replace("?", "_");
         word = removeAccents(word);
-        qDebug() << word;
+        //qDebug() << word;
 
         QSqlQuery cur(db);
-        cur.prepare("select word from dict where word like ? limit ?");
+        if(limit !=0)
+            cur.prepare("select word from dict where word like ? limit ?");
+        else
+            cur.prepare("select word from dict where word like ?");
         cur.addBindValue(word);
-        cur.addBindValue(limit);
+        if(limit !=0)
+            cur.addBindValue(limit);
         cur.exec();
-        while(cur.next())
-            translations.insert(new TranslationXdxf(
+        while(cur.next()){
+            bool ok=true;
+            Translation *tran;
+            foreach(tran,translations) {
+                if(tran->key().toLower()==cur.value(0).toString().toLower())
+                        ok=false;
+            }
+            if(ok)  /*add key word to list*/
+                translations.insert(new TranslationXdxf(
                         cur.value(0).toString().toLower(),
                         _infoNote, this));
-        return translations.toList();
+        }
+        db.close();
+       return translations.toList();
 }
 
-
-
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
@@ -125,22 +134,22 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
         return translations.toList();
     }
 
-    QXmlStreamReader dictionaryReader(&dictionaryFile);
+    QXmlStreamReader reader(&dictionaryFile);
     /*search words list*/
     QString a;
     int i=0;
-    while(!dictionaryReader.atEnd() && !stopped){
-        dictionaryReader.readNextStartElement();
-        if(dictionaryReader.name()=="ar"){
-            while(dictionaryReader.name()!="k" && !dictionaryReader.atEnd())
-                dictionaryReader.readNextStartElement();
-            if(!dictionaryReader.atEnd())
-                a = dictionaryReader.readElementText();
+    while(!reader.atEnd() && !stopped){
+        reader.readNextStartElement();
+        if(reader.name()=="ar") {
+            while(reader.name()!="k" && !reader.atEnd())
+                reader.readNextStartElement();
+            if(!reader.atEnd())
+                a = reader.readElementText();
             if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
-                    if(tran->key()==a)
+                    if(tran->key().toLower()==a.toLower())
                         ok=false;  /*if key word is in the dictionary more that one */
                 }
                 if(ok)  /*add key word to list*/
@@ -165,10 +174,8 @@ QString XdxfPlugin::search(QString key) {
     return searchFile(key);
 }
 
-
-
 QString XdxfPlugin::searchCache(QString key) {
-    QString result;
+    QString result("");
     QString cacheFilePath = _settings->value("cache_path");
     db.setDatabaseName(cacheFilePath);
     key = key.toLower();
@@ -179,18 +186,19 @@ QString XdxfPlugin::searchCache(QString key) {
     }
 
     QSqlQuery cur(db);
-    cur.prepare("select translation from dict where word like ? limit 1");
+//    cur.prepare("select translation from dict where word like ? limit 1");
+    cur.prepare("select translation from dict where word like ?");
     cur.addBindValue(key);
     cur.exec();
-    if(cur.next())
-        result = cur.value(0).toString();
+//  if(cur.next())
+    while(cur.next())
+//      result = cur.value(0).toString();
+        result += cur.value(0).toString();
+    db.close();
     return result;
 
 }
 
-
-
-
 QString XdxfPlugin::searchFile(QString key) {
     key = key.toLower();
     QFile dictionaryFile(path);
@@ -199,69 +207,48 @@ QString XdxfPlugin::searchFile(QString key) {
         qDebug()<<"Error: could not open file";
         return "";
     }
-    QXmlStreamReader dictionaryReader(&dictionaryFile);
+    QXmlStreamReader reader(&dictionaryFile);
 
 
     QString a;
 
     bool match =false;
     stopped = false;
-    while (!dictionaryReader.atEnd()&& !stopped) {
-        dictionaryReader.readNext();
-        if(dictionaryReader.tokenType() == QXmlStreamReader::StartElement) {
-            if(dictionaryReader.name()=="k") {
-                a = dictionaryReader.readElementText();
-                if(a==key)
+    while (!reader.atEnd()&& !stopped) {
+        reader.readNext();
+        if(reader.tokenType() == QXmlStreamReader::StartElement) {
+            if(reader.name()=="k") {
+                a = reader.readElementText();
+                if(a.toLower()==key.toLower())
                     match = true;
             }
         }
-        else if(dictionaryReader.tokenType() == QXmlStreamReader::Characters) {
-            if(match) {
- /*             QString temp(dictionaryReader.text().toString());
-                if(temp=="\n")
-                    temp=dictionaryReader.readElementText();
-                temp.replace("\n","");
-                if(temp == ""){
-                    int i=0;
-                    while(dictionaryReader.name()!="ar"&&
-                                !dictionaryReader.atEnd()){
-                        dictionaryReader.readNext();
-                        if(dictionaryReader.name()!="" &&
-                                         dictionaryReader.name()!="ar") {
-                            if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
-                                temp+=tr("</");
-                            if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
-                                temp+=tr("<");
-                            qDebug()<<dictionaryReader.tokenType();
-                            temp=temp+dictionaryReader.name().toString() + tr(">");
-                            i++;
-                        }
-                        temp+=dictionaryReader.text().toString();
-                    }
+        if(match) {
+            QString temp("");
+            while(reader.name()!="ar" && !reader.atEnd()) {
+                if(reader.name()!="" && reader.name()!="k") {
+                    if(reader.tokenType()==QXmlStreamReader::EndElement)
+                        temp+=tr("</");
+                    if(reader.tokenType()==QXmlStreamReader::StartElement)
+                        temp+=tr("<");
+                    temp+=reader.name().toString();
+                    if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
+                       temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
+                    temp+=tr(">");
                 }
-                resultString+=temp.replace("\n","")+"\n";
-                match=false;
-*/
-                QString temp("");
-                while(dictionaryReader.name()!="ar" && !dictionaryReader.atEnd()) {
-                    if(dictionaryReader.name()!="") {
-                        if(dictionaryReader.tokenType()==QXmlStreamReader::EndElement)
-                            temp+=tr("</");
-                        if(dictionaryReader.tokenType()==QXmlStreamReader::StartElement)
-                            temp+=tr("<");
-                        temp=temp+dictionaryReader.name().toString() + tr(">");
-                    }
-                    temp+= dictionaryReader.text().toString();
-                    dictionaryReader.readNext();
-                }
-                resultString+=tr("<t>") + temp.replace("\n","") + tr("</t>");
-                match=false;
+                temp+= reader.text().toString();
+                reader.readNext();
             }
+            if(temp.at(0)==QChar('\n'))
+                temp.remove(0,1);
+            resultString+=tr("<t>") + temp + tr("</t>");    //.replace("\n","")
+            match=false;
         }
         this->thread()->yieldCurrentThread();
     }
     stopped=false;
     dictionaryFile.close();
+
     return resultString;
 }
 
@@ -279,7 +266,6 @@ void XdxfPlugin::setPath(QString path){
     //getDictionaryInfo();
 }
 
-
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
     if(settings){
@@ -292,7 +278,8 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
 
         plugin->db_name = plugin->_settings->value("type")
                + plugin->_settings->value("path");
-        plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
+ //       if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
+            plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
 
         if(settings->value("cached").isEmpty() &&
            settings->value("generateCache") == "true") {
@@ -345,7 +332,6 @@ void XdxfPlugin::setSettings(Settings *settings) {
     emit settingsChanged();
 }
 
-
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
@@ -353,20 +339,29 @@ void XdxfPlugin::getDictionaryInfo() {
         return;
     }
 
-    QXmlStreamReader dictionaryReader(&dictionaryFile);
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="xdxf") {
-      if(dictionaryReader.attributes().hasAttribute("lang_from"))
-        _langFrom = dictionaryReader.attributes().value("lang_from").toString();
-      if(dictionaryReader.attributes().hasAttribute("lang_to"))
-        _langTo = dictionaryReader.attributes().value("lang_to").toString();
+    QXmlStreamReader reader(&dictionaryFile);
+    reader.readNextStartElement();
+    if(reader.name()=="xdxf") {
+      if(reader.attributes().hasAttribute("lang_from"))
+        _langFrom = reader.attributes().value("lang_from").toString();
+      if(reader.attributes().hasAttribute("lang_to"))
+        _langTo = reader.attributes().value("lang_to").toString();
     }
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="full_name")
-        _name=dictionaryReader.readElementText();
-    dictionaryReader.readNextStartElement();
-    if(dictionaryReader.name()=="description")
-        _infoNote=dictionaryReader.readElementText();
+    reader.readNextStartElement();
+    if(reader.name()=="full_name")
+        _name=reader.readElementText();
+    reader.readNextStartElement();
+    if(reader.name()=="description")
+        _infoNote=reader.readElementText();
+
+    QString format = "png";
+    QString initialPath = QDir::currentPath() + tr("/xdxf.") + format;
+//    qDebug()<<initialPath;
+//    qDebug()<<QPixmap(":/icons/xdxf.png").save(initialPath,format.toAscii());
+
+    _infoNote="<info path=\""+initialPath+"\">"+"\n" + _name + "(" + _type + ")"  + "</info>";
+
+
 
     dictionaryFile.close();
 }
@@ -419,8 +414,6 @@ int XdxfPlugin::countWords() {
     return wordsCount;
 }
 
-
-
 bool XdxfPlugin::makeCache(QString dir) {
     cachingDialog->setVisible(true);
     QCoreApplication::processEvents();
@@ -474,35 +467,37 @@ bool XdxfPlugin::makeCache(QString dir) {
                 match = true;
             }
         }
-        else if(reader.tokenType() == QXmlStreamReader::Characters) {
-             if(match) {
-                QString temp("");
-                while(reader.name()!="ar" && !reader.atEnd()) {
-                    if(reader.name()!="") {
-                        if(reader.tokenType()==QXmlStreamReader::EndElement)
-                            temp+=tr("</");
-                        if(reader.tokenType()==QXmlStreamReader::StartElement)
-                            temp+=tr("<");
-                        temp=temp+reader.name().toString() + tr(">");
-                    }
-                    temp+= reader.text().toString();
-                    reader.readNext();
-                }
-                temp=tr("<t>") + temp.replace("\n","") + tr("</t>");
-                match = false;
-                cur.prepare("insert into dict values(?,?)");
-                cur.addBindValue(a);
-                cur.addBindValue(temp);
-                cur.exec();
-                counter++;
-                int prog = counter*100/_wordsCount;
-                if(prog % 5 == 0 && lastProg != prog) {
-                    Q_EMIT updateCachingProgress(prog,
-                                                 timer.restart());
-                    lastProg = prog;
+        if(match) {
+            QString temp("");
+            while(reader.name()!="ar" && !reader.atEnd()) {
+                if(reader.name()!="" && reader.name()!="k") {
+                    if(reader.tokenType()==QXmlStreamReader::EndElement)
+                        temp+=tr("</");
+                    if(reader.tokenType()==QXmlStreamReader::StartElement)
+                        temp+=tr("<");
+                    temp+=reader.name().toString();
+                    if(reader.name().toString()=="c" && reader.tokenType()==QXmlStreamReader::StartElement)
+                       temp= temp + tr(" c=\"") + reader.attributes().value(tr("c")).toString() + tr("\"");
+                    temp+=tr(">");
                 }
+                temp+= reader.text().toString();
+                reader.readNext();
+            }
+            if(temp.at(0)==QChar('\n'))
+                temp.remove(0,1);
+            temp=tr("<t>") + temp+ tr("</t>");  //.replace("\n","")
+            match=false;
+            cur.prepare("insert into dict values(?,?)");
+            cur.addBindValue(a);
+            cur.addBindValue(temp);
+            cur.exec();
+            counter++;
+            int prog = counter*100/_wordsCount;
+            if(prog % 5 == 0 && lastProg != prog) {
+                Q_EMIT updateCachingProgress(prog,
+                                             timer.restart());
+                lastProg = prog;
             }
-
         }
     }
 
@@ -513,12 +508,15 @@ bool XdxfPlugin::makeCache(QString dir) {
     cachingDialog->setVisible(false);
 
     if(!cur.next() || countWords() != cur.value(0).toInt())
+    {
+        db.close();
         return false;
+    }
     _settings->setValue("cache_path", cachePathN);
     _settings->setValue("cached", "true");
 
+    db.close();
     return true;
 }
 
-
 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)
index d1b7d73..acd02f0 100644 (file)
@@ -19,6 +19,5 @@ sto</ar>
 tysišc</ar>
 <ar><k>house</k>
 dom</ar>
-<ar><k>test</k>
-<pos>krowa</pos>test01</ar>
+<ar><k>test</k><c c="FF00FF">kro</c>test01<pos>krowa</pos></ar>
 </xdxf>
index 77b7d9d..69c8f5b 100644 (file)
 #include <QSignalSpy>
 
 void XdxfTest::getNew() {
+
     XdxfPlugin xdxfPlugin(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
-
     CommonDictInterface *xdxfPlugin2 = xdxfPlugin.getNew(settings);
+
     QList<Translation*> te6=xdxfPlugin2->searchWordList("*",8);
-        QCOMPARE(te6.size(),8);
+    QCOMPARE(te6.size(),8);
 }
 
-void XdxfTest::search() {
-    /*test for English-Polish dictionary */
+void XdxfTest::searchFile() {
+
     XdxfPlugin xdxfPluginB(this);
+    Settings *settings=new Settings;
+    settings->setValue("path","../tests/dict.xdxf");
+    CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
 
+    QCOMPARE(xdxfPlugin->search("."), QString("<t>kropka</t>"));
+    QCOMPARE(xdxfPlugin->search("1"), QString("<t>one</t>"));
+    QCOMPARE(xdxfPlugin->search("test"), QString("<t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
+}
 
+void XdxfTest::makeCache()
+{
+    XdxfPlugin xdxfPluginB(this);
+    Settings *settings=new Settings;
+    settings->setValue("path","../tests/dict.xdxf");
+    settings->setValue("generateCache", "true");
+    CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
+    QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
+}
 
+void XdxfTest::searchCache() {
+    XdxfPlugin xdxfPluginB(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
+    settings->setValue("generateCache", "true");
     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
 
-    xdxfPlugin->search("wino");
+    QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
     QCOMPARE(xdxfPlugin->search("."), QString("<t>kropka</t>"));
     QCOMPARE(xdxfPlugin->search("1"), QString("<t>one</t>"));
-
-    QCOMPARE(xdxfPlugin->search("test"), QString("<t><pos>krowa</pos>test01</t>"));
+    QCOMPARE(xdxfPlugin->search("test"), QString("<t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
 }
 
-void XdxfTest::searchWordList() {
-     /*test for English-Polish dictionary */
+void XdxfTest::searchWordListCache(){
     XdxfPlugin xdxfPluginB(this);
-
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
+    settings->setValue("generateCache", "true");
     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
+
     QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
     if(te.size()>0)
         QCOMPARE(te.size(), 9);
@@ -88,14 +107,54 @@ void XdxfTest::searchWordList() {
 
     QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
         QCOMPARE(te11.at(0)->key(), QString("house"));
+}
+
+void XdxfTest::searchWordListFile() {
+
+    XdxfPlugin xdxfPluginB(this);
+    Settings *settings=new Settings;
+    settings->setValue("path","../tests/dict.xdxf");
+    CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
 
+    QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
+    if(te.size()>0)
+        QCOMPARE(te.size(), 9);
+    QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
+    QCOMPARE(te2.size(), 5);
+
+    QList<Translation*> te3 = xdxfPlugin->searchWordList("1", 2);
+        QCOMPARE(te3.size(), 2);
+
+    QList<Translation*> te4 = xdxfPlugin->searchWordList("ho*SE", 10);
+        QCOMPARE(te4.at(0)->key(), QString("house"));
+
+    QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
+        QCOMPARE(te5.size(), 9);
+
+    QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
+        QCOMPARE(te6.size(), 8);
+
+    QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
+        QCOMPARE(te7.at(0)->key(), QString("house"));
+
+    QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
+        QCOMPARE(te8.at(0)->key(), QString("10"));
+
+    QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
+        QCOMPARE(te9.at(0)->key(), QString("1000"));
+
+    QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
+        QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
+
+    QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
+        QCOMPARE(te11.at(0)->key(), QString("house"));
 
 }
 
 void XdxfTest::stop() {
-    /*test for English-Polish dictionary */
+
     XdxfPlugin xdxfPluginB(this);
-//    xdxfPlugin.setPatch("dict.xdxf");
+//  xdxfPlugin.setPatch("dict.xdxf");
 
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
@@ -104,20 +163,17 @@ void XdxfTest::stop() {
     QString string("*");
     QFuture<QList<Translation*> > future = QtConcurrent::run(xdxfPlugin, &CommonDictInterface::searchWordList, string, 10);
     QList<Translation*> te5 = future.result();
-
     QCOMPARE(te5.size(), 9);
 }
 
 
 void XdxfTest::langFrom() {
-     /*test for English-Polish dictionary */
-    XdxfPlugin xdxfPluginB(this);
-//    xdxfPlugin.setPatch("dict.xdxf");
 
+    XdxfPlugin xdxfPluginB(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
-    xdxfPlugin->searchWordList("rope", 10);
+
     QCOMPARE(xdxfPlugin->langFrom(), QString("ENG"));
 }
 
index 084c5d8..100b171 100644 (file)
 
 
  private slots:
-
-     void search();
-     void searchWordList();
      void langFrom();
      void getNew();
      void stop();
+     void searchFile();
+     void searchWordListFile();
+     void makeCache();
+     void searchCache();
+     void searchWordListCache();
  };