add xslt transformation
[mdictionary] / trunk / src / base / gui / TranslationWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24 #include "TranslationWidget.h"
25 #include "TranslationWidgetAutoResizer.h"
26 #include <QDebug>
27
28 TranslationWidget::TranslationWidget(QWidget *parent):
29     QScrollArea(parent) {
30
31     #ifdef Q_WS_MAEMO_5
32         setAttribute(Qt::WA_Maemo5StackedWindow);
33     #endif
34     setWindowFlags(windowFlags() | Qt::Window);
35
36     initializeUI();
37     //Q_INIT_RESOURCE(xslt);
38     setWindowTitle(tr("Translation"));
39 }
40
41
42 void TranslationWidget::show() {
43     QScrollArea::show();
44 }
45
46 void TranslationWidget::show(QStringList translations) {
47     show();
48
49     textEdit->clear();
50
51     QString trans;
52     QString t;
53     foreach(t, translations) {
54         trans += t + "\n";
55     }
56
57 //    qDebug()<<trans;
58     trans=tr("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
59
60     trans=XslConversion(trans);
61     textEdit->insertHtml(trans);
62   // textEdit->setPlainText(trans);
63
64     textEdit->repaint(this->rect());
65
66     update(this->rect());
67
68     emit updateSize();
69 }
70
71 QString TranslationWidget::XslConversion(QString translation)
72 {
73     QXmlQuery myQuery(QXmlQuery::XSLT20);
74     myQuery.setFocus(translation);
75 //    qDebug()<<translation;
76     QFile file(":/xsl/xsl.xsl");
77     if(!file.open(QFile::ReadOnly)){
78         qDebug()<<"can't open a xslt file";
79         return translation;
80     }
81     QString xslt;
82     xslt=file.readAll();
83     myQuery.setQuery(xslt);
84     QString result("");
85     myQuery.evaluateTo(&result);
86     return result;
87 }
88
89 void TranslationWidget::initializeUI() {
90
91     zoomInToolButton = new QToolButton;
92     zoomInToolButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
93
94     zoomOutToolButton = new QToolButton;
95     zoomOutToolButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
96
97    // horizontalLayout = new QHBoxLayout;
98     //horizontalLayout->addWidget(zoomInToolButton);
99    // horizontalLayout->addWidget(zoomOutToolButton);
100
101     textEdit = new QTextEdit;
102     textEdit->setReadOnly(true);
103
104     resizer = new TranslationWidgetAutoResizer(textEdit);
105     connect(this, SIGNAL(updateSize()),
106             resizer, SLOT(textEditChanged()));
107
108     QWidget*w = new QWidget;
109     verticalLayout = new QVBoxLayout(w);
110     //verticalLayout->addLayout(horizontalLayout);
111     verticalLayout->addWidget(textEdit);
112
113     this->setWidget(w);
114     this->setWidgetResizable(true);
115
116     connect(zoomInToolButton, SIGNAL(clicked()),
117             textEdit, SLOT(zoomIn()));
118
119     connect(zoomInToolButton, SIGNAL(clicked()),
120             this, SIGNAL(updateSize()));
121
122     connect(zoomOutToolButton, SIGNAL(clicked()),
123             textEdit, SLOT(zoomOut()));
124
125     connect(zoomInToolButton, SIGNAL(clicked()),
126             this, SIGNAL(updateSize()));
127
128 }
129