NetworkAccess* http();
}
-GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent) {
+GSuggestCompletion::GSuggestCompletion(QWidget *parent, QLineEdit *editor):
+ QObject(parent), buddy(parent), editor(editor) {
+
+ enabled = true;
popup = new QListWidget;
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-
popup->installEventFilter(this);
popup->setMouseTracking(true);
connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
SLOT(doneCompletion()));
- connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
- SLOT(currentItemChanged(QListWidgetItem *)));
+ // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
+ // SLOT(currentItemChanged(QListWidgetItem *)));
- connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
- SLOT(currentItemChanged(QListWidgetItem *)));
+ // mouse hover
+ // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
+ // SLOT(currentItemChanged(QListWidgetItem *)));
popup->setWindowFlags(Qt::Popup);
popup->setFocusPolicy(Qt::NoFocus);
timer = new QTimer(this);
timer->setSingleShot(true);
- timer->setInterval(250);
+ timer->setInterval(300);
connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
if (ev->type() == QEvent::KeyPress) {
bool consumed = false;
- int key = static_cast<QKeyEvent*>(ev)->key();
+
+ QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
+ int key = keyEvent->key();
switch (key) {
case Qt::Key_Enter:
case Qt::Key_Return:
break;
default:
+
editor->setFocus();
editor->event(ev);
popup->hide();
popup->adjustSize();
popup->setUpdatesEnabled(true);
- popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
+ int h = popup->sizeHintForRow(0) * choices.count() + 4;
+ popup->resize(buddy->width(), h);
+
+ popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
+
popup->setFocus();
popup->show();
}
void GSuggestCompletion::preventSuggest() {
timer->stop();
+ enabled = false;
+ popup->hide();
+}
+
+void GSuggestCompletion::enableSuggest() {
+ enabled = true;
}
void GSuggestCompletion::autoSuggest() {
- QString str = editor->text();
- originalText = str;
+ if (!enabled) return;
+
+ QString query = editor->text();
+ originalText = query;
qDebug() << "originalText" << originalText;
- if (str.isEmpty()) return;
+ if (query.isEmpty()) return;
+
+ QString locale = QLocale::system().name().replace("_", "-");
+ // case for system locales such as "C"
+ if (locale.length() < 2) {
+ locale = "en-US";
+ }
- QString url = QString(GSUGGEST_URL).arg(QLocale::system().name().replace("_", "-"), str);
+ QString url = QString(GSUGGEST_URL).arg(locale, query);
QObject *reply = The::http()->get(url);
connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
}
void GSuggestCompletion::handleNetworkData(QByteArray response) {
+ if (!enabled) return;
QStringList choices;
current->setSelected(true);
editor->setText(current->text());
editor->setSelection(originalText.length(), editor->text().length());
- } else {
- popup->clearSelection();
}
}