serachBar + progressBar
[mdictionary] / src / mdictionary / 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 /*! \file SearchBarWidget.cpp
23     \brief Displays search bar
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28
29 #include "SearchBarWidget.h"
30 #include <QDebug>
31 #include "../../include/DictDialog.h"
32 #include "HistoryListDialog.h"
33
34
35 SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
36 #ifndef Q_WS_MAEMO_5
37     this->setMaximumHeight(50);
38
39     view= new QDeclarativeView();
40     view->setSource(QUrl("src/mdictionary/qml/SearchBarWidget.qml"));
41     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
42     view->setAlignment(Qt::AlignCenter);
43     view->show();
44
45     mainLayout = new QVBoxLayout;
46     mainLayout->addWidget(view);
47     setLayout(mainLayout);
48
49     QGraphicsObject *rootObject = view->rootObject();
50
51     connect(rootObject, SIGNAL(searchButtonClicked(QString)),
52             this, SLOT(searchButtonClicked(QString)));
53     connect(rootObject, SIGNAL(historyNextToolButtonClicked()),
54             this, SIGNAL(historyNext()));
55     connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
56             this, SIGNAL(historyPrev()));
57     connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
58             this, SIGNAL(historyShow()));
59
60     connect(this, SIGNAL(setEnableHistoryNext(QVariant)),
61             rootObject, SLOT(setEnableHistoryNext(QVariant)));
62     connect(this, SIGNAL(setEnableHistoryShow(QVariant)),
63             rootObject, SLOT(setEnableHistoryShow(QVariant)));
64     connect(this, SIGNAL(setEnableHistoryPrev(QVariant)),
65             rootObject, SLOT(setEnableHistoryPrev(QVariant)));
66     connect(this, SIGNAL(setButtonText(QVariant)),
67             rootObject, SLOT(setButtonText(QVariant)));
68     connect(this, SIGNAL(setLineEditText(QVariant)),
69             rootObject, SLOT(setLineEditText(QVariant)));
70     connect(this, SIGNAL(setLineEditEnables(QVariant)),
71             rootObject, SLOT(setEnableLineEdit(QVariant)));
72
73     emit setEnableHistoryNext(false);
74     emit setEnableHistoryShow(false);
75     emit setEnableHistoryPrev(false);
76
77     completerModel = new QStringListModel(this);
78     connect(&delayTimer, SIGNAL(timeout()),
79             this, SLOT(delaySearchTimeout()));
80 #else
81     initializeUI();
82     connect(searchPushButton, SIGNAL(clicked()),
83             this, SLOT(searchPushButtonClicked()));
84     connect(searchWordLineEdit, SIGNAL(returnPressed()),
85             this, SLOT(searchPushButtonClicked()));
86     connect(historyNextToolButton, SIGNAL(clicked()),
87             this, SIGNAL(historyNext()));
88     connect(historyPrevToolButton, SIGNAL(clicked()),
89             this, SIGNAL(historyPrev()));
90     connect(historyShowToolButton, SIGNAL(clicked()),
91             this, SLOT(showHistoryButtonClicked()));
92     connect(clearSearchWordToolButton, SIGNAL(clicked()),
93             this, SLOT(clearSearchWordToolButtonClicked()));
94     connect(&delayTimer, SIGNAL(timeout()),
95             this, SLOT(delaySearchTimeout()));
96
97
98     searchWordLineEdit->setFocus();
99 #endif
100
101     busy = false;
102     setEnabled(true);
103     updateHistoryButtons(false,false,false);
104 }
105
106 SearchBarWidget::~SearchBarWidget() {
107
108 }
109
110 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
111     QPixmap p = original.pixmap(64);
112
113     if(rotation != 0) {
114         QMatrix m;
115         m.rotate(rotation);
116
117         p = p.transformed(m);
118     }
119
120     QIcon newIcon;
121     newIcon.addPixmap(p);
122
123
124     #ifdef Q_WS_MAEMO_5
125         QImage img = p.toImage();
126
127         for(int i=0; i < img.width(); i++) {
128             for(int j=0; j < img.height(); j++) {
129                 QColor c = img.pixel(i,j);
130                 if(c != QColor(0,0,0,255)) {
131                     c.setRed(c.red()/2);
132                     c.setGreen(c.green()/2);
133                     c.setBlue(c.blue()/2);
134                     img.setPixel(i, j, c.rgb());
135                 }
136             }
137         }
138         p = p.fromImage(img);
139
140         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
141     #endif
142
143     return newIcon;
144 }
145
146 void SearchBarWidget::setFocus() {
147 #ifdef Q_WS_MAEMO_5
148         searchWordLineEdit->setFocus();
149 #endif
150 }
151
152 void SearchBarWidget::initializeUI() {
153 #ifdef Q_WS_MAEMO_5
154     setMaximumHeight(150);
155
156     horizontalLayout = new QHBoxLayout;
157     verticalLayout = new QVBoxLayout;
158
159     searchPushButton = new QPushButton(tr("Search"));
160     searchPushButton->setMinimumWidth(125);
161
162     searchWordLineEdit = new QLineEdit;
163     searchWordLineEdit->setMinimumWidth(250);
164
165     completerModel = new QStringListModel(this);
166
167     lineEditCompleter = new QCompleter(searchWordLineEdit);
168     lineEditCompleter->setModel(completerModel);
169     lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
170     lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
171     searchWordLineEdit->setCompleter(lineEditCompleter);
172
173     #ifndef Q_WS_MAEMO_5
174         searchWordLineEdit->setMinimumHeight(
175                 searchWordLineEdit->sizeHint().height()*3/2);
176     #endif
177
178     //create layout for lineEdit to have clear button on it
179     QHBoxLayout* lineEditLayout = new QHBoxLayout;
180     searchWordLineEdit->setLayout(lineEditLayout);
181
182
183     clearSearchWordToolButton = new QToolButton;
184     #ifdef Q_WS_MAEMO_5
185         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
186         clearSearchWordToolButton->setMaximumSize(
187                 clearSearchWordToolButton->sizeHint().height()/2,
188                 clearSearchWordToolButton->sizeHint().height()/2);
189         lineEditLayout->setContentsMargins(0,0,15,0);
190     #else
191         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
192         clearSearchWordToolButton->setMinimumSize(
193                 searchWordLineEdit->sizeHint().height()*1.2,
194                 searchWordLineEdit->sizeHint().height()*1.2);
195         lineEditLayout->setContentsMargins(0,0,5,0);
196     #endif
197
198
199     historyNextToolButton = new QToolButton;
200     #ifdef Q_WS_MAEMO_5
201         historyNextToolButton->setIcon(
202                 generateIcon(QIcon::fromTheme("general_forward")));
203     #else
204         historyNextToolButton->setIcon(
205                 generateIcon(QIcon::fromTheme("go-next")));
206     #endif
207
208
209
210     historyPrevToolButton = new QToolButton;
211     #ifdef Q_WS_MAEMO_5
212         historyPrevToolButton->setIcon(
213                 generateIcon(QIcon::fromTheme("general_back")));
214     #else
215         historyPrevToolButton->setIcon(
216                 generateIcon(QIcon::fromTheme("go-previous")));
217     #endif
218
219
220
221     historyShowToolButton = new QToolButton;
222     #ifdef Q_WS_MAEMO_5
223         historyShowToolButton->setIcon(
224                 generateIcon(QIcon::fromTheme("general_back"), 90));
225     #else
226         historyShowToolButton->setIcon(
227                 generateIcon(QIcon::fromTheme("go-up")));
228     #endif
229
230     searchingProgressBar = new QProgressBar;
231     //progress bar has minimum and maximum values set to 0, which will effect
232     //with "I'm alive" bar
233     searchingProgressBar->setMinimum(0);
234     searchingProgressBar->setMaximum(0);
235     #ifdef Q_WS_MAEMO_5
236         searchingProgressBar->setMaximumHeight(50);
237     #endif
238     searchingProgressBar->hide();
239
240     setLayout(verticalLayout);
241
242     verticalLayout->addWidget(searchingProgressBar);
243
244     //adding widgets to layout
245     horizontalLayout->addWidget(searchWordLineEdit);
246     horizontalLayout->addWidget(searchPushButton);
247     horizontalLayout->addWidget(historyPrevToolButton);
248     horizontalLayout->addWidget(historyShowToolButton);
249     horizontalLayout->addWidget(historyNextToolButton);
250
251     //adding clear toolButton to textEdit with right alignment
252     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
253
254     verticalLayout->addLayout(horizontalLayout);
255 #endif
256 }
257
258 void SearchBarWidget::searchButtonClicked(QString text) {
259     if(busy)
260         Q_EMIT stopSearching();
261     else
262         search(text);
263 }
264
265 void SearchBarWidget::searchPushButtonClicked() {
266 #ifdef Q_WS_MAEMO_5
267     if(busy) {
268         Q_EMIT stopSearching();
269     }
270     else {
271         search(searchWordLineEdit->text());
272     }
273 #endif
274 }
275
276 void SearchBarWidget::search(QString word) {
277     qDebug()<<word;
278     if(!busy && !word.isEmpty()) {
279         completerModel->insertRow(completerModel->rowCount());
280         QModelIndex index=completerModel->index(completerModel->rowCount() -1);
281         completerModel->setData(index, word);
282
283 #ifndef Q_WS_MAEMO_5
284         emit setLineEditText(word);
285 #else
286         searchWordLineEdit->setText(word);
287 #endif
288         Q_EMIT searchForTranslations(word);
289     }
290 }
291
292 void SearchBarWidget::searchDelay(QString word) {
293     if(!busy && !word.isEmpty()) {
294         #ifndef Q_WS_MAEMO_5
295             emit setLineEditText(word);
296         #else
297             searchWordLineEdit->setText(word);
298         #endif
299             if(delayTimer.isActive())
300                 delayTimer.stop();
301             delayString = word;
302             delayTimer.start(500);
303
304     }
305 }
306
307 void SearchBarWidget::delaySearchTimeout() {
308     delayTimer.stop();
309     if(!busy) {
310         Q_EMIT searchForTranslations(delayString);
311     }
312 }
313
314 void SearchBarWidget::setEnabled(bool enabled) {
315 #ifndef Q_WS_MAEMO_5
316     emit setLineEditEnables(enabled);
317     if(!enabled) {
318         emit setEnableHistoryNext(false);
319         emit setEnableHistoryShow(false);
320         emit setEnableHistoryPrev(false);
321     }
322 #else
323     searchWordLineEdit->setEnabled(enabled);
324     if(!enabled) {
325         historyPrevToolButton->setEnabled(false);
326         historyNextToolButton->setEnabled(false);
327         historyShowToolButton->setEnabled(false);
328     }
329 #endif
330 }
331
332 void SearchBarWidget::setBusy() {
333     if(busy) return;
334
335 #ifndef Q_WS_MAEMO_5
336     emit setButtonText(tr("Stop"));
337 //  searchingProgressBar->show();
338 #else
339     searchingProgressBar->show();
340     searchPushButton->setText(tr("Stop"));
341 #endif
342
343     setEnabled(false);
344     busy = true;
345 }
346
347 void SearchBarWidget::setIdle() {
348     if(!busy) return;
349 #ifndef Q_WS_MAEMO_5
350 //    searchingProgressBar->hide();
351     emit setButtonText(tr("Search"));
352 #else
353     searchingProgressBar->hide();
354     searchPushButton->setText(tr("Search"));
355 #endif
356     setEnabled(true);
357     busy = false;
358     Q_EMIT refreshHistoryButtons();
359 }
360
361
362 void SearchBarWidget::clearSearchWordToolButtonClicked() {
363 #ifdef Q_WS_MAEMO_5
364     searchWordLineEdit->clear();
365 #endif
366 }
367
368
369
370 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
371     if(!busy) {
372         #ifndef Q_WS_MAEMO_5
373             emit setEnableHistoryNext(next);
374             emit setEnableHistoryShow(list);
375             emit setEnableHistoryPrev(prev);
376         #else
377             historyPrevToolButton->setEnabled(prev);
378             historyNextToolButton->setEnabled(next);
379             historyShowToolButton->setEnabled(list);
380         #endif
381     }
382 }
383
384 void SearchBarWidget::showHistoryButtonClicked() {
385 #ifndef Q_WS_MAEMO_5
386     QPoint p=view->pos(); // = historyShowToolButton->pos();
387     p.setY(p.y());
388     emit historyShow(mapToGlobal(p));
389  #else
390     emit historyShow();
391 #endif
392 }