File indexing completed on 2024-05-12 16:25:47

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "bannerinfosmodel.h"
0008 #include <KLocalizedString>
0009 
0010 BannerInfosModel::BannerInfosModel(QObject *parent)
0011     : QAbstractListModel{parent}
0012 {
0013 }
0014 
0015 BannerInfosModel::~BannerInfosModel() = default;
0016 
0017 int BannerInfosModel::rowCount(const QModelIndex &parent) const
0018 {
0019     Q_UNUSED(parent)
0020     return mBannerInfos.count();
0021 }
0022 
0023 QVariant BannerInfosModel::data(const QModelIndex &index, int role) const
0024 {
0025     if (index.row() < 0 || index.row() >= mBannerInfos.count()) {
0026         return {};
0027     }
0028     const auto info = mBannerInfos.at(index.row());
0029     switch (role) {
0030     case BannerInfosModel::Read: {
0031         return info.read();
0032     }
0033     case BannerInfosRoles::Identifier: {
0034         return info.identifier();
0035     }
0036     case BannerInfosRoles::Title: {
0037         return info.title();
0038     }
0039     case Qt::DisplayRole:
0040     case BannerInfosRoles::Text: {
0041         return text(info);
0042     }
0043     }
0044     return {};
0045 }
0046 
0047 QString BannerInfosModel::text(const BannerInfo &info) const
0048 {
0049     QString str = BannerInfo::defaultText(info);
0050     if (!info.link().isEmpty()) {
0051         // Use markdown url
0052         str += QStringLiteral(" [%1](%2)").arg(i18n("link"), info.link());
0053     }
0054     return str;
0055 }
0056 
0057 void BannerInfosModel::clear()
0058 {
0059     if (rowCount() != 0) {
0060         beginResetModel();
0061         mBannerInfos.clear();
0062         endResetModel();
0063     }
0064 }
0065 
0066 void BannerInfosModel::insertBannerInfos(const BannerInfos &infos)
0067 {
0068     clear();
0069     if (!infos.isEmpty()) {
0070         beginInsertRows(QModelIndex(), 0, infos.count() - 1);
0071         mBannerInfos = infos;
0072         endInsertRows();
0073     }
0074 }
0075 
0076 #include "moc_bannerinfosmodel.cpp"