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/lib/mdictionary/staron.png");
96 webkit->setHtml(trans, QUrl().fromLocalFile("/"));
99 //webkit->repaint(this->rect());
100 //update(this->rect());
102 // Q_EMIT updateSize();
105 QString TranslationWidget::XslConversion(QString translation)
107 QXmlQuery myQuery(QXmlQuery::XSLT20);
108 myQuery.setFocus(translation);
109 myQuery.setQuery(xslt);
111 myQuery.evaluateTo(&result);
117 void TranslationWidget::initButtons() {
119 int x = width() - showButtonsButton->sizeHint().width();
120 int y = height() - showButtonsButton->sizeHint().height();
122 showButtonsButton->move(QPoint(x,y));
123 showButtonsButton->show();
127 x = width() - zoomOutButton->sizeHint().width();
128 y = height() - 2*zoomOutButton->sizeHint().height();
129 zoomOutButton->move(QPoint(x, height()));
131 zoomOutButtonAnimation =
132 new QPropertyAnimation(zoomOutButton, "pos", this);
134 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
135 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
136 zoomOutButtonAnimation->setDuration(200);
137 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
141 x = width() - zoomInButton->sizeHint().width();
142 y = height() - 3*zoomInButton->sizeHint().height();
143 zoomInButton->move(QPoint(x, height()));
145 zoomInButtonAnimation =
146 new QPropertyAnimation(zoomInButton, "pos", this);
148 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
149 zoomInButtonAnimation->setEndValue(QPoint(x,y));
150 zoomInButtonAnimation->setDuration(400);
151 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
156 y = height() - copyButton->sizeHint().height();
158 copyButton->move(QPoint(x, height()));
160 copyButtonAnimation =
161 new QPropertyAnimation(copyButton, "pos", this);
163 copyButtonAnimation->setStartValue(QPoint(x, height()));
164 copyButtonAnimation->setEndValue(QPoint(x,y));
165 copyButtonAnimation->setDuration(200);
166 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
171 y = height() - 2*copyButton->sizeHint().height();
173 selectAllButton->move(QPoint(x, height()));
175 selectAllButtonAnimation =
176 new QPropertyAnimation(selectAllButton, "pos", this);
178 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
179 selectAllButtonAnimation->setEndValue(QPoint(x,y));
180 selectAllButtonAnimation->setDuration(400);
181 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
186 y = height() - 3*copyButton->sizeHint().height();
188 searchButton->move(QPoint(x, height()));
190 searchButtonAnimation =
191 new QPropertyAnimation(searchButton, "pos", this);
193 searchButtonAnimation->setStartValue(QPoint(x, height()));
194 searchButtonAnimation->setEndValue(QPoint(x,y));
195 searchButtonAnimation->setDuration(600);
196 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
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;
210 connect(showButtonsButton, SIGNAL(clicked()),
211 this, SLOT(showButtons()));
215 void TranslationWidget::initializeUI() {
217 webkit = new TranslationTextEdit(this);
218 Settings* set = guiinterface->settings();
219 qreal fac = set->value("zoom").toFloat();
222 webkit->setZoomFactor(fac);
225 QWidget*w = new QWidget(this);
226 verticalLayout = new QVBoxLayout(w);
227 verticalLayout->addWidget(webkit);
230 this->setWidgetResizable(true);
233 zoomInButton = new QToolButton(this);
234 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
235 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
237 zoomOutButton = new QToolButton(this);
238 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
239 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
241 selectAllButton = new QToolButton(this);
242 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
243 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
245 copyButton = new QToolButton(this);
246 copyButton->setIcon(QIcon::fromTheme("general_notes"));
247 copyButton->setMinimumSize(copyButton->sizeHint());
248 copyButton->setEnabled(false);
250 searchButton = new QToolButton(this);
251 searchButton->setIcon(QIcon::fromTheme("general_search"));
252 searchButton->setMinimumSize(searchButton->sizeHint());
253 searchButton->setEnabled(false);
255 showButtonsButton = new QToolButton(this);
256 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
257 showButtonsButton->setMinimumSize(searchButton->sizeHint());
259 connect(zoomInButton, SIGNAL(clicked()),
260 webkit, SLOT(zoomIn()));
262 connect(zoomOutButton, SIGNAL(clicked()),
263 webkit, SLOT(zoomOut()));
266 connect(searchButton, SIGNAL(clicked()),
267 this, SLOT(searchSelected()));
269 connect(copyButton, SIGNAL(clicked()),
270 webkit, SLOT(copy()));
272 connect(webkit, SIGNAL(copyAvailable(bool)),
273 searchButton, SLOT(setEnabled(bool)));
275 connect(webkit, SIGNAL(copyAvailable(bool)),
276 copyButton, SLOT(setEnabled(bool)));
278 connect(selectAllButton, SIGNAL(clicked()),
279 webkit, SLOT(selectAll()));
281 buttonsInitialized = false;
289 void TranslationWidget::searchSelected() {
293 Q_EMIT search(webkit->selectedText().toLower());
297 void TranslationWidget::showButtons() {
298 if(!buttonsVisible) {
299 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
300 buttonsAnimation->start();
301 buttonsVisible = true;
303 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
305 else if(buttonsVisible) {
306 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
307 buttonsAnimation->start();
308 buttonsVisible = false;
309 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
313 void TranslationWidget::grabZoomKeys(bool grab) {
318 unsigned long val = (grab) ? 1 : 0;
319 Atom atom = XInternAtom(QX11Info::display(),
320 "_HILDON_ZOOM_KEY_ATOM", False);
325 XChangeProperty (QX11Info::display(),
331 reinterpret_cast<unsigned char *>(&val),
335 void TranslationWidget::hideEvent(QHideEvent* e) {
339 QScrollArea::hideEvent(e);
342 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
343 switch (event->key()) {
354 QWidget::keyPressEvent(event);
359 void TranslationWidget::updateZoom(qreal factor) {
360 Settings* set = guiinterface->settings();
361 set->setValue("zoom", QString("%1").arg(factor));
362 guiinterface->setSettings(set);