add test for searchCache and searchWordListCache
[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
48     show();
49
50     textEdit->clear();
51
52     QString trans;
53     QString t;
54     foreach(t, translations) {
55         trans += t + "\n";
56     }
57
58  //   qDebug()<<trans;
59     trans=tr("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
60
61     trans=XslConversion(trans);
62     textEdit->insertHtml(trans);
63   // textEdit->setPlainText(trans);
64
65     textEdit->repaint(this->rect());
66
67     update(this->rect());
68
69     emit updateSize();
70 }
71
72 QString TranslationWidget::XslConversion(QString translation)
73 {
74     QXmlQuery myQuery(QXmlQuery::XSLT20);
75     myQuery.setFocus(translation);
76 //    qDebug()<<translation;
77     QFile file(":/xsl/xsl.xsl");
78     if(!file.open(QFile::ReadOnly)){
79         qDebug()<<"can't open a xslt file";
80         return translation;
81     }
82     QString xslt;
83     xslt=file.readAll();
84     myQuery.setQuery(xslt);
85     QString result("");
86     myQuery.evaluateTo(&result);
87     return result;
88 }
89
90 void TranslationWidget::initializeUI() {
91
92     zoomInToolButton = new QToolButton;
93     zoomInToolButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
94
95     zoomOutToolButton = new QToolButton;
96     zoomOutToolButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
97
98    // horizontalLayout = new QHBoxLayout;
99     //horizontalLayout->addWidget(zoomInToolButton);
100    // horizontalLayout->addWidget(zoomOutToolButton);
101
102     textEdit = new QTextEdit;
103     textEdit->setReadOnly(true);
104
105     resizer = new TranslationWidgetAutoResizer(textEdit);
106     connect(this, SIGNAL(updateSize()),
107             resizer, SLOT(textEditChanged()));
108
109     QWidget*w = new QWidget;
110     verticalLayout = new QVBoxLayout(w);
111     verticalLayout->addWidget(textEdit);
112
113     textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
114     textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
115
116
117     this->setWidget(w);
118     this->setWidgetResizable(true);
119
120     connect(zoomInToolButton, SIGNAL(clicked()),
121             textEdit, SLOT(zoomIn()));
122
123     connect(zoomInToolButton, SIGNAL(clicked()),
124             this, SIGNAL(updateSize()));
125
126     connect(zoomOutToolButton, SIGNAL(clicked()),
127             textEdit, SLOT(zoomOut()));
128
129     connect(zoomInToolButton, SIGNAL(clicked()),
130             this, SIGNAL(updateSize()));
131
132    /* #ifdef Q_WS_MAEMO_5
133         fullScreenButton = new QToolButton(this);
134         fullScreenButton->setIcon(QIcon::fromTheme("general_fullsize"));
135         fullScreenButton->setMinimumSize(fullScreenButton->sizeHint());
136         int x = QApplication::desktop()->screenGeometry(this).width()  -
137                 fullScreenButton->sizeHint().width();
138         int y = QApplication::desktop()->screenGeometry(this).height() -
139                 fullScreenButton->sizeHint().height();
140         fullScreenButton->move(QPoint(x,y));
141         fullScreenButton->show();
142         fullScreenButton->setWindowOpacity(0.5);
143
144
145         backButton = new QToolButton(this);
146         backButton->setIcon(QIcon::fromTheme("general_overlay_back"));
147         backButton->setMinimumSize(fullScreenButton->sizeHint());
148         x = QApplication::desktop()->screenGeometry(this).width()  -
149                 backButton->sizeHint().width();
150         y = 0;
151         backButton->move(QPoint(x,y));
152         backButton->show();
153         backButton->setWindowOpacity(0.5);
154
155         connect(backButton, SIGNAL(clicked()),
156                 this, SLOT(hide()));
157     #endif*/
158 }
159