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