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