f62ffdaf459e767b208d7888114a541c977871f7
[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     QImage image(":icons/16x16/staron.png");
75     QTextDocument *document = textEdit->document();
76     document->addResource(QTextDocument::ImageResource, QUrl("mydata://image.png"), QVariant(image));
77
78     trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
79     trans=XslConversion(trans);
80
81     textEdit->insertHtml(trans);
82
83 //  textEdit->setPlainText(trans);
84
85     textEdit->repaint(this->rect());
86     update(this->rect());
87
88     emit updateSize();
89 }
90
91 QString TranslationWidget::XslConversion(QString translation)
92 {
93     QXmlQuery myQuery(QXmlQuery::XSLT20);
94     myQuery.setFocus(translation);
95
96     QFile file(":/xsl/xsl.xsl");
97     if(!file.open(QFile::ReadOnly)){
98         qDebug()<<"can't open a xslt file";
99         return translation;
100     }
101     QString xslt;
102     xslt=file.readAll();
103     myQuery.setQuery(xslt);
104     QString result("");
105     myQuery.evaluateTo(&result);
106     return result;
107 }
108
109
110 #ifdef Q_WS_MAEMO_5
111 void TranslationWidget::initButtons() {
112
113         int x = width() - showButtonsButton->sizeHint().width();
114         int y = height() - showButtonsButton->sizeHint().height();
115
116         showButtonsButton->move(QPoint(x,y));
117         showButtonsButton->show();
118 //==================================================================
119
120         x = width() - zoomOutButton->sizeHint().width();
121         y = height() - 2*zoomOutButton->sizeHint().height();
122         zoomOutButton->move(QPoint(x, height()));
123
124         zoomOutButtonAnimation =
125                 new QPropertyAnimation(zoomOutButton, "pos", this);
126
127         zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
128         zoomOutButtonAnimation->setEndValue(QPoint(x,y));
129         zoomOutButtonAnimation->setDuration(200);
130         zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
131 //==================================================================
132         x = width() - zoomInButton->sizeHint().width();
133         y = height() - 3*zoomInButton->sizeHint().height();
134         zoomInButton->move(QPoint(x, height()));
135
136         zoomInButtonAnimation =
137                 new QPropertyAnimation(zoomInButton, "pos", this);
138
139         zoomInButtonAnimation->setStartValue(QPoint(x, height()));
140         zoomInButtonAnimation->setEndValue(QPoint(x,y));
141         zoomInButtonAnimation->setDuration(400);
142         zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
143 //==================================================================
144         x = 0;
145         y = height() - copyButton->sizeHint().height();
146
147         copyButton->move(QPoint(x, height()));
148
149         copyButtonAnimation =
150                 new QPropertyAnimation(copyButton, "pos", this);
151
152         copyButtonAnimation->setStartValue(QPoint(x, height()));
153         copyButtonAnimation->setEndValue(QPoint(x,y));
154         copyButtonAnimation->setDuration(200);
155         copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
156 //==================================================================
157         x = 0;
158         y = height() - 2*copyButton->sizeHint().height();
159
160         selectAllButton->move(QPoint(x, height()));
161
162         selectAllButtonAnimation =
163                 new QPropertyAnimation(selectAllButton, "pos", this);
164
165         selectAllButtonAnimation->setStartValue(QPoint(x, height()));
166         selectAllButtonAnimation->setEndValue(QPoint(x,y));
167         selectAllButtonAnimation->setDuration(400);
168         selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
169 //==================================================================
170         x = 0;
171         y = height() - 3*copyButton->sizeHint().height();
172
173         searchButton->move(QPoint(x, height()));
174
175         searchButtonAnimation =
176                 new QPropertyAnimation(searchButton, "pos", this);
177
178         searchButtonAnimation->setStartValue(QPoint(x, height()));
179         searchButtonAnimation->setEndValue(QPoint(x,y));
180         searchButtonAnimation->setDuration(600);
181         searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
182 //==================================================================
183
184         buttonsAnimation = new QParallelAnimationGroup(this);
185         buttonsAnimation->addAnimation(zoomInButtonAnimation);
186         buttonsAnimation->addAnimation(zoomOutButtonAnimation);
187         buttonsAnimation->addAnimation(selectAllButtonAnimation);
188         buttonsAnimation->addAnimation(copyButtonAnimation);
189         buttonsAnimation->addAnimation(searchButtonAnimation);
190         buttonsInitialized = true;
191         buttonsVisible = false;
192
193         connect(showButtonsButton, SIGNAL(clicked()),
194                 this, SLOT(showButtons()));
195 }
196 #endif
197
198 void TranslationWidget::initializeUI() {
199
200     textEdit = new TranslationTextEdit(this);
201     textEdit->setReadOnly(true);
202
203     resizer = new TranslationWidgetAutoResizer(textEdit);
204     connect(this, SIGNAL(updateSize()),
205             resizer, SLOT(textEditChanged()));
206
207     QWidget*w = new QWidget(this);
208     verticalLayout = new QVBoxLayout(w);
209     verticalLayout->addWidget(textEdit);
210
211     textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
212     textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
213
214     this->setWidget(w);
215     this->setWidgetResizable(true);
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(":/icons/48x48/edit-select-all.png"));
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_sent"));
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() {
283     if(!buttonsVisible) {
284         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
285         buttonsAnimation->start();
286         buttonsVisible = true;
287
288         showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
289     }
290     else if(buttonsVisible) {
291         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
292         buttonsAnimation->start();
293         buttonsVisible = false;
294         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
295     }
296 }
297
298 void TranslationWidget::grabZoomKeys(bool grab) {
299      if (!winId()) {
300          return;
301      }
302
303     unsigned long val = (grab) ? 1 : 0;
304     Atom atom = XInternAtom(QX11Info::display(),
305                             "_HILDON_ZOOM_KEY_ATOM", False);
306     if (!atom) {
307         return;
308     }
309
310     XChangeProperty (QX11Info::display(),
311          winId(),
312          atom,
313          XA_INTEGER,
314          32,
315          PropModeReplace,
316          reinterpret_cast<unsigned char *>(&val),
317          1);
318 }
319
320 void TranslationWidget::hideEvent(QHideEvent* e) {
321     if(buttonsVisible)
322         showButtons();
323
324     QScrollArea::hideEvent(e);
325 }
326
327 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
328     switch (event->key()) {
329         case Qt::Key_F7:
330         textEdit->zoomIn();
331         event->accept();
332         break;
333
334         case Qt::Key_F8:
335         textEdit->zoomOut();
336         event->accept();
337         break;
338     }
339     QWidget::keyPressEvent(event);
340 }
341 #endif
342
343
344