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