File indexing completed on 2024-04-21 15:02:24
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 #include "knewstuffquick_debug.h" 0014 0015 namespace KNewStuffQuick 0016 { 0017 class CommentsModelPrivate 0018 { 0019 public: 0020 CommentsModelPrivate(CommentsModel *qq) 0021 : q(qq) 0022 { 0023 } 0024 CommentsModel *q; 0025 ItemsModel *itemsModel{nullptr}; 0026 int entryIndex{-1}; 0027 bool componentCompleted{false}; 0028 CommentsModel::IncludedComments includedComments{CommentsModel::IncludeAllComments}; 0029 0030 QSharedPointer<KNSCore::Provider> provider; 0031 void resetConnections() 0032 { 0033 if (componentCompleted && itemsModel) { 0034 q->setSourceModel( 0035 qobject_cast<QAbstractListModel *>(itemsModel->data(itemsModel->index(entryIndex), ItemsModel::CommentsModelRole).value<QObject *>())); 0036 } 0037 } 0038 0039 bool hasReview(const QModelIndex &index, bool checkParents = false) 0040 { 0041 bool result{false}; 0042 if (q->sourceModel()) { 0043 if (q->sourceModel()->data(index, KNSCore::CommentsModel::ScoreRole).toInt() > 0) { 0044 result = true; 0045 } 0046 if (result == false && checkParents) { 0047 QModelIndex parentIndex = q->sourceModel()->index(q->sourceModel()->data(index, KNSCore::CommentsModel::ParentIndexRole).toInt(), 0); 0048 if (parentIndex.isValid()) { 0049 result = hasReview(parentIndex, true); 0050 } 0051 } 0052 } 0053 return result; 0054 } 0055 }; 0056 } 0057 0058 using namespace KNewStuffQuick; 0059 0060 CommentsModel::CommentsModel(QObject *parent) 0061 : QSortFilterProxyModel(parent) 0062 , d(new CommentsModelPrivate(this)) 0063 { 0064 } 0065 0066 CommentsModel::~CommentsModel() = default; 0067 0068 void KNewStuffQuick::CommentsModel::classBegin() 0069 { 0070 } 0071 0072 void KNewStuffQuick::CommentsModel::componentComplete() 0073 { 0074 d->componentCompleted = true; 0075 d->resetConnections(); 0076 } 0077 0078 QObject *CommentsModel::itemsModel() const 0079 { 0080 return d->itemsModel; 0081 } 0082 0083 void CommentsModel::setItemsModel(QObject *newItemsModel) 0084 { 0085 if (d->itemsModel != newItemsModel) { 0086 d->itemsModel = qobject_cast<ItemsModel *>(newItemsModel); 0087 d->resetConnections(); 0088 Q_EMIT itemsModelChanged(); 0089 } 0090 } 0091 0092 int CommentsModel::entryIndex() const 0093 { 0094 return d->entryIndex; 0095 } 0096 0097 void CommentsModel::setEntryIndex(int entryIndex) 0098 { 0099 if (d->entryIndex != entryIndex) { 0100 d->entryIndex = entryIndex; 0101 d->resetConnections(); 0102 Q_EMIT entryIndexChanged(); 0103 } 0104 } 0105 0106 CommentsModel::IncludedComments KNewStuffQuick::CommentsModel::includedComments() const 0107 { 0108 return d->includedComments; 0109 } 0110 0111 void KNewStuffQuick::CommentsModel::setIncludedComments(CommentsModel::IncludedComments includedComments) 0112 { 0113 if (d->includedComments != includedComments) { 0114 d->includedComments = includedComments; 0115 invalidateFilter(); 0116 Q_EMIT includedCommentsChanged(); 0117 } 0118 } 0119 0120 bool KNewStuffQuick::CommentsModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const 0121 { 0122 bool result{false}; 0123 switch (d->includedComments) { 0124 case IncludeOnlyReviews: 0125 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent)); 0126 break; 0127 case IncludeReviewsAndReplies: 0128 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent), true); 0129 break; 0130 case IncludeAllComments: 0131 default: 0132 result = true; 0133 break; 0134 } 0135 return result; 0136 } 0137 0138 #include "moc_commentsmodel.cpp"