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