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>");
75 trans=XslConversion(trans);
76 textEdit->insertHtml(trans);
77 // textEdit->setPlainText(trans);
79 textEdit->repaint(this->rect());
86 QString TranslationWidget::XslConversion(QString translation)
88 QXmlQuery myQuery(QXmlQuery::XSLT20);
89 myQuery.setFocus(translation);
90 // qDebug()<<translation;
91 QFile file(":/xsl/xsl.xsl");
92 if(!file.open(QFile::ReadOnly)){
93 qDebug()<<"can't open a xslt file";
98 myQuery.setQuery(xslt);
100 myQuery.evaluateTo(&result);
106 void TranslationWidget::initButtons() {
108 int x = width() - showButtonsButton->sizeHint().width();
109 int y = height() - showButtonsButton->sizeHint().height();
111 showButtonsButton->move(QPoint(x,y));
112 showButtonsButton->show();
113 showButtonsButton->setCheckable(true);
114 //==================================================================
116 x = width() - zoomOutButton->sizeHint().width();
117 y = height() - 2*zoomOutButton->sizeHint().height();
118 zoomOutButton->move(QPoint(x, height()));
120 zoomOutButtonAnimation =
121 new QPropertyAnimation(zoomOutButton, "pos", this);
123 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
124 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
125 zoomOutButtonAnimation->setDuration(200);
126 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
127 //==================================================================
128 x = width() - zoomInButton->sizeHint().width();
129 y = height() - 3*zoomInButton->sizeHint().height();
130 zoomInButton->move(QPoint(x, height()));
132 zoomInButtonAnimation =
133 new QPropertyAnimation(zoomInButton, "pos", this);
135 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
136 zoomInButtonAnimation->setEndValue(QPoint(x,y));
137 zoomInButtonAnimation->setDuration(400);
138 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
139 //==================================================================
141 y = height() - copyButton->sizeHint().height();
143 copyButton->move(QPoint(x, height()));
145 copyButtonAnimation =
146 new QPropertyAnimation(copyButton, "pos", this);
148 copyButtonAnimation->setStartValue(QPoint(x, height()));
149 copyButtonAnimation->setEndValue(QPoint(x,y));
150 copyButtonAnimation->setDuration(200);
151 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
152 //==================================================================
154 y = height() - 2*copyButton->sizeHint().height();
156 selectAllButton->move(QPoint(x, height()));
158 selectAllButtonAnimation =
159 new QPropertyAnimation(selectAllButton, "pos", this);
161 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
162 selectAllButtonAnimation->setEndValue(QPoint(x,y));
163 selectAllButtonAnimation->setDuration(400);
164 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
165 //==================================================================
167 y = height() - 3*copyButton->sizeHint().height();
169 searchButton->move(QPoint(x, height()));
171 searchButtonAnimation =
172 new QPropertyAnimation(searchButton, "pos", this);
174 searchButtonAnimation->setStartValue(QPoint(x, height()));
175 searchButtonAnimation->setEndValue(QPoint(x,y));
176 searchButtonAnimation->setDuration(600);
177 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
178 //==================================================================
180 buttonsAnimation = new QParallelAnimationGroup(this);
181 buttonsAnimation->addAnimation(zoomInButtonAnimation);
182 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
183 buttonsAnimation->addAnimation(selectAllButtonAnimation);
184 buttonsAnimation->addAnimation(copyButtonAnimation);
185 buttonsAnimation->addAnimation(searchButtonAnimation);
186 buttonsInitialized = true;
187 buttonsVisible = false;
189 connect(showButtonsButton, SIGNAL(toggled(bool)),
190 this, SLOT(showButtons(bool)));
194 void TranslationWidget::initializeUI() {
196 textEdit = new TranslationTextEdit;
197 textEdit->setReadOnly(true);
199 resizer = new TranslationWidgetAutoResizer(textEdit);
200 connect(this, SIGNAL(updateSize()),
201 resizer, SLOT(textEditChanged()));
203 QWidget*w = new QWidget;
204 verticalLayout = new QVBoxLayout(w);
205 verticalLayout->addWidget(textEdit);
207 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
208 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
212 this->setWidgetResizable(true);
216 zoomInButton = new QToolButton(this);
217 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
218 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
220 zoomOutButton = new QToolButton(this);
221 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
222 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
224 selectAllButton = new QToolButton(this);
225 selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
226 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
228 copyButton = new QToolButton(this);
229 copyButton->setIcon(QIcon::fromTheme("general_notes"));
230 copyButton->setMinimumSize(copyButton->sizeHint());
231 copyButton->setEnabled(false);
233 searchButton = new QToolButton(this);
234 searchButton->setIcon(QIcon::fromTheme("general_search"));
235 searchButton->setMinimumSize(searchButton->sizeHint());
236 searchButton->setEnabled(false);
238 showButtonsButton = new QToolButton(this);
239 showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
240 showButtonsButton->setMinimumSize(searchButton->sizeHint());
242 connect(zoomInButton, SIGNAL(clicked()),
243 textEdit, SLOT(zoomIn()));
245 connect(zoomOutButton, SIGNAL(clicked()),
246 textEdit, SLOT(zoomOut()));
249 connect(searchButton, SIGNAL(clicked()),
250 this, SLOT(searchSelected()));
252 connect(copyButton, SIGNAL(clicked()),
253 textEdit, SLOT(copy()));
255 connect(textEdit, SIGNAL(copyAvailable(bool)),
256 searchButton, SLOT(setEnabled(bool)));
258 connect(textEdit, SIGNAL(copyAvailable(bool)),
259 copyButton, SLOT(setEnabled(bool)));
261 connect(selectAllButton, SIGNAL(clicked()),
262 textEdit, SLOT(selectAll()));
264 buttonsInitialized = false;
272 void TranslationWidget::searchSelected() {
276 emit search(textEdit->textCursor().selectedText());
280 void TranslationWidget::showButtons(bool show) {
281 if(show && !buttonsVisible) {
282 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
283 buttonsAnimation->start();
284 buttonsVisible = true;
286 else if(!show && buttonsVisible) {
287 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
288 buttonsAnimation->start();
289 buttonsVisible = false;
293 void TranslationWidget::grabZoomKeys(bool grab) {
298 unsigned long val = (grab) ? 1 : 0;
299 Atom atom = XInternAtom(QX11Info::display(),
300 "_HILDON_ZOOM_KEY_ATOM", False);
305 XChangeProperty (QX11Info::display(),
311 reinterpret_cast<unsigned char *>(&val),
315 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
316 switch (event->key()) {
327 QWidget::keyPressEvent(event);