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