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);
104 void TranslationWidget::initializeUI() {
106 zoomInToolButton = new QToolButton;
107 zoomInToolButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
109 zoomOutToolButton = new QToolButton;
110 zoomOutToolButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
112 void TranslationWidget::initButtons() {
114 int x = width() - showButtonsButton->sizeHint().width();
115 int y = height() - showButtonsButton->sizeHint().height();
117 showButtonsButton->move(QPoint(x,y));
118 showButtonsButton->show();
119 showButtonsButton->setCheckable(true);
120 //==================================================================
122 x = width() - zoomOutButton->sizeHint().width();
123 y = height() - 2*zoomOutButton->sizeHint().height();
124 zoomOutButton->move(QPoint(x, height()));
126 zoomOutButtonAnimation =
127 new QPropertyAnimation(zoomOutButton, "pos", this);
129 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
130 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
131 zoomOutButtonAnimation->setDuration(200);
132 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
133 //==================================================================
134 x = width() - zoomInButton->sizeHint().width();
135 y = height() - 3*zoomInButton->sizeHint().height();
136 zoomInButton->move(QPoint(x, height()));
138 zoomInButtonAnimation =
139 new QPropertyAnimation(zoomInButton, "pos", this);
141 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
142 zoomInButtonAnimation->setEndValue(QPoint(x,y));
143 zoomInButtonAnimation->setDuration(400);
144 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
145 //==================================================================
147 y = height() - copyButton->sizeHint().height();
149 copyButton->move(QPoint(x, height()));
151 copyButtonAnimation =
152 new QPropertyAnimation(copyButton, "pos", this);
154 copyButtonAnimation->setStartValue(QPoint(x, height()));
155 copyButtonAnimation->setEndValue(QPoint(x,y));
156 copyButtonAnimation->setDuration(200);
157 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
158 //==================================================================
160 y = height() - 2*copyButton->sizeHint().height();
162 selectAllButton->move(QPoint(x, height()));
164 selectAllButtonAnimation =
165 new QPropertyAnimation(selectAllButton, "pos", this);
167 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
168 selectAllButtonAnimation->setEndValue(QPoint(x,y));
169 selectAllButtonAnimation->setDuration(400);
170 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
171 //==================================================================
173 y = height() - 3*copyButton->sizeHint().height();
175 searchButton->move(QPoint(x, height()));
177 searchButtonAnimation =
178 new QPropertyAnimation(searchButton, "pos", this);
180 searchButtonAnimation->setStartValue(QPoint(x, height()));
181 searchButtonAnimation->setEndValue(QPoint(x,y));
182 searchButtonAnimation->setDuration(600);
183 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
184 //==================================================================
186 buttonsAnimation = new QParallelAnimationGroup(this);
187 buttonsAnimation->addAnimation(zoomInButtonAnimation);
188 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
189 buttonsAnimation->addAnimation(selectAllButtonAnimation);
190 buttonsAnimation->addAnimation(copyButtonAnimation);
191 buttonsAnimation->addAnimation(searchButtonAnimation);
192 buttonsInitialized = true;
193 buttonsVisible = false;
195 connect(showButtonsButton, SIGNAL(toggled(bool)),
196 this, SLOT(showButtons(bool)));
200 void TranslationWidget::initializeUI() {
202 textEdit = new TranslationTextEdit;
203 textEdit->setReadOnly(true);
205 resizer = new TranslationWidgetAutoResizer(textEdit);
206 connect(this, SIGNAL(updateSize()),
207 resizer, SLOT(textEditChanged()));
209 QWidget*w = new QWidget;
210 verticalLayout = new QVBoxLayout(w);
211 verticalLayout->addWidget(textEdit);
213 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
218 this->setWidgetResizable(true);
222 zoomInButton = new QToolButton(this);
223 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
224 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
226 zoomOutButton = new QToolButton(this);
227 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
228 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
230 selectAllButton = new QToolButton(this);
231 selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
232 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
234 copyButton = new QToolButton(this);
235 copyButton->setIcon(QIcon::fromTheme("general_notes"));
236 copyButton->setMinimumSize(copyButton->sizeHint());
237 copyButton->setEnabled(false);
239 searchButton = new QToolButton(this);
240 searchButton->setIcon(QIcon::fromTheme("general_search"));
241 searchButton->setMinimumSize(searchButton->sizeHint());
242 searchButton->setEnabled(false);
244 showButtonsButton = new QToolButton(this);
245 showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
246 showButtonsButton->setMinimumSize(searchButton->sizeHint());
248 connect(zoomInButton, SIGNAL(clicked()),
249 textEdit, SLOT(zoomIn()));
251 connect(zoomOutButton, SIGNAL(clicked()),
252 textEdit, SLOT(zoomOut()));
255 connect(searchButton, SIGNAL(clicked()),
256 this, SLOT(searchSelected()));
258 connect(copyButton, SIGNAL(clicked()),
259 textEdit, SLOT(copy()));
261 connect(textEdit, SIGNAL(copyAvailable(bool)),
262 searchButton, SLOT(setEnabled(bool)));
264 connect(textEdit, SIGNAL(copyAvailable(bool)),
265 copyButton, SLOT(setEnabled(bool)));
267 connect(selectAllButton, SIGNAL(clicked()),
268 textEdit, SLOT(selectAll()));
270 buttonsInitialized = false;
278 void TranslationWidget::searchSelected() {
282 emit search(textEdit->textCursor().selectedText());
286 void TranslationWidget::showButtons(bool show) {
287 if(show && !buttonsVisible) {
288 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
289 buttonsAnimation->start();
290 buttonsVisible = true;
292 else if(!show && buttonsVisible) {
293 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
294 buttonsAnimation->start();
295 buttonsVisible = false;
299 void TranslationWidget::grabZoomKeys(bool grab) {
304 unsigned long val = (grab) ? 1 : 0;
305 Atom atom = XInternAtom(QX11Info::display(),
306 "_HILDON_ZOOM_KEY_ATOM", False);
311 XChangeProperty (QX11Info::display(),
317 reinterpret_cast<unsigned char *>(&val),
321 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
322 switch (event->key()) {
333 QWidget::keyPressEvent(event);