File indexing completed on 2024-05-19 16:31:38
0001 /* 0002 * SPDX-FileCopyrightText: 2017 David Faure <faure@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 #ifndef DICTIONARIES_MODEL_H 0007 #define DICTIONARIES_MODEL_H 0008 0009 #include "../../dict/dictengine.h" 0010 0011 #include <unordered_map> 0012 0013 #include <QAbstractListModel> 0014 #include <QAbstractSocket> 0015 #include <vector> 0016 0017 struct AvailableDict { 0018 QString id; 0019 QString description; 0020 bool enabled = false; 0021 }; 0022 0023 class EnabledDictModel : public QAbstractListModel 0024 { 0025 public: 0026 explicit EnabledDictModel(QObject *parent = nullptr); 0027 0028 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0029 int rowCount(const QModelIndex &index = QModelIndex()) const override; 0030 QHash<int, QByteArray> roleNames() const override; 0031 bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override; 0032 0033 void appendDict(const AvailableDict &dict); 0034 void removeDict(int _index); 0035 0036 private: 0037 QList<AvailableDict> m_enabledDicts; 0038 }; 0039 0040 class DictionariesModel : public QAbstractListModel 0041 { 0042 Q_OBJECT 0043 0044 /** 0045 * @return the list of enabled dictionaries 0046 */ 0047 Q_PROPERTY(QString enabledDicts READ enabledDicts WRITE setEnabledDicts NOTIFY enabledDictsChanged) 0048 0049 /** 0050 * @return the model that contains all enabled dictionaries 0051 */ 0052 Q_PROPERTY(QAbstractListModel *enabledDictModel READ enabledDictModel CONSTANT) 0053 0054 /** 0055 * @return @c true if the engine is downloading dict list from 0056 * the Internet, @c false otherwise. 0057 */ 0058 Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged) 0059 0060 /** 0061 * @return the type of the last socket error 0062 */ 0063 Q_PROPERTY(QAbstractSocket::SocketError errorCode READ errorCode NOTIFY errorCodeChanged) 0064 0065 /** 0066 * @return a human-readable description of the last socket error 0067 */ 0068 Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged) 0069 0070 public: 0071 explicit DictionariesModel(QObject *parent = nullptr); 0072 0073 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0074 bool setData(const QModelIndex &index, const QVariant &value, int role) override; 0075 int rowCount(const QModelIndex &index = QModelIndex()) const override; 0076 QHash<int, QByteArray> roleNames() const override; 0077 0078 QString enabledDicts() const; 0079 void setEnabledDicts(const QString &dicts); 0080 0081 QAbstractListModel *enabledDictModel() const; 0082 0083 Q_INVOKABLE void setEnabled(const QString &dict); 0084 Q_INVOKABLE void setDisabled(int _index); 0085 Q_INVOKABLE void move(int oldIndex, int newIndex); 0086 0087 bool loading() const; 0088 0089 QAbstractSocket::SocketError errorCode() const; 0090 QString errorString() const; 0091 0092 Q_SIGNALS: 0093 void enabledDictsChanged(); 0094 void loadingChanged(); 0095 void errorCodeChanged(); 0096 void errorStringChanged(); 0097 0098 private Q_SLOTS: 0099 void slotDictErrorOccurred(QAbstractSocket::SocketError socketError, const QString &errorString); 0100 void slotDictLoadingChanged(bool loading); 0101 0102 private: 0103 void setAvailableDicts(const QVariantMap &data); 0104 0105 DictEngine m_engine; 0106 EnabledDictModel *m_enabledDictModel; 0107 0108 std::vector<AvailableDict> m_availableDicts; 0109 std::unordered_map<QString /*id*/, int /*index*/> m_idIndexProxyMap; 0110 QString m_enabledDicts; 0111 QStringList m_enabledDictIdList; 0112 0113 bool m_loading = false; 0114 QAbstractSocket::SocketError m_errorCode = QAbstractSocket::UnknownSocketError; 0115 QString m_errorString; 0116 }; 0117 0118 #endif