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 \brief Displays translation of a word found in dictionaries
25 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
28 #include "TranslationWidget.h"
31 #include <QtGui/QX11Info>
33 #include <X11/Xatom.h>
37 TranslationWidget::TranslationWidget(QWidget *parent):
40 guiinterface = qobject_cast<GUIInterface*>(parent);
43 setAttribute(Qt::WA_Maemo5StackedWindow);
44 setWindowFlags(windowFlags() | Qt::Window);
49 setWindowTitle("mDictionary");
51 connect(webkit, SIGNAL(search()),
52 this, SLOT(searchSelected()));
54 QFile file(":/xsl/xsl.xsl");
55 if(!file.open(QFile::ReadOnly))
56 qDebug()<<"error can't open a xslt file";
62 void TranslationWidget::show() {
67 void TranslationWidget::show(QStringList translations) {
71 if(!buttonsInitialized)
75 QString trans= createTranslation(translations);
77 QString head = "<html><head>";
78 head += "<link rel=\"stylesheet\" type=\"text/css\" "
79 "href=\"/usr/share/mdictionary/css/" "style.css"
80 "\" /></head><body ondragstart=\"return false\">";
81 trans = head + trans + "</body></html>";
83 trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
85 webkit->setHtml(trans, QUrl().fromLocalFile("/"));
89 QString TranslationWidget::createTranslation(QStringList translations) {
91 if(!translations.size())
92 return QString("Empty translation");
96 foreach(QString t, translations){
101 trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
102 + trans + QString("\n </ar>");
104 trans=checkTranslation(trans);
105 trans=XslConversion(trans);
107 if(element>translations.size())
108 return QString("Empty translation");
109 } while(trans==""); // trans=="" only when XslConversation have a error;
114 QString TranslationWidget::XslConversion(QString translation)
116 QXmlQuery myQuery(QXmlQuery::XSLT20);
117 myQuery.setFocus(translation);
118 myQuery.setQuery(xslt);
120 myQuery.evaluateTo(&result);
125 QString TranslationWidget::checkTranslation(QString trans){
131 /*check "<" and ">" in key */
133 pos=trans.indexOf("&",pos+1);
134 pos2=trans.indexOf("amp;",pos);
135 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
137 pos2=trans.indexOf("gt;",pos);
138 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
140 pos2=trans.indexOf("lt;",pos);
141 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
143 pos2=trans.indexOf("apos;",pos);
144 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
146 pos2=trans.indexOf("quot;",pos);
147 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
150 trans.insert(pos+1,"amp;");
153 /*check "<" and ">" in key */
156 while(pos<trans.size()){
157 if(pos==trans.indexOf("<key>",pos)){
161 else if(pos==trans.indexOf("</key>",pos))
163 else if(count==1 && (trans.at(pos)=='<' || trans.at(pos)=='>' ))
168 /*checking unclosed tag's*/
171 while(pos<trans.size()){
172 if(trans.at(pos)=='<')
174 else if(trans.at(pos)=='>')
176 if(count==-1){ // remove ">"
185 if(trans.at(pos)=='>')
187 else if(trans.at(pos)=='<')
189 if(count==-1){ // remove "<"
196 /*check """ and "'" */
199 while(pos<trans.size()){
200 if(trans.at(pos)=='<')
202 else if(trans.at(pos)=='>')
204 else if(trans.at(pos)=='\"' && count==0)
205 trans.replace(pos,1,""");
206 else if(trans.at(pos)=='\'' && count==0)
207 trans.replace(pos,1,"'");
215 void TranslationWidget::initButtons() {
216 int x = width() - showButtonsButton->sizeHint().width();
217 int y = height() - showButtonsButton->sizeHint().height();
219 showButtonsButton->move(QPoint(x,y));
220 showButtonsButton->show();
222 x = width() - zoomOutButton->sizeHint().width();
223 y = height() - 2*zoomOutButton->sizeHint().height();
224 zoomOutButton->move(QPoint(x, height()));
226 zoomOutButtonAnimation = new QPropertyAnimation(zoomOutButton,
228 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
229 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
230 zoomOutButtonAnimation->setDuration(200);
231 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
233 x = width() - zoomInButton->sizeHint().width();
234 y = height() - 3*zoomInButton->sizeHint().height();
235 zoomInButton->move(QPoint(x, height()));
237 zoomInButtonAnimation = new QPropertyAnimation(zoomInButton,
239 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
240 zoomInButtonAnimation->setEndValue(QPoint(x,y));
241 zoomInButtonAnimation->setDuration(400);
242 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
245 y = height() - copyButton->sizeHint().height();
247 copyButton->move(QPoint(x, height()));
248 copyButtonAnimation = new QPropertyAnimation(copyButton, "pos", this);
249 copyButtonAnimation->setStartValue(QPoint(x, height()));
250 copyButtonAnimation->setEndValue(QPoint(x,y));
251 copyButtonAnimation->setDuration(200);
252 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
255 y = height() - 2*copyButton->sizeHint().height();
257 selectAllButton->move(QPoint(x, height()));
258 selectAllButtonAnimation = new QPropertyAnimation(selectAllButton,
260 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
261 selectAllButtonAnimation->setEndValue(QPoint(x,y));
262 selectAllButtonAnimation->setDuration(400);
263 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
266 y = height() - 3*copyButton->sizeHint().height();
268 searchButton->move(QPoint(x, height()));
269 searchButtonAnimation = new QPropertyAnimation(searchButton,
271 searchButtonAnimation->setStartValue(QPoint(x, height()));
272 searchButtonAnimation->setEndValue(QPoint(x,y));
273 searchButtonAnimation->setDuration(600);
274 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
276 buttonsAnimation = new QParallelAnimationGroup(this);
277 buttonsAnimation->addAnimation(zoomInButtonAnimation);
278 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
279 buttonsAnimation->addAnimation(selectAllButtonAnimation);
280 buttonsAnimation->addAnimation(copyButtonAnimation);
281 buttonsAnimation->addAnimation(searchButtonAnimation);
282 buttonsInitialized = true;
283 buttonsVisible = false;
285 connect(showButtonsButton, SIGNAL(clicked()),
286 this, SLOT(showButtons()));
290 void TranslationWidget::initializeUI() {
291 webkit = new TranslationView(this);
292 Settings* set = guiinterface->settings();
293 qreal fac = set->value("zoom").toFloat();
296 webkit->setZoomFactor(fac);
299 QWidget*w = new QWidget(this);
300 verticalLayout = new QVBoxLayout(w);
301 verticalLayout->addWidget(webkit);
304 this->setWidgetResizable(true);
307 zoomInButton = new QToolButton(this);
308 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
309 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
311 zoomOutButton = new QToolButton(this);
312 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
313 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
315 selectAllButton = new QToolButton(this);
316 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
317 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
319 copyButton = new QToolButton(this);
320 copyButton->setIcon(QIcon::fromTheme("general_notes"));
321 copyButton->setMinimumSize(copyButton->sizeHint());
323 searchButton = new QToolButton(this);
324 searchButton->setIcon(QIcon::fromTheme("general_search"));
325 searchButton->setMinimumSize(searchButton->sizeHint());
327 showButtonsButton = new QToolButton(this);
328 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
329 showButtonsButton->setMinimumSize(searchButton->sizeHint());
331 connect(zoomInButton, SIGNAL(clicked()),
332 webkit, SLOT(zoomIn()));
334 connect(zoomOutButton, SIGNAL(clicked()),
335 webkit, SLOT(zoomOut()));
338 connect(searchButton, SIGNAL(clicked()),
339 this, SLOT(searchSelected()));
341 connect(copyButton, SIGNAL(clicked()),
342 this, SLOT(copySelected()));
344 connect(selectAllButton, SIGNAL(clicked()),
345 webkit, SLOT(selectAll()));
347 buttonsInitialized = false;
355 void TranslationWidget::searchSelected() {
356 if(webkit->selectedText().isEmpty()) {
357 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
363 Q_EMIT search(webkit->selectedText().toLower());
367 void TranslationWidget::copySelected() {
368 if(webkit->selectedText().isEmpty()) {
369 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
372 Q_EMIT notify(Notify::Info, tr("Copyied selected text"));
377 void TranslationWidget::showButtons() {
378 if(!buttonsVisible) {
379 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
380 buttonsAnimation->start();
381 buttonsVisible = true;
383 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
385 else if(buttonsVisible) {
386 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
387 buttonsAnimation->start();
388 buttonsVisible = false;
389 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
393 void TranslationWidget::grabZoomKeys(bool grab) {
398 unsigned long val = (grab) ? 1 : 0;
399 Atom atom = XInternAtom(QX11Info::display(),
400 "_HILDON_ZOOM_KEY_ATOM", False);
405 XChangeProperty (QX11Info::display(),
411 reinterpret_cast<unsigned char *>(&val),
415 void TranslationWidget::hideEvent(QHideEvent* e) {
419 QScrollArea::hideEvent(e);
422 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
423 switch (event->key()) {
434 QWidget::keyPressEvent(event);
439 void TranslationWidget::updateZoom(qreal factor) {
440 Settings* set = guiinterface->settings();
441 set->setValue("zoom", QString("%1").arg(factor));
442 guiinterface->setSettings(set);