File indexing completed on 2024-02-18 15:43:49

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "autotranslatelanguagesmodel.h"
0008 #include <QJsonObject>
0009 
0010 AutotranslateLanguagesModel::AutotranslateLanguagesModel(QObject *parent)
0011     : QAbstractListModel(parent)
0012 {
0013 }
0014 
0015 AutotranslateLanguagesModel::~AutotranslateLanguagesModel() = default;
0016 
0017 int AutotranslateLanguagesModel::rowCount(const QModelIndex &parent) const
0018 {
0019     Q_UNUSED(parent)
0020     return mAutoTranslateLanguages.count();
0021 }
0022 
0023 QVariant AutotranslateLanguagesModel::data(const QModelIndex &index, int role) const
0024 {
0025     if (index.row() < 0 || index.row() >= mAutoTranslateLanguages.count()) {
0026         return {};
0027     }
0028     const AutotranslateLanguage language = mAutoTranslateLanguages.at(index.row());
0029 
0030     switch (role) {
0031     case AutotranslateLanguagesModel::LanguageName:
0032         return language.language();
0033     case Qt::DisplayRole:
0034     case AutotranslateLanguagesModel::DisplayName:
0035         return language.displayLanguage();
0036     }
0037     return {};
0038 }
0039 
0040 void AutotranslateLanguagesModel::parseLanguages(const QJsonObject &obj)
0041 {
0042     if (rowCount() != 0) {
0043         beginResetModel();
0044         mAutoTranslateLanguages.clear();
0045         endResetModel();
0046     }
0047     mAutoTranslateLanguages.parseLanguages(obj);
0048     if (!mAutoTranslateLanguages.isEmpty()) {
0049         beginInsertRows(QModelIndex(), 0, mAutoTranslateLanguages.count() - 1);
0050         endInsertRows();
0051     }
0052 }
0053 
0054 int AutotranslateLanguagesModel::currentLanguage(const QString &lang) const
0055 {
0056     for (int i = 0; i < mAutoTranslateLanguages.autotranslateLanguage().count(); ++i) {
0057         if (mAutoTranslateLanguages.autotranslateLanguage().at(i).language() == lang) {
0058             return i;
0059         }
0060     }
0061     return 1;
0062 }
0063 
0064 QString AutotranslateLanguagesModel::selectedLanguage(int index) const
0065 {
0066     return mAutoTranslateLanguages.autotranslateLanguage().at(index).language();
0067 }
0068 
0069 #include "moc_autotranslatelanguagesmodel.cpp"