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);
46 setWindowTitle("mDictionary");
48 connect(webkit, SIGNAL(search()),
49 this, SLOT(searchSelected()));
51 QFile file(":/xsl/xsl.xsl");
52 if(!file.open(QFile::ReadOnly))
53 qDebug()<<"error can't open a xslt file";
59 void TranslationWidget::show() {
64 void TranslationWidget::show(QStringList translations) {
68 if(!buttonsInitialized)
72 QString trans= createTranslation(translations);
74 QString head = "<html><head>";
75 head += "<link rel=\"stylesheet\" type=\"text/css\" "
76 "href=\"/usr/share/mdictionary/css/" "style.css"
77 "\" /></head><body ondragstart=\"return false\">";
78 trans = head + trans + "</body></html>";
80 trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
82 webkit->setHtml(trans, QUrl().fromLocalFile("/"));
86 QString TranslationWidget::createTranslation(QStringList translations) {
88 if(!translations.size())
89 return QString("Empty translation");
93 foreach(QString t, translations){
98 trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
99 + trans + QString("\n </ar>");
101 trans=checkTranslation(trans);
102 trans=XslConversion(trans);
104 if(element>translations.size())
105 return QString("Empty translation");
106 } while(trans==""); // trans=="" only when XslConversation have a error;
111 QString TranslationWidget::XslConversion(QString translation)
113 QXmlQuery myQuery(QXmlQuery::XSLT20);
114 myQuery.setFocus(translation);
115 myQuery.setQuery(xslt);
117 myQuery.evaluateTo(&result);
122 QString TranslationWidget::checkTranslation(QString trans){
128 /*check "<" and ">" in key */
130 pos=trans.indexOf("&",pos+1);
131 pos2=trans.indexOf("amp;",pos);
132 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
134 pos2=trans.indexOf("gt;",pos);
135 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
137 pos2=trans.indexOf("lt;",pos);
138 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
140 pos2=trans.indexOf("apos;",pos);
141 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
143 pos2=trans.indexOf("quot;",pos);
144 if(pos!=-1 && pos2!=-1 && pos+1==pos2)
147 trans.insert(pos+1,"amp;");
150 /*check "<" and ">" in key */
153 while(pos<trans.size()){
154 if(pos==trans.indexOf("<key>",pos)){
158 else if(pos==trans.indexOf("</key>",pos))
160 else if(count==1 && (trans.at(pos)=='<' || trans.at(pos)=='>' ))
165 /*checking unclosed tag's*/
168 while(pos<trans.size()){
169 if(trans.at(pos)=='<')
171 else if(trans.at(pos)=='>')
173 if(count==-1){ // remove ">"
182 if(trans.at(pos)=='>')
184 else if(trans.at(pos)=='<')
186 if(count==-1){ // remove "<"
193 /*check """ and "'" */
196 while(pos<trans.size()){
197 if(trans.at(pos)=='<')
199 else if(trans.at(pos)=='>')
201 else if(trans.at(pos)=='\"' && count==0)
202 trans.replace(pos,1,""");
203 else if(trans.at(pos)=='\'' && count==0)
204 trans.replace(pos,1,"'");
212 void TranslationWidget::initButtons() {
213 int x = width() - showButtonsButton->sizeHint().width();
214 int y = height() - showButtonsButton->sizeHint().height();
216 showButtonsButton->move(QPoint(x,y));
217 showButtonsButton->show();
219 x = width() - zoomOutButton->sizeHint().width();
220 y = height() - 2*zoomOutButton->sizeHint().height();
221 zoomOutButton->move(QPoint(x, height()));
223 zoomOutButtonAnimation = new QPropertyAnimation(zoomOutButton,
225 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
226 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
227 zoomOutButtonAnimation->setDuration(200);
228 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
230 x = width() - zoomInButton->sizeHint().width();
231 y = height() - 3*zoomInButton->sizeHint().height();
232 zoomInButton->move(QPoint(x, height()));
234 zoomInButtonAnimation = new QPropertyAnimation(zoomInButton,
236 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
237 zoomInButtonAnimation->setEndValue(QPoint(x,y));
238 zoomInButtonAnimation->setDuration(400);
239 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
242 y = height() - copyButton->sizeHint().height();
244 copyButton->move(QPoint(x, height()));
245 copyButtonAnimation = new QPropertyAnimation(copyButton, "pos", this);
246 copyButtonAnimation->setStartValue(QPoint(x, height()));
247 copyButtonAnimation->setEndValue(QPoint(x,y));
248 copyButtonAnimation->setDuration(200);
249 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
252 y = height() - 2*copyButton->sizeHint().height();
254 selectAllButton->move(QPoint(x, height()));
255 selectAllButtonAnimation = new QPropertyAnimation(selectAllButton,
257 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
258 selectAllButtonAnimation->setEndValue(QPoint(x,y));
259 selectAllButtonAnimation->setDuration(400);
260 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
263 y = height() - 3*copyButton->sizeHint().height();
265 searchButton->move(QPoint(x, height()));
266 searchButtonAnimation = new QPropertyAnimation(searchButton,
268 searchButtonAnimation->setStartValue(QPoint(x, height()));
269 searchButtonAnimation->setEndValue(QPoint(x,y));
270 searchButtonAnimation->setDuration(600);
271 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
273 buttonsAnimation = new QParallelAnimationGroup(this);
274 buttonsAnimation->addAnimation(zoomInButtonAnimation);
275 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
276 buttonsAnimation->addAnimation(selectAllButtonAnimation);
277 buttonsAnimation->addAnimation(copyButtonAnimation);
278 buttonsAnimation->addAnimation(searchButtonAnimation);
279 buttonsInitialized = true;
280 buttonsVisible = false;
282 connect(showButtonsButton, SIGNAL(clicked()),
283 this, SLOT(showButtons()));
287 void TranslationWidget::initializeUI() {
288 webkit = new TranslationView(this);
289 Settings* set = guiinterface->settings();
290 qreal fac = set->value("zoom").toFloat();
293 webkit->setZoomFactor(fac);
296 QWidget*w = new QWidget(this);
297 verticalLayout = new QVBoxLayout(w);
298 verticalLayout->addWidget(webkit);
301 this->setWidgetResizable(true);
304 zoomInButton = new QToolButton(this);
305 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
306 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
308 zoomOutButton = new QToolButton(this);
309 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
310 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
312 selectAllButton = new QToolButton(this);
313 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
314 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
316 copyButton = new QToolButton(this);
317 copyButton->setIcon(QIcon::fromTheme("general_notes"));
318 copyButton->setMinimumSize(copyButton->sizeHint());
320 searchButton = new QToolButton(this);
321 searchButton->setIcon(QIcon::fromTheme("general_search"));
322 searchButton->setMinimumSize(searchButton->sizeHint());
324 showButtonsButton = new QToolButton(this);
325 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
326 showButtonsButton->setMinimumSize(searchButton->sizeHint());
328 connect(zoomInButton, SIGNAL(clicked()),
329 webkit, SLOT(zoomIn()));
331 connect(zoomOutButton, SIGNAL(clicked()),
332 webkit, SLOT(zoomOut()));
335 connect(searchButton, SIGNAL(clicked()),
336 this, SLOT(searchSelected()));
338 connect(copyButton, SIGNAL(clicked()),
339 this, SLOT(copySelected()));
341 connect(selectAllButton, SIGNAL(clicked()),
342 webkit, SLOT(selectAll()));
344 buttonsInitialized = false;
352 void TranslationWidget::searchSelected() {
353 if(webkit->selectedText().isEmpty()) {
354 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
360 Q_EMIT search(webkit->selectedText().toLower());
364 void TranslationWidget::copySelected() {
365 if(webkit->selectedText().isEmpty()) {
366 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
369 Q_EMIT notify(Notify::Info, tr("Copyied selected text"));
374 void TranslationWidget::showButtons() {
375 if(!buttonsVisible) {
376 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
377 buttonsAnimation->start();
378 buttonsVisible = true;
380 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
382 else if(buttonsVisible) {
383 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
384 buttonsAnimation->start();
385 buttonsVisible = false;
386 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
390 void TranslationWidget::grabZoomKeys(bool grab) {
395 unsigned long val = (grab) ? 1 : 0;
396 Atom atom = XInternAtom(QX11Info::display(),
397 "_HILDON_ZOOM_KEY_ATOM", False);
402 XChangeProperty (QX11Info::display(),
408 reinterpret_cast<unsigned char *>(&val),
412 void TranslationWidget::hideEvent(QHideEvent* e) {
416 QScrollArea::hideEvent(e);
419 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
420 switch (event->key()) {
431 QWidget::keyPressEvent(event);
436 void TranslationWidget::updateZoom(qreal factor) {
437 Settings* set = guiinterface->settings();
438 set->setValue("zoom", QString("%1").arg(factor));
439 guiinterface->setSettings(set);