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) {
79 QImage image(":icons/16x16/staron.png");
80 QTextDocument *document = textEdit->document();
81 document->addResource(QTextDocument::ImageResource, QUrl("mydata://image.png"), QVariant(image));
86 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
87 trans=XslConversion(trans);
91 textEdit->insertHtml(trans);
93 // textEdit->setPlainText(trans);
95 textEdit->repaint(this->rect());
101 QString TranslationWidget::XslConversion(QString translation)
103 QXmlQuery myQuery(QXmlQuery::XSLT20);
104 myQuery.setFocus(translation);
105 myQuery.setQuery(xslt);
107 myQuery.evaluateTo(&result);
113 void TranslationWidget::initButtons() {
115 int x = width() - showButtonsButton->sizeHint().width();
116 int y = height() - showButtonsButton->sizeHint().height();
118 showButtonsButton->move(QPoint(x,y));
119 showButtonsButton->show();
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(clicked()),
196 this, SLOT(showButtons()));
200 void TranslationWidget::initializeUI() {
202 textEdit = new TranslationTextEdit(this);
203 textEdit->setReadOnly(true);
205 resizer = new TranslationWidgetAutoResizer(textEdit);
206 connect(this, SIGNAL(updateSize()),
207 resizer, SLOT(textEditChanged()));
209 QWidget*w = new QWidget(this);
210 verticalLayout = new QVBoxLayout(w);
211 verticalLayout->addWidget(textEdit);
213 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
214 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
217 this->setWidgetResizable(true);
220 zoomInButton = new QToolButton(this);
221 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
222 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
224 zoomOutButton = new QToolButton(this);
225 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
226 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
228 selectAllButton = new QToolButton(this);
229 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
230 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
232 copyButton = new QToolButton(this);
233 copyButton->setIcon(QIcon::fromTheme("general_notes"));
234 copyButton->setMinimumSize(copyButton->sizeHint());
235 copyButton->setEnabled(false);
237 searchButton = new QToolButton(this);
238 searchButton->setIcon(QIcon::fromTheme("general_search"));
239 searchButton->setMinimumSize(searchButton->sizeHint());
240 searchButton->setEnabled(false);
242 showButtonsButton = new QToolButton(this);
243 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
244 showButtonsButton->setMinimumSize(searchButton->sizeHint());
246 connect(zoomInButton, SIGNAL(clicked()),
247 textEdit, SLOT(zoomIn()));
249 connect(zoomOutButton, SIGNAL(clicked()),
250 textEdit, SLOT(zoomOut()));
253 connect(searchButton, SIGNAL(clicked()),
254 this, SLOT(searchSelected()));
256 connect(copyButton, SIGNAL(clicked()),
257 textEdit, SLOT(copy()));
259 connect(textEdit, SIGNAL(copyAvailable(bool)),
260 searchButton, SLOT(setEnabled(bool)));
262 connect(textEdit, SIGNAL(copyAvailable(bool)),
263 copyButton, SLOT(setEnabled(bool)));
265 connect(selectAllButton, SIGNAL(clicked()),
266 textEdit, SLOT(selectAll()));
268 buttonsInitialized = false;
276 void TranslationWidget::searchSelected() {
280 Q_EMIT search(textEdit->textCursor().selectedText());
284 void TranslationWidget::showButtons() {
285 if(!buttonsVisible) {
286 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
287 buttonsAnimation->start();
288 buttonsVisible = true;
290 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
292 else if(buttonsVisible) {
293 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
294 buttonsAnimation->start();
295 buttonsVisible = false;
296 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
300 void TranslationWidget::grabZoomKeys(bool grab) {
305 unsigned long val = (grab) ? 1 : 0;
306 Atom atom = XInternAtom(QX11Info::display(),
307 "_HILDON_ZOOM_KEY_ATOM", False);
312 XChangeProperty (QX11Info::display(),
318 reinterpret_cast<unsigned char *>(&val),
322 void TranslationWidget::hideEvent(QHideEvent* e) {
326 QScrollArea::hideEvent(e);
329 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
330 switch (event->key()) {
341 QWidget::keyPressEvent(event);