plugins icon in translation
[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/share/mdictionary/staron.png");
94
95 //    qDebug()<<trans;
96
97     webkit->setHtml(trans, QUrl().fromLocalFile("/"));
98
99
100     //webkit->repaint(this->rect());
101     //update(this->rect());
102
103   //  Q_EMIT updateSize();
104 }
105
106 QString TranslationWidget::XslConversion(QString translation)
107 {
108     QXmlQuery myQuery(QXmlQuery::XSLT20);
109     myQuery.setFocus(translation);
110     myQuery.setQuery(xslt);
111     QString result("");
112     myQuery.evaluateTo(&result);
113     return result;
114 }
115
116
117 #ifdef Q_WS_MAEMO_5
118 void TranslationWidget::initButtons() {
119
120         int x = width() - showButtonsButton->sizeHint().width();
121         int y = height() - showButtonsButton->sizeHint().height();
122
123         showButtonsButton->move(QPoint(x,y));
124         showButtonsButton->show();
125
126
127
128         x = width() - zoomOutButton->sizeHint().width();
129         y = height() - 2*zoomOutButton->sizeHint().height();
130         zoomOutButton->move(QPoint(x, height()));
131
132         zoomOutButtonAnimation =
133                 new QPropertyAnimation(zoomOutButton, "pos", this);
134
135         zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
136         zoomOutButtonAnimation->setEndValue(QPoint(x,y));
137         zoomOutButtonAnimation->setDuration(200);
138         zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
139
140
141
142         x = width() - zoomInButton->sizeHint().width();
143         y = height() - 3*zoomInButton->sizeHint().height();
144         zoomInButton->move(QPoint(x, height()));
145
146         zoomInButtonAnimation =
147                 new QPropertyAnimation(zoomInButton, "pos", this);
148
149         zoomInButtonAnimation->setStartValue(QPoint(x, height()));
150         zoomInButtonAnimation->setEndValue(QPoint(x,y));
151         zoomInButtonAnimation->setDuration(400);
152         zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
153
154
155
156         x = 0;
157         y = height() - copyButton->sizeHint().height();
158
159         copyButton->move(QPoint(x, height()));
160
161         copyButtonAnimation =
162                 new QPropertyAnimation(copyButton, "pos", this);
163
164         copyButtonAnimation->setStartValue(QPoint(x, height()));
165         copyButtonAnimation->setEndValue(QPoint(x,y));
166         copyButtonAnimation->setDuration(200);
167         copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
168
169
170
171         x = 0;
172         y = height() - 2*copyButton->sizeHint().height();
173
174         selectAllButton->move(QPoint(x, height()));
175
176         selectAllButtonAnimation =
177                 new QPropertyAnimation(selectAllButton, "pos", this);
178
179         selectAllButtonAnimation->setStartValue(QPoint(x, height()));
180         selectAllButtonAnimation->setEndValue(QPoint(x,y));
181         selectAllButtonAnimation->setDuration(400);
182         selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
183
184
185
186         x = 0;
187         y = height() - 3*copyButton->sizeHint().height();
188
189         searchButton->move(QPoint(x, height()));
190
191         searchButtonAnimation =
192                 new QPropertyAnimation(searchButton, "pos", this);
193
194         searchButtonAnimation->setStartValue(QPoint(x, height()));
195         searchButtonAnimation->setEndValue(QPoint(x,y));
196         searchButtonAnimation->setDuration(600);
197         searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
198
199
200
201
202         buttonsAnimation = new QParallelAnimationGroup(this);
203         buttonsAnimation->addAnimation(zoomInButtonAnimation);
204         buttonsAnimation->addAnimation(zoomOutButtonAnimation);
205         buttonsAnimation->addAnimation(selectAllButtonAnimation);
206         buttonsAnimation->addAnimation(copyButtonAnimation);
207         buttonsAnimation->addAnimation(searchButtonAnimation);
208         buttonsInitialized = true;
209         buttonsVisible = false;
210
211         connect(showButtonsButton, SIGNAL(clicked()),
212                 this, SLOT(showButtons()));
213 }
214 #endif
215
216 void TranslationWidget::initializeUI() {
217
218     webkit = new TranslationTextEdit(this);
219     Settings* set = guiinterface->settings();
220     qreal fac = set->value("zoom").toFloat();
221     if(!fac)
222         fac++;
223     webkit->setZoomFactor(fac);
224     delete set;
225
226     QWidget*w = new QWidget(this);
227     verticalLayout = new QVBoxLayout(w);
228     verticalLayout->addWidget(webkit);
229
230     this->setWidget(w);
231     this->setWidgetResizable(true);
232
233     #ifdef Q_WS_MAEMO_5
234         zoomInButton = new QToolButton(this);
235         zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
236         zoomInButton->setMinimumSize(zoomInButton->sizeHint());
237
238         zoomOutButton = new QToolButton(this);
239         zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
240         zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
241
242         selectAllButton = new QToolButton(this);
243         selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
244         selectAllButton->setMinimumSize(selectAllButton->sizeHint());
245
246         copyButton = new QToolButton(this);
247         copyButton->setIcon(QIcon::fromTheme("general_notes"));
248         copyButton->setMinimumSize(copyButton->sizeHint());
249         copyButton->setEnabled(false);
250
251         searchButton = new QToolButton(this);
252         searchButton->setIcon(QIcon::fromTheme("general_search"));
253         searchButton->setMinimumSize(searchButton->sizeHint());
254         searchButton->setEnabled(false);
255
256         showButtonsButton = new QToolButton(this);
257         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
258         showButtonsButton->setMinimumSize(searchButton->sizeHint());
259
260         connect(zoomInButton, SIGNAL(clicked()),
261                 webkit, SLOT(zoomIn()));
262
263         connect(zoomOutButton, SIGNAL(clicked()),
264                 webkit, SLOT(zoomOut()));
265
266
267         connect(searchButton, SIGNAL(clicked()),
268                 this, SLOT(searchSelected()));
269
270         connect(copyButton, SIGNAL(clicked()),
271                 webkit, SLOT(copy()));
272
273         connect(webkit, SIGNAL(copyAvailable(bool)),
274                 searchButton, SLOT(setEnabled(bool)));
275
276         connect(webkit, SIGNAL(copyAvailable(bool)),
277                 copyButton, SLOT(setEnabled(bool)));
278
279         connect(selectAllButton, SIGNAL(clicked()),
280                 webkit, SLOT(selectAll()));
281
282         buttonsInitialized = false;
283
284
285
286         grabZoomKeys(true);
287     #endif
288 }
289
290 void TranslationWidget::searchSelected() {
291     #ifdef Q_WS_MAEMO_5
292         hide();
293     #endif
294     Q_EMIT search(webkit->selectedText().toLower());
295 }
296
297 #ifdef Q_WS_MAEMO_5
298 void TranslationWidget::showButtons() {
299     if(!buttonsVisible) {
300         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
301         buttonsAnimation->start();
302         buttonsVisible = true;
303
304         showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
305     }
306     else if(buttonsVisible) {
307         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
308         buttonsAnimation->start();
309         buttonsVisible = false;
310         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
311     }
312 }
313
314 void TranslationWidget::grabZoomKeys(bool grab) {
315      if (!winId()) {
316          return;
317      }
318
319     unsigned long val = (grab) ? 1 : 0;
320     Atom atom = XInternAtom(QX11Info::display(),
321                             "_HILDON_ZOOM_KEY_ATOM", False);
322     if (!atom) {
323         return;
324     }
325
326     XChangeProperty (QX11Info::display(),
327          winId(),
328          atom,
329          XA_INTEGER,
330          32,
331          PropModeReplace,
332          reinterpret_cast<unsigned char *>(&val),
333          1);
334 }
335
336 void TranslationWidget::hideEvent(QHideEvent* e) {
337     if(buttonsVisible)
338         showButtons();
339
340     QScrollArea::hideEvent(e);
341 }
342
343 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
344     switch (event->key()) {
345         case Qt::Key_F7:
346         webkit->zoomIn();
347         event->accept();
348         break;
349
350         case Qt::Key_F8:
351         webkit->zoomOut();
352         event->accept();
353         break;
354     }
355     QWidget::keyPressEvent(event);
356 }
357 #endif
358
359
360 void TranslationWidget::updateZoom(qreal factor) {
361     Settings* set = guiinterface->settings();
362     set->setValue("zoom", QString("%1").arg(factor));
363     guiinterface->setSettings(set);
364 }
365
366
367