KNewStuff

qtquick/commentsmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <[email protected]>
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 #include "knewstuffquick_debug.h"
14 
15 #include <memory>
16 
17 namespace KNewStuffQuick {
18 
19 class CommentsModel::Private {
20 public:
21  Private(CommentsModel *qq)
22  : q(qq)
23  {}
24  CommentsModel *q;
25  ItemsModel *itemsModel{nullptr};
26  int entryIndex{-1};
27  bool componentCompleted{false};
28  CommentsModel::IncludedComments includedComments{CommentsModel::IncludeAllComments};
29 
31  void resetConnections() {
32  if (componentCompleted && itemsModel) {
33  q->setSourceModel(qobject_cast<QAbstractListModel*>(itemsModel->data(itemsModel->index(entryIndex), ItemsModel::CommentsModelRole).value<QObject*>()));
34  }
35  }
36 
37  bool hasReview(const QModelIndex& index, bool checkParents = false) {
38  bool result{false};
39  if (q->sourceModel()) {
40  if (q->sourceModel()->data(index, KNSCore::CommentsModel::ScoreRole).toInt() > 0) {
41  result = true;
42  }
43  if (result == false && checkParents) {
44  QModelIndex parentIndex = q->sourceModel()->index(q->sourceModel()->data(index, KNSCore::CommentsModel::ParentIndexRole).toInt(), 0);
45  if (parentIndex.isValid()) {
46  result = hasReview(parentIndex, true);
47  }
48  }
49  }
50  return result;
51  }
52 };
53 }
54 
55 using namespace KNewStuffQuick;
56 
57 CommentsModel::CommentsModel(QObject *parent)
58  : QSortFilterProxyModel(parent)
59  , d(new Private(this))
60 {
61 }
62 
63 CommentsModel::~CommentsModel()
64 {
65  delete d;
66 }
67 
68 void KNewStuffQuick::CommentsModel::classBegin()
69 {}
70 
71 void KNewStuffQuick::CommentsModel::componentComplete()
72 {
73  d->componentCompleted = true;
74  d->resetConnections();
75 }
76 
77 QObject *CommentsModel::itemsModel() const
78 {
79  return d->itemsModel;
80 }
81 
82 void CommentsModel::setItemsModel(QObject *newItemsModel)
83 {
84  if (d->itemsModel != newItemsModel) {
85  d->itemsModel = qobject_cast<ItemsModel*>(newItemsModel);
86  d->resetConnections();
87  emit itemsModelChanged();
88  }
89 }
90 
91 int CommentsModel::entryIndex() const
92 {
93  return d->entryIndex;
94 }
95 
96 void CommentsModel::setEntryIndex(int entryIndex)
97 {
98  if (d->entryIndex != entryIndex) {
99  d->entryIndex = entryIndex;
100  d->resetConnections();
101  emit entryIndexChanged();
102  }
103 }
104 
106 {
107  return d->includedComments;
108 }
109 
111 {
112  if (d->includedComments != includedComments) {
113  d->includedComments = includedComments;
114  invalidateFilter();
115  emit includedCommentsChanged();
116  }
117 }
118 
119 bool KNewStuffQuick::CommentsModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
120 {
121  bool result{false};
122  switch (d->includedComments) {
123  case IncludeOnlyReviews:
124  result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent));
125  break;
126  case IncludeReviewsAndReplies:
127  result = d->hasReview(sourceModel()->index(sourceRow, 0, sourceParent), true);
128  break;
129  case IncludeAllComments:
130  default:
131  result = true;
132  break;
133  }
134  return result;
135 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
Encapsulates a KNSCore::CommentsModel for use in Qt Quick.
IncludedComments
The options which can be set for which comments to include.
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
CommentsModel::IncludedComments includedComments() const
Which comments should be included.
bool isValid() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setIncludedComments(CommentsModel::IncludedComments includedComments)
Set which comments should be included.
A model which shows the contents found in an Engine.
T qobject_cast(QObject *object)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 11 2020 22:43:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.