File indexing completed on 2024-12-01 10:41:18

0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QLocale>
0008 #include <QSortFilterProxyModel>
0009 
0010 class RawLanguageModel : public QAbstractListModel
0011 {
0012     Q_OBJECT
0013 public:
0014     enum CustomRoles { NameRole = Qt::UserRole + 1, CodeRole, PreferredRole };
0015 
0016     explicit RawLanguageModel(QObject *parent = nullptr);
0017 
0018     QVariant data(const QModelIndex &index, int role) const override;
0019     int rowCount(const QModelIndex &parent) const override;
0020     QHash<int, QByteArray> roleNames() const override;
0021 
0022     Q_INVOKABLE QString getCode(int index) const;
0023 
0024 private:
0025     QList<QLocale::Language> m_languages;
0026     QList<QString> m_iso639codes;
0027     QList<QString> m_preferredLanguages;
0028 };
0029 
0030 class LanguageModel : public QSortFilterProxyModel
0031 {
0032     Q_OBJECT
0033 public:
0034     explicit LanguageModel(QObject *parent = nullptr);
0035 
0036     Q_INVOKABLE QString getCode(int index) const;
0037 
0038 protected:
0039     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0040 
0041 private:
0042     RawLanguageModel *m_model = nullptr;
0043 };