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