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 //Created by Mateusz Półrola
24 #include "TranslationWidget.h"
25 #include "TranslationWidgetAutoResizer.h"
28 #include <QtGui/QX11Info>
30 #include <X11/Xatom.h>
33 TranslationWidget::TranslationWidget(QWidget *parent):
37 setAttribute(Qt::WA_Maemo5StackedWindow);
39 setWindowFlags(windowFlags() | Qt::Window);
42 //Q_INIT_RESOURCE(xslt);
43 setWindowTitle(tr("Translation"));
45 connect(textEdit, SIGNAL(search()),
46 this, SLOT(searchSelected()));
51 void TranslationWidget::show() {
55 void TranslationWidget::show(QStringList translations) {
60 if(!buttonsInitialized)
68 foreach(t, translations) {
73 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
74 // trans.replace("&","&");
75 trans=XslConversion(trans);
77 // trans.replace("&","&");
78 textEdit->insertHtml(trans);
79 // textEdit->setPlainText(trans);
81 textEdit->repaint(this->rect());
88 QString TranslationWidget::XslConversion(QString translation)
90 QXmlQuery myQuery(QXmlQuery::XSLT20);
91 myQuery.setFocus(translation);
93 QFile file(":/xsl/xsl.xsl");
94 if(!file.open(QFile::ReadOnly)){
95 qDebug()<<"can't open a xslt file";
100 myQuery.setQuery(xslt);
102 myQuery.evaluateTo(&result);
108 void TranslationWidget::initButtons() {
110 int x = width() - showButtonsButton->sizeHint().width();
111 int y = height() - showButtonsButton->sizeHint().height();
113 showButtonsButton->move(QPoint(x,y));
114 showButtonsButton->show();
115 showButtonsButton->setCheckable(true);
116 //==================================================================
118 x = width() - zoomOutButton->sizeHint().width();
119 y = height() - 2*zoomOutButton->sizeHint().height();
120 zoomOutButton->move(QPoint(x, height()));
122 zoomOutButtonAnimation =
123 new QPropertyAnimation(zoomOutButton, "pos", this);
125 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
126 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
127 zoomOutButtonAnimation->setDuration(200);
128 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
129 //==================================================================
130 x = width() - zoomInButton->sizeHint().width();
131 y = height() - 3*zoomInButton->sizeHint().height();
132 zoomInButton->move(QPoint(x, height()));
134 zoomInButtonAnimation =
135 new QPropertyAnimation(zoomInButton, "pos", this);
137 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
138 zoomInButtonAnimation->setEndValue(QPoint(x,y));
139 zoomInButtonAnimation->setDuration(400);
140 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
141 //==================================================================
143 y = height() - copyButton->sizeHint().height();
145 copyButton->move(QPoint(x, height()));
147 copyButtonAnimation =
148 new QPropertyAnimation(copyButton, "pos", this);
150 copyButtonAnimation->setStartValue(QPoint(x, height()));
151 copyButtonAnimation->setEndValue(QPoint(x,y));
152 copyButtonAnimation->setDuration(200);
153 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
154 //==================================================================
156 y = height() - 2*copyButton->sizeHint().height();
158 selectAllButton->move(QPoint(x, height()));
160 selectAllButtonAnimation =
161 new QPropertyAnimation(selectAllButton, "pos", this);
163 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
164 selectAllButtonAnimation->setEndValue(QPoint(x,y));
165 selectAllButtonAnimation->setDuration(400);
166 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
167 //==================================================================
169 y = height() - 3*copyButton->sizeHint().height();
171 searchButton->move(QPoint(x, height()));
173 searchButtonAnimation =
174 new QPropertyAnimation(searchButton, "pos", this);
176 searchButtonAnimation->setStartValue(QPoint(x, height()));
177 searchButtonAnimation->setEndValue(QPoint(x,y));
178 searchButtonAnimation->setDuration(600);
179 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
180 //==================================================================
182 buttonsAnimation = new QParallelAnimationGroup(this);
183 buttonsAnimation->addAnimation(zoomInButtonAnimation);
184 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
185 buttonsAnimation->addAnimation(selectAllButtonAnimation);
186 buttonsAnimation->addAnimation(copyButtonAnimation);
187 buttonsAnimation->addAnimation(searchButtonAnimation);
188 buttonsInitialized = true;
189 buttonsVisible = false;
191 connect(showButtonsButton, SIGNAL(toggled(bool)),
192 this, SLOT(showButtons(bool)));
196 void TranslationWidget::initializeUI() {
198 textEdit = new TranslationTextEdit;
199 textEdit->setReadOnly(true);
201 resizer = new TranslationWidgetAutoResizer(textEdit);
202 connect(this, SIGNAL(updateSize()),
203 resizer, SLOT(textEditChanged()));
205 QWidget*w = new QWidget;
206 verticalLayout = new QVBoxLayout(w);
207 verticalLayout->addWidget(textEdit);
209 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
210 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214 this->setWidgetResizable(true);
218 zoomInButton = new QToolButton(this);
219 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
220 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
222 zoomOutButton = new QToolButton(this);
223 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
224 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
226 selectAllButton = new QToolButton(this);
227 selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
228 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
230 copyButton = new QToolButton(this);
231 copyButton->setIcon(QIcon::fromTheme("general_notes"));
232 copyButton->setMinimumSize(copyButton->sizeHint());
233 copyButton->setEnabled(false);
235 searchButton = new QToolButton(this);
236 searchButton->setIcon(QIcon::fromTheme("general_search"));
237 searchButton->setMinimumSize(searchButton->sizeHint());
238 searchButton->setEnabled(false);
240 showButtonsButton = new QToolButton(this);
241 showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
242 showButtonsButton->setMinimumSize(searchButton->sizeHint());
244 connect(zoomInButton, SIGNAL(clicked()),
245 textEdit, SLOT(zoomIn()));
247 connect(zoomOutButton, SIGNAL(clicked()),
248 textEdit, SLOT(zoomOut()));
251 connect(searchButton, SIGNAL(clicked()),
252 this, SLOT(searchSelected()));
254 connect(copyButton, SIGNAL(clicked()),
255 textEdit, SLOT(copy()));
257 connect(textEdit, SIGNAL(copyAvailable(bool)),
258 searchButton, SLOT(setEnabled(bool)));
260 connect(textEdit, SIGNAL(copyAvailable(bool)),
261 copyButton, SLOT(setEnabled(bool)));
263 connect(selectAllButton, SIGNAL(clicked()),
264 textEdit, SLOT(selectAll()));
266 buttonsInitialized = false;
274 void TranslationWidget::searchSelected() {
278 emit search(textEdit->textCursor().selectedText());
282 void TranslationWidget::showButtons(bool show) {
283 if(show && !buttonsVisible) {
284 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
285 buttonsAnimation->start();
286 buttonsVisible = true;
288 else if(!show && buttonsVisible) {
289 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
290 buttonsAnimation->start();
291 buttonsVisible = false;
295 void TranslationWidget::grabZoomKeys(bool grab) {
300 unsigned long val = (grab) ? 1 : 0;
301 Atom atom = XInternAtom(QX11Info::display(),
302 "_HILDON_ZOOM_KEY_ATOM", False);
307 XChangeProperty (QX11Info::display(),
313 reinterpret_cast<unsigned char *>(&val),
317 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
318 switch (event->key()) {
329 QWidget::keyPressEvent(event);