Merge branch 'gui'
[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
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     textEdit->setPlainText(trans);
73
74     textEdit->repaint(this->rect());
75
76     update(this->rect());
77
78     emit updateSize();
79 }
80
81 #ifdef Q_WS_MAEMO_5
82 void TranslationWidget::initButtons() {
83
84         int x = width() - showButtonsButton->sizeHint().width();
85         int y = height() - showButtonsButton->sizeHint().height();
86
87         showButtonsButton->move(QPoint(x,y));
88         showButtonsButton->show();
89         showButtonsButton->setCheckable(true);
90 //==================================================================
91
92         x = width() - zoomOutButton->sizeHint().width();
93         y = height() - 2*zoomOutButton->sizeHint().height();
94         zoomOutButton->move(QPoint(x, height()));
95
96         zoomOutButtonAnimation =
97                 new QPropertyAnimation(zoomOutButton, "pos", this);
98
99         zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
100         zoomOutButtonAnimation->setEndValue(QPoint(x,y));
101         zoomOutButtonAnimation->setDuration(200);
102         zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
103 //==================================================================
104         x = width() - zoomInButton->sizeHint().width();
105         y = height() - 3*zoomInButton->sizeHint().height();
106         zoomInButton->move(QPoint(x, height()));
107
108         zoomInButtonAnimation =
109                 new QPropertyAnimation(zoomInButton, "pos", this);
110
111         zoomInButtonAnimation->setStartValue(QPoint(x, height()));
112         zoomInButtonAnimation->setEndValue(QPoint(x,y));
113         zoomInButtonAnimation->setDuration(400);
114         zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
115 //==================================================================
116         x = 0;
117         y = height() - copyButton->sizeHint().height();
118
119         copyButton->move(QPoint(x, height()));
120
121         copyButtonAnimation =
122                 new QPropertyAnimation(copyButton, "pos", this);
123
124         copyButtonAnimation->setStartValue(QPoint(x, height()));
125         copyButtonAnimation->setEndValue(QPoint(x,y));
126         copyButtonAnimation->setDuration(200);
127         copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
128 //==================================================================
129         x = 0;
130         y = height() - 2*copyButton->sizeHint().height();
131
132         selectAllButton->move(QPoint(x, height()));
133
134         selectAllButtonAnimation =
135                 new QPropertyAnimation(selectAllButton, "pos", this);
136
137         selectAllButtonAnimation->setStartValue(QPoint(x, height()));
138         selectAllButtonAnimation->setEndValue(QPoint(x,y));
139         selectAllButtonAnimation->setDuration(400);
140         selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
141 //==================================================================
142         x = 0;
143         y = height() - 3*copyButton->sizeHint().height();
144
145         searchButton->move(QPoint(x, height()));
146
147         searchButtonAnimation =
148                 new QPropertyAnimation(searchButton, "pos", this);
149
150         searchButtonAnimation->setStartValue(QPoint(x, height()));
151         searchButtonAnimation->setEndValue(QPoint(x,y));
152         searchButtonAnimation->setDuration(600);
153         searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
154 //==================================================================
155
156         buttonsAnimation = new QParallelAnimationGroup(this);
157         buttonsAnimation->addAnimation(zoomInButtonAnimation);
158         buttonsAnimation->addAnimation(zoomOutButtonAnimation);
159         buttonsAnimation->addAnimation(selectAllButtonAnimation);
160         buttonsAnimation->addAnimation(copyButtonAnimation);
161         buttonsAnimation->addAnimation(searchButtonAnimation);
162         buttonsInitialized = true;
163         buttonsVisible = false;
164
165         connect(showButtonsButton, SIGNAL(toggled(bool)),
166                 this, SLOT(showButtons(bool)));
167 }
168 #endif
169
170 void TranslationWidget::initializeUI() {
171
172     textEdit = new TranslationTextEdit;
173     textEdit->setReadOnly(true);
174
175     resizer = new TranslationWidgetAutoResizer(textEdit);
176     connect(this, SIGNAL(updateSize()),
177             resizer, SLOT(textEditChanged()));
178
179     QWidget*w = new QWidget;
180     verticalLayout = new QVBoxLayout(w);
181     verticalLayout->addWidget(textEdit);
182
183     textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
184     textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
185
186
187     this->setWidget(w);
188     this->setWidgetResizable(true);
189
190
191     #ifdef Q_WS_MAEMO_5
192         zoomInButton = new QToolButton(this);
193         zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
194         zoomInButton->setMinimumSize(zoomInButton->sizeHint());
195
196         zoomOutButton = new QToolButton(this);
197         zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
198         zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
199
200         selectAllButton = new QToolButton(this);
201         selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
202         selectAllButton->setMinimumSize(selectAllButton->sizeHint());
203
204         copyButton = new QToolButton(this);
205         copyButton->setIcon(QIcon::fromTheme("general_notes"));
206         copyButton->setMinimumSize(copyButton->sizeHint());
207         copyButton->setEnabled(false);
208
209         searchButton = new QToolButton(this);
210         searchButton->setIcon(QIcon::fromTheme("general_search"));
211         searchButton->setMinimumSize(searchButton->sizeHint());
212         searchButton->setEnabled(false);
213
214         showButtonsButton = new QToolButton(this);
215         showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
216         showButtonsButton->setMinimumSize(searchButton->sizeHint());
217
218         connect(zoomInButton, SIGNAL(clicked()),
219                 textEdit, SLOT(zoomIn()));
220
221         connect(zoomOutButton, SIGNAL(clicked()),
222                 textEdit, SLOT(zoomOut()));
223
224
225         connect(searchButton, SIGNAL(clicked()),
226                 this, SLOT(searchSelected()));
227
228         connect(copyButton, SIGNAL(clicked()),
229                 textEdit, SLOT(copy()));
230
231         connect(textEdit, SIGNAL(copyAvailable(bool)),
232                 searchButton, SLOT(setEnabled(bool)));
233
234         connect(textEdit, SIGNAL(copyAvailable(bool)),
235                 copyButton, SLOT(setEnabled(bool)));
236
237         connect(selectAllButton, SIGNAL(clicked()),
238                 textEdit, SLOT(selectAll()));
239
240         buttonsInitialized = false;
241
242
243
244         grabZoomKeys(true);
245     #endif
246 }
247
248 void TranslationWidget::searchSelected() {
249     #ifdef Q_WS_MAEMO_5
250         hide();
251     #endif
252     emit search(textEdit->textCursor().selectedText());
253 }
254
255 #ifdef Q_WS_MAEMO_5
256 void TranslationWidget::showButtons(bool show) {
257     if(show && !buttonsVisible) {
258         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
259         buttonsAnimation->start();
260         buttonsVisible = true;
261     }
262     else if(!show && buttonsVisible) {
263         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
264         buttonsAnimation->start();
265         buttonsVisible = false;
266     }
267 }
268
269 void TranslationWidget::grabZoomKeys(bool grab) {
270      if (!winId()) {
271          return;
272      }
273
274     unsigned long val = (grab) ? 1 : 0;
275     Atom atom = XInternAtom(QX11Info::display(),
276                             "_HILDON_ZOOM_KEY_ATOM", False);
277     if (!atom) {
278         return;
279     }
280
281     XChangeProperty (QX11Info::display(),
282          winId(),
283          atom,
284          XA_INTEGER,
285          32,
286          PropModeReplace,
287          reinterpret_cast<unsigned char *>(&val),
288          1);
289 }
290
291 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
292     switch (event->key()) {
293         case Qt::Key_F7:
294         textEdit->zoomIn();
295         event->accept();
296         break;
297
298         case Qt::Key_F8:
299         textEdit->zoomOut();
300         event->accept();
301         break;
302     }
303     QWidget::keyPressEvent(event);
304 }
305 #endif
306
307
308