]> git.sur5r.net Git - minitube/blobdiff - src/autocomplete.cpp
Merge tag 'upstream/2.1.5'
[minitube] / src / autocomplete.cpp
index 1db03ee648084d95578ee4f65fb64730bb8e597f..f91540892d972d5266c323821b4c555f080fbb3b 100644 (file)
@@ -1,19 +1,47 @@
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
 #include "autocomplete.h"
 #include "suggester.h"
+#ifdef APP_MAC
+#include "searchlineedit_mac.h"
+#else
+#include "searchlineedit.h"
+#endif
 
-AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
-        QObject(parent), buddy(parent), editor(editor), suggester(0) {
+AutoComplete::AutoComplete(SearchLineEdit *parent, QLineEdit *editor):
+    QObject(parent), editor(editor), suggester(0) {
 
+    buddy = parent;
     enabled = true;
 
     popup = new QListWidget;
     popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-    popup->installEventFilter(this);
     popup->setMouseTracking(true);
     popup->setWindowOpacity(.9);
+    popup->installEventFilter(this);
+    popup->setWindowFlags(Qt::Popup);
+    popup->setFocusPolicy(Qt::NoFocus);
+    popup->setFocusProxy(buddy);
 
-    connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
-            SLOT(doneCompletion()));
+    connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
 
     // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
     //    SLOT(currentItemChanged(QListWidgetItem *)));
@@ -22,15 +50,11 @@ AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
     // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
     //    SLOT(currentItemChanged(QListWidgetItem *)));
 
-    popup->setWindowFlags(Qt::Popup);
-    popup->setFocusPolicy(Qt::NoFocus);
-    popup->setFocusProxy(parent);
-
     timer = new QTimer(this);
     timer->setSingleShot(true);
-    timer->setInterval(300);
+    timer->setInterval(600);
     connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
-    connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
+    connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
 
 }
 
@@ -42,10 +66,16 @@ bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
     if (obj != popup)
         return false;
 
+    if (ev->type() == QEvent::FocusOut) {
+        popup->hide();
+        buddy->setFocus();
+        return true;
+    }
+
     if (ev->type() == QEvent::MouseButtonPress) {
         popup->hide();
-        editor->setFocus();
-        editor->setText(originalText);
+        buddy->setFocus();
+        buddy->setText(originalText);
         return true;
     }
 
@@ -63,14 +93,14 @@ bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
                 doneCompletion();
                 consumed = true;
             } else {
-                editor->setFocus();
+                buddy->setFocus();
                 editor->event(ev);
                 popup->hide();
             }
             break;
 
         case Qt::Key_Escape:
-            editor->setFocus();
+            buddy->setFocus();
             editor->setText(originalText);
             popup->hide();
             consumed = true;
@@ -86,7 +116,7 @@ bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
 
         default:
             // qDebug() << keyEvent->text();
-            editor->setFocus();
+            buddy->setFocus();
             editor->event(ev);
             popup->hide();
             break;
@@ -126,15 +156,11 @@ void AutoComplete::showCompletion(const QStringList &choices) {
 void AutoComplete::doneCompletion() {
     timer->stop();
     popup->hide();
-    editor->setFocus();
+    buddy->setFocus();
     QListWidgetItem *item = popup->currentItem();
     if (item) {
-        editor->setText(item->text());
-        QKeyEvent *e;
-        e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
-        QApplication::postEvent(editor, e);
-        e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
-        QApplication::postEvent(editor, e);
+        buddy->setText(item->text());
+        emit suggestionAccepted(item->text());
     }
 }
 
@@ -158,11 +184,16 @@ void AutoComplete::setSuggester(Suggester* suggester) {
 
 void AutoComplete::autoSuggest() {
     if (!enabled) return;
+    if (!buddy->hasFocus()) return;
 
     QString query = editor->text();
     originalText = query;
     // qDebug() << "originalText" << originalText;
-    if (query.isEmpty()) return;
+    if (query.isEmpty()) {
+        popup->hide();
+        buddy->setFocus();
+        return;
+    }
 
     if (suggester)
         suggester->suggest(query);
@@ -177,7 +208,7 @@ void AutoComplete::currentItemChanged(QListWidgetItem *current) {
     if (current) {
         // qDebug() << "current" << current->text();
         current->setSelected(true);
-        editor->setText(current->text());
+        buddy->setText(current->text());
         editor->setSelection(originalText.length(), editor->text().length());
     }
 }