1/*
2 SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "commentsmodel.h"
8
9#include "quickitemsmodel.h"
10
11#include "core/commentsmodel.h"
12
13namespace KNewStuffQuick
14{
15class CommentsModelPrivate
16{
17public:
18 CommentsModelPrivate(CommentsModel *qq)
19 : q(qq)
20 {
21 }
22 CommentsModel *q;
23 ItemsModel *itemsModel{nullptr};
24 KNSCore::Entry entry;
25 bool componentCompleted{false};
26 CommentsModel::IncludedComments includedComments{CommentsModel::IncludeAllComments};
27
28 QSharedPointer<KNSCore::Provider> provider;
29 void resetConnections()
30 {
31 if (componentCompleted && itemsModel) {
32 q->setSourceModel(qobject_cast<QAbstractListModel *>(
33 object: itemsModel->data(index: itemsModel->index(row: itemsModel->indexOfEntry(e: entry)), role: ItemsModel::CommentsModelRole).value<QObject *>()));
34 }
35 }
36
37 bool hasReview(const QModelIndex &index, bool checkParents = false)
38 {
39 bool result{false};
40 if (q->sourceModel()) {
41 if (q->sourceModel()->data(index, role: KNSCore::CommentsModel::ScoreRole).toInt() > 0) {
42 result = true;
43 }
44 if (result == false && checkParents) {
45 QModelIndex parentIndex = q->sourceModel()->index(row: q->sourceModel()->data(index, role: KNSCore::CommentsModel::ParentIndexRole).toInt(), column: 0);
46 if (parentIndex.isValid()) {
47 result = hasReview(index: parentIndex, checkParents: true);
48 }
49 }
50 }
51 return result;
52 }
53};
54}
55
56using namespace KNewStuffQuick;
57
58CommentsModel::CommentsModel(QObject *parent)
59 : QSortFilterProxyModel(parent)
60 , d(new CommentsModelPrivate(this))
61{
62}
63
64CommentsModel::~CommentsModel() = default;
65
66void KNewStuffQuick::CommentsModel::classBegin()
67{
68}
69
70void KNewStuffQuick::CommentsModel::componentComplete()
71{
72 d->componentCompleted = true;
73 d->resetConnections();
74}
75
76QObject *CommentsModel::itemsModel() const
77{
78 return d->itemsModel;
79}
80
81void CommentsModel::setItemsModel(QObject *newItemsModel)
82{
83 if (d->itemsModel != newItemsModel) {
84 d->itemsModel = qobject_cast<ItemsModel *>(object: newItemsModel);
85 d->resetConnections();
86 Q_EMIT itemsModelChanged();
87 }
88}
89
90KNSCore::Entry CommentsModel::entry() const
91{
92 return d->entry;
93}
94
95void CommentsModel::setEntry(const KNSCore::Entry &entry)
96{
97 d->entry = entry;
98 d->resetConnections();
99 Q_EMIT entryChanged();
100}
101
102CommentsModel::IncludedComments KNewStuffQuick::CommentsModel::includedComments() const
103{
104 return d->includedComments;
105}
106
107void KNewStuffQuick::CommentsModel::setIncludedComments(CommentsModel::IncludedComments includedComments)
108{
109 if (d->includedComments != includedComments) {
110 d->includedComments = includedComments;
111 invalidateFilter();
112 Q_EMIT includedCommentsChanged();
113 }
114}
115
116bool KNewStuffQuick::CommentsModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
117{
118 bool result{false};
119 switch (d->includedComments) {
120 case IncludeOnlyReviews:
121 result = d->hasReview(index: sourceModel()->index(row: sourceRow, column: 0, parent: sourceParent));
122 break;
123 case IncludeReviewsAndReplies:
124 result = d->hasReview(index: sourceModel()->index(row: sourceRow, column: 0, parent: sourceParent), checkParents: true);
125 break;
126 case IncludeAllComments:
127 default:
128 result = true;
129 break;
130 }
131 return result;
132}
133
134#include "moc_commentsmodel.cpp"
135

source code of knewstuff/src/qtquick/commentsmodel.cpp