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 "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 }
23 ItemsModel *itemsModel{nullptr};
24 KNSCore::Entry entry;
25 bool componentCompleted{false};
26 CommentsModel::IncludedComments includedComments{CommentsModel::IncludeAllComments};
27
29 void resetConnections()
30 {
31 if (componentCompleted && itemsModel) {
33 itemsModel->data(itemsModel->index(itemsModel->indexOfEntry(entry)), 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, KNSCore::CommentsModel::ScoreRole).toInt() > 0) {
42 result = true;
43 }
44 if (result == false && checkParents) {
45 QModelIndex parentIndex = q->sourceModel()->index(q->sourceModel()->data(index, KNSCore::CommentsModel::ParentIndexRole).toInt(), 0);
46 if (parentIndex.isValid()) {
47 result = hasReview(parentIndex, 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
77{
78 return d->itemsModel;
79}
80
81void CommentsModel::setItemsModel(QObject *newItemsModel)
82{
83 if (d->itemsModel != newItemsModel) {
85 d->resetConnections();
86 Q_EMIT itemsModelChanged();
87 }
88}
89
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
106
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(sourceModel()->index(sourceRow, 0, sourceParent));
122 break;
123 case IncludeReviewsAndReplies:
124 result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent), true);
125 break;
126 case IncludeAllComments:
127 default:
128 result = true;
129 break;
130 }
131 return result;
132}
133
134#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.
QObject * itemsModel
The KNewStufQuick::ItemsModel to interact with servers through.
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.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
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 Tue Mar 26 2024 11:21:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.