Fix build.
This commit is contained in:
parent
45e163ecba
commit
b18762bd7a
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
#include "definitions/definitions.h"
|
#include "definitions/definitions.h"
|
||||||
#include "gui/locationlineedit.h"
|
#include "gui/locationlineedit.h"
|
||||||
#include "network-web/silentnetworkaccessmanager.h"
|
#include "network-web/downloader.h"
|
||||||
|
|
||||||
#include <QDomDocument>
|
#include <QDomDocument>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
@ -44,7 +44,7 @@
|
|||||||
#include <QXmlStreamReader>
|
#include <QXmlStreamReader>
|
||||||
|
|
||||||
GoogleSuggest::GoogleSuggest(LocationLineEdit* editor, QObject* parent)
|
GoogleSuggest::GoogleSuggest(LocationLineEdit* editor, QObject* parent)
|
||||||
: QObject(parent), editor(editor), popup(new QListWidget()), m_enteredText(QString()) {
|
: QObject(parent), editor(editor), m_downloader(new Downloader(this)), popup(new QListWidget()), m_enteredText(QString()) {
|
||||||
popup->setWindowFlags(Qt::Popup);
|
popup->setWindowFlags(Qt::Popup);
|
||||||
popup->setFocusPolicy(Qt::NoFocus);
|
popup->setFocusPolicy(Qt::NoFocus);
|
||||||
popup->setFocusProxy(editor);
|
popup->setFocusProxy(editor);
|
||||||
@ -56,13 +56,13 @@ GoogleSuggest::GoogleSuggest(LocationLineEdit* editor, QObject* parent)
|
|||||||
timer = new QTimer(this);
|
timer = new QTimer(this);
|
||||||
timer->setSingleShot(true);
|
timer->setSingleShot(true);
|
||||||
timer->setInterval(500);
|
timer->setInterval(500);
|
||||||
|
|
||||||
connect(popup.data(), &QListWidget::itemClicked, this, &GoogleSuggest::doneCompletion);
|
connect(popup.data(), &QListWidget::itemClicked, this, &GoogleSuggest::doneCompletion);
|
||||||
connect(timer, &QTimer::timeout, this, &GoogleSuggest::autoSuggest);
|
connect(timer, &QTimer::timeout, this, &GoogleSuggest::autoSuggest);
|
||||||
connect(editor, &LocationLineEdit::textEdited, timer, static_cast<void (QTimer::*)()>(&QTimer::start));
|
connect(editor, &LocationLineEdit::textEdited, timer, static_cast<void (QTimer::*)()>(&QTimer::start));
|
||||||
|
connect(m_downloader.data(), &Downloader::completed, this, &GoogleSuggest::handleNetworkData);
|
||||||
}
|
}
|
||||||
|
|
||||||
GoogleSuggest::~GoogleSuggest() = default;
|
|
||||||
|
|
||||||
bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) {
|
bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) {
|
||||||
if (object != popup.data()) {
|
if (object != popup.data()) {
|
||||||
return false;
|
return false;
|
||||||
@ -120,7 +120,7 @@ void GoogleSuggest::showCompletion(const QStringList& choices) {
|
|||||||
popup->setUpdatesEnabled(false);
|
popup->setUpdatesEnabled(false);
|
||||||
popup->clear();
|
popup->clear();
|
||||||
|
|
||||||
foreach (const QString& choice, choices) {
|
for (const QString& choice : choices) {
|
||||||
new QListWidgetItem(choice, popup.data());
|
new QListWidgetItem(choice, popup.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,20 +152,16 @@ void GoogleSuggest::autoSuggest() {
|
|||||||
m_enteredText = QUrl::toPercentEncoding(editor->text());
|
m_enteredText = QUrl::toPercentEncoding(editor->text());
|
||||||
QString url = QString(GOOGLE_SUGGEST_URL).arg(m_enteredText);
|
QString url = QString(GOOGLE_SUGGEST_URL).arg(m_enteredText);
|
||||||
|
|
||||||
connect(SilentNetworkAccessManager::instance()->get(QNetworkRequest(QString(url))), &QNetworkReply::finished,
|
m_downloader->downloadFile(url);
|
||||||
this, &GoogleSuggest::handleNetworkData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GoogleSuggest::handleNetworkData() {
|
void GoogleSuggest::handleNetworkData(QNetworkReply::NetworkError status, const QByteArray& contents) {
|
||||||
QScopedPointer<QNetworkReply> reply(static_cast<QNetworkReply*>(sender()));
|
if (status == QNetworkReply::NetworkError::NoError) {
|
||||||
|
|
||||||
if (reply->error() == 0) {
|
|
||||||
QStringList choices;
|
QStringList choices;
|
||||||
QDomDocument xml;
|
QDomDocument xml;
|
||||||
QByteArray response = reply->readAll();
|
const QTextCodec* c = QTextCodec::codecForUtfText(contents);
|
||||||
const QTextCodec* c = QTextCodec::codecForUtfText(response);
|
|
||||||
|
|
||||||
xml.setContent(c->toUnicode(response));
|
xml.setContent(c->toUnicode(contents));
|
||||||
QDomNodeList suggestions = xml.elementsByTagName(QSL("suggestion"));
|
QDomNodeList suggestions = xml.elementsByTagName(QSL("suggestion"));
|
||||||
|
|
||||||
for (int i = 0; i < suggestions.size(); i++) {
|
for (int i = 0; i < suggestions.size(); i++) {
|
||||||
|
@ -33,20 +33,21 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
|
#include <QNetworkReply>
|
||||||
|
|
||||||
class LocationLineEdit;
|
class LocationLineEdit;
|
||||||
class QNetworkReply;
|
|
||||||
class QTimer;
|
class QTimer;
|
||||||
|
|
||||||
class QListWidget;
|
class QListWidget;
|
||||||
class QNetworkAccessManager;
|
|
||||||
|
class Downloader;
|
||||||
|
|
||||||
class GoogleSuggest : public QObject {
|
class GoogleSuggest : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Constructors.
|
|
||||||
explicit GoogleSuggest(LocationLineEdit* editor, QObject* parent = nullptr);
|
explicit GoogleSuggest(LocationLineEdit* editor, QObject* parent = nullptr);
|
||||||
virtual ~GoogleSuggest();
|
|
||||||
|
|
||||||
bool eventFilter(QObject* object, QEvent* event);
|
bool eventFilter(QObject* object, QEvent* event);
|
||||||
void showCompletion(const QStringList& choices);
|
void showCompletion(const QStringList& choices);
|
||||||
@ -55,11 +56,11 @@ class GoogleSuggest : public QObject {
|
|||||||
void doneCompletion();
|
void doneCompletion();
|
||||||
void preventSuggest();
|
void preventSuggest();
|
||||||
void autoSuggest();
|
void autoSuggest();
|
||||||
void handleNetworkData();
|
void handleNetworkData(QNetworkReply::NetworkError status, const QByteArray& contents);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LocationLineEdit* editor;
|
LocationLineEdit* editor;
|
||||||
|
QScopedPointer<Downloader> m_downloader;
|
||||||
QScopedPointer<QListWidget> popup;
|
QScopedPointer<QListWidget> popup;
|
||||||
QTimer* timer;
|
QTimer* timer;
|
||||||
QString m_enteredText;
|
QString m_enteredText;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user