KNewStuff

qtquick/commentsmodel.cpp
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 "core/commentsmodel.h"
10
11namespace KNewStuffQuick
12{
13class CommentsModelPrivate
14{
15public:
16 CommentsModelPrivate(CommentsModel *qq)
17 : q(qq)
18 {
19 }
21 ItemsModel *itemsModel{nullptr};
22 KNSCore::Entry entry;
23 bool componentCompleted{false};
24 CommentsModel::IncludedComments includedComments{CommentsModel::IncludeAllComments};
25
27 void resetConnections()
28 {
29 if (componentCompleted && itemsModel) {
30 q->setSourceModel(qobject_cast<QAbstractListModel *>(
31 itemsModel->data(itemsModel->index(itemsModel->indexOfEntry(entry)), ItemsModel::CommentsModelRole).value<QObject *>()));
32 }
33 }
34
35 bool hasReview(const QModelIndex &index, bool checkParents = false)
36 {
37 bool result{false};
38 if (q->sourceModel()) {
39 if (q->sourceModel()->data(index, KNSCore::CommentsModel::ScoreRole).toInt() > 0) {
40 result = true;
41 }
42 if (result == false && checkParents) {
43 QModelIndex parentIndex = q->sourceModel()->index(q->sourceModel()->data(index, KNSCore::CommentsModel::ParentIndexRole).toInt(), 0);
44 if (parentIndex.isValid()) {
45 result = hasReview(parentIndex, true);
46 }
47 }
48 }
49 return result;
50 }
51};
52}
53
54using namespace KNewStuffQuick;
55
56CommentsModel::CommentsModel(QObject *parent)
57 : QSortFilterProxyModel(parent)
58 , d(new CommentsModelPrivate(this))
59{
60}
61
62CommentsModel::~CommentsModel() = default;
63
64void KNewStuffQuick::CommentsModel::classBegin()
65{
66}
67
68void KNewStuffQuick::CommentsModel::componentComplete()
69{
70 d->componentCompleted = true;
71 d->resetConnections();
72}
73
75{
76 return d->itemsModel;
77}
78
79void CommentsModel::setItemsModel(ItemsModel *newItemsModel)
80{
81 if (d->itemsModel != newItemsModel) {
82 d->itemsModel = newItemsModel;
83 d->resetConnections();
84 Q_EMIT itemsModelChanged();
85 }
86}
87
89{
90 return d->entry;
91}
92
93void CommentsModel::setEntry(const KNSCore::Entry &entry)
94{
95 d->entry = entry;
96 d->resetConnections();
97 Q_EMIT entryChanged();
98}
99
104
106{
107 if (d->includedComments != includedComments) {
108 d->includedComments = includedComments;
109 invalidateFilter();
110 Q_EMIT includedCommentsChanged();
111 }
112}
113
114bool KNewStuffQuick::CommentsModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
115{
116 bool result{false};
117 switch (d->includedComments) {
118 case IncludeOnlyReviews:
119 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent));
120 break;
121 case IncludeReviewsAndReplies:
122 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent), true);
123 break;
124 case IncludeAllComments:
125 default:
126 result = true;
127 break;
128 }
129 return result;
130}
131
132#include "moc_commentsmodel.cpp"
A model which shows the contents found in an Engine.
KNewStuff data entry container.
Definition entry.h:48
Encapsulates a KNSCore::CommentsModel for use in Qt Quick.
KNewStuffQuick::CommentsModel::IncludedComments includedComments
Which types of comments should be included @default AllComments.
KNSCore::Entry entry
The index in the model of the entry to fetch comments for.
IncludedComments
The options which can be set for which comments to include.
void setIncludedComments(CommentsModel::IncludedComments includedComments)
Set which comments should be included.
ItemsModel * itemsModel
The KNewStufQuick::ItemsModel to interact with servers through.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
Q_EMITQ_EMIT
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:56:35 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.