File indexing completed on 2024-04-21 03:56:25
0001 /* 0002 SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "commentsmodel.h" 0008 0009 #include "quickitemsmodel.h" 0010 0011 #include "core/commentsmodel.h" 0012 0013 namespace KNewStuffQuick 0014 { 0015 class CommentsModelPrivate 0016 { 0017 public: 0018 CommentsModelPrivate(CommentsModel *qq) 0019 : q(qq) 0020 { 0021 } 0022 CommentsModel *q; 0023 ItemsModel *itemsModel{nullptr}; 0024 KNSCore::Entry entry; 0025 bool componentCompleted{false}; 0026 CommentsModel::IncludedComments includedComments{CommentsModel::IncludeAllComments}; 0027 0028 QSharedPointer<KNSCore::Provider> provider; 0029 void resetConnections() 0030 { 0031 if (componentCompleted && itemsModel) { 0032 q->setSourceModel(qobject_cast<QAbstractListModel *>( 0033 itemsModel->data(itemsModel->index(itemsModel->indexOfEntry(entry)), ItemsModel::CommentsModelRole).value<QObject *>())); 0034 } 0035 } 0036 0037 bool hasReview(const QModelIndex &index, bool checkParents = false) 0038 { 0039 bool result{false}; 0040 if (q->sourceModel()) { 0041 if (q->sourceModel()->data(index, KNSCore::CommentsModel::ScoreRole).toInt() > 0) { 0042 result = true; 0043 } 0044 if (result == false && checkParents) { 0045 QModelIndex parentIndex = q->sourceModel()->index(q->sourceModel()->data(index, KNSCore::CommentsModel::ParentIndexRole).toInt(), 0); 0046 if (parentIndex.isValid()) { 0047 result = hasReview(parentIndex, true); 0048 } 0049 } 0050 } 0051 return result; 0052 } 0053 }; 0054 } 0055 0056 using namespace KNewStuffQuick; 0057 0058 CommentsModel::CommentsModel(QObject *parent) 0059 : QSortFilterProxyModel(parent) 0060 , d(new CommentsModelPrivate(this)) 0061 { 0062 } 0063 0064 CommentsModel::~CommentsModel() = default; 0065 0066 void KNewStuffQuick::CommentsModel::classBegin() 0067 { 0068 } 0069 0070 void KNewStuffQuick::CommentsModel::componentComplete() 0071 { 0072 d->componentCompleted = true; 0073 d->resetConnections(); 0074 } 0075 0076 QObject *CommentsModel::itemsModel() const 0077 { 0078 return d->itemsModel; 0079 } 0080 0081 void CommentsModel::setItemsModel(QObject *newItemsModel) 0082 { 0083 if (d->itemsModel != newItemsModel) { 0084 d->itemsModel = qobject_cast<ItemsModel *>(newItemsModel); 0085 d->resetConnections(); 0086 Q_EMIT itemsModelChanged(); 0087 } 0088 } 0089 0090 KNSCore::Entry CommentsModel::entry() const 0091 { 0092 return d->entry; 0093 } 0094 0095 void CommentsModel::setEntry(const KNSCore::Entry &entry) 0096 { 0097 d->entry = entry; 0098 d->resetConnections(); 0099 Q_EMIT entryChanged(); 0100 } 0101 0102 CommentsModel::IncludedComments KNewStuffQuick::CommentsModel::includedComments() const 0103 { 0104 return d->includedComments; 0105 } 0106 0107 void KNewStuffQuick::CommentsModel::setIncludedComments(CommentsModel::IncludedComments includedComments) 0108 { 0109 if (d->includedComments != includedComments) { 0110 d->includedComments = includedComments; 0111 invalidateFilter(); 0112 Q_EMIT includedCommentsChanged(); 0113 } 0114 } 0115 0116 bool KNewStuffQuick::CommentsModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const 0117 { 0118 bool result{false}; 0119 switch (d->includedComments) { 0120 case IncludeOnlyReviews: 0121 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent)); 0122 break; 0123 case IncludeReviewsAndReplies: 0124 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent), true); 0125 break; 0126 case IncludeAllComments: 0127 default: 0128 result = true; 0129 break; 0130 } 0131 return result; 0132 } 0133 0134 #include "moc_commentsmodel.cpp"