bi change + completer in line edit
[mdictionary] / src / mdictionary / gui / SearchBarWidget.cpp
index 0726d76..338b87a 100644 (file)
 
 
 SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
+
+    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;
+
+    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();
-    view->setSource(QUrl("src/mdictionary/qml/SearchBarWidget.qml"));
+    ctxt = view->rootContext();
+
+    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)));
@@ -55,7 +73,22 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
     connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
             this, SIGNAL(historyPrev()));
     connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
-            this, SIGNAL(historyShow()));
+            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(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)));
@@ -70,13 +103,21 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
     connect(this, SIGNAL(setLineEditEnables(QVariant)),
             rootObject, SLOT(setEnableLineEdit(QVariant)));
 
+    connect(this, SIGNAL(setCompleterText(QVariant)),
+            rootObject, SLOT(setCompleterText(QVariant)));
+
+    connect(busyTimer, SIGNAL(timeout()),
+            this, SLOT(updateBusyTimer()));
+
     emit setEnableHistoryNext(false);
     emit setEnableHistoryShow(false);
     emit setEnableHistoryPrev(false);
 
-    completerModel = new QStringListModel(this);
     connect(&delayTimer, SIGNAL(timeout()),
             this, SLOT(delaySearchTimeout()));
+
+    view->setFocus();
+
 #else
     initializeUI();
     connect(searchPushButton, SIGNAL(clicked()),
@@ -94,7 +135,6 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
     connect(&delayTimer, SIGNAL(timeout()),
             this, SLOT(delaySearchTimeout()));
 
-
     searchWordLineEdit->setFocus();
 #endif
 
@@ -107,7 +147,73 @@ SearchBarWidget::~SearchBarWidget() {
 
 }
 
+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) {
@@ -144,12 +250,16 @@ QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
 }
 
 void SearchBarWidget::setFocus() {
-#ifdef Q_WS_MAEMO_5
+    qDebug()<<"test3";
+#ifndef Q_WS_MAEMO_5
+        view->setFocus();
+#else
         searchWordLineEdit->setFocus();
 #endif
 }
 
 void SearchBarWidget::initializeUI() {
+    qDebug()<<"test4";
 #ifdef Q_WS_MAEMO_5
     setMaximumHeight(150);
 
@@ -162,12 +272,6 @@ void SearchBarWidget::initializeUI() {
     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
@@ -256,6 +360,7 @@ void SearchBarWidget::initializeUI() {
 }
 
 void SearchBarWidget::searchButtonClicked(QString text) {
+    qDebug()<<"test5";
     if(busy)
         Q_EMIT stopSearching();
     else
@@ -263,6 +368,7 @@ void SearchBarWidget::searchButtonClicked(QString text) {
 }
 
 void SearchBarWidget::searchPushButtonClicked() {
+    qDebug()<<"test6";
 #ifdef Q_WS_MAEMO_5
     if(busy) {
         Q_EMIT stopSearching();
@@ -274,12 +380,14 @@ void SearchBarWidget::searchPushButtonClicked() {
 }
 
 void SearchBarWidget::search(QString word) {
-    qDebug()<<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
@@ -290,6 +398,7 @@ void SearchBarWidget::search(QString word) {
 }
 
 void SearchBarWidget::searchDelay(QString word) {
+    qDebug()<<"test8";
     if(!busy && !word.isEmpty()) {
         #ifndef Q_WS_MAEMO_5
             emit setLineEditText(word);
@@ -305,6 +414,7 @@ void SearchBarWidget::searchDelay(QString word) {
 }
 
 void SearchBarWidget::delaySearchTimeout() {
+    qDebug()<<"test9";
     delayTimer.stop();
     if(!busy) {
         Q_EMIT searchForTranslations(delayString);
@@ -312,9 +422,11 @@ 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);
@@ -327,14 +439,23 @@ void SearchBarWidget::setEnabled(bool enabled) {
         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"));
-//  searchingProgressBar->show();
+    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"));
@@ -344,10 +465,27 @@ void SearchBarWidget::setBusy() {
     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
-//    searchingProgressBar->hide();
+    progressBar->hide();
+    busyTimer->stop();
+    emit progresSetValue2(0);
+    this->setMaximumHeight(50);
     emit setButtonText(tr("Search"));
 #else
     searchingProgressBar->hide();
@@ -360,6 +498,7 @@ void SearchBarWidget::setIdle() {
 
 
 void SearchBarWidget::clearSearchWordToolButtonClicked() {
+    qDebug()<<"test14";
 #ifdef Q_WS_MAEMO_5
     searchWordLineEdit->clear();
 #endif
@@ -368,6 +507,7 @@ void SearchBarWidget::clearSearchWordToolButtonClicked() {
 
 
 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
+    qDebug()<<"test15";
     if(!busy) {
         #ifndef Q_WS_MAEMO_5
             emit setEnableHistoryNext(next);
@@ -382,11 +522,13 @@ void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
 }
 
 void SearchBarWidget::showHistoryButtonClicked() {
+    qDebug()<<"test16";
 #ifndef Q_WS_MAEMO_5
     QPoint p=view->pos(); // = historyShowToolButton->pos();
-    p.setY(p.y());
-    emit historyShow(mapToGlobal(p));
- #else
+    p=mapToGlobal(p);
+    p.setX(p.x()+view->width());
+    emit historyShow(p);
+#else
     emit historyShow();
 #endif
 }