Fixed stylesheet handling and automatic default generation
[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()<<"error 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     textEdit->clear();
64
65     showMaximized();
66
67     #ifdef Q_WS_MAEMO_5
68         if(!buttonsInitialized)
69             initButtons();
70     #endif
71     textEdit->repaint(this->rect());
72     update(this->rect());
73
74     QString trans;
75     QString t;
76     foreach(t, translations) {
77         trans += t + "\n";
78     }
79
80     QImage image(":icons/16x16/staron.png");
81     QTextDocument *document = textEdit->document();
82     document->addResource(QTextDocument::ImageResource,
83             QUrl("mydata://image.png"), QVariant(image));
84
85     QFile cssFile(QDir::homePath() + "/.mdictionary/style.css");
86     cssFile.open(QFile::ReadOnly);
87     document->addResource(QTextDocument::StyleSheetResource,
88             QUrl("mydata://style.css"),
89             QVariant(cssFile.readAll()));
90
91
92     trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
93     trans=XslConversion(trans);
94
95     textEdit->insertHtml(trans);
96
97     textEdit->repaint(this->rect());
98     update(this->rect());
99
100     Q_EMIT updateSize();
101 }
102
103 QString TranslationWidget::XslConversion(QString translation)
104 {
105     QXmlQuery myQuery(QXmlQuery::XSLT20);
106     myQuery.setFocus(translation);
107     myQuery.setQuery(xslt);
108     QString result("");
109     myQuery.evaluateTo(&result);
110     return result;
111 }
112
113
114 #ifdef Q_WS_MAEMO_5
115 void TranslationWidget::initButtons() {
116
117         int x = width() - showButtonsButton->sizeHint().width();
118         int y = height() - showButtonsButton->sizeHint().height();
119
120         showButtonsButton->move(QPoint(x,y));
121         showButtonsButton->show();
122 //==================================================================
123
124         x = width() - zoomOutButton->sizeHint().width();
125         y = height() - 2*zoomOutButton->sizeHint().height();
126         zoomOutButton->move(QPoint(x, height()));
127
128         zoomOutButtonAnimation =
129                 new QPropertyAnimation(zoomOutButton, "pos", this);
130
131         zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
132         zoomOutButtonAnimation->setEndValue(QPoint(x,y));
133         zoomOutButtonAnimation->setDuration(200);
134         zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
135 //==================================================================
136         x = width() - zoomInButton->sizeHint().width();
137         y = height() - 3*zoomInButton->sizeHint().height();
138         zoomInButton->move(QPoint(x, height()));
139
140         zoomInButtonAnimation =
141                 new QPropertyAnimation(zoomInButton, "pos", this);
142
143         zoomInButtonAnimation->setStartValue(QPoint(x, height()));
144         zoomInButtonAnimation->setEndValue(QPoint(x,y));
145         zoomInButtonAnimation->setDuration(400);
146         zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
147 //==================================================================
148         x = 0;
149         y = height() - copyButton->sizeHint().height();
150
151         copyButton->move(QPoint(x, height()));
152
153         copyButtonAnimation =
154                 new QPropertyAnimation(copyButton, "pos", this);
155
156         copyButtonAnimation->setStartValue(QPoint(x, height()));
157         copyButtonAnimation->setEndValue(QPoint(x,y));
158         copyButtonAnimation->setDuration(200);
159         copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
160 //==================================================================
161         x = 0;
162         y = height() - 2*copyButton->sizeHint().height();
163
164         selectAllButton->move(QPoint(x, height()));
165
166         selectAllButtonAnimation =
167                 new QPropertyAnimation(selectAllButton, "pos", this);
168
169         selectAllButtonAnimation->setStartValue(QPoint(x, height()));
170         selectAllButtonAnimation->setEndValue(QPoint(x,y));
171         selectAllButtonAnimation->setDuration(400);
172         selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
173 //==================================================================
174         x = 0;
175         y = height() - 3*copyButton->sizeHint().height();
176
177         searchButton->move(QPoint(x, height()));
178
179         searchButtonAnimation =
180                 new QPropertyAnimation(searchButton, "pos", this);
181
182         searchButtonAnimation->setStartValue(QPoint(x, height()));
183         searchButtonAnimation->setEndValue(QPoint(x,y));
184         searchButtonAnimation->setDuration(600);
185         searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
186 //==================================================================
187
188         buttonsAnimation = new QParallelAnimationGroup(this);
189         buttonsAnimation->addAnimation(zoomInButtonAnimation);
190         buttonsAnimation->addAnimation(zoomOutButtonAnimation);
191         buttonsAnimation->addAnimation(selectAllButtonAnimation);
192         buttonsAnimation->addAnimation(copyButtonAnimation);
193         buttonsAnimation->addAnimation(searchButtonAnimation);
194         buttonsInitialized = true;
195         buttonsVisible = false;
196
197         connect(showButtonsButton, SIGNAL(clicked()),
198                 this, SLOT(showButtons()));
199 }
200 #endif
201
202 void TranslationWidget::initializeUI() {
203
204     textEdit = new TranslationTextEdit(this);
205     textEdit->setReadOnly(true);
206
207     resizer = new TranslationWidgetAutoResizer(textEdit);
208     connect(this, SIGNAL(updateSize()),
209             resizer, SLOT(textEditChanged()));
210
211     QWidget*w = new QWidget(this);
212     verticalLayout = new QVBoxLayout(w);
213     verticalLayout->addWidget(textEdit);
214
215     textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
216     textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
217
218     this->setWidget(w);
219     this->setWidgetResizable(true);
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(":/icons/48x48/edit-select-all.png"));
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_sent"));
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     Q_EMIT search(textEdit->textCursor().selectedText());
283 }
284
285 #ifdef Q_WS_MAEMO_5
286 void TranslationWidget::showButtons() {
287     if(!buttonsVisible) {
288         buttonsAnimation->setDirection(QAbstractAnimation::Forward);
289         buttonsAnimation->start();
290         buttonsVisible = true;
291
292         showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
293     }
294     else if(buttonsVisible) {
295         buttonsAnimation->setDirection(QAbstractAnimation::Backward);
296         buttonsAnimation->start();
297         buttonsVisible = false;
298         showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
299     }
300 }
301
302 void TranslationWidget::grabZoomKeys(bool grab) {
303      if (!winId()) {
304          return;
305      }
306
307     unsigned long val = (grab) ? 1 : 0;
308     Atom atom = XInternAtom(QX11Info::display(),
309                             "_HILDON_ZOOM_KEY_ATOM", False);
310     if (!atom) {
311         return;
312     }
313
314     XChangeProperty (QX11Info::display(),
315          winId(),
316          atom,
317          XA_INTEGER,
318          32,
319          PropModeReplace,
320          reinterpret_cast<unsigned char *>(&val),
321          1);
322 }
323
324 void TranslationWidget::hideEvent(QHideEvent* e) {
325     if(buttonsVisible)
326         showButtons();
327
328     QScrollArea::hideEvent(e);
329 }
330
331 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
332     switch (event->key()) {
333         case Qt::Key_F7:
334         textEdit->zoomIn();
335         event->accept();
336         break;
337
338         case Qt::Key_F8:
339         textEdit->zoomOut();
340         event->accept();
341         break;
342     }
343     QWidget::keyPressEvent(event);
344 }
345 #endif
346
347
348