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>
33 TranslationWidget::TranslationWidget(QWidget *parent):
36 guiinterface = qobject_cast<GUIInterface*>(parent);
39 setAttribute(Qt::WA_Maemo5StackedWindow);
40 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 // webkit->repaint(this->rect());
73 // update(this->rect());
78 foreach(t, translations) {
82 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>")
83 + trans + tr("\n </ar>");
84 trans=XslConversion(trans);
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\">";
92 trans+= "</body></html>";
93 trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
97 webkit->setHtml(trans, QUrl().fromLocalFile("/"));
100 //webkit->repaint(this->rect());
101 //update(this->rect());
103 // Q_EMIT updateSize();
106 QString TranslationWidget::XslConversion(QString translation)
108 QXmlQuery myQuery(QXmlQuery::XSLT20);
109 myQuery.setFocus(translation);
110 myQuery.setQuery(xslt);
112 myQuery.evaluateTo(&result);
118 void TranslationWidget::initButtons() {
120 int x = width() - showButtonsButton->sizeHint().width();
121 int y = height() - showButtonsButton->sizeHint().height();
123 showButtonsButton->move(QPoint(x,y));
124 showButtonsButton->show();
128 x = width() - zoomOutButton->sizeHint().width();
129 y = height() - 2*zoomOutButton->sizeHint().height();
130 zoomOutButton->move(QPoint(x, height()));
132 zoomOutButtonAnimation =
133 new QPropertyAnimation(zoomOutButton, "pos", this);
135 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
136 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
137 zoomOutButtonAnimation->setDuration(200);
138 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
142 x = width() - zoomInButton->sizeHint().width();
143 y = height() - 3*zoomInButton->sizeHint().height();
144 zoomInButton->move(QPoint(x, height()));
146 zoomInButtonAnimation =
147 new QPropertyAnimation(zoomInButton, "pos", this);
149 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
150 zoomInButtonAnimation->setEndValue(QPoint(x,y));
151 zoomInButtonAnimation->setDuration(400);
152 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
157 y = height() - copyButton->sizeHint().height();
159 copyButton->move(QPoint(x, height()));
161 copyButtonAnimation =
162 new QPropertyAnimation(copyButton, "pos", this);
164 copyButtonAnimation->setStartValue(QPoint(x, height()));
165 copyButtonAnimation->setEndValue(QPoint(x,y));
166 copyButtonAnimation->setDuration(200);
167 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
172 y = height() - 2*copyButton->sizeHint().height();
174 selectAllButton->move(QPoint(x, height()));
176 selectAllButtonAnimation =
177 new QPropertyAnimation(selectAllButton, "pos", this);
179 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
180 selectAllButtonAnimation->setEndValue(QPoint(x,y));
181 selectAllButtonAnimation->setDuration(400);
182 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
187 y = height() - 3*copyButton->sizeHint().height();
189 searchButton->move(QPoint(x, height()));
191 searchButtonAnimation =
192 new QPropertyAnimation(searchButton, "pos", this);
194 searchButtonAnimation->setStartValue(QPoint(x, height()));
195 searchButtonAnimation->setEndValue(QPoint(x,y));
196 searchButtonAnimation->setDuration(600);
197 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
202 buttonsAnimation = new QParallelAnimationGroup(this);
203 buttonsAnimation->addAnimation(zoomInButtonAnimation);
204 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
205 buttonsAnimation->addAnimation(selectAllButtonAnimation);
206 buttonsAnimation->addAnimation(copyButtonAnimation);
207 buttonsAnimation->addAnimation(searchButtonAnimation);
208 buttonsInitialized = true;
209 buttonsVisible = false;
211 connect(showButtonsButton, SIGNAL(clicked()),
212 this, SLOT(showButtons()));
216 void TranslationWidget::initializeUI() {
218 webkit = new TranslationTextEdit(this);
219 Settings* set = guiinterface->settings();
220 qreal fac = set->value("zoom").toFloat();
223 webkit->setZoomFactor(fac);
226 QWidget*w = new QWidget(this);
227 verticalLayout = new QVBoxLayout(w);
228 verticalLayout->addWidget(webkit);
231 this->setWidgetResizable(true);
234 zoomInButton = new QToolButton(this);
235 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
236 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
238 zoomOutButton = new QToolButton(this);
239 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
240 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
242 selectAllButton = new QToolButton(this);
243 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
244 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
246 copyButton = new QToolButton(this);
247 copyButton->setIcon(QIcon::fromTheme("general_notes"));
248 copyButton->setMinimumSize(copyButton->sizeHint());
249 copyButton->setEnabled(false);
251 searchButton = new QToolButton(this);
252 searchButton->setIcon(QIcon::fromTheme("general_search"));
253 searchButton->setMinimumSize(searchButton->sizeHint());
254 searchButton->setEnabled(false);
256 showButtonsButton = new QToolButton(this);
257 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
258 showButtonsButton->setMinimumSize(searchButton->sizeHint());
260 connect(zoomInButton, SIGNAL(clicked()),
261 webkit, SLOT(zoomIn()));
263 connect(zoomOutButton, SIGNAL(clicked()),
264 webkit, SLOT(zoomOut()));
267 connect(searchButton, SIGNAL(clicked()),
268 this, SLOT(searchSelected()));
270 connect(copyButton, SIGNAL(clicked()),
271 webkit, SLOT(copy()));
273 connect(webkit, SIGNAL(copyAvailable(bool)),
274 searchButton, SLOT(setEnabled(bool)));
276 connect(webkit, SIGNAL(copyAvailable(bool)),
277 copyButton, SLOT(setEnabled(bool)));
279 connect(selectAllButton, SIGNAL(clicked()),
280 webkit, SLOT(selectAll()));
282 buttonsInitialized = false;
290 void TranslationWidget::searchSelected() {
294 Q_EMIT search(webkit->selectedText().toLower());
298 void TranslationWidget::showButtons() {
299 if(!buttonsVisible) {
300 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
301 buttonsAnimation->start();
302 buttonsVisible = true;
304 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
306 else if(buttonsVisible) {
307 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
308 buttonsAnimation->start();
309 buttonsVisible = false;
310 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
314 void TranslationWidget::grabZoomKeys(bool grab) {
319 unsigned long val = (grab) ? 1 : 0;
320 Atom atom = XInternAtom(QX11Info::display(),
321 "_HILDON_ZOOM_KEY_ATOM", False);
326 XChangeProperty (QX11Info::display(),
332 reinterpret_cast<unsigned char *>(&val),
336 void TranslationWidget::hideEvent(QHideEvent* e) {
340 QScrollArea::hideEvent(e);
343 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
344 switch (event->key()) {
355 QWidget::keyPressEvent(event);
360 void TranslationWidget::updateZoom(qreal factor) {
361 Settings* set = guiinterface->settings();
362 set->setValue("zoom", QString("%1").arg(factor));
363 guiinterface->setSettings(set);