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=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>")
84 + trans + tr("\n </ar>");
87 trans=XslConversion(trans);
89 QString head = "<html><head>";
90 head += "<link rel=\"stylesheet\" type=\"text/css\" " ;
91 head += "href=\"" + QDir::homePath() + "/.mdictionary/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("/"));
101 //webkit->repaint(this->rect());
102 //update(this->rect());
104 // Q_EMIT updateSize();
107 QString TranslationWidget::XslConversion(QString translation)
109 QXmlQuery myQuery(QXmlQuery::XSLT20);
110 myQuery.setFocus(translation);
111 myQuery.setQuery(xslt);
113 myQuery.evaluateTo(&result);
119 void TranslationWidget::initButtons() {
121 int x = width() - showButtonsButton->sizeHint().width();
122 int y = height() - showButtonsButton->sizeHint().height();
124 showButtonsButton->move(QPoint(x,y));
125 showButtonsButton->show();
129 x = width() - zoomOutButton->sizeHint().width();
130 y = height() - 2*zoomOutButton->sizeHint().height();
131 zoomOutButton->move(QPoint(x, height()));
133 zoomOutButtonAnimation =
134 new QPropertyAnimation(zoomOutButton, "pos", this);
136 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
137 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
138 zoomOutButtonAnimation->setDuration(200);
139 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
143 x = width() - zoomInButton->sizeHint().width();
144 y = height() - 3*zoomInButton->sizeHint().height();
145 zoomInButton->move(QPoint(x, height()));
147 zoomInButtonAnimation =
148 new QPropertyAnimation(zoomInButton, "pos", this);
150 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
151 zoomInButtonAnimation->setEndValue(QPoint(x,y));
152 zoomInButtonAnimation->setDuration(400);
153 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
158 y = height() - copyButton->sizeHint().height();
160 copyButton->move(QPoint(x, height()));
162 copyButtonAnimation =
163 new QPropertyAnimation(copyButton, "pos", this);
165 copyButtonAnimation->setStartValue(QPoint(x, height()));
166 copyButtonAnimation->setEndValue(QPoint(x,y));
167 copyButtonAnimation->setDuration(200);
168 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
173 y = height() - 2*copyButton->sizeHint().height();
175 selectAllButton->move(QPoint(x, height()));
177 selectAllButtonAnimation =
178 new QPropertyAnimation(selectAllButton, "pos", this);
180 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
181 selectAllButtonAnimation->setEndValue(QPoint(x,y));
182 selectAllButtonAnimation->setDuration(400);
183 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
188 y = height() - 3*copyButton->sizeHint().height();
190 searchButton->move(QPoint(x, height()));
192 searchButtonAnimation =
193 new QPropertyAnimation(searchButton, "pos", this);
195 searchButtonAnimation->setStartValue(QPoint(x, height()));
196 searchButtonAnimation->setEndValue(QPoint(x,y));
197 searchButtonAnimation->setDuration(600);
198 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
203 buttonsAnimation = new QParallelAnimationGroup(this);
204 buttonsAnimation->addAnimation(zoomInButtonAnimation);
205 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
206 buttonsAnimation->addAnimation(selectAllButtonAnimation);
207 buttonsAnimation->addAnimation(copyButtonAnimation);
208 buttonsAnimation->addAnimation(searchButtonAnimation);
209 buttonsInitialized = true;
210 buttonsVisible = false;
212 connect(showButtonsButton, SIGNAL(clicked()),
213 this, SLOT(showButtons()));
217 void TranslationWidget::initializeUI() {
219 webkit = new TranslationTextEdit(this);
220 Settings* set = guiinterface->settings();
221 qreal fac = set->value("zoom").toFloat();
224 webkit->setZoomFactor(fac);
227 QWidget*w = new QWidget(this);
228 verticalLayout = new QVBoxLayout(w);
229 verticalLayout->addWidget(webkit);
232 this->setWidgetResizable(true);
235 zoomInButton = new QToolButton(this);
236 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
237 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
239 zoomOutButton = new QToolButton(this);
240 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
241 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
243 selectAllButton = new QToolButton(this);
244 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
245 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
247 copyButton = new QToolButton(this);
248 copyButton->setIcon(QIcon::fromTheme("general_notes"));
249 copyButton->setMinimumSize(copyButton->sizeHint());
250 copyButton->setEnabled(false);
252 searchButton = new QToolButton(this);
253 searchButton->setIcon(QIcon::fromTheme("general_search"));
254 searchButton->setMinimumSize(searchButton->sizeHint());
255 searchButton->setEnabled(false);
257 showButtonsButton = new QToolButton(this);
258 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
259 showButtonsButton->setMinimumSize(searchButton->sizeHint());
261 connect(zoomInButton, SIGNAL(clicked()),
262 webkit, SLOT(zoomIn()));
264 connect(zoomOutButton, SIGNAL(clicked()),
265 webkit, SLOT(zoomOut()));
268 connect(searchButton, SIGNAL(clicked()),
269 this, SLOT(searchSelected()));
271 connect(copyButton, SIGNAL(clicked()),
272 webkit, SLOT(copy()));
274 connect(webkit, SIGNAL(copyAvailable(bool)),
275 searchButton, SLOT(setEnabled(bool)));
277 connect(webkit, SIGNAL(copyAvailable(bool)),
278 copyButton, SLOT(setEnabled(bool)));
280 connect(selectAllButton, SIGNAL(clicked()),
281 webkit, SLOT(selectAll()));
283 buttonsInitialized = false;
291 void TranslationWidget::searchSelected() {
295 Q_EMIT search(webkit->selectedText().toLower());
299 void TranslationWidget::showButtons() {
300 if(!buttonsVisible) {
301 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
302 buttonsAnimation->start();
303 buttonsVisible = true;
305 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
307 else if(buttonsVisible) {
308 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
309 buttonsAnimation->start();
310 buttonsVisible = false;
311 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
315 void TranslationWidget::grabZoomKeys(bool grab) {
320 unsigned long val = (grab) ? 1 : 0;
321 Atom atom = XInternAtom(QX11Info::display(),
322 "_HILDON_ZOOM_KEY_ATOM", False);
327 XChangeProperty (QX11Info::display(),
333 reinterpret_cast<unsigned char *>(&val),
337 void TranslationWidget::hideEvent(QHideEvent* e) {
341 QScrollArea::hideEvent(e);
344 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
345 switch (event->key()) {
356 QWidget::keyPressEvent(event);
361 void TranslationWidget::updateZoom(qreal factor) {
362 Settings* set = guiinterface->settings();
363 set->setValue("zoom", QString("%1").arg(factor));
364 guiinterface->setSettings(set);