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 
13 namespace KNewStuffQuick
14 {
15 class CommentsModelPrivate
16 {
17 public:
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 
29  void resetConnections()
30  {
31  if (componentCompleted && itemsModel) {
32  q->setSourceModel(qobject_cast<QAbstractListModel *>(
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 
56 using namespace KNewStuffQuick;
57 
58 CommentsModel::CommentsModel(QObject *parent)
59  : QSortFilterProxyModel(parent)
60  , d(new CommentsModelPrivate(this))
61 {
62 }
63 
64 CommentsModel::~CommentsModel() = default;
65 
66 void KNewStuffQuick::CommentsModel::classBegin()
67 {
68 }
69 
70 void KNewStuffQuick::CommentsModel::componentComplete()
71 {
72  d->componentCompleted = true;
73  d->resetConnections();
74 }
75 
76 QObject *CommentsModel::itemsModel() const
77 {
78  return d->itemsModel;
79 }
80 
81 void CommentsModel::setItemsModel(QObject *newItemsModel)
82 {
83  if (d->itemsModel != newItemsModel) {
84  d->itemsModel = qobject_cast<ItemsModel *>(newItemsModel);
85  d->resetConnections();
86  Q_EMIT itemsModelChanged();
87  }
88 }
89 
91 {
92  return d->entry;
93 }
94 
95 void CommentsModel::setEntry(const KNSCore::Entry &entry)
96 {
97  d->entry = entry;
98  d->resetConnections();
99  Q_EMIT entryChanged();
100 }
101 
103 {
104  return d->includedComments;
105 }
106 
108 {
109  if (d->includedComments != includedComments) {
110  d->includedComments = includedComments;
111  invalidateFilter();
112  Q_EMIT includedCommentsChanged();
113  }
114 }
115 
116 bool 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"
void setIncludedComments(CommentsModel::IncludedComments includedComments)
Set which comments should be included.
Encapsulates a KNSCore::CommentsModel for use in Qt Quick.
Q_EMITQ_EMIT
KNSCore::Entry entry
The Entry for which this model should handle comments.
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
bool isValid() const const
KNewStuffQuick::CommentsModel::IncludedComments includedComments
Which types of comments should be included @default AllComments.
A model which shows the contents found in an Engine.
KNewStuff data entry container.
Definition: entry.h:47
IncludedComments
The options which can be set for which comments to include.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:56:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.