File indexing completed on 2024-05-12 04:42:52

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPUBLICTRANSPORT_LOCATIONHISTORYMODEL_H
0008 #define KPUBLICTRANSPORT_LOCATIONHISTORYMODEL_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <KPublicTransport/Location>
0013 
0014 #include <QAbstractTableModel>
0015 #include <QDateTime>
0016 
0017 namespace KPublicTransport {
0018 
0019 /** Model of frequently/recently used locations.
0020  *  Content is persisted globally, ie. all applications sharing this see
0021  *  the same data.
0022  */
0023 class KPUBLICTRANSPORT_EXPORT LocationHistoryModel : public QAbstractListModel
0024 {
0025     Q_OBJECT
0026 public:
0027     explicit LocationHistoryModel(QObject *parent = nullptr);
0028     ~LocationHistoryModel();
0029 
0030     enum Role {
0031         LocationRole = Qt::UserRole,
0032         LocationNameRole,
0033         LastUsedRole,
0034         UseCountRole,
0035     };
0036     Q_ENUM(Role)
0037 
0038     int rowCount(const QModelIndex &parent = {}) const override;
0039     QVariant data(const QModelIndex &index, int role) const override;
0040     QHash<int, QByteArray> roleNames() const override;
0041     Q_INVOKABLE bool removeRow(int row, const QModelIndex &parent = QModelIndex()); // not exported to QML by default...
0042     Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = {}) override;
0043 
0044 public Q_SLOTS:
0045     /** Add a location to the history.
0046      *  If already present, just the usage data will be updated.
0047      */
0048     void addLocation(const KPublicTransport::Location &loc);
0049 
0050     /** Delete the entire history content. */
0051     void clear();
0052 
0053 private:
0054     struct Data {
0055         QString id;
0056         Location loc;
0057         QDateTime lastUse;
0058         int useCount = 0;
0059     };
0060 
0061     void rescan();
0062     void store(const Data &data) const;
0063 
0064     std::vector<Data> m_locations;
0065 };
0066 
0067 }
0068 
0069 #endif // KPUBLICTRANSPORT_LOCATIONHISTORYMODEL_H