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