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