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()<<"error can't open a xslt file";
57 void TranslationWidget::show() {
62 void TranslationWidget::show(QStringList translations) {
68 if(!buttonsInitialized)
71 textEdit->repaint(this->rect());
76 foreach(t, translations) {
80 QImage image(":icons/16x16/staron.png");
81 QTextDocument *document = textEdit->document();
82 document->addResource(QTextDocument::ImageResource,
83 QUrl("mydata://image.png"), QVariant(image));
85 QFile cssFile(QDir::homePath() + "/.mdictionary/style.css");
86 cssFile.open(QFile::ReadOnly);
87 document->addResource(QTextDocument::StyleSheetResource,
88 QUrl("mydata://style.css"),
89 QVariant(cssFile.readAll()));
92 trans=tr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
93 trans=XslConversion(trans);
95 textEdit->insertHtml(trans);
97 textEdit->repaint(this->rect());
103 QString TranslationWidget::XslConversion(QString translation)
105 QXmlQuery myQuery(QXmlQuery::XSLT20);
106 myQuery.setFocus(translation);
107 myQuery.setQuery(xslt);
109 myQuery.evaluateTo(&result);
115 void TranslationWidget::initButtons() {
117 int x = width() - showButtonsButton->sizeHint().width();
118 int y = height() - showButtonsButton->sizeHint().height();
120 showButtonsButton->move(QPoint(x,y));
121 showButtonsButton->show();
122 //==================================================================
124 x = width() - zoomOutButton->sizeHint().width();
125 y = height() - 2*zoomOutButton->sizeHint().height();
126 zoomOutButton->move(QPoint(x, height()));
128 zoomOutButtonAnimation =
129 new QPropertyAnimation(zoomOutButton, "pos", this);
131 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
132 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
133 zoomOutButtonAnimation->setDuration(200);
134 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
135 //==================================================================
136 x = width() - zoomInButton->sizeHint().width();
137 y = height() - 3*zoomInButton->sizeHint().height();
138 zoomInButton->move(QPoint(x, height()));
140 zoomInButtonAnimation =
141 new QPropertyAnimation(zoomInButton, "pos", this);
143 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
144 zoomInButtonAnimation->setEndValue(QPoint(x,y));
145 zoomInButtonAnimation->setDuration(400);
146 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
147 //==================================================================
149 y = height() - copyButton->sizeHint().height();
151 copyButton->move(QPoint(x, height()));
153 copyButtonAnimation =
154 new QPropertyAnimation(copyButton, "pos", this);
156 copyButtonAnimation->setStartValue(QPoint(x, height()));
157 copyButtonAnimation->setEndValue(QPoint(x,y));
158 copyButtonAnimation->setDuration(200);
159 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
160 //==================================================================
162 y = height() - 2*copyButton->sizeHint().height();
164 selectAllButton->move(QPoint(x, height()));
166 selectAllButtonAnimation =
167 new QPropertyAnimation(selectAllButton, "pos", this);
169 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
170 selectAllButtonAnimation->setEndValue(QPoint(x,y));
171 selectAllButtonAnimation->setDuration(400);
172 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
173 //==================================================================
175 y = height() - 3*copyButton->sizeHint().height();
177 searchButton->move(QPoint(x, height()));
179 searchButtonAnimation =
180 new QPropertyAnimation(searchButton, "pos", this);
182 searchButtonAnimation->setStartValue(QPoint(x, height()));
183 searchButtonAnimation->setEndValue(QPoint(x,y));
184 searchButtonAnimation->setDuration(600);
185 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
186 //==================================================================
188 buttonsAnimation = new QParallelAnimationGroup(this);
189 buttonsAnimation->addAnimation(zoomInButtonAnimation);
190 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
191 buttonsAnimation->addAnimation(selectAllButtonAnimation);
192 buttonsAnimation->addAnimation(copyButtonAnimation);
193 buttonsAnimation->addAnimation(searchButtonAnimation);
194 buttonsInitialized = true;
195 buttonsVisible = false;
197 connect(showButtonsButton, SIGNAL(clicked()),
198 this, SLOT(showButtons()));
202 void TranslationWidget::initializeUI() {
204 textEdit = new TranslationTextEdit(this);
205 textEdit->setReadOnly(true);
207 resizer = new TranslationWidgetAutoResizer(textEdit);
208 connect(this, SIGNAL(updateSize()),
209 resizer, SLOT(textEditChanged()));
211 QWidget*w = new QWidget(this);
212 verticalLayout = new QVBoxLayout(w);
213 verticalLayout->addWidget(textEdit);
215 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
216 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
219 this->setWidgetResizable(true);
222 zoomInButton = new QToolButton(this);
223 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
224 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
226 zoomOutButton = new QToolButton(this);
227 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
228 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
230 selectAllButton = new QToolButton(this);
231 selectAllButton->setIcon(QIcon(":/icons/48x48/edit-select-all.png"));
232 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
234 copyButton = new QToolButton(this);
235 copyButton->setIcon(QIcon::fromTheme("general_notes"));
236 copyButton->setMinimumSize(copyButton->sizeHint());
237 copyButton->setEnabled(false);
239 searchButton = new QToolButton(this);
240 searchButton->setIcon(QIcon::fromTheme("general_search"));
241 searchButton->setMinimumSize(searchButton->sizeHint());
242 searchButton->setEnabled(false);
244 showButtonsButton = new QToolButton(this);
245 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
246 showButtonsButton->setMinimumSize(searchButton->sizeHint());
248 connect(zoomInButton, SIGNAL(clicked()),
249 textEdit, SLOT(zoomIn()));
251 connect(zoomOutButton, SIGNAL(clicked()),
252 textEdit, SLOT(zoomOut()));
255 connect(searchButton, SIGNAL(clicked()),
256 this, SLOT(searchSelected()));
258 connect(copyButton, SIGNAL(clicked()),
259 textEdit, SLOT(copy()));
261 connect(textEdit, SIGNAL(copyAvailable(bool)),
262 searchButton, SLOT(setEnabled(bool)));
264 connect(textEdit, SIGNAL(copyAvailable(bool)),
265 copyButton, SLOT(setEnabled(bool)));
267 connect(selectAllButton, SIGNAL(clicked()),
268 textEdit, SLOT(selectAll()));
270 buttonsInitialized = false;
278 void TranslationWidget::searchSelected() {
282 Q_EMIT search(textEdit->textCursor().selectedText());
286 void TranslationWidget::showButtons() {
287 if(!buttonsVisible) {
288 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
289 buttonsAnimation->start();
290 buttonsVisible = true;
292 showButtonsButton->setIcon(QIcon::fromTheme("general_received"));
294 else if(buttonsVisible) {
295 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
296 buttonsAnimation->start();
297 buttonsVisible = false;
298 showButtonsButton->setIcon(QIcon::fromTheme("general_sent"));
302 void TranslationWidget::grabZoomKeys(bool grab) {
307 unsigned long val = (grab) ? 1 : 0;
308 Atom atom = XInternAtom(QX11Info::display(),
309 "_HILDON_ZOOM_KEY_ATOM", False);
314 XChangeProperty (QX11Info::display(),
320 reinterpret_cast<unsigned char *>(&val),
324 void TranslationWidget::hideEvent(QHideEvent* e) {
328 QScrollArea::hideEvent(e);
331 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
332 switch (event->key()) {
343 QWidget::keyPressEvent(event);