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