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