Moved icons from resources to data/ fixed bug with segmentation fault when starting...
[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     qDebug()<<parent;
37     #ifdef Q_WS_MAEMO_5
38         setAttribute(Qt::WA_Maemo5StackedWindow);
39         setWindowFlags(windowFlags() | Qt::Window);
40     #endif
41
42
43     initializeUI();
44
45     setWindowTitle("mDictionary");
46
47     connect(textEdit, SIGNAL(search()),
48            this, SLOT(searchSelected()));
49
50 }
51
52
53 void TranslationWidget::show() {
54     QScrollArea::show();
55 }
56
57
58 void TranslationWidget::show(QStringList translations) {
59
60     showMaximized();
61
62     #ifdef Q_WS_MAEMO_5
63         if(!buttonsInitialized)
64             initButtons();
65     #endif
66
67     textEdit->clear();
68
69     QString trans;
70     QString t;
71     foreach(t, translations) {
72         trans += t + "\n";
73     }
74
75
76     trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") +
77           QString("\n <ar>") + trans + QString("\n </ar>");
78 //    trans.replace("&","&amp;");
79     trans=XslConversion(trans);
80     qDebug()<<trans;
81 //    trans.replace("&amp;","&");
82     textEdit->insertHtml(trans);
83  //   textEdit->setPlainText(trans);
84
85     textEdit->repaint(this->rect());
86
87     update(this->rect());
88
89     emit updateSize();
90 }
91
92 QString TranslationWidget::XslConversion(QString translation)
93 {
94     QXmlQuery myQuery(QXmlQuery::XSLT20);
95     myQuery.setFocus(translation);
96
97     QFile file(":/xsl/xsl.xsl");
98     if(!file.open(QFile::ReadOnly)){
99         qDebug()<<"can't open a xslt file";
100         return translation;
101     }
102     QString xslt;
103     xslt=file.readAll();
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     textEdit = new TranslationTextEdit;
202     textEdit->setReadOnly(true);
203
204     resizer = new TranslationWidgetAutoResizer(textEdit);
205     connect(this, SIGNAL(updateSize()),
206             resizer, SLOT(textEditChanged()));
207
208     QWidget*w = new QWidget;
209     verticalLayout = new QVBoxLayout(w);
210     verticalLayout->addWidget(textEdit);
211
212     #ifndef Q_WS_MAEMO_5
213         textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214         textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
215     #endif
216
217     this->setWidget(w);
218     this->setWidgetResizable(true);
219
220     #ifdef Q_WS_MAEMO_5
221         zoomInButton = new QToolButton(this);
222         zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
223         zoomInButton->setMinimumSize(zoomInButton->sizeHint());
224
225         zoomOutButton = new QToolButton(this);
226         zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
227         zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
228
229         selectAllButton = new QToolButton(this);
230         selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
231         selectAllButton->setMinimumSize(selectAllButton->sizeHint());
232
233         copyButton = new QToolButton(this);
234         copyButton->setIcon(QIcon::fromTheme("general_notes"));
235         copyButton->setMinimumSize(copyButton->sizeHint());
236         copyButton->setEnabled(false);
237
238         searchButton = new QToolButton(this);
239         searchButton->setIcon(QIcon::fromTheme("general_search"));
240         searchButton->setMinimumSize(searchButton->sizeHint());
241         searchButton->setEnabled(false);
242
243         showButtonsButton = new QToolButton(this);
244         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
245         showButtonsButton->setMinimumSize(searchButton->sizeHint());
246
247         connect(zoomInButton, SIGNAL(clicked()),
248                 textEdit, SLOT(zoomIn()));
249
250         connect(zoomOutButton, SIGNAL(clicked()),
251                 textEdit, SLOT(zoomOut()));
252
253
254         connect(searchButton, SIGNAL(clicked()),
255                 this, SLOT(searchSelected()));
256
257         connect(copyButton, SIGNAL(clicked()),
258                 textEdit, SLOT(copy()));
259
260         connect(textEdit, SIGNAL(copyAvailable(bool)),
261                 searchButton, SLOT(setEnabled(bool)));
262
263         connect(textEdit, SIGNAL(copyAvailable(bool)),
264                 copyButton, SLOT(setEnabled(bool)));
265
266         connect(selectAllButton, SIGNAL(clicked()),
267                 textEdit, SLOT(selectAll()));
268
269         buttonsInitialized = false;
270
271
272
273         grabZoomKeys(true);
274     #endif
275 }
276
277 void TranslationWidget::searchSelected() {
278     #ifdef Q_WS_MAEMO_5
279         hide();
280     #endif
281     emit search(textEdit->textCursor().selectedText());
282 }
283
284 #ifdef Q_WS_MAEMO_5
285 void TranslationWidget::showButtons() {
286     if(!buttonsVisible) {
287         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
288         buttonsAnimation->start();
289         buttonsVisible = true;
290
291         showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
292     }
293     else if(buttonsVisible) {
294         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
295         buttonsAnimation->start();
296         buttonsVisible = false;
297         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
298     }
299 }
300
301 void TranslationWidget::grabZoomKeys(bool grab) {
302      if (!winId()) {
303          return;
304      }
305
306     unsigned long val = (grab) ? 1 : 0;
307     Atom atom = XInternAtom(QX11Info::display(),
308                             "_HILDON_ZOOM_KEY_ATOM", False);
309     if (!atom) {
310         return;
311     }
312
313     XChangeProperty (QX11Info::display(),
314          winId(),
315          atom,
316          XA_INTEGER,
317          32,
318          PropModeReplace,
319          reinterpret_cast<unsigned char *>(&val),
320          1);
321 }
322
323 void TranslationWidget::hideEvent(QHideEvent* e) {
324     if(buttonsVisible)
325         showButtons();
326
327     QScrollArea::hideEvent(e);
328 }
329
330 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
331     switch (event->key()) {
332         case Qt::Key_F7:
333         textEdit->zoomIn();
334         event->accept();
335         break;
336
337         case Qt::Key_F8:
338         textEdit->zoomOut();
339         event->accept();
340         break;
341     }
342     QWidget::keyPressEvent(event);
343 }
344 #endif
345
346
347