do not suggest with google when entered url

This commit is contained in:
Martin Rotter 2023-09-23 20:55:15 +02:00
parent 3ec181f021
commit 82b977961a
2 changed files with 50 additions and 43 deletions

View File

@ -44,34 +44,34 @@
#include <QXmlStreamReader> #include <QXmlStreamReader>
GoogleSuggest::GoogleSuggest(LocationLineEdit* editor, QObject* parent) GoogleSuggest::GoogleSuggest(LocationLineEdit* editor, QObject* parent)
: QObject(parent), editor(editor), m_downloader(new Downloader(this)), popup(new QListWidget()), : QObject(parent), m_editor(editor), m_downloader(new Downloader(this)), m_popup(new QListWidget()),
m_enteredText(QString()) { m_enteredText(QString()) {
popup->setWindowFlags(Qt::WindowType::Popup); m_popup->setWindowFlags(Qt::WindowType::Popup);
popup->setFocusPolicy(Qt::FocusPolicy::NoFocus); m_popup->setFocusPolicy(Qt::FocusPolicy::NoFocus);
popup->setFocusProxy(editor); m_popup->setFocusProxy(editor);
popup->setMouseTracking(true); m_popup->setMouseTracking(true);
popup->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows); m_popup->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
popup->setFrameStyle(QFrame::Shape::Box | QFrame::Shadow::Plain); m_popup->setFrameStyle(QFrame::Shape::Box | QFrame::Shadow::Plain);
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); m_popup->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
popup->installEventFilter(this); m_popup->installEventFilter(this);
timer = new QTimer(this); m_timer = new QTimer(this);
timer->setSingleShot(true); m_timer->setSingleShot(true);
timer->setInterval(500); m_timer->setInterval(500);
connect(popup.data(), &QListWidget::itemClicked, this, &GoogleSuggest::doneCompletion); connect(m_popup.data(), &QListWidget::itemClicked, this, &GoogleSuggest::doneCompletion);
connect(timer, &QTimer::timeout, this, &GoogleSuggest::autoSuggest); connect(m_timer, &QTimer::timeout, this, &GoogleSuggest::autoSuggest);
connect(editor, &LocationLineEdit::textEdited, timer, static_cast<void (QTimer::*)()>(&QTimer::start)); connect(editor, &LocationLineEdit::textEdited, m_timer, static_cast<void (QTimer::*)()>(&QTimer::start));
connect(m_downloader.data(), &Downloader::completed, this, &GoogleSuggest::handleNetworkData); connect(m_downloader.data(), &Downloader::completed, this, &GoogleSuggest::handleNetworkData);
} }
bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) { bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) {
if (object != popup.data()) { if (object != m_popup.data()) {
return false; return false;
} }
if (event->type() == QEvent::MouseButtonPress) { if (event->type() == QEvent::MouseButtonPress) {
popup->hide(); m_popup->hide();
editor->setFocus(); m_editor->setFocus();
return true; return true;
} }
@ -87,8 +87,8 @@ bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) {
break; break;
case Qt::Key_Escape: case Qt::Key_Escape:
editor->setFocus(); m_editor->setFocus();
popup->hide(); m_popup->hide();
consumed = true; consumed = true;
break; break;
@ -101,9 +101,9 @@ bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) {
break; break;
default: default:
editor->setFocus(); m_editor->setFocus();
editor->event(event); m_editor->event(event);
popup->hide(); m_popup->hide();
break; break;
} }
@ -118,39 +118,46 @@ void GoogleSuggest::showCompletion(const QStringList& choices) {
return; return;
} }
popup->setUpdatesEnabled(false); m_popup->setUpdatesEnabled(false);
popup->clear(); m_popup->clear();
for (const QString& choice : choices) { for (const QString& choice : choices) {
new QListWidgetItem(choice, popup.data()); new QListWidgetItem(choice, m_popup.data());
} }
popup->setCurrentItem(popup->item(0)); m_popup->setCurrentItem(m_popup->item(0));
popup->adjustSize(); m_popup->adjustSize();
popup->setUpdatesEnabled(true); m_popup->setUpdatesEnabled(true);
popup->resize(editor->width(), popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3); m_popup->resize(m_editor->width(), m_popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3);
popup->move(editor->mapToGlobal(QPoint(0, editor->height()))); m_popup->move(m_editor->mapToGlobal(QPoint(0, m_editor->height())));
popup->setFocus(); m_popup->setFocus();
popup->show(); m_popup->show();
} }
void GoogleSuggest::doneCompletion() { void GoogleSuggest::doneCompletion() {
timer->stop(); m_timer->stop();
popup->hide(); m_popup->hide();
editor->setFocus(); m_editor->setFocus();
QListWidgetItem* item = popup->currentItem(); QListWidgetItem* item = m_popup->currentItem();
if (item != nullptr) { if (item != nullptr) {
editor->submit(QSL(GOOGLE_SEARCH_URL).arg(item->text())); m_editor->submit(QSL(GOOGLE_SEARCH_URL).arg(item->text()));
} }
} }
void GoogleSuggest::preventSuggest() { void GoogleSuggest::preventSuggest() {
timer->stop(); m_timer->stop();
} }
void GoogleSuggest::autoSuggest() { void GoogleSuggest::autoSuggest() {
m_enteredText = QUrl::toPercentEncoding(editor->text()); if ((m_editor->text().startsWith(QSL("http")) || m_editor->text().startsWith(QSL("www"))) &&
QUrl::fromUserInput(m_editor->text()).isValid()) {
// Do not suggest when entered URL.
preventSuggest();
return;
}
m_enteredText = QUrl::toPercentEncoding(m_editor->text());
QString url = QSL(GOOGLE_SUGGEST_URL).arg(m_enteredText); QString url = QSL(GOOGLE_SUGGEST_URL).arg(m_enteredText);
m_downloader->downloadFile(url); m_downloader->downloadFile(url);

View File

@ -60,10 +60,10 @@ class GoogleSuggest : public QObject {
const QByteArray& contents); const QByteArray& contents);
private: private:
LocationLineEdit* editor; LocationLineEdit* m_editor;
QScopedPointer<Downloader> m_downloader; QScopedPointer<Downloader> m_downloader;
QScopedPointer<QListWidget> popup; QScopedPointer<QListWidget> m_popup;
QTimer* timer; QTimer* m_timer;
QString m_enteredText; QString m_enteredText;
}; };