File indexing completed on 2024-05-19 05:55:48
0001 /* 0002 * SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com> 0003 * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #pragma once 0009 0010 #include <QAbstractListModel> 0011 #include <QObject> 0012 #include <qqmlregistration.h> 0013 0014 #include <KWeatherCore/LocationQuery> 0015 0016 class QTimer; 0017 class LocationQueryModel : public QAbstractListModel 0018 { 0019 Q_OBJECT 0020 QML_ELEMENT 0021 Q_PROPERTY(bool loading READ loading NOTIFY propertyChanged) 0022 Q_PROPERTY(bool networkError READ networkError NOTIFY propertyChanged) 0023 0024 public: 0025 explicit LocationQueryModel(); 0026 enum Roles { 0027 NameRole = Qt::DisplayRole, 0028 }; 0029 0030 int rowCount(const QModelIndex &parent) const override; 0031 QVariant data(const QModelIndex &index, int role) const override; 0032 QHash<int, QByteArray> roleNames() const override; 0033 Q_INVOKABLE bool loading() const; 0034 Q_INVOKABLE bool networkError() const; 0035 Q_INVOKABLE void textChanged(QString query, int timeout = 2000); 0036 Q_INVOKABLE void addLocation(int index); 0037 Q_INVOKABLE KWeatherCore::LocationQueryResult get(int index); 0038 Q_INVOKABLE void clearResults(); 0039 void setQuery(); 0040 Q_SIGNALS: 0041 void propertyChanged(); 0042 void appendLocation(const KWeatherCore::LocationQueryResult &result); 0043 private Q_SLOTS: 0044 void handleQueryResults(const std::vector<KWeatherCore::LocationQueryResult> &results); 0045 0046 private: 0047 bool m_loading = false, m_networkError = false; 0048 std::vector<KWeatherCore::LocationQueryResult> m_results; 0049 KWeatherCore::LocationQuery m_querySource; 0050 QTimer *inputTimer = nullptr; 0051 QString m_text; 0052 };