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);
38 setWindowFlags(windowFlags() | Qt::Window);
44 setWindowTitle("mDictionary");
46 connect(textEdit, SIGNAL(search()),
47 this, SLOT(searchSelected()));
49 QFile file(":/xsl/xsl.xsl");
50 if(!file.open(QFile::ReadOnly))
51 qDebug()<<"can't open a xslt file";
57 void TranslationWidget::show() {
62 void TranslationWidget::show(QStringList translations) {
67 if(!buttonsInitialized)
75 foreach(t, translations) {
81 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
82 trans=XslConversion(trans);
83 textEdit->insertHtml(trans);
84 // textEdit->setPlainText(trans);
86 textEdit->repaint(this->rect());
93 QString TranslationWidget::XslConversion(QString translation)
95 QXmlQuery myQuery(QXmlQuery::XSLT20);
96 myQuery.setFocus(translation);
97 myQuery.setQuery(xslt);
99 myQuery.evaluateTo(&result);
105 void TranslationWidget::initButtons() {
107 int x = width() - showButtonsButton->sizeHint().width();
108 int y = height() - showButtonsButton->sizeHint().height();
110 showButtonsButton->move(QPoint(x,y));
111 showButtonsButton->show();
112 //==================================================================
114 x = width() - zoomOutButton->sizeHint().width();
115 y = height() - 2*zoomOutButton->sizeHint().height();
116 zoomOutButton->move(QPoint(x, height()));
118 zoomOutButtonAnimation =
119 new QPropertyAnimation(zoomOutButton, "pos", this);
121 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
122 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
123 zoomOutButtonAnimation->setDuration(200);
124 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
125 //==================================================================
126 x = width() - zoomInButton->sizeHint().width();
127 y = height() - 3*zoomInButton->sizeHint().height();
128 zoomInButton->move(QPoint(x, height()));
130 zoomInButtonAnimation =
131 new QPropertyAnimation(zoomInButton, "pos", this);
133 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
134 zoomInButtonAnimation->setEndValue(QPoint(x,y));
135 zoomInButtonAnimation->setDuration(400);
136 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
137 //==================================================================
139 y = height() - copyButton->sizeHint().height();
141 copyButton->move(QPoint(x, height()));
143 copyButtonAnimation =
144 new QPropertyAnimation(copyButton, "pos", this);
146 copyButtonAnimation->setStartValue(QPoint(x, height()));
147 copyButtonAnimation->setEndValue(QPoint(x,y));
148 copyButtonAnimation->setDuration(200);
149 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
150 //==================================================================
152 y = height() - 2*copyButton->sizeHint().height();
154 selectAllButton->move(QPoint(x, height()));
156 selectAllButtonAnimation =
157 new QPropertyAnimation(selectAllButton, "pos", this);
159 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
160 selectAllButtonAnimation->setEndValue(QPoint(x,y));
161 selectAllButtonAnimation->setDuration(400);
162 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
163 //==================================================================
165 y = height() - 3*copyButton->sizeHint().height();
167 searchButton->move(QPoint(x, height()));
169 searchButtonAnimation =
170 new QPropertyAnimation(searchButton, "pos", this);
172 searchButtonAnimation->setStartValue(QPoint(x, height()));
173 searchButtonAnimation->setEndValue(QPoint(x,y));
174 searchButtonAnimation->setDuration(600);
175 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
176 //==================================================================
178 buttonsAnimation = new QParallelAnimationGroup(this);
179 buttonsAnimation->addAnimation(zoomInButtonAnimation);
180 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
181 buttonsAnimation->addAnimation(selectAllButtonAnimation);
182 buttonsAnimation->addAnimation(copyButtonAnimation);
183 buttonsAnimation->addAnimation(searchButtonAnimation);
184 buttonsInitialized = true;
185 buttonsVisible = false;
187 connect(showButtonsButton, SIGNAL(clicked()),
188 this, SLOT(showButtons()));
192 void TranslationWidget::initializeUI() {
194 textEdit = new TranslationTextEdit;
195 textEdit->setReadOnly(true);
197 resizer = new TranslationWidgetAutoResizer(textEdit);
198 connect(this, SIGNAL(updateSize()),
199 resizer, SLOT(textEditChanged()));
201 QWidget*w = new QWidget;
202 verticalLayout = new QVBoxLayout(w);
203 verticalLayout->addWidget(textEdit);
205 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
206 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
209 this->setWidgetResizable(true);
212 zoomInButton = new QToolButton(this);
213 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
214 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
216 zoomOutButton = new QToolButton(this);
217 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
218 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
220 selectAllButton = new QToolButton(this);
221 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
222 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
224 copyButton = new QToolButton(this);
225 copyButton->setIcon(QIcon::fromTheme("general_notes"));
226 copyButton->setMinimumSize(copyButton->sizeHint());
227 copyButton->setEnabled(false);
229 searchButton = new QToolButton(this);
230 searchButton->setIcon(QIcon::fromTheme("general_search"));
231 searchButton->setMinimumSize(searchButton->sizeHint());
232 searchButton->setEnabled(false);
234 showButtonsButton = new QToolButton(this);
235 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
236 showButtonsButton->setMinimumSize(searchButton->sizeHint());
238 connect(zoomInButton, SIGNAL(clicked()),
239 textEdit, SLOT(zoomIn()));
241 connect(zoomOutButton, SIGNAL(clicked()),
242 textEdit, SLOT(zoomOut()));
245 connect(searchButton, SIGNAL(clicked()),
246 this, SLOT(searchSelected()));
248 connect(copyButton, SIGNAL(clicked()),
249 textEdit, SLOT(copy()));
251 connect(textEdit, SIGNAL(copyAvailable(bool)),
252 searchButton, SLOT(setEnabled(bool)));
254 connect(textEdit, SIGNAL(copyAvailable(bool)),
255 copyButton, SLOT(setEnabled(bool)));
257 connect(selectAllButton, SIGNAL(clicked()),
258 textEdit, SLOT(selectAll()));
260 buttonsInitialized = false;
268 void TranslationWidget::searchSelected() {
272 emit search(textEdit->textCursor().selectedText());
276 void TranslationWidget::showButtons() {
277 if(!buttonsVisible) {
278 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
279 buttonsAnimation->start();
280 buttonsVisible = true;
282 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
284 else if(buttonsVisible) {
285 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
286 buttonsAnimation->start();
287 buttonsVisible = false;
288 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
292 void TranslationWidget::grabZoomKeys(bool grab) {
297 unsigned long val = (grab) ? 1 : 0;
298 Atom atom = XInternAtom(QX11Info::display(),
299 "_HILDON_ZOOM_KEY_ATOM", False);
304 XChangeProperty (QX11Info::display(),
310 reinterpret_cast<unsigned char *>(&val),
314 void TranslationWidget::hideEvent(QHideEvent* e) {
318 QScrollArea::hideEvent(e);
321 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
322 switch (event->key()) {
333 QWidget::keyPressEvent(event);