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));
83 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
84 trans=XslConversion(trans);
86 textEdit->insertHtml(trans);
88 // textEdit->setPlainText(trans);
90 textEdit->repaint(this->rect());
96 QString TranslationWidget::XslConversion(QString translation)
98 QXmlQuery myQuery(QXmlQuery::XSLT20);
99 myQuery.setFocus(translation);
100 myQuery.setQuery(xslt);
102 myQuery.evaluateTo(&result);
108 void TranslationWidget::initButtons() {
110 int x = width() - showButtonsButton->sizeHint().width();
111 int y = height() - showButtonsButton->sizeHint().height();
113 showButtonsButton->move(QPoint(x,y));
114 showButtonsButton->show();
115 //==================================================================
117 x = width() - zoomOutButton->sizeHint().width();
118 y = height() - 2*zoomOutButton->sizeHint().height();
119 zoomOutButton->move(QPoint(x, height()));
121 zoomOutButtonAnimation =
122 new QPropertyAnimation(zoomOutButton, "pos", this);
124 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
125 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
126 zoomOutButtonAnimation->setDuration(200);
127 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
128 //==================================================================
129 x = width() - zoomInButton->sizeHint().width();
130 y = height() - 3*zoomInButton->sizeHint().height();
131 zoomInButton->move(QPoint(x, height()));
133 zoomInButtonAnimation =
134 new QPropertyAnimation(zoomInButton, "pos", this);
136 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
137 zoomInButtonAnimation->setEndValue(QPoint(x,y));
138 zoomInButtonAnimation->setDuration(400);
139 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
140 //==================================================================
142 y = height() - copyButton->sizeHint().height();
144 copyButton->move(QPoint(x, height()));
146 copyButtonAnimation =
147 new QPropertyAnimation(copyButton, "pos", this);
149 copyButtonAnimation->setStartValue(QPoint(x, height()));
150 copyButtonAnimation->setEndValue(QPoint(x,y));
151 copyButtonAnimation->setDuration(200);
152 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
153 //==================================================================
155 y = height() - 2*copyButton->sizeHint().height();
157 selectAllButton->move(QPoint(x, height()));
159 selectAllButtonAnimation =
160 new QPropertyAnimation(selectAllButton, "pos", this);
162 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
163 selectAllButtonAnimation->setEndValue(QPoint(x,y));
164 selectAllButtonAnimation->setDuration(400);
165 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
166 //==================================================================
168 y = height() - 3*copyButton->sizeHint().height();
170 searchButton->move(QPoint(x, height()));
172 searchButtonAnimation =
173 new QPropertyAnimation(searchButton, "pos", this);
175 searchButtonAnimation->setStartValue(QPoint(x, height()));
176 searchButtonAnimation->setEndValue(QPoint(x,y));
177 searchButtonAnimation->setDuration(600);
178 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
179 //==================================================================
181 buttonsAnimation = new QParallelAnimationGroup(this);
182 buttonsAnimation->addAnimation(zoomInButtonAnimation);
183 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
184 buttonsAnimation->addAnimation(selectAllButtonAnimation);
185 buttonsAnimation->addAnimation(copyButtonAnimation);
186 buttonsAnimation->addAnimation(searchButtonAnimation);
187 buttonsInitialized = true;
188 buttonsVisible = false;
190 connect(showButtonsButton, SIGNAL(clicked()),
191 this, SLOT(showButtons()));
195 void TranslationWidget::initializeUI() {
197 textEdit = new TranslationTextEdit(this);
198 textEdit->setReadOnly(true);
200 resizer = new TranslationWidgetAutoResizer(textEdit);
201 connect(this, SIGNAL(updateSize()),
202 resizer, SLOT(textEditChanged()));
204 QWidget*w = new QWidget(this);
205 verticalLayout = new QVBoxLayout(w);
206 verticalLayout->addWidget(textEdit);
208 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
209 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
212 this->setWidgetResizable(true);
215 zoomInButton = new QToolButton(this);
216 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
217 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
219 zoomOutButton = new QToolButton(this);
220 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
221 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
223 selectAllButton = new QToolButton(this);
224 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
225 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
227 copyButton = new QToolButton(this);
228 copyButton->setIcon(QIcon::fromTheme("general_notes"));
229 copyButton->setMinimumSize(copyButton->sizeHint());
230 copyButton->setEnabled(false);
232 searchButton = new QToolButton(this);
233 searchButton->setIcon(QIcon::fromTheme("general_search"));
234 searchButton->setMinimumSize(searchButton->sizeHint());
235 searchButton->setEnabled(false);
237 showButtonsButton = new QToolButton(this);
238 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
239 showButtonsButton->setMinimumSize(searchButton->sizeHint());
241 connect(zoomInButton, SIGNAL(clicked()),
242 textEdit, SLOT(zoomIn()));
244 connect(zoomOutButton, SIGNAL(clicked()),
245 textEdit, SLOT(zoomOut()));
248 connect(searchButton, SIGNAL(clicked()),
249 this, SLOT(searchSelected()));
251 connect(copyButton, SIGNAL(clicked()),
252 textEdit, SLOT(copy()));
254 connect(textEdit, SIGNAL(copyAvailable(bool)),
255 searchButton, SLOT(setEnabled(bool)));
257 connect(textEdit, SIGNAL(copyAvailable(bool)),
258 copyButton, SLOT(setEnabled(bool)));
260 connect(selectAllButton, SIGNAL(clicked()),
261 textEdit, SLOT(selectAll()));
263 buttonsInitialized = false;
271 void TranslationWidget::searchSelected() {
275 Q_EMIT search(textEdit->textCursor().selectedText());
279 void TranslationWidget::showButtons() {
280 if(!buttonsVisible) {
281 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
282 buttonsAnimation->start();
283 buttonsVisible = true;
285 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
287 else if(buttonsVisible) {
288 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
289 buttonsAnimation->start();
290 buttonsVisible = false;
291 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
295 void TranslationWidget::grabZoomKeys(bool grab) {
300 unsigned long val = (grab) ? 1 : 0;
301 Atom atom = XInternAtom(QX11Info::display(),
302 "_HILDON_ZOOM_KEY_ATOM", False);
307 XChangeProperty (QX11Info::display(),
313 reinterpret_cast<unsigned char *>(&val),
317 void TranslationWidget::hideEvent(QHideEvent* e) {
321 QScrollArea::hideEvent(e);
324 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
325 switch (event->key()) {
336 QWidget::keyPressEvent(event);