KNewStuff

searchpresetmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "searchpresetmodel.h"
8
9#include "knewstuffquick_debug.h"
10
11#include <KLocalizedString>
12
13#include "../core/enginebase_p.h"
14
15SearchPresetModel::SearchPresetModel(KNSCore::EngineBase *engine)
16 : QAbstractListModel(engine)
17 , m_engine(engine)
18{
19 connect(m_engine, qOverload<const QList<KNSCore::SearchPreset> &>(&KNSCore::EngineBase::signalSearchPresetsLoaded), this, [this]() {
20 beginResetModel();
21 endResetModel();
22 });
23}
24
25SearchPresetModel::~SearchPresetModel() = default;
26
27QHash<int, QByteArray> SearchPresetModel::roleNames() const
28{
29 static const QHash<int, QByteArray> roles{{DisplayNameRole, "displayName"}, {IconRole, "iconName"}};
30 return roles;
31}
32
33QVariant SearchPresetModel::data(const QModelIndex &index, int role) const
34{
35 if (index.isValid() && checkIndex(index)) {
36 const QList<KNSCore::SearchPreset> presets = m_engine->d->searchPresets;
37 const KNSCore::SearchPreset &preset = presets[index.row()];
38
39 if (role == DisplayNameRole) {
40 if (QString name = preset.displayName(); !name.isEmpty()) {
41 return name;
42 }
43
44 switch (preset.type()) {
46 return i18nc("Knewstuff5", "Back");
48 return i18nc("Knewstuff5", "Popular");
50 return i18nc("Knewstuff5", "Featured");
52 return i18nc("Knewstuff5", "Restart");
54 return i18nc("Knewstuff5", "New");
56 return i18nc("Knewstuff5", "Home");
58 return i18nc("Knewstuff5", "Shelf");
60 return i18nc("Knewstuff5", "Up");
62 return i18nc("Knewstuff5", "Recommended");
64 return i18nc("Knewstuff5", "Subscriptions");
66 return i18nc("Knewstuff5", "All Entries");
67 case KNSCore::SearchPreset::Type::NoPresetType:
68 break;
69 }
70 return i18nc("Knewstuff5", "Search Preset: %1", preset.request().searchTerm());
71 }
72 if (role == IconRole) {
73 if (QString name = preset.iconName(); !name.isEmpty()) {
74 return name;
75 }
76
77 switch (preset.type()) {
79 return QStringLiteral("arrow-left");
83 return QStringLiteral("rating");
85 return QStringLiteral("change-date-symbolic");
87 return QStringLiteral("start-over");
89 return QStringLiteral("go-home");
92 return QStringLiteral("bookmark");
94 return QStringLiteral("arrow-up");
96 case KNSCore::SearchPreset::Type::NoPresetType:
97 break;
98 }
99 return QStringLiteral("search");
100 }
101 }
102 return QVariant();
103}
104
105int SearchPresetModel::rowCount(const QModelIndex &parent) const
106{
107 if (parent.isValid()) {
108 return 0;
109 }
110 return m_engine->d->searchPresets.count();
111}
112
113void SearchPresetModel::loadSearch(const QModelIndex &index)
114{
115 if (index.row() >= rowCount() || !index.isValid()) {
116 qCWarning(KNEWSTUFFQUICK) << "index SearchPresetModel::loadSearch invalid" << index;
117 return;
118 }
119 const auto preset = m_engine->d->searchPresets.at(index.row());
120 m_engine->search(preset.request());
121}
122
123#include "moc_searchpresetmodel.cpp"
KNewStuff engine.
Definition enginebase.h:56
void signalSearchPresetsLoaded(const QList< Provider::SearchPreset > &presets)
Fires when the engine has loaded search presets.
ResultsStream * search(const KNSCore::Provider::SearchRequest &request)
Returns a stream object that will fulfill the request.
Describes a search request that may come from the provider.
@ FolderUp
preset indicating going up in the search result hierarchy.
@ New
preset indicating new items.
@ Featured
preset for featured items.
@ Shelf
preset indicating previously acquired items.
@ Popular
preset indicating popular items.
@ Recommended
preset for recommended. This may be customized by the server per user.
@ AllEntries
preset indicating all possible entries, such as a crawlable list. Might be intense to load.
@ Subscription
preset indicating items that the user is subscribed to.
@ Start
preset indicating the first entry.
@ GoBack
preset representing the previous search.
@ Root
preset indicating a root directory.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString name(StandardAction id)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
int row() const const
QObject * parent() const const
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.