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) {
62 if(!buttonsInitialized)
70 foreach(t, translations) {
74 QImage image(":icons/16x16/staron.png");
75 QTextDocument *document = textEdit->document();
76 document->addResource(QTextDocument::ImageResource, QUrl("mydata://image.png"), QVariant(image));
78 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
79 trans=XslConversion(trans);
81 textEdit->insertHtml(trans);
83 // textEdit->setPlainText(trans);
85 textEdit->repaint(this->rect());
91 QString TranslationWidget::XslConversion(QString translation)
93 QXmlQuery myQuery(QXmlQuery::XSLT20);
94 myQuery.setFocus(translation);
96 QFile file(":/xsl/xsl.xsl");
97 if(!file.open(QFile::ReadOnly)){
98 qDebug()<<"can't open a xslt file";
103 myQuery.setQuery(xslt);
105 myQuery.evaluateTo(&result);
111 void TranslationWidget::initButtons() {
113 int x = width() - showButtonsButton->sizeHint().width();
114 int y = height() - showButtonsButton->sizeHint().height();
116 showButtonsButton->move(QPoint(x,y));
117 showButtonsButton->show();
118 //==================================================================
120 x = width() - zoomOutButton->sizeHint().width();
121 y = height() - 2*zoomOutButton->sizeHint().height();
122 zoomOutButton->move(QPoint(x, height()));
124 zoomOutButtonAnimation =
125 new QPropertyAnimation(zoomOutButton, "pos", this);
127 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
128 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
129 zoomOutButtonAnimation->setDuration(200);
130 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
131 //==================================================================
132 x = width() - zoomInButton->sizeHint().width();
133 y = height() - 3*zoomInButton->sizeHint().height();
134 zoomInButton->move(QPoint(x, height()));
136 zoomInButtonAnimation =
137 new QPropertyAnimation(zoomInButton, "pos", this);
139 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
140 zoomInButtonAnimation->setEndValue(QPoint(x,y));
141 zoomInButtonAnimation->setDuration(400);
142 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
143 //==================================================================
145 y = height() - copyButton->sizeHint().height();
147 copyButton->move(QPoint(x, height()));
149 copyButtonAnimation =
150 new QPropertyAnimation(copyButton, "pos", this);
152 copyButtonAnimation->setStartValue(QPoint(x, height()));
153 copyButtonAnimation->setEndValue(QPoint(x,y));
154 copyButtonAnimation->setDuration(200);
155 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
156 //==================================================================
158 y = height() - 2*copyButton->sizeHint().height();
160 selectAllButton->move(QPoint(x, height()));
162 selectAllButtonAnimation =
163 new QPropertyAnimation(selectAllButton, "pos", this);
165 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
166 selectAllButtonAnimation->setEndValue(QPoint(x,y));
167 selectAllButtonAnimation->setDuration(400);
168 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
169 //==================================================================
171 y = height() - 3*copyButton->sizeHint().height();
173 searchButton->move(QPoint(x, height()));
175 searchButtonAnimation =
176 new QPropertyAnimation(searchButton, "pos", this);
178 searchButtonAnimation->setStartValue(QPoint(x, height()));
179 searchButtonAnimation->setEndValue(QPoint(x,y));
180 searchButtonAnimation->setDuration(600);
181 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
182 //==================================================================
184 buttonsAnimation = new QParallelAnimationGroup(this);
185 buttonsAnimation->addAnimation(zoomInButtonAnimation);
186 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
187 buttonsAnimation->addAnimation(selectAllButtonAnimation);
188 buttonsAnimation->addAnimation(copyButtonAnimation);
189 buttonsAnimation->addAnimation(searchButtonAnimation);
190 buttonsInitialized = true;
191 buttonsVisible = false;
193 connect(showButtonsButton, SIGNAL(clicked()),
194 this, SLOT(showButtons()));
198 void TranslationWidget::initializeUI() {
200 textEdit = new TranslationTextEdit(this);
201 textEdit->setReadOnly(true);
203 resizer = new TranslationWidgetAutoResizer(textEdit);
204 connect(this, SIGNAL(updateSize()),
205 resizer, SLOT(textEditChanged()));
207 QWidget*w = new QWidget(this);
208 verticalLayout = new QVBoxLayout(w);
209 verticalLayout->addWidget(textEdit);
211 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
212 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
215 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(":/icons/48x48/edit-select-all.png"));
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_sent"));
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() {
283 if(!buttonsVisible) {
284 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
285 buttonsAnimation->start();
286 buttonsVisible = true;
288 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
290 else if(buttonsVisible) {
291 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
292 buttonsAnimation->start();
293 buttonsVisible = false;
294 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
298 void TranslationWidget::grabZoomKeys(bool grab) {
303 unsigned long val = (grab) ? 1 : 0;
304 Atom atom = XInternAtom(QX11Info::display(),
305 "_HILDON_ZOOM_KEY_ATOM", False);
310 XChangeProperty (QX11Info::display(),
316 reinterpret_cast<unsigned char *>(&val),
320 void TranslationWidget::hideEvent(QHideEvent* e) {
324 QScrollArea::hideEvent(e);
327 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
328 switch (event->key()) {
339 QWidget::keyPressEvent(event);