change comment's and fix bug (xslt transform)
[mdictionary] / src / mdictionary / gui / TranslationWidget.cpp
index 7312d32..ca4356f 100644 (file)
@@ -41,7 +41,6 @@ TranslationWidget::TranslationWidget(QWidget *parent):
         setWindowFlags(windowFlags() | Qt::Window);
     #endif
 
-
     initializeUI();
 
     setWindowTitle("mDictionary");
@@ -70,41 +69,45 @@ void TranslationWidget::show(QStringList translations) {
             initButtons();
     #endif
 
-   // webkit->repaint(this->rect());
-   // update(this->rect());
-
-    QString trans;
-    QString t;
-
-    foreach(t, translations) {
-        trans += t + "\n";
-    }
-
-
-    trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
-           + trans + QString("\n </ar>");
-
-    trans=checkTranslation(trans);
-    qDebug()<<"\n\n trans"<<trans;
-    trans=XslConversion(trans);
+    QString trans= createTranslation(translations);
 
     QString head = "<html><head>";
-    head += "<link rel=\"stylesheet\" type=\"text/css\" " ;
-    head += "href=\"/usr/share/mdictionary/css/" "style.css";
-    head += "\" /></head><body ondragstart=\"return false\">";
-    trans = head + trans;
+    head += "<link rel=\"stylesheet\" type=\"text/css\" "
+            "href=\"/usr/share/mdictionary/css/" "style.css"
+            "\" /></head><body ondragstart=\"return false\">";
+    trans = head + trans + "</body></html>";
 
-    trans+= "</body></html>";
     trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
 
- //   qDebug()<<"\n\n trans"<<trans;
     webkit->setHtml(trans, QUrl().fromLocalFile("/"));
-    //webkit->repaint(this->rect());
-    //update(this->rect());
+}
+
 
-  //  Q_EMIT updateSize();
+QString TranslationWidget::createTranslation(QStringList translations) {
+    QString trans;
+    if(!translations.size())
+        return QString("Empty translation");
+    int element=-1;
+    do{
+        int a=0;
+        foreach(QString t, translations){
+            if(a!=element)
+                trans += t + "\n";
+            a++;
+        }
+        trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
+               + trans + QString("\n </ar>");
+
+        trans=checkTranslation(trans);
+        trans=XslConversion(trans);
+        element++;
+        if(element>translations.size())
+            return QString("Empty translation");
+    } while(trans==""); // trans=="" only when XslConversation have a error;
+    return trans;
 }
 
+
 QString TranslationWidget::XslConversion(QString translation)
 {
     QXmlQuery myQuery(QXmlQuery::XSLT20);
@@ -115,12 +118,14 @@ QString TranslationWidget::XslConversion(QString translation)
     return result;
 }
 
+
 QString TranslationWidget::checkTranslation(QString trans){
     /*check all "&"*/
     int pos=0;
     int pos2;
     int count=0;
 
+    /*check "<" and ">" in key */
     while(pos!=-1){
         pos=trans.indexOf("&",pos+1);
         pos2=trans.indexOf("amp;",pos);
@@ -142,13 +147,11 @@ QString TranslationWidget::checkTranslation(QString trans){
             trans.insert(pos+1,"amp;");
     }
 
-
-    /*check all "<" and ">" in key */
+    /*check "<" and ">" in key */
     pos=0;
     count=0;
     while(pos<trans.size()){
         if(pos==trans.indexOf("<key>",pos)){
-            qDebug()<<"key";
             count++;
             pos+=4;
         }
@@ -159,8 +162,35 @@ QString TranslationWidget::checkTranslation(QString trans){
         pos++;
     }
 
+    /*checking unclosed tag's*/
+    pos=0;
+    count=0;
+    while(pos<trans.size()){
+        if(trans.at(pos)=='<')
+            count++;
+        else if(trans.at(pos)=='>')
+            count--;
+        if(count==-1){     // remove ">"
+            trans.remove(pos,1);
+            count=0;
+        }
+        pos++;
+    }
+    pos=trans.size()-1;
+    count=0;
+    while(pos>-1){
+        if(trans.at(pos)=='>')
+            count++;
+        else if(trans.at(pos)=='<')
+            count--;
+        if(count==-1){     // remove "<"
+            trans.remove(pos,1);
+            count=0;
+        }
+        pos--;
+    }
 
-    /*check all """ and "'" */
+    /*check """ and "'" */
     pos=0;
     count=0;
     while(pos<trans.size()){
@@ -177,91 +207,69 @@ QString TranslationWidget::checkTranslation(QString trans){
     return trans;
 }
 
+
 #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();
 
-
-
         x = width() - zoomOutButton->sizeHint().width();
         y = height() - 2*zoomOutButton->sizeHint().height();
         zoomOutButton->move(QPoint(x, height()));
 
-        zoomOutButtonAnimation =
-                new QPropertyAnimation(zoomOutButton, "pos", this);
-
+        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 = 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 = 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 = 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 = 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);
@@ -277,7 +285,6 @@ void TranslationWidget::initButtons() {
 #endif
 
 void TranslationWidget::initializeUI() {
-
     webkit = new TranslationView(this);
     Settings* set = guiinterface->settings();
     qreal fac = set->value("zoom").toFloat();