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->setDocument(document);
82 textEdit->insertHtml(trans);
84 // textEdit->append("<img src=\"mydata://image.png\" />");
85 // textEdit->textCursor().insertImage("starimage");
87 // textEdit->setPlainText(trans);
89 textEdit->repaint(this->rect());
96 QString TranslationWidget::XslConversion(QString translation)
98 QXmlQuery myQuery(QXmlQuery::XSLT20);
99 myQuery.setFocus(translation);
101 QFile file(":/xsl/xsl.xsl");
102 if(!file.open(QFile::ReadOnly)){
103 qDebug()<<"can't open a xslt file";
108 myQuery.setQuery(xslt);
110 myQuery.evaluateTo(&result);
116 void TranslationWidget::initButtons() {
118 int x = width() - showButtonsButton->sizeHint().width();
119 int y = height() - showButtonsButton->sizeHint().height();
121 showButtonsButton->move(QPoint(x,y));
122 showButtonsButton->show();
123 //==================================================================
125 x = width() - zoomOutButton->sizeHint().width();
126 y = height() - 2*zoomOutButton->sizeHint().height();
127 zoomOutButton->move(QPoint(x, height()));
129 zoomOutButtonAnimation =
130 new QPropertyAnimation(zoomOutButton, "pos", this);
132 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
133 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
134 zoomOutButtonAnimation->setDuration(200);
135 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
136 //==================================================================
137 x = width() - zoomInButton->sizeHint().width();
138 y = height() - 3*zoomInButton->sizeHint().height();
139 zoomInButton->move(QPoint(x, height()));
141 zoomInButtonAnimation =
142 new QPropertyAnimation(zoomInButton, "pos", this);
144 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
145 zoomInButtonAnimation->setEndValue(QPoint(x,y));
146 zoomInButtonAnimation->setDuration(400);
147 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
148 //==================================================================
150 y = height() - copyButton->sizeHint().height();
152 copyButton->move(QPoint(x, height()));
154 copyButtonAnimation =
155 new QPropertyAnimation(copyButton, "pos", this);
157 copyButtonAnimation->setStartValue(QPoint(x, height()));
158 copyButtonAnimation->setEndValue(QPoint(x,y));
159 copyButtonAnimation->setDuration(200);
160 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
161 //==================================================================
163 y = height() - 2*copyButton->sizeHint().height();
165 selectAllButton->move(QPoint(x, height()));
167 selectAllButtonAnimation =
168 new QPropertyAnimation(selectAllButton, "pos", this);
170 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
171 selectAllButtonAnimation->setEndValue(QPoint(x,y));
172 selectAllButtonAnimation->setDuration(400);
173 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
174 //==================================================================
176 y = height() - 3*copyButton->sizeHint().height();
178 searchButton->move(QPoint(x, height()));
180 searchButtonAnimation =
181 new QPropertyAnimation(searchButton, "pos", this);
183 searchButtonAnimation->setStartValue(QPoint(x, height()));
184 searchButtonAnimation->setEndValue(QPoint(x,y));
185 searchButtonAnimation->setDuration(600);
186 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
187 //==================================================================
189 buttonsAnimation = new QParallelAnimationGroup(this);
190 buttonsAnimation->addAnimation(zoomInButtonAnimation);
191 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
192 buttonsAnimation->addAnimation(selectAllButtonAnimation);
193 buttonsAnimation->addAnimation(copyButtonAnimation);
194 buttonsAnimation->addAnimation(searchButtonAnimation);
195 buttonsInitialized = true;
196 buttonsVisible = false;
198 connect(showButtonsButton, SIGNAL(clicked()),
199 this, SLOT(showButtons()));
203 void TranslationWidget::initializeUI() {
205 textEdit = new TranslationTextEdit;
206 textEdit->setReadOnly(true);
208 resizer = new TranslationWidgetAutoResizer(textEdit);
209 connect(this, SIGNAL(updateSize()),
210 resizer, SLOT(textEditChanged()));
212 QWidget*w = new QWidget;
213 verticalLayout = new QVBoxLayout(w);
214 verticalLayout->addWidget(textEdit);
216 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
217 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
220 this->setWidgetResizable(true);
223 zoomInButton = new QToolButton(this);
224 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
225 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
227 zoomOutButton = new QToolButton(this);
228 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
229 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
231 selectAllButton = new QToolButton(this);
232 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
233 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
235 copyButton = new QToolButton(this);
236 copyButton->setIcon(QIcon::fromTheme("general_notes"));
237 copyButton->setMinimumSize(copyButton->sizeHint());
238 copyButton->setEnabled(false);
240 searchButton = new QToolButton(this);
241 searchButton->setIcon(QIcon::fromTheme("general_search"));
242 searchButton->setMinimumSize(searchButton->sizeHint());
243 searchButton->setEnabled(false);
245 showButtonsButton = new QToolButton(this);
246 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
247 showButtonsButton->setMinimumSize(searchButton->sizeHint());
249 connect(zoomInButton, SIGNAL(clicked()),
250 textEdit, SLOT(zoomIn()));
252 connect(zoomOutButton, SIGNAL(clicked()),
253 textEdit, SLOT(zoomOut()));
256 connect(searchButton, SIGNAL(clicked()),
257 this, SLOT(searchSelected()));
259 connect(copyButton, SIGNAL(clicked()),
260 textEdit, SLOT(copy()));
262 connect(textEdit, SIGNAL(copyAvailable(bool)),
263 searchButton, SLOT(setEnabled(bool)));
265 connect(textEdit, SIGNAL(copyAvailable(bool)),
266 copyButton, SLOT(setEnabled(bool)));
268 connect(selectAllButton, SIGNAL(clicked()),
269 textEdit, SLOT(selectAll()));
271 buttonsInitialized = false;
279 void TranslationWidget::searchSelected() {
283 emit search(textEdit->textCursor().selectedText());
287 void TranslationWidget::showButtons() {
288 if(!buttonsVisible) {
289 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
290 buttonsAnimation->start();
291 buttonsVisible = true;
293 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
295 else if(buttonsVisible) {
296 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
297 buttonsAnimation->start();
298 buttonsVisible = false;
299 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
303 void TranslationWidget::grabZoomKeys(bool grab) {
308 unsigned long val = (grab) ? 1 : 0;
309 Atom atom = XInternAtom(QX11Info::display(),
310 "_HILDON_ZOOM_KEY_ATOM", False);
315 XChangeProperty (QX11Info::display(),
321 reinterpret_cast<unsigned char *>(&val),
325 void TranslationWidget::hideEvent(QHideEvent* e) {
329 QScrollArea::hideEvent(e);
332 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
333 switch (event->key()) {
344 QWidget::keyPressEvent(event);