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) {
83 trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
84 + trans + QString("\n </ar>");
87 trans=XslConversion(trans);
89 QString head = "<html><head>";
90 head += "<link rel=\"stylesheet\" type=\"text/css\" " ;
91 head += "href=\"/usr/share/mdictionary/css/" "style.css";
92 head += "\" /></head><body ondragstart=\"return false\">";
95 trans+= "</body></html>";
96 trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
98 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 TranslationView(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());
249 searchButton = new QToolButton(this);
250 searchButton->setIcon(QIcon::fromTheme("general_search"));
251 searchButton->setMinimumSize(searchButton->sizeHint());
253 showButtonsButton = new QToolButton(this);
254 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
255 showButtonsButton->setMinimumSize(searchButton->sizeHint());
257 connect(zoomInButton, SIGNAL(clicked()),
258 webkit, SLOT(zoomIn()));
260 connect(zoomOutButton, SIGNAL(clicked()),
261 webkit, SLOT(zoomOut()));
264 connect(searchButton, SIGNAL(clicked()),
265 this, SLOT(searchSelected()));
267 connect(copyButton, SIGNAL(clicked()),
268 this, SLOT(copySelected()));
270 connect(selectAllButton, SIGNAL(clicked()),
271 webkit, SLOT(selectAll()));
273 buttonsInitialized = false;
281 void TranslationWidget::searchSelected() {
282 if(webkit->selectedText().isEmpty()) {
283 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
289 Q_EMIT search(webkit->selectedText().toLower());
293 void TranslationWidget::copySelected() {
294 if(webkit->selectedText().isEmpty()) {
295 Q_EMIT notify(Notify::Warning, tr("No text is selected"));
298 Q_EMIT notify(Notify::Info, tr("Copyied selected text"));
303 void TranslationWidget::showButtons() {
304 if(!buttonsVisible) {
305 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
306 buttonsAnimation->start();
307 buttonsVisible = true;
309 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
311 else if(buttonsVisible) {
312 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
313 buttonsAnimation->start();
314 buttonsVisible = false;
315 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
319 void TranslationWidget::grabZoomKeys(bool grab) {
324 unsigned long val = (grab) ? 1 : 0;
325 Atom atom = XInternAtom(QX11Info::display(),
326 "_HILDON_ZOOM_KEY_ATOM", False);
331 XChangeProperty (QX11Info::display(),
337 reinterpret_cast<unsigned char *>(&val),
341 void TranslationWidget::hideEvent(QHideEvent* e) {
345 QScrollArea::hideEvent(e);
348 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
349 switch (event->key()) {
360 QWidget::keyPressEvent(event);
365 void TranslationWidget::updateZoom(qreal factor) {
366 Settings* set = guiinterface->settings();
367 set->setValue("zoom", QString("%1").arg(factor));
368 guiinterface->setSettings(set);