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