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