1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 //! \file TranslationWidget.cpp
23 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25 #include "TranslationWidget.h"
28 #include <QtGui/QX11Info>
30 #include <X11/Xatom.h>
34 TranslationWidget::TranslationWidget(QWidget *parent):
37 guiinterface = qobject_cast<GUIInterface*>(parent);
40 setAttribute(Qt::WA_Maemo5StackedWindow);
41 setWindowFlags(windowFlags() | Qt::Window);
47 setWindowTitle("mDictionary");
49 connect(webkit, SIGNAL(search()),
50 this, SLOT(searchSelected()));
52 QFile file(":/xsl/xsl.xsl");
53 if(!file.open(QFile::ReadOnly))
54 qDebug()<<"error can't open a xslt file";
60 void TranslationWidget::show() {
65 void TranslationWidget::show(QStringList translations) {
69 if(!buttonsInitialized)
73 // webkit->repaint(this->rect());
74 // update(this->rect());
79 foreach(t, translations) {
84 trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
85 + trans + QString("\n </ar>");
87 trans=checkTranslation(trans);
88 trans=XslConversion(trans);
90 qDebug()<<"\n\n trans"<<trans;
92 QString head = "<html><head>";
93 head += "<link rel=\"stylesheet\" type=\"text/css\" " ;
94 head += "href=\"/usr/share/mdictionary/css/" "style.css";
95 head += "\" /></head><body ondragstart=\"return false\">";
98 trans+= "</body></html>";
99 trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
101 webkit->setHtml(trans, QUrl().fromLocalFile("/"));
102 //webkit->repaint(this->rect());
103 //update(this->rect());
105 // Q_EMIT updateSize();
108 QString TranslationWidget::XslConversion(QString translation)
110 QXmlQuery myQuery(QXmlQuery::XSLT20);
111 myQuery.setFocus(translation);
112 myQuery.setQuery(xslt);
114 myQuery.evaluateTo(&result);
118 QString TranslationWidget::checkTranslation(QString trans){
125 pos=trans.indexOf("&",pos+1);
126 pos2=trans.indexOf("amp;",pos);
127 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
129 pos2=trans.indexOf("gt;",pos);
130 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
132 pos2=trans.indexOf("lt;",pos);
133 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
135 pos2=trans.indexOf("apos;",pos);
136 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
138 pos2=trans.indexOf("quot;",pos);
139 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
142 trans.insert(pos+1,"amp;");
146 /*check all "<" and ">" in key */
149 while(pos<trans.size()){
150 if(pos==trans.indexOf("<key>",pos)){
155 else if(pos==trans.indexOf("</key>",pos))
157 else if(count==1 && (trans.at(pos)=='<' || trans.at(pos)=='>' ))
163 /*check all """ and "'" */
166 while(pos<trans.size()){
167 if(trans.at(pos)=='<')
169 else if(trans.at(pos)=='>')
171 else if(trans.at(pos)=='\"' && count==0)
172 trans.replace(pos,1,""");
173 else if(trans.at(pos)=='\'' && count==0)
174 trans.replace(pos,1,"'");
181 void TranslationWidget::initButtons() {
183 int x = width() - showButtonsButton->sizeHint().width();
184 int y = height() - showButtonsButton->sizeHint().height();
186 showButtonsButton->move(QPoint(x,y));
187 showButtonsButton->show();
191 x = width() - zoomOutButton->sizeHint().width();
192 y = height() - 2*zoomOutButton->sizeHint().height();
193 zoomOutButton->move(QPoint(x, height()));
195 zoomOutButtonAnimation =
196 new QPropertyAnimation(zoomOutButton, "pos", this);
198 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
199 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
200 zoomOutButtonAnimation->setDuration(200);
201 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
205 x = width() - zoomInButton->sizeHint().width();
206 y = height() - 3*zoomInButton->sizeHint().height();
207 zoomInButton->move(QPoint(x, height()));
209 zoomInButtonAnimation =
210 new QPropertyAnimation(zoomInButton, "pos", this);
212 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
213 zoomInButtonAnimation->setEndValue(QPoint(x,y));
214 zoomInButtonAnimation->setDuration(400);
215 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
220 y = height() - copyButton->sizeHint().height();
222 copyButton->move(QPoint(x, height()));
224 copyButtonAnimation =
225 new QPropertyAnimation(copyButton, "pos", this);
227 copyButtonAnimation->setStartValue(QPoint(x, height()));
228 copyButtonAnimation->setEndValue(QPoint(x,y));
229 copyButtonAnimation->setDuration(200);
230 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
235 y = height() - 2*copyButton->sizeHint().height();
237 selectAllButton->move(QPoint(x, height()));
239 selectAllButtonAnimation =
240 new QPropertyAnimation(selectAllButton, "pos", this);
242 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
243 selectAllButtonAnimation->setEndValue(QPoint(x,y));
244 selectAllButtonAnimation->setDuration(400);
245 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
250 y = height() - 3*copyButton->sizeHint().height();
252 searchButton->move(QPoint(x, height()));
254 searchButtonAnimation =
255 new QPropertyAnimation(searchButton, "pos", this);
257 searchButtonAnimation->setStartValue(QPoint(x, height()));
258 searchButtonAnimation->setEndValue(QPoint(x,y));
259 searchButtonAnimation->setDuration(600);
260 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
265 buttonsAnimation = new QParallelAnimationGroup(this);
266 buttonsAnimation->addAnimation(zoomInButtonAnimation);
267 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
268 buttonsAnimation->addAnimation(selectAllButtonAnimation);
269 buttonsAnimation->addAnimation(copyButtonAnimation);
270 buttonsAnimation->addAnimation(searchButtonAnimation);
271 buttonsInitialized = true;
272 buttonsVisible = false;
274 connect(showButtonsButton, SIGNAL(clicked()),
275 this, SLOT(showButtons()));
279 void TranslationWidget::initializeUI() {
281 webkit = new TranslationView(this);
282 Settings* set = guiinterface->settings();
283 qreal fac = set->value("zoom").toFloat();
286 webkit->setZoomFactor(fac);
289 QWidget*w = new QWidget(this);
290 verticalLayout = new QVBoxLayout(w);
291 verticalLayout->addWidget(webkit);
294 this->setWidgetResizable(true);
297 zoomInButton = new QToolButton(this);
298 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
299 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
301 zoomOutButton = new QToolButton(this);
302 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
303 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
305 selectAllButton = new QToolButton(this);
306 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
307 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
309 copyButton = new QToolButton(this);
310 copyButton->setIcon(QIcon::fromTheme("general_notes"));
311 copyButton->setMinimumSize(copyButton->sizeHint());
313 searchButton = new QToolButton(this);
314 searchButton->setIcon(QIcon::fromTheme("general_search"));
315 searchButton->setMinimumSize(searchButton->sizeHint());
317 showButtonsButton = new QToolButton(this);
318 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
319 showButtonsButton->setMinimumSize(searchButton->sizeHint());
321 connect(zoomInButton, SIGNAL(clicked()),
322 webkit, SLOT(zoomIn()));
324 connect(zoomOutButton, SIGNAL(clicked()),
325 webkit, SLOT(zoomOut()));
328 connect(searchButton, SIGNAL(clicked()),
329 this, SLOT(searchSelected()));
331 connect(copyButton, SIGNAL(clicked()),
332 this, SLOT(copySelected()));
334 connect(selectAllButton, SIGNAL(clicked()),
335 webkit, SLOT(selectAll()));
337 buttonsInitialized = false;
345 void TranslationWidget::searchSelected() {
346 if(webkit->selectedText().isEmpty()) {
347 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
353 Q_EMIT search(webkit->selectedText().toLower());
357 void TranslationWidget::copySelected() {
358 if(webkit->selectedText().isEmpty()) {
359 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
362 Q_EMIT notify(Notify::Info, tr("Copyied selected text"));
367 void TranslationWidget::showButtons() {
368 if(!buttonsVisible) {
369 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
370 buttonsAnimation->start();
371 buttonsVisible = true;
373 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
375 else if(buttonsVisible) {
376 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
377 buttonsAnimation->start();
378 buttonsVisible = false;
379 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
383 void TranslationWidget::grabZoomKeys(bool grab) {
388 unsigned long val = (grab) ? 1 : 0;
389 Atom atom = XInternAtom(QX11Info::display(),
390 "_HILDON_ZOOM_KEY_ATOM", False);
395 XChangeProperty (QX11Info::display(),
401 reinterpret_cast<unsigned char *>(&val),
405 void TranslationWidget::hideEvent(QHideEvent* e) {
409 QScrollArea::hideEvent(e);
412 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
413 switch (event->key()) {
424 QWidget::keyPressEvent(event);
429 void TranslationWidget::updateZoom(qreal factor) {
430 Settings* set = guiinterface->settings();
431 set->setValue("zoom", QString("%1").arg(factor));
432 guiinterface->setSettings(set);