fix xslt conversion (show all translation)
[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 #ifdef Q_WS_MAEMO_5
28     #include <QtGui/QX11Info>
29     #include <X11/Xlib.h>
30     #include <X11/Xatom.h>
31 #endif
32
33 TranslationWidget::TranslationWidget(QWidget *parent):
34     QScrollArea(parent) {
35
36     #ifdef Q_WS_MAEMO_5
37         setAttribute(Qt::WA_Maemo5StackedWindow);
38     #endif
39     setWindowFlags(windowFlags() | Qt::Window);
40
41     initializeUI();
42     //Q_INIT_RESOURCE(xslt);
43     setWindowTitle(tr("Translation"));
44
45     connect(textEdit, SIGNAL(search()),
46             this, SLOT(searchSelected()));
47
48 }
49
50
51 void TranslationWidget::show() {
52     QScrollArea::show();
53 }
54
55 void TranslationWidget::show(QStringList translations) {
56
57     showMaximized();
58
59     #ifdef Q_WS_MAEMO_5
60         if(!buttonsInitialized)
61             initButtons();
62     #endif
63
64     textEdit->clear();
65
66     QString trans;
67     QString t;
68     foreach(t, translations) {
69         trans += t + "\n";
70     }
71
72
73     trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
74 //    trans.replace("&","&amp;");
75     trans=XslConversion(trans);
76     qDebug()<<trans;
77 //    trans.replace("&amp;","&");
78     textEdit->insertHtml(trans);
79  //   textEdit->setPlainText(trans);
80
81     textEdit->repaint(this->rect());
82
83     update(this->rect());
84
85     emit updateSize();
86 }
87
88 QString TranslationWidget::XslConversion(QString translation)
89 {
90     QXmlQuery myQuery(QXmlQuery::XSLT20);
91     myQuery.setFocus(translation);
92
93     QFile file(":/xsl/xsl.xsl");
94     if(!file.open(QFile::ReadOnly)){
95         qDebug()<<"can't open a xslt file";
96         return translation;
97     }
98     QString xslt;
99     xslt=file.readAll();
100     myQuery.setQuery(xslt);
101     QString result("");
102     myQuery.evaluateTo(&result);
103     return result;
104 }
105
106
107 #ifdef Q_WS_MAEMO_5
108 void TranslationWidget::initButtons() {
109
110         int x = width() - showButtonsButton->sizeHint().width();
111         int y = height() - showButtonsButton->sizeHint().height();
112
113         showButtonsButton->move(QPoint(x,y));
114         showButtonsButton->show();
115         showButtonsButton->setCheckable(true);
116 //==================================================================
117
118         x = width() - zoomOutButton->sizeHint().width();
119         y = height() - 2*zoomOutButton->sizeHint().height();
120         zoomOutButton->move(QPoint(x, height()));
121
122         zoomOutButtonAnimation =
123                 new QPropertyAnimation(zoomOutButton, "pos", this);
124
125         zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
126         zoomOutButtonAnimation->setEndValue(QPoint(x,y));
127         zoomOutButtonAnimation->setDuration(200);
128         zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
129 //==================================================================
130         x = width() - zoomInButton->sizeHint().width();
131         y = height() - 3*zoomInButton->sizeHint().height();
132         zoomInButton->move(QPoint(x, height()));
133
134         zoomInButtonAnimation =
135                 new QPropertyAnimation(zoomInButton, "pos", this);
136
137         zoomInButtonAnimation->setStartValue(QPoint(x, height()));
138         zoomInButtonAnimation->setEndValue(QPoint(x,y));
139         zoomInButtonAnimation->setDuration(400);
140         zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
141 //==================================================================
142         x = 0;
143         y = height() - copyButton->sizeHint().height();
144
145         copyButton->move(QPoint(x, height()));
146
147         copyButtonAnimation =
148                 new QPropertyAnimation(copyButton, "pos", this);
149
150         copyButtonAnimation->setStartValue(QPoint(x, height()));
151         copyButtonAnimation->setEndValue(QPoint(x,y));
152         copyButtonAnimation->setDuration(200);
153         copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
154 //==================================================================
155         x = 0;
156         y = height() - 2*copyButton->sizeHint().height();
157
158         selectAllButton->move(QPoint(x, height()));
159
160         selectAllButtonAnimation =
161                 new QPropertyAnimation(selectAllButton, "pos", this);
162
163         selectAllButtonAnimation->setStartValue(QPoint(x, height()));
164         selectAllButtonAnimation->setEndValue(QPoint(x,y));
165         selectAllButtonAnimation->setDuration(400);
166         selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
167 //==================================================================
168         x = 0;
169         y = height() - 3*copyButton->sizeHint().height();
170
171         searchButton->move(QPoint(x, height()));
172
173         searchButtonAnimation =
174                 new QPropertyAnimation(searchButton, "pos", this);
175
176         searchButtonAnimation->setStartValue(QPoint(x, height()));
177         searchButtonAnimation->setEndValue(QPoint(x,y));
178         searchButtonAnimation->setDuration(600);
179         searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
180 //==================================================================
181
182         buttonsAnimation = new QParallelAnimationGroup(this);
183         buttonsAnimation->addAnimation(zoomInButtonAnimation);
184         buttonsAnimation->addAnimation(zoomOutButtonAnimation);
185         buttonsAnimation->addAnimation(selectAllButtonAnimation);
186         buttonsAnimation->addAnimation(copyButtonAnimation);
187         buttonsAnimation->addAnimation(searchButtonAnimation);
188         buttonsInitialized = true;
189         buttonsVisible = false;
190
191         connect(showButtonsButton, SIGNAL(toggled(bool)),
192                 this, SLOT(showButtons(bool)));
193 }
194 #endif
195
196 void TranslationWidget::initializeUI() {
197
198     textEdit = new TranslationTextEdit;
199     textEdit->setReadOnly(true);
200
201     resizer = new TranslationWidgetAutoResizer(textEdit);
202     connect(this, SIGNAL(updateSize()),
203             resizer, SLOT(textEditChanged()));
204
205     QWidget*w = new QWidget;
206     verticalLayout = new QVBoxLayout(w);
207     verticalLayout->addWidget(textEdit);
208
209     textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
210     textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
211
212
213     this->setWidget(w);
214     this->setWidgetResizable(true);
215
216
217     #ifdef Q_WS_MAEMO_5
218         zoomInButton = new QToolButton(this);
219         zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
220         zoomInButton->setMinimumSize(zoomInButton->sizeHint());
221
222         zoomOutButton = new QToolButton(this);
223         zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
224         zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
225
226         selectAllButton = new QToolButton(this);
227         selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
228         selectAllButton->setMinimumSize(selectAllButton->sizeHint());
229
230         copyButton = new QToolButton(this);
231         copyButton->setIcon(QIcon::fromTheme("general_notes"));
232         copyButton->setMinimumSize(copyButton->sizeHint());
233         copyButton->setEnabled(false);
234
235         searchButton = new QToolButton(this);
236         searchButton->setIcon(QIcon::fromTheme("general_search"));
237         searchButton->setMinimumSize(searchButton->sizeHint());
238         searchButton->setEnabled(false);
239
240         showButtonsButton = new QToolButton(this);
241         showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
242         showButtonsButton->setMinimumSize(searchButton->sizeHint());
243
244         connect(zoomInButton, SIGNAL(clicked()),
245                 textEdit, SLOT(zoomIn()));
246
247         connect(zoomOutButton, SIGNAL(clicked()),
248                 textEdit, SLOT(zoomOut()));
249
250
251         connect(searchButton, SIGNAL(clicked()),
252                 this, SLOT(searchSelected()));
253
254         connect(copyButton, SIGNAL(clicked()),
255                 textEdit, SLOT(copy()));
256
257         connect(textEdit, SIGNAL(copyAvailable(bool)),
258                 searchButton, SLOT(setEnabled(bool)));
259
260         connect(textEdit, SIGNAL(copyAvailable(bool)),
261                 copyButton, SLOT(setEnabled(bool)));
262
263         connect(selectAllButton, SIGNAL(clicked()),
264                 textEdit, SLOT(selectAll()));
265
266         buttonsInitialized = false;
267
268
269
270         grabZoomKeys(true);
271     #endif
272 }
273
274 void TranslationWidget::searchSelected() {
275     #ifdef Q_WS_MAEMO_5
276         hide();
277     #endif
278     emit search(textEdit->textCursor().selectedText());
279 }
280
281 #ifdef Q_WS_MAEMO_5
282 void TranslationWidget::showButtons(bool show) {
283     if(show && !buttonsVisible) {
284         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
285         buttonsAnimation->start();
286         buttonsVisible = true;
287     }
288     else if(!show && buttonsVisible) {
289         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
290         buttonsAnimation->start();
291         buttonsVisible = false;
292     }
293 }
294
295 void TranslationWidget::grabZoomKeys(bool grab) {
296      if (!winId()) {
297          return;
298      }
299
300     unsigned long val = (grab) ? 1 : 0;
301     Atom atom = XInternAtom(QX11Info::display(),
302                             "_HILDON_ZOOM_KEY_ATOM", False);
303     if (!atom) {
304         return;
305     }
306
307     XChangeProperty (QX11Info::display(),
308          winId(),
309          atom,
310          XA_INTEGER,
311          32,
312          PropModeReplace,
313          reinterpret_cast<unsigned char *>(&val),
314          1);
315 }
316
317 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
318     switch (event->key()) {
319         case Qt::Key_F7:
320         textEdit->zoomIn();
321         event->accept();
322         break;
323
324         case Qt::Key_F8:
325         textEdit->zoomOut();
326         event->accept();
327         break;
328     }
329     QWidget::keyPressEvent(event);
330 }
331 #endif
332
333
334