detect a disconect error in GooglePlugin
[mdictionary] / trunk / src / base / gui / SearchBarWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24
25 #include "SearchBarWidget.h"
26 #include <QDebug>
27 #include "../../includes/DictDialog.h"
28 #include "HistoryListDialog.h"
29
30
31 SearchBarWidget::SearchBarWidget(QWidget *parent) :
32     QWidget(parent) {
33
34     initializeUI();
35
36
37     _isSearching = false;
38
39     connect(searchPushButton, SIGNAL(clicked()),
40             this, SLOT(searchPushButtonClicked()));
41
42     connect(searchWordLineEdit, SIGNAL(returnPressed()),
43             this, SLOT(searchPushButtonClicked()));
44
45     connect(historyNextToolButton, SIGNAL(clicked()),
46             this, SIGNAL(historyNext()));
47
48     connect(historyPrevToolButton, SIGNAL(clicked()),
49             this, SIGNAL(historyPrev()));
50
51     connect(historyShowToolButton, SIGNAL(clicked()),
52             this, SIGNAL(historyShow()));
53
54     connect(clearSearchWordToolButton, SIGNAL(clicked()),
55             this, SLOT(clearSearchWordToolButtonClicked()));
56
57
58     connect(&delayTimer, SIGNAL(timeout()),
59             this, SLOT(delaySearchTimeout()));
60
61
62     searchWordLineEdit->setFocus();
63
64     historyPrevToolButton->setEnabled(false);
65     historyNextToolButton->setEnabled(false);
66     historyShowToolButton->setEnabled(false);
67     setEnabled(true);
68 }
69
70 SearchBarWidget::~SearchBarWidget() {
71
72 }
73
74 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
75     QPixmap p = oryginal.pixmap(64);
76
77     if(rotation != 0) {
78         QMatrix m;
79         m.rotate(rotation);
80
81         p = p.transformed(m);
82     }
83
84     QIcon newIcon;
85     newIcon.addPixmap(p);
86
87
88     #ifdef Q_WS_MAEMO_5
89         QPainter painter(&p);
90         painter.fillRect(p.rect(), QColor(0,0,0,192));
91
92         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
93     #endif
94
95     return newIcon;
96 }
97
98
99 void SearchBarWidget::initializeUI() {
100
101     #ifdef Q_WS_MAEMO_5
102         setMaximumHeight(150);
103     #else
104         setMaximumHeight(70);
105     #endif
106
107
108     horizontalLayout = new QHBoxLayout(this);
109     verticalLayout = new QVBoxLayout(this);
110
111
112     searchPushButton = new QPushButton(tr("Search"),this);
113     searchPushButton->setMinimumWidth(125);
114
115
116     searchWordLineEdit = new QLineEdit(this);
117     searchWordLineEdit->setMinimumWidth(250);
118
119     #ifndef Q_WS_MAEMO_5
120         searchWordLineEdit->setMinimumHeight(
121                 searchWordLineEdit->sizeHint().height()*3/2);
122     #endif
123
124
125     //create layout for lineEdit to have clear button on it
126     QHBoxLayout* lineEditLayout = new QHBoxLayout(this);
127     searchWordLineEdit->setLayout(lineEditLayout);
128
129
130     clearSearchWordToolButton = new QToolButton(this);
131     #ifdef Q_WS_MAEMO_5
132         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
133         //tool buttons will have size 2 times smaller
134         clearSearchWordToolButton->setMaximumSize(
135                 clearSearchWordToolButton->sizeHint().height()/2,
136                 clearSearchWordToolButton->sizeHint().height()/2);
137     #else
138         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
139         clearSearchWordToolButton->setMinimumSize(
140                 clearSearchWordToolButton->sizeHint().height(),
141                 clearSearchWordToolButton->sizeHint().height());
142     #endif
143
144
145     historyNextToolButton = new QToolButton(this);
146     #ifdef Q_WS_MAEMO_5
147         historyNextToolButton->setIcon(
148                 generateIcon(QIcon::fromTheme("general_forward")));
149     #else
150         historyNextToolButton->setIcon(
151                 generateIcon(QIcon::fromTheme("go-next")));
152     #endif
153
154
155
156     historyPrevToolButton = new QToolButton(this);
157     #ifdef Q_WS_MAEMO_5
158         historyPrevToolButton->setIcon(
159                 generateIcon(QIcon::fromTheme("general_back")));
160     #else
161         historyPrevToolButton->setIcon(
162                 generateIcon(QIcon::fromTheme("go-previous")));
163     #endif
164
165
166
167     historyShowToolButton = new QToolButton(this);
168     #ifdef Q_WS_MAEMO_5
169         historyShowToolButton->setIcon(
170                 generateIcon(QIcon::fromTheme("general_back"), 90));
171     #else
172         historyShowToolButton->setIcon(
173                 generateIcon(QIcon::fromTheme("go-up")));
174     #endif
175
176     /*fullScreenToolButton = new QToolButton();
177     #ifdef Q_WS_MAEMO_5
178         fullScreenToolButton->setIcon(
179                 generateIcon(QIcon::fromTheme("general_fullsize")));
180     #else
181         fullScreenToolButton->setIcon(
182                 generateIcon(QIcon::fromTheme("view-fullscreen")));
183         fullScreenToolButton->setMinimumSize(
184                 fullScreenToolButton->sizeHint().height()*2,
185                 fullScreenToolButton->sizeHint().height()*2);
186     #endif*/
187
188     searchingProgressBar = new QProgressBar(this);
189     //progress bar has minimum and maximum values set to 0, which will effect
190     //with "I'm alive" bar
191     searchingProgressBar->setMinimum(0);
192     searchingProgressBar->setMaximum(0);
193     searchingProgressBar->hide();
194     #ifdef Q_WS_MAEMO_5
195         searchingProgressBar->setMaximumHeight(50);
196     #endif
197
198
199     setLayout(verticalLayout);
200
201     verticalLayout->addWidget(searchingProgressBar);
202
203     //adding widgets to layout
204     horizontalLayout->addWidget(searchWordLineEdit);
205     horizontalLayout->addWidget(searchPushButton);
206     horizontalLayout->addWidget(historyPrevToolButton);
207     horizontalLayout->addWidget(historyShowToolButton);
208     horizontalLayout->addWidget(historyNextToolButton);
209    // horizontalLayout->addWidget(fullScreenToolButton);
210
211     //adding clear toolButton to textEdit with right alignment
212     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
213                               Qt::AlignRight | Qt::AlignVCenter);
214
215     verticalLayout->addLayout(horizontalLayout);
216 }
217
218
219 void SearchBarWidget::searchPushButtonClicked() {
220     if(_isSearching) {
221         Q_EMIT stopSearching();
222     }
223     else {
224         search(searchWordLineEdit->text());
225     }
226 }
227
228
229 void SearchBarWidget::search(QString word) {
230     if(!_isSearching && !word.isEmpty()) {
231         searchWordLineEdit->setText(word);
232         Q_EMIT searchForTranslations(word);
233     }
234 }
235
236 void SearchBarWidget::searchDelay(QString word) {
237     if(!_isSearching && !word.isEmpty()) {
238         searchWordLineEdit->setText(word);
239
240
241         if(delayTimer.isActive()) {
242             delayTimer.stop();
243         }
244
245         delayString = word;
246         delayTimer.start(500);
247     }
248 }
249
250 void SearchBarWidget::delaySearchTimeout() {
251     delayTimer.stop();
252     if(!_isSearching) {
253         Q_EMIT searchForTranslations(delayString);
254     }
255 }
256
257 void SearchBarWidget::setEnabled(bool enabled) {
258     searchWordLineEdit->setEnabled(enabled);
259
260     if(!enabled) {
261         historyPrevToolButton->setEnabled(false);
262         historyNextToolButton->setEnabled(false);
263         historyShowToolButton->setEnabled(false);
264     }
265 }
266
267 void SearchBarWidget::setBusy() {
268     if(_isSearching) return;
269     searchingProgressBar->show();
270     searchPushButton->setText(tr("Stop"));
271     setEnabled(false);
272     _isSearching = true;
273 }
274
275 void SearchBarWidget::setIdle() {
276     if(!_isSearching) return;
277     searchingProgressBar->hide();
278     searchPushButton->setText(tr("Search"));
279     setEnabled(true);
280     _isSearching = false;
281     Q_EMIT refreshHistoryButtons();
282 }
283
284
285 void SearchBarWidget::clearSearchWordToolButtonClicked() {
286     searchWordLineEdit->clear();
287 }
288
289
290
291 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
292     if(!_isSearching) {
293         historyPrevToolButton->setEnabled(prev);
294         historyNextToolButton->setEnabled(next);
295         historyShowToolButton->setEnabled(list);
296     }
297 }