Change of tool button
[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
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;
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;
203     verticalLayout = new QVBoxLayout(w);
204     verticalLayout->addWidget(textEdit);
205
206     textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
207     textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
208
209
210     this->setWidget(w);
211     this->setWidgetResizable(true);
212
213
214     #ifdef Q_WS_MAEMO_5
215         zoomInButton = new QToolButton(this);
216         zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
217         zoomInButton->setMinimumSize(zoomInButton->sizeHint());
218
219         zoomOutButton = new QToolButton(this);
220         zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
221         zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
222
223         selectAllButton = new QToolButton(this);
224         selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
225         selectAllButton->setMinimumSize(selectAllButton->sizeHint());
226
227         copyButton = new QToolButton(this);
228         copyButton->setIcon(QIcon::fromTheme("general_notes"));
229         copyButton->setMinimumSize(copyButton->sizeHint());
230         copyButton->setEnabled(false);
231
232         searchButton = new QToolButton(this);
233         searchButton->setIcon(QIcon::fromTheme("general_search"));
234         searchButton->setMinimumSize(searchButton->sizeHint());
235         searchButton->setEnabled(false);
236
237         showButtonsButton = new QToolButton(this);
238         showButtonsButton->setArrowType(Qt::UpArrow);
239         showButtonsButton->setMinimumSize(searchButton->sizeHint());
240
241         connect(zoomInButton, SIGNAL(clicked()),
242                 textEdit, SLOT(zoomIn()));
243
244         connect(zoomOutButton, SIGNAL(clicked()),
245                 textEdit, SLOT(zoomOut()));
246
247
248         connect(searchButton, SIGNAL(clicked()),
249                 this, SLOT(searchSelected()));
250
251         connect(copyButton, SIGNAL(clicked()),
252                 textEdit, SLOT(copy()));
253
254         connect(textEdit, SIGNAL(copyAvailable(bool)),
255                 searchButton, SLOT(setEnabled(bool)));
256
257         connect(textEdit, SIGNAL(copyAvailable(bool)),
258                 copyButton, SLOT(setEnabled(bool)));
259
260         connect(selectAllButton, SIGNAL(clicked()),
261                 textEdit, SLOT(selectAll()));
262
263         buttonsInitialized = false;
264
265
266
267         grabZoomKeys(true);
268     #endif
269 }
270
271 void TranslationWidget::searchSelected() {
272     #ifdef Q_WS_MAEMO_5
273         hide();
274     #endif
275     emit search(textEdit->textCursor().selectedText());
276 }
277
278 #ifdef Q_WS_MAEMO_5
279 void TranslationWidget::showButtons() {
280     if(!buttonsVisible) {
281         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
282         buttonsAnimation->start();
283         buttonsVisible = true;
284
285         showButtonsButton->setArrowType(Qt::DownArrow);
286     }
287     else if(buttonsVisible) {
288         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
289         buttonsAnimation->start();
290         buttonsVisible = false;
291         showButtonsButton->setArrowType(Qt::UpArrow);
292     }
293 }
294
295 void TranslationWidget::grabZoomKeys(bool grab) {
296      if (!winId()) {
297          return;
298      }
299
300     unsigned long val = (grab) ? 1 : 0;
301     Atom atom = XInternAtom(QX11Info::display(),
302                             "_HILDON_ZOOM_KEY_ATOM", False);
303     if (!atom) {
304         return;
305     }
306
307     XChangeProperty (QX11Info::display(),
308          winId(),
309          atom,
310          XA_INTEGER,
311          32,
312          PropModeReplace,
313          reinterpret_cast<unsigned char *>(&val),
314          1);
315 }
316
317 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
318     switch (event->key()) {
319         case Qt::Key_F7:
320         textEdit->zoomIn();
321         event->accept();
322         break;
323
324         case Qt::Key_F8:
325         textEdit->zoomOut();
326         event->accept();
327         break;
328     }
329     QWidget::keyPressEvent(event);
330 }
331 #endif
332
333
334