File indexing completed on 2024-05-19 05:55:49

0001 // SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0002 // SPDX-FileCopyrightText: 2020-2022 Devin Lin <espidev@gmail.com>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #pragma once
0006 
0007 #include <QAbstractListModel>
0008 #include <QObject>
0009 #include <QQmlEngine>
0010 
0011 #include <KWeatherCore/LocationQuery>
0012 
0013 #include "weatherlocation.h"
0014 
0015 class WeatherLocationListModel : public QAbstractListModel
0016 {
0017     Q_OBJECT
0018     QML_ELEMENT
0019     QML_SINGLETON
0020     Q_PROPERTY(int count READ count NOTIFY locationsChanged)
0021     Q_PROPERTY(QList<WeatherLocation *> locations READ locations NOTIFY locationsChanged)
0022 
0023 public:
0024     enum Roles { LocationRole = Qt::UserRole };
0025 
0026     static WeatherLocationListModel *inst();
0027     static WeatherLocationListModel *create(QQmlEngine *, QJSEngine *);
0028 
0029     void load();
0030     void saveOrder();
0031 
0032     int count() const;
0033     int rowCount(const QModelIndex &parent) const override;
0034     QVariant data(const QModelIndex &index, int role) const override;
0035     QHash<int, QByteArray> roleNames() const override;
0036 
0037     Q_INVOKABLE void insert(int index, WeatherLocation *weatherLocation);
0038     Q_INVOKABLE void remove(int index);
0039     Q_INVOKABLE void move(int oldIndex, int newIndex);
0040     Q_INVOKABLE void requestCurrentLocation(); // invoked by UI
0041 
0042     QList<WeatherLocation *> &locations();
0043 
0044 public Q_SLOTS:
0045     void addLocation(const KWeatherCore::LocationQueryResult &ret);
0046 
0047 Q_SIGNALS:
0048     void locationsChanged();
0049     void networkErrorCreating(); // error creating a location
0050     void networkErrorCreatingDefault(); // error getting current location
0051     void successfullyCreatedDefault(); // successful in getting current location
0052 
0053 protected:
0054     explicit WeatherLocationListModel(QObject *parent = nullptr);
0055 
0056 private Q_SLOTS:
0057     void addCurrentLocation(const KWeatherCore::LocationQueryResult &ret);
0058 
0059 private:
0060     QList<WeatherLocation *> m_locations;
0061 };