KNewStuff

categoriesmodel.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 "categoriesmodel.h"
8 
9 #include "engine.h"
10 
11 #include <KLocalizedString>
12 
13 class CategoriesModel::Private {
14 public:
15  Private() {}
16  KNSCore::Engine *engine;
17 };
18 
19 CategoriesModel::CategoriesModel(Engine *parent)
20  : QAbstractListModel(parent)
21  , d(new Private)
22 {
23  d->engine = qobject_cast<KNSCore::Engine*>(parent->engine());
24  connect(d->engine, &KNSCore::Engine::signalCategoriesMetadataLoded, this, [this](){ beginResetModel(); endResetModel(); });
25 }
26 
27 CategoriesModel::~CategoriesModel()
28 {
29  delete d;
30 }
31 
32 QHash<int, QByteArray> CategoriesModel::roleNames() const
33 {
34  static const QHash<int, QByteArray> roles{
35  {NameRole, "name"},
36  {IdRole, "id"},
37  {DisplayNameRole, "displayName"}
38  };
39  return roles;
40 }
41 
42 int CategoriesModel::rowCount(const QModelIndex &parent) const
43 {
44  if(parent.isValid()) {
45  return 0;
46  }
47  return d->engine->categoriesMetadata().count() + 1;
48 }
49 
50 QVariant CategoriesModel::data(const QModelIndex &index, int role) const
51 {
52  QVariant result;
53  const QList<KNSCore::Provider::CategoryMetadata> categoriesMetadata = d->engine->categoriesMetadata();
54  if (index.isValid()) {
55  if (index.row() == 0) {
56  switch (role) {
57  case NameRole:
58  result.setValue(QString::fromLatin1(""));
59  break;
60  case IdRole:
61  result.setValue(0);
62  break;
63  case DisplayNameRole:
64  result.setValue(i18nc("The first entry in the category selection list (also the default value)", "All Categories"));
65  break;
66  default:
67  result.setValue(QString::fromLatin1("Unknown role"));
68  break;
69  }
70  } else if (index.row() <= categoriesMetadata.count()) {
71  const KNSCore::Provider::CategoryMetadata category = categoriesMetadata[index.row() - 1];
72  switch (role) {
73  case NameRole:
74  result.setValue(category.name);
75  break;
76  case IdRole:
77  result.setValue(category.id);
78  break;
79  case DisplayNameRole:
80  result.setValue(category.displayName);
81  break;
82  default:
83  result.setValue(QString::fromLatin1("Unknown role"));
84  break;
85  }
86  }
87  }
88  return result;
89 }
90 
92 {
93  QString dispName = i18nc("The string passed back in the case the requested category is not known", "Unknown Category");
94  for (const KNSCore::Provider::CategoryMetadata &cat : d->engine->categoriesMetadata()) {
95  if (cat.id == id) {
96  dispName = cat.displayName;
97  break;
98  }
99  }
100  return dispName;
101 }
Encapsulates a KNSCore::Engine for use in Qt Quick.
Definition: quickengine.h:23
Q_INVOKABLE QString idToDisplayName(const QString &id) const
Get the display name for the category with the id passed to the function.
bool isValid() const const
int count(const T &value) const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int row() const const
void setValue(const T &value)
KNewStuff engine.
Definition: engine.h:59
Describes a category: id/name/disaplayName.
Definition: provider.h:85
QString fromLatin1(const char *str, int size)
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.