Changed css for desktop, add notifications for search and copy button in TranslationW...
[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=\"/usr/share/mdictionary/css/" "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     //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 TranslationView(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
249         searchButton = new QToolButton(this);
250         searchButton->setIcon(QIcon::fromTheme("general_search"));
251         searchButton->setMinimumSize(searchButton->sizeHint());
252
253         showButtonsButton = new QToolButton(this);
254         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
255         showButtonsButton->setMinimumSize(searchButton->sizeHint());
256
257         connect(zoomInButton, SIGNAL(clicked()),
258                 webkit, SLOT(zoomIn()));
259
260         connect(zoomOutButton, SIGNAL(clicked()),
261                 webkit, SLOT(zoomOut()));
262
263
264         connect(searchButton, SIGNAL(clicked()),
265                 this, SLOT(searchSelected()));
266
267         connect(copyButton, SIGNAL(clicked()),
268                 this, SLOT(copySelected()));
269
270         connect(selectAllButton, SIGNAL(clicked()),
271                 webkit, SLOT(selectAll()));
272
273         buttonsInitialized = false;
274
275
276
277         grabZoomKeys(true);
278     #endif
279 }
280
281 void TranslationWidget::searchSelected() {
282     if(webkit->selectedText().isEmpty()) {
283         Q_EMIT notify(Notify::Warning, tr("No text is selected"));
284     }
285     else {
286         #ifdef Q_WS_MAEMO_5
287             hide();
288         #endif
289         Q_EMIT search(webkit->selectedText().toLower());
290     }
291 }
292
293 void TranslationWidget::copySelected() {
294     if(webkit->selectedText().isEmpty()) {
295         Q_EMIT notify(Notify::Warning, tr("No text is selected"));
296     }
297     else {
298         Q_EMIT notify(Notify::Info, tr("Copyied selected text"));
299     }
300 }
301
302 #ifdef Q_WS_MAEMO_5
303 void TranslationWidget::showButtons() {
304     if(!buttonsVisible) {
305         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
306         buttonsAnimation->start();
307         buttonsVisible = true;
308
309         showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
310     }
311     else if(buttonsVisible) {
312         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
313         buttonsAnimation->start();
314         buttonsVisible = false;
315         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
316     }
317 }
318
319 void TranslationWidget::grabZoomKeys(bool grab) {
320      if (!winId()) {
321          return;
322      }
323
324     unsigned long val = (grab) ? 1 : 0;
325     Atom atom = XInternAtom(QX11Info::display(),
326                             "_HILDON_ZOOM_KEY_ATOM", False);
327     if (!atom) {
328         return;
329     }
330
331     XChangeProperty (QX11Info::display(),
332          winId(),
333          atom,
334          XA_INTEGER,
335          32,
336          PropModeReplace,
337          reinterpret_cast<unsigned char *>(&val),
338          1);
339 }
340
341 void TranslationWidget::hideEvent(QHideEvent* e) {
342     if(buttonsVisible)
343         showButtons();
344
345     QScrollArea::hideEvent(e);
346 }
347
348 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
349     switch (event->key()) {
350         case Qt::Key_F7:
351         webkit->zoomIn();
352         event->accept();
353         break;
354
355         case Qt::Key_F8:
356         webkit->zoomOut();
357         event->accept();
358         break;
359     }
360     QWidget::keyPressEvent(event);
361 }
362 #endif
363
364
365 void TranslationWidget::updateZoom(qreal factor) {
366     Settings* set = guiinterface->settings();
367     set->setValue("zoom", QString("%1").arg(factor));
368     guiinterface->setSettings(set);
369 }
370
371
372