popup = new QListWidget;
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-
popup->installEventFilter(this);
popup->setMouseTracking(true);
connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
SLOT(currentItemChanged(QListWidgetItem *)));
+ // mouse hover
connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
SLOT(currentItemChanged(QListWidgetItem *)));
timer = new QTimer(this);
timer->setSingleShot(true);
- timer->setInterval(250);
+ timer->setInterval(100);
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(popup->width(), h);
+
+ popup->move(editor->mapToGlobal(QPoint(0, editor->height()+4)));
popup->setFocus();
popup->show();
}
}
void GSuggestCompletion::autoSuggest() {
- QString str = editor->text();
- originalText = str;
+ 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)));
current->setSelected(true);
editor->setText(current->text());
editor->setSelection(originalText.length(), editor->text().length());
- } else {
- popup->clearSelection();
}
}