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()));
52 void TranslationWidget::show() {
57 void TranslationWidget::show(QStringList translations) {
63 if(!buttonsInitialized)
66 textEdit->repaint(this->rect());
71 foreach(t, translations) {
78 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
79 trans=XslConversion(trans);
80 textEdit->insertHtml(trans);
81 // textEdit->setPlainText(trans);
83 textEdit->repaint(this->rect());
92 QString TranslationWidget::XslConversion(QString translation)
94 QXmlQuery myQuery(QXmlQuery::XSLT20);
95 myQuery.setFocus(translation);
97 QFile file(":/xsl/xsl.xsl");
98 if(!file.open(QFile::ReadOnly)){
99 qDebug()<<"can't open a xslt file";
104 myQuery.setQuery(xslt);
106 myQuery.evaluateTo(&result);
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 //==================================================================
121 x = width() - zoomOutButton->sizeHint().width();
122 y = height() - 2*zoomOutButton->sizeHint().height();
123 zoomOutButton->move(QPoint(x, height()));
125 zoomOutButtonAnimation =
126 new QPropertyAnimation(zoomOutButton, "pos", this);
128 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
129 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
130 zoomOutButtonAnimation->setDuration(200);
131 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
132 //==================================================================
133 x = width() - zoomInButton->sizeHint().width();
134 y = height() - 3*zoomInButton->sizeHint().height();
135 zoomInButton->move(QPoint(x, height()));
137 zoomInButtonAnimation =
138 new QPropertyAnimation(zoomInButton, "pos", this);
140 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
141 zoomInButtonAnimation->setEndValue(QPoint(x,y));
142 zoomInButtonAnimation->setDuration(400);
143 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
144 //==================================================================
146 y = height() - copyButton->sizeHint().height();
148 copyButton->move(QPoint(x, height()));
150 copyButtonAnimation =
151 new QPropertyAnimation(copyButton, "pos", this);
153 copyButtonAnimation->setStartValue(QPoint(x, height()));
154 copyButtonAnimation->setEndValue(QPoint(x,y));
155 copyButtonAnimation->setDuration(200);
156 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
157 //==================================================================
159 y = height() - 2*copyButton->sizeHint().height();
161 selectAllButton->move(QPoint(x, height()));
163 selectAllButtonAnimation =
164 new QPropertyAnimation(selectAllButton, "pos", this);
166 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
167 selectAllButtonAnimation->setEndValue(QPoint(x,y));
168 selectAllButtonAnimation->setDuration(400);
169 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
170 //==================================================================
172 y = height() - 3*copyButton->sizeHint().height();
174 searchButton->move(QPoint(x, height()));
176 searchButtonAnimation =
177 new QPropertyAnimation(searchButton, "pos", this);
179 searchButtonAnimation->setStartValue(QPoint(x, height()));
180 searchButtonAnimation->setEndValue(QPoint(x,y));
181 searchButtonAnimation->setDuration(600);
182 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
183 //==================================================================
185 buttonsAnimation = new QParallelAnimationGroup(this);
186 buttonsAnimation->addAnimation(zoomInButtonAnimation);
187 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
188 buttonsAnimation->addAnimation(selectAllButtonAnimation);
189 buttonsAnimation->addAnimation(copyButtonAnimation);
190 buttonsAnimation->addAnimation(searchButtonAnimation);
191 buttonsInitialized = true;
192 buttonsVisible = false;
194 connect(showButtonsButton, SIGNAL(clicked()),
195 this, SLOT(showButtons()));
199 void TranslationWidget::initializeUI() {
201 textEdit = new TranslationTextEdit;
202 textEdit->setReadOnly(true);
204 resizer = new TranslationWidgetAutoResizer(textEdit);
205 connect(this, SIGNAL(updateSize()),
206 resizer, SLOT(textEditChanged()));
208 QWidget*w = new QWidget;
209 verticalLayout = new QVBoxLayout(w);
210 verticalLayout->addWidget(textEdit);
212 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
213 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
216 this->setWidgetResizable(true);
219 zoomInButton = new QToolButton(this);
220 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
221 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
223 zoomOutButton = new QToolButton(this);
224 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
225 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
227 selectAllButton = new QToolButton(this);
228 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
229 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
231 copyButton = new QToolButton(this);
232 copyButton->setIcon(QIcon::fromTheme("general_notes"));
233 copyButton->setMinimumSize(copyButton->sizeHint());
234 copyButton->setEnabled(false);
236 searchButton = new QToolButton(this);
237 searchButton->setIcon(QIcon::fromTheme("general_search"));
238 searchButton->setMinimumSize(searchButton->sizeHint());
239 searchButton->setEnabled(false);
241 showButtonsButton = new QToolButton(this);
242 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
243 showButtonsButton->setMinimumSize(searchButton->sizeHint());
245 connect(zoomInButton, SIGNAL(clicked()),
246 textEdit, SLOT(zoomIn()));
248 connect(zoomOutButton, SIGNAL(clicked()),
249 textEdit, SLOT(zoomOut()));
252 connect(searchButton, SIGNAL(clicked()),
253 this, SLOT(searchSelected()));
255 connect(copyButton, SIGNAL(clicked()),
256 textEdit, SLOT(copy()));
258 connect(textEdit, SIGNAL(copyAvailable(bool)),
259 searchButton, SLOT(setEnabled(bool)));
261 connect(textEdit, SIGNAL(copyAvailable(bool)),
262 copyButton, SLOT(setEnabled(bool)));
264 connect(selectAllButton, SIGNAL(clicked()),
265 textEdit, SLOT(selectAll()));
267 buttonsInitialized = false;
275 void TranslationWidget::searchSelected() {
279 emit search(textEdit->textCursor().selectedText());
283 void TranslationWidget::showButtons() {
284 if(!buttonsVisible) {
285 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
286 buttonsAnimation->start();
287 buttonsVisible = true;
289 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
291 else if(buttonsVisible) {
292 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
293 buttonsAnimation->start();
294 buttonsVisible = false;
295 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
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::hideEvent(QHideEvent* e) {
325 QScrollArea::hideEvent(e);
328 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
329 switch (event->key()) {
340 QWidget::keyPressEvent(event);