add focus in qml
[mdictionary] / src / mdictionary / gui / SearchBarWidget.cpp
index b53d664..da482d0 100644 (file)
 
 SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
 
-    initializeUI();
+    completerModel = new QStringList;
+    lineEditCompleter = new QCompleter(this);
+    lineEditCompleter->setModel(new QStringListModel(*completerModel));
+    lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
+    lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
 
+#ifndef Q_WS_MAEMO_5
+    this->setMaximumHeight(50);
+    busyTimer=new QTimer;
 
-    busy = false;
+    progressBar = new QDeclarativeView();
+    progressBar->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/ProgressBar.qml"));
+    progressBar->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    progressBar->setAlignment(Qt::AlignCenter);
+    progressBar->hide();
+
+    view= new QDeclarativeView();
+    ctxt = view->rootContext();
+    ctxt->setContextProperty("focusss",true);
+
+    view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SearchBarWidget.qml"));
+    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    view->setAlignment(Qt::AlignCenter);
+    view->show();
+
+    mainLayout = new QVBoxLayout;
+    mainLayout->addWidget(progressBar);
+    mainLayout->addWidget(view);
+    setLayout(mainLayout);
+
+    QGraphicsObject *rootObject = view->rootObject();
+    QGraphicsObject *rootObject2 = progressBar->rootObject();
+
+    connect(rootObject, SIGNAL(searchButtonClicked(QString)),
+            this, SLOT(searchButtonClicked(QString)));
+    connect(rootObject, SIGNAL(historyNextToolButtonClicked()),
+            this, SIGNAL(historyNext()));
+    connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
+            this, SIGNAL(historyPrev()));
+    connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
+            this, SLOT(showHistoryButtonClicked()));
+    connect(rootObject, SIGNAL(textChange(QString)),
+            this, SLOT(textChange(QString)));
+    connect(rootObject, SIGNAL(nextCompleter()),
+            this, SLOT(nextCompleter()));
+    connect(rootObject, SIGNAL(prevCompleter()),
+            this, SLOT(prevCompleter()));
+    connect(rootObject, SIGNAL(checkFocus()),
+            this, SLOT(checkFocus()));
+    connect(rootObject, SIGNAL(nextFocus()),
+            this, SLOT(nextFocus()));
+
+    connect(this, SIGNAL(progresSetMax(QVariant)),
+            rootObject2, SLOT(setMax(QVariant)));
+    connect(this, SIGNAL(progresSetMin(QVariant)),
+            rootObject2, SLOT(setMin(QVariant)));
+    connect(this, SIGNAL(progresSetValue(QVariant)),
+            rootObject2, SLOT(setValue(QVariant)));
+    connect(this, SIGNAL(progresSetValue2(QVariant)),
+            rootObject2, SLOT(setValue2(QVariant)));
+
+    connect(this, SIGNAL(setEnableHistoryNext(QVariant)),
+            rootObject, SLOT(setEnableHistoryNext(QVariant)));
+    connect(this, SIGNAL(setEnableHistoryShow(QVariant)),
+            rootObject, SLOT(setEnableHistoryShow(QVariant)));
+    connect(this, SIGNAL(setEnableHistoryPrev(QVariant)),
+            rootObject, SLOT(setEnableHistoryPrev(QVariant)));
+    connect(this, SIGNAL(setButtonText(QVariant)),
+            rootObject, SLOT(setButtonText(QVariant)));
+    connect(this, SIGNAL(setLineEditText(QVariant)),
+            rootObject, SLOT(setLineEditText(QVariant)));
+    connect(this, SIGNAL(setLineEditEnables(QVariant)),
+            rootObject, SLOT(setEnableLineEdit(QVariant)));
+
+    connect(this, SIGNAL(setCompleterText(QVariant)),
+            rootObject, SLOT(setCompleterText(QVariant)));
+    connect(this, SIGNAL(focusOff()),
+            rootObject, SLOT(focusOff()));
+
+    connect(busyTimer, SIGNAL(timeout()),
+            this, SLOT(updateBusyTimer()));
+
+    emit setEnableHistoryNext(false);
+    emit setEnableHistoryShow(false);
+    emit setEnableHistoryPrev(false);
+
+    connect(&delayTimer, SIGNAL(timeout()),
+            this, SLOT(delaySearchTimeout()));
 
+#else
+    initializeUI();
     connect(searchPushButton, SIGNAL(clicked()),
             this, SLOT(searchPushButtonClicked()));
-
     connect(searchWordLineEdit, SIGNAL(returnPressed()),
             this, SLOT(searchPushButtonClicked()));
-
     connect(historyNextToolButton, SIGNAL(clicked()),
             this, SIGNAL(historyNext()));
-
     connect(historyPrevToolButton, SIGNAL(clicked()),
             this, SIGNAL(historyPrev()));
-
     connect(historyShowToolButton, SIGNAL(clicked()),
             this, SLOT(showHistoryButtonClicked()));
-
     connect(clearSearchWordToolButton, SIGNAL(clicked()),
             this, SLOT(clearSearchWordToolButtonClicked()));
-
-
     connect(&delayTimer, SIGNAL(timeout()),
             this, SLOT(delaySearchTimeout()));
 
-
     searchWordLineEdit->setFocus();
-
-    historyPrevToolButton->setEnabled(false);
-    historyNextToolButton->setEnabled(false);
-    historyShowToolButton->setEnabled(false);
-
+#endif
+    setFocus();
+    busy = false;
     setEnabled(true);
+    updateHistoryButtons(false,false,false);
 }
 
 SearchBarWidget::~SearchBarWidget() {
 
 }
 
+void SearchBarWidget::checkFocus(){
+    qDebug()<<"focus:"<<QApplication::focusWidget();
+    if(focusWidget()!=QApplication::focusWidget())
+        emit focusOff();
+}
+
+void SearchBarWidget::nextFocus(){
+    parentWidget()->nextInFocusChain()->setFocus();
+}
+
+void SearchBarWidget::textChange(QString text){
+    QLineEdit line;
+    QString toSend="";
+    QString tempString;
+    actualString=text;
+    completerActualList.clear();
+
+    line.setCompleter(lineEditCompleter);
+    line.completer()->setCompletionPrefix(text);
+    for (int i = 0; lineEditCompleter->setCurrentRow(i); i++)
+        completerActualList.append(lineEditCompleter->currentCompletion());
+
+    completerActualList.sort();
+    if(completerActualList.contains(preferedCompliter)){
+        tempString = preferedCompliter;
+        toSend = tempString.remove(0,text.size());
+        emit setCompleterText(toSend);
+    }
+    else if(completerActualList.size()>0 && text.size()>0){
+        toSend = completerActualList.at(0);
+        preferedCompliter= toSend;
+        toSend=toSend.remove(0,text.size());
+        if(toSend.size()>0)
+            emit setCompleterText(toSend);
+        else if(completerActualList.size()>1){
+            toSend = completerActualList.at(1);
+            preferedCompliter= toSend;
+            toSend=toSend.remove(0,text.size());
+            emit setCompleterText(toSend);
+        }
+    }
+    else{
+        preferedCompliter="";
+        emit setCompleterText(toSend);
+    }
+}
+
+void SearchBarWidget::prevCompleter(){
+    QString tempString;
+    if(!preferedCompliter.isEmpty()){
+        int index = completerActualList.indexOf(preferedCompliter);
+        qDebug()<<"index"<<index<<"size"<<completerActualList.size()<<"+1";
+        if(index!=-1 && completerActualList.size()>index+1){
+            preferedCompliter = completerActualList.at(index+1);
+            tempString=preferedCompliter;
+            QString toSend = tempString.remove(0,actualString.size());
+            emit setCompleterText(toSend);
+        }
+    }
+}
+
+void SearchBarWidget::nextCompleter(){
+    QString tempString;
+    if(!preferedCompliter.isEmpty()){
+        int index = completerActualList.indexOf(preferedCompliter);
+        if(index>0){
+            preferedCompliter = completerActualList.at(index-1);
+            tempString=preferedCompliter;
+            QString toSend = tempString.remove(0,actualString.size());
+            emit setCompleterText(toSend);
+        }
+    }
+}
+
+
 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
+    qDebug()<<"test2";
     QPixmap p = original.pixmap(64);
 
-
     if(rotation != 0) {
         QMatrix m;
         m.rotate(rotation);
@@ -112,49 +264,35 @@ QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
     return newIcon;
 }
 
-
 void SearchBarWidget::setFocus() {
-    searchWordLineEdit->setFocus();
+#ifndef Q_WS_MAEMO_5
+        view->setFocus();
+#else
+        searchWordLineEdit->setFocus();
+#endif
 }
 
 void SearchBarWidget::initializeUI() {
-
-    #ifdef Q_WS_MAEMO_5
-        setMaximumHeight(150);
-    #else
-        setMaximumHeight(100);
-    #endif
-
+    qDebug()<<"test4";
+#ifdef Q_WS_MAEMO_5
+    setMaximumHeight(150);
 
     horizontalLayout = new QHBoxLayout;
     verticalLayout = new QVBoxLayout;
 
-
     searchPushButton = new QPushButton(tr("Search"));
     searchPushButton->setMinimumWidth(125);
 
-
     searchWordLineEdit = new QLineEdit;
     searchWordLineEdit->setMinimumWidth(250);
 
-
-
-    completerModel = new QStringListModel(this);
-
-
-    lineEditCompleter = new QCompleter(searchWordLineEdit);
-    lineEditCompleter->setModel(completerModel);
-    lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
-    lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
     searchWordLineEdit->setCompleter(lineEditCompleter);
 
-
     #ifndef Q_WS_MAEMO_5
         searchWordLineEdit->setMinimumHeight(
                 searchWordLineEdit->sizeHint().height()*3/2);
     #endif
 
-
     //create layout for lineEdit to have clear button on it
     QHBoxLayout* lineEditLayout = new QHBoxLayout;
     searchWordLineEdit->setLayout(lineEditLayout);
@@ -217,7 +355,6 @@ void SearchBarWidget::initializeUI() {
     #endif
     searchingProgressBar->hide();
 
-
     setLayout(verticalLayout);
 
     verticalLayout->addWidget(searchingProgressBar);
@@ -232,50 +369,66 @@ void SearchBarWidget::initializeUI() {
     //adding clear toolButton to textEdit with right alignment
     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
 
-
     verticalLayout->addLayout(horizontalLayout);
+#endif
 }
 
+void SearchBarWidget::searchButtonClicked(QString text) {
+    qDebug()<<"test5";
+    if(busy)
+        Q_EMIT stopSearching();
+    else
+        search(text);
+}
 
 void SearchBarWidget::searchPushButtonClicked() {
+    qDebug()<<"test6";
+#ifdef Q_WS_MAEMO_5
     if(busy) {
         Q_EMIT stopSearching();
     }
     else {
         search(searchWordLineEdit->text());
     }
+#endif
 }
 
-
 void SearchBarWidget::search(QString word) {
     if(!busy && !word.isEmpty()) {
-        completerModel->insertRow(completerModel->rowCount());
-        QModelIndex index =
-                completerModel->index(completerModel->rowCount() -1);
-
-        completerModel->setData(index, word);
-
-
+        while(word.lastIndexOf(" ")==word.size()-1 && word.size()>0)
+            word=word.remove(word.size()-1,1);
+        if(!completerModel->contains(word))
+            completerModel->append(word);
+        QAbstractItemModel *temp=lineEditCompleter->model();
+        lineEditCompleter->setModel(new QStringListModel(*completerModel));
+        delete temp;
+#ifndef Q_WS_MAEMO_5
+        emit setLineEditText(word);
+#else
         searchWordLineEdit->setText(word);
+#endif
         Q_EMIT searchForTranslations(word);
     }
 }
 
 void SearchBarWidget::searchDelay(QString word) {
+    qDebug()<<"test8";
     if(!busy && !word.isEmpty()) {
-        searchWordLineEdit->setText(word);
-
+        #ifndef Q_WS_MAEMO_5
+            emit setLineEditText(word);
+        #else
+            searchWordLineEdit->setText(word);
+        #endif
+            if(delayTimer.isActive())
+                delayTimer.stop();
+            delayString = word;
+            delayTimer.start(500);
 
-        if(delayTimer.isActive()) {
-            delayTimer.stop();
-        }
-
-        delayString = word;
-        delayTimer.start(500);
     }
 }
 
 void SearchBarWidget::delaySearchTimeout() {
+    qDebug()<<"test9";
     delayTimer.stop();
     if(!busy) {
         Q_EMIT searchForTranslations(delayString);
@@ -283,27 +436,75 @@ void SearchBarWidget::delaySearchTimeout() {
 }
 
 void SearchBarWidget::setEnabled(bool enabled) {
+    qDebug()<<"test10";
+#ifndef Q_WS_MAEMO_5
+    emit setLineEditEnables(enabled);
+    if(!enabled) {
+        qDebug()<<"tu???";
+        emit setEnableHistoryNext(false);
+        emit setEnableHistoryShow(false);
+        emit setEnableHistoryPrev(false);
+    }
+#else
     searchWordLineEdit->setEnabled(enabled);
-
     if(!enabled) {
         historyPrevToolButton->setEnabled(false);
         historyNextToolButton->setEnabled(false);
         historyShowToolButton->setEnabled(false);
     }
+#endif
+qDebug()<<"tu2???";
 }
 
 void SearchBarWidget::setBusy() {
+    qDebug()<<"test11";
     if(busy) return;
+
+#ifndef Q_WS_MAEMO_5
+    busyTimer->start(50);
+    emit setButtonText(tr("Stop"));
+    this->setMaximumHeight(88);
+    progressBar->show();
+    emit progresSetMax(100);
+    emit progresSetMin(0);
+    emit progresSetValue(-1);
+    emit progresSetValue2(100);
+    progressMax=true;
+#else
     searchingProgressBar->show();
     searchPushButton->setText(tr("Stop"));
+#endif
+
     setEnabled(false);
     busy = true;
 }
 
+void SearchBarWidget::updateBusyTimer(){
+    qDebug()<<"test12";
+    if(progressMax==true){
+        emit progresSetValue2(0);
+        progressMax=false;
+    }
+    else{
+        emit progresSetValue2(100);
+        progressMax=true;
+    }
+    busyTimer->start(50);
+}
+
 void SearchBarWidget::setIdle() {
+    qDebug()<<"test13";
     if(!busy) return;
+#ifndef Q_WS_MAEMO_5
+    progressBar->hide();
+    busyTimer->stop();
+    emit progresSetValue2(0);
+    this->setMaximumHeight(50);
+    emit setButtonText(tr("Search"));
+#else
     searchingProgressBar->hide();
     searchPushButton->setText(tr("Search"));
+#endif
     setEnabled(true);
     busy = false;
     Q_EMIT refreshHistoryButtons();
@@ -311,25 +512,37 @@ void SearchBarWidget::setIdle() {
 
 
 void SearchBarWidget::clearSearchWordToolButtonClicked() {
+    qDebug()<<"test14";
+#ifdef Q_WS_MAEMO_5
     searchWordLineEdit->clear();
+#endif
 }
 
 
 
 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
+    qDebug()<<"test15";
     if(!busy) {
-        historyPrevToolButton->setEnabled(prev);
-        historyNextToolButton->setEnabled(next);
-        historyShowToolButton->setEnabled(list);
+        #ifndef Q_WS_MAEMO_5
+            emit setEnableHistoryNext(next);
+            emit setEnableHistoryShow(list);
+            emit setEnableHistoryPrev(prev);
+        #else
+            historyPrevToolButton->setEnabled(prev);
+            historyNextToolButton->setEnabled(next);
+            historyShowToolButton->setEnabled(list);
+        #endif
     }
 }
 
 void SearchBarWidget::showHistoryButtonClicked() {
-    #ifdef Q_WS_MAEMO_5
-        emit historyShow();
-    #else
-        QPoint p = historyShowToolButton->pos();
-        p.setY(p.y());
-        emit historyShow(mapToGlobal(p));
-    #endif
+    qDebug()<<"test16";
+#ifndef Q_WS_MAEMO_5
+    QPoint p=view->pos(); // = historyShowToolButton->pos();
+    p=mapToGlobal(p);
+    p.setX(p.x()+view->width());
+    emit historyShow(p);
+#else
+    emit historyShow();
+#endif
 }