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);
39 setWindowFlags(windowFlags() | Qt::Window);
43 setWindowTitle(tr("Translation"));
45 connect(textEdit, SIGNAL(search()),
46 this, SLOT(searchSelected()));
51 void TranslationWidget::show() {
55 void TranslationWidget::show(QStringList translations) {
60 if(!buttonsInitialized)
68 foreach(t, translations) {
72 textEdit->setPlainText(trans);
74 textEdit->repaint(this->rect());
82 void TranslationWidget::initButtons() {
84 int x = width() - showButtonsButton->sizeHint().width();
85 int y = height() - showButtonsButton->sizeHint().height();
87 showButtonsButton->move(QPoint(x,y));
88 showButtonsButton->show();
89 showButtonsButton->setCheckable(true);
90 //==================================================================
92 x = width() - zoomOutButton->sizeHint().width();
93 y = height() - 2*zoomOutButton->sizeHint().height();
94 zoomOutButton->move(QPoint(x, height()));
96 zoomOutButtonAnimation =
97 new QPropertyAnimation(zoomOutButton, "pos", this);
99 zoomOutButtonAnimation->setStartValue(QPoint(x, height()));
100 zoomOutButtonAnimation->setEndValue(QPoint(x,y));
101 zoomOutButtonAnimation->setDuration(200);
102 zoomOutButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
103 //==================================================================
104 x = width() - zoomInButton->sizeHint().width();
105 y = height() - 3*zoomInButton->sizeHint().height();
106 zoomInButton->move(QPoint(x, height()));
108 zoomInButtonAnimation =
109 new QPropertyAnimation(zoomInButton, "pos", this);
111 zoomInButtonAnimation->setStartValue(QPoint(x, height()));
112 zoomInButtonAnimation->setEndValue(QPoint(x,y));
113 zoomInButtonAnimation->setDuration(400);
114 zoomInButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
115 //==================================================================
117 y = height() - copyButton->sizeHint().height();
119 copyButton->move(QPoint(x, height()));
121 copyButtonAnimation =
122 new QPropertyAnimation(copyButton, "pos", this);
124 copyButtonAnimation->setStartValue(QPoint(x, height()));
125 copyButtonAnimation->setEndValue(QPoint(x,y));
126 copyButtonAnimation->setDuration(200);
127 copyButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
128 //==================================================================
130 y = height() - 2*copyButton->sizeHint().height();
132 selectAllButton->move(QPoint(x, height()));
134 selectAllButtonAnimation =
135 new QPropertyAnimation(selectAllButton, "pos", this);
137 selectAllButtonAnimation->setStartValue(QPoint(x, height()));
138 selectAllButtonAnimation->setEndValue(QPoint(x,y));
139 selectAllButtonAnimation->setDuration(400);
140 selectAllButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
141 //==================================================================
143 y = height() - 3*copyButton->sizeHint().height();
145 searchButton->move(QPoint(x, height()));
147 searchButtonAnimation =
148 new QPropertyAnimation(searchButton, "pos", this);
150 searchButtonAnimation->setStartValue(QPoint(x, height()));
151 searchButtonAnimation->setEndValue(QPoint(x,y));
152 searchButtonAnimation->setDuration(600);
153 searchButtonAnimation->setEasingCurve(QEasingCurve::InOutBack);
154 //==================================================================
156 buttonsAnimation = new QParallelAnimationGroup(this);
157 buttonsAnimation->addAnimation(zoomInButtonAnimation);
158 buttonsAnimation->addAnimation(zoomOutButtonAnimation);
159 buttonsAnimation->addAnimation(selectAllButtonAnimation);
160 buttonsAnimation->addAnimation(copyButtonAnimation);
161 buttonsAnimation->addAnimation(searchButtonAnimation);
162 buttonsInitialized = true;
163 buttonsVisible = false;
165 connect(showButtonsButton, SIGNAL(toggled(bool)),
166 this, SLOT(showButtons(bool)));
170 void TranslationWidget::initializeUI() {
172 textEdit = new TranslationTextEdit;
173 textEdit->setReadOnly(true);
175 resizer = new TranslationWidgetAutoResizer(textEdit);
176 connect(this, SIGNAL(updateSize()),
177 resizer, SLOT(textEditChanged()));
179 QWidget*w = new QWidget;
180 verticalLayout = new QVBoxLayout(w);
181 verticalLayout->addWidget(textEdit);
183 textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
184 textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
188 this->setWidgetResizable(true);
192 zoomInButton = new QToolButton(this);
193 zoomInButton->setIcon(QIcon::fromTheme("pdf_zoomin"));
194 zoomInButton->setMinimumSize(zoomInButton->sizeHint());
196 zoomOutButton = new QToolButton(this);
197 zoomOutButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
198 zoomOutButton->setMinimumSize(zoomOutButton->sizeHint());
200 selectAllButton = new QToolButton(this);
201 selectAllButton->setIcon(QIcon::fromTheme("pdf_zoomout"));
202 selectAllButton->setMinimumSize(selectAllButton->sizeHint());
204 copyButton = new QToolButton(this);
205 copyButton->setIcon(QIcon::fromTheme("general_notes"));
206 copyButton->setMinimumSize(copyButton->sizeHint());
207 copyButton->setEnabled(false);
209 searchButton = new QToolButton(this);
210 searchButton->setIcon(QIcon::fromTheme("general_search"));
211 searchButton->setMinimumSize(searchButton->sizeHint());
212 searchButton->setEnabled(false);
214 showButtonsButton = new QToolButton(this);
215 showButtonsButton->setIcon(QIcon::fromTheme("general_search"));
216 showButtonsButton->setMinimumSize(searchButton->sizeHint());
218 connect(zoomInButton, SIGNAL(clicked()),
219 textEdit, SLOT(zoomIn()));
221 connect(zoomOutButton, SIGNAL(clicked()),
222 textEdit, SLOT(zoomOut()));
225 connect(searchButton, SIGNAL(clicked()),
226 this, SLOT(searchSelected()));
228 connect(copyButton, SIGNAL(clicked()),
229 textEdit, SLOT(copy()));
231 connect(textEdit, SIGNAL(copyAvailable(bool)),
232 searchButton, SLOT(setEnabled(bool)));
234 connect(textEdit, SIGNAL(copyAvailable(bool)),
235 copyButton, SLOT(setEnabled(bool)));
237 connect(selectAllButton, SIGNAL(clicked()),
238 textEdit, SLOT(selectAll()));
240 buttonsInitialized = false;
248 void TranslationWidget::searchSelected() {
252 emit search(textEdit->textCursor().selectedText());
256 void TranslationWidget::showButtons(bool show) {
257 if(show && !buttonsVisible) {
258 buttonsAnimation->setDirection(QAbstractAnimation::Forward);
259 buttonsAnimation->start();
260 buttonsVisible = true;
262 else if(!show && buttonsVisible) {
263 buttonsAnimation->setDirection(QAbstractAnimation::Backward);
264 buttonsAnimation->start();
265 buttonsVisible = false;
269 void TranslationWidget::grabZoomKeys(bool grab) {
274 unsigned long val = (grab) ? 1 : 0;
275 Atom atom = XInternAtom(QX11Info::display(),
276 "_HILDON_ZOOM_KEY_ATOM", False);
281 XChangeProperty (QX11Info::display(),
287 reinterpret_cast<unsigned char *>(&val),
291 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
292 switch (event->key()) {
303 QWidget::keyPressEvent(event);