mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-20 09:21:55 +00:00
Everywhere: Move the Ladybird folder to UI
This commit is contained in:
parent
93712b24bf
commit
db47cc41f8
Notes:
github-actions[bot]
2024-11-10 11:51:45 +00:00
Author: https://github.com/trflynn89
Commit: db47cc41f8
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
203 changed files with 266 additions and 244 deletions
94
UI/Qt/AutoComplete.h
Normal file
94
UI/Qt/AutoComplete.h
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/String.h>
|
||||
#include <UI/Qt/StringUtils.h>
|
||||
|
||||
#include <QCompleter>
|
||||
#include <QNetworkReply>
|
||||
#include <QTreeView>
|
||||
|
||||
namespace Ladybird {
|
||||
|
||||
class AutoCompleteModel final : public QAbstractListModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit AutoCompleteModel(QObject* parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
virtual int rowCount(QModelIndex const& parent = QModelIndex()) const override { return parent.isValid() ? 0 : m_suggestions.size(); }
|
||||
virtual QVariant data(QModelIndex const& index, int role = Qt::DisplayRole) const override
|
||||
{
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
||||
return qstring_from_ak_string(m_suggestions[index.row()]);
|
||||
return {};
|
||||
}
|
||||
|
||||
void add(String const& result)
|
||||
{
|
||||
beginInsertRows({}, m_suggestions.size(), m_suggestions.size());
|
||||
m_suggestions.append(result);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
beginResetModel();
|
||||
m_suggestions.clear();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void replace_suggestions(Vector<String> suggestions)
|
||||
{
|
||||
beginInsertRows({}, m_suggestions.size(), m_suggestions.size());
|
||||
m_suggestions = suggestions;
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
private:
|
||||
AK::Vector<String> m_suggestions;
|
||||
};
|
||||
|
||||
class AutoComplete final : public QCompleter {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AutoComplete(QWidget* parent);
|
||||
|
||||
virtual QString pathFromIndex(QModelIndex const& index) const override
|
||||
{
|
||||
return index.data(Qt::DisplayRole).toString();
|
||||
}
|
||||
|
||||
void get_search_suggestions(String);
|
||||
void clear_suggestions();
|
||||
|
||||
signals:
|
||||
void activated(QModelIndex const&);
|
||||
|
||||
private:
|
||||
static String auto_complete_url_from_query(StringView query);
|
||||
|
||||
ErrorOr<void> got_network_response(QNetworkReply* reply);
|
||||
|
||||
ErrorOr<Vector<String>> parse_google_autocomplete(Vector<JsonValue> const&);
|
||||
ErrorOr<Vector<String>> parse_duckduckgo_autocomplete(Vector<JsonValue> const&);
|
||||
ErrorOr<Vector<String>> parse_yahoo_autocomplete(JsonObject const&);
|
||||
|
||||
QNetworkAccessManager* m_manager;
|
||||
AutoCompleteModel* m_auto_complete_model;
|
||||
QTreeView* m_tree_view;
|
||||
QNetworkReply* m_reply { nullptr };
|
||||
|
||||
String m_query;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue