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
13SearchPresetModel::SearchPresetModel(KNSCore::EngineBase *engine)
14 : QAbstractListModel(engine)
15 , m_engine(engine)
16{
18 beginResetModel();
19 endResetModel();
20 });
21}
22
23SearchPresetModel::~SearchPresetModel() = default;
24
25QHash<int, QByteArray> SearchPresetModel::roleNames() const
26{
27 static const QHash<int, QByteArray> roles{{DisplayNameRole, "displayName"}, {IconRole, "iconName"}};
28 return roles;
29}
30
31QVariant SearchPresetModel::data(const QModelIndex &index, int role) const
32{
33 if (index.isValid() && checkIndex(index)) {
34 const QList<KNSCore::Provider::SearchPreset> presets = m_engine->searchPresets();
35 const KNSCore::Provider::SearchPreset preset = presets[index.row()];
36
37 if (role == DisplayNameRole) {
38 if (QString name = preset.displayName; !name.isEmpty()) {
39 return name;
40 }
41
42 switch (preset.type) {
44 return i18nc("Knewstuff5", "Back");
46 return i18nc("Knewstuff5", "Popular");
48 return i18nc("Knewstuff5", "Featured");
50 return i18nc("Knewstuff5", "Restart");
52 return i18nc("Knewstuff5", "New");
54 return i18nc("Knewstuff5", "Home");
56 return i18nc("Knewstuff5", "Shelf");
58 return i18nc("Knewstuff5", "Up");
60 return i18nc("Knewstuff5", "Recommended");
62 return i18nc("Knewstuff5", "Subscriptions");
64 return i18nc("Knewstuff5", "All Entries");
65 default:
66 return i18nc("Knewstuff5", "Search Preset: %1", preset.request.searchTerm);
67 }
68 } else if (role == IconRole) {
69 if (QString name = preset.iconName; !name.isEmpty()) {
70 return name;
71 }
72
73 switch (preset.type) {
75 return QStringLiteral("arrow-left");
79 return QStringLiteral("rating");
81 return QStringLiteral("change-date-symbolic");
83 return QStringLiteral("start-over");
85 return QStringLiteral("go-home");
88 return QStringLiteral("bookmark");
90 return QStringLiteral("arrow-up");
91 default:
92 return QStringLiteral("search");
93 }
94 }
95 }
96 return QVariant();
97}
98
99int SearchPresetModel::rowCount(const QModelIndex &parent) const
100{
101 if (parent.isValid()) {
102 return 0;
103 }
104 return m_engine->searchPresets().count();
105}
106
107void SearchPresetModel::loadSearch(const QModelIndex &index)
108{
109 if (index.row() >= rowCount() || !index.isValid()) {
110 qCWarning(KNEWSTUFFQUICK) << "index SearchPresetModel::loadSearch invalid" << index;
111 return;
112 }
113 const KNSCore::Provider::SearchPreset preset = m_engine->searchPresets().at(index.row());
114 m_engine->search(preset.request);
115}
116
117#include "moc_searchpresetmodel.cpp"
KNewStuff engine.
Definition enginebase.h:52
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.
@ Root
preset indicating a root directory.
Definition provider.h:119
@ Start
preset indicating the first entry.
Definition provider.h:120
@ New
preset indicating new items.
Definition provider.h:126
@ Subscription
preset indicating items that the user is subscribed to.
Definition provider.h:125
@ Shelf
preset indicating previously acquired items.
Definition provider.h:124
@ FolderUp
preset indicating going up in the search result hierarchy.
Definition provider.h:127
@ AllEntries
preset indicating all possible entries, such as a crawlable list. Might be intense to load.
Definition provider.h:128
@ GoBack
preset representing the previous search.
Definition provider.h:118
@ Popular
preset indicating popular items.
Definition provider.h:121
@ Recommended
preset for recommended. This may be customized by the server per user.
Definition provider.h:123
@ Featured
preset for featured items.
Definition provider.h:122
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)
Describes a search request that may come from the provider.
Definition provider.h:135
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:12:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.