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