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#include <KLocalizedString>
11
12SearchPresetModel::SearchPresetModel(KNSCore::EngineBase *engine)
13 : QAbstractListModel(engine)
14 , m_engine(engine)
15{
17 beginResetModel();
18 endResetModel();
19 });
20}
21
22SearchPresetModel::~SearchPresetModel() = default;
23
24QHash<int, QByteArray> SearchPresetModel::roleNames() const
25{
26 static const QHash<int, QByteArray> roles{{DisplayNameRole, "displayName"}, {IconRole, "iconName"}};
27 return roles;
28}
29
30QVariant SearchPresetModel::data(const QModelIndex &index, int role) const
31{
32 if (index.isValid() && checkIndex(index)) {
33 const QList<KNSCore::Provider::SearchPreset> presets = m_engine->searchPresets();
35
36 if (role == DisplayNameRole) {
37 if (QString name = preset.displayName; !name.isEmpty()) {
38 return name;
39 }
40
41 switch (preset.type) {
43 return i18nc("Knewstuff5", "Back");
45 return i18nc("Knewstuff5", "Popular");
47 return i18nc("Knewstuff5", "Featured");
49 return i18nc("Knewstuff5", "Restart");
51 return i18nc("Knewstuff5", "New");
53 return i18nc("Knewstuff5", "Home");
55 return i18nc("Knewstuff5", "Shelf");
57 return i18nc("Knewstuff5", "Up");
59 return i18nc("Knewstuff5", "Recommended");
61 return i18nc("Knewstuff5", "Subscriptions");
63 return i18nc("Knewstuff5", "All Entries");
64 default:
65 return i18nc("Knewstuff5", "Search Preset: %1", preset.request.searchTerm);
66 }
67 } else if (role == IconRole) {
68 if (QString name = preset.iconName; !name.isEmpty()) {
69 return name;
70 }
71
72 switch (preset.type) {
74 return QStringLiteral("arrow-left");
78 return QStringLiteral("rating");
80 return QStringLiteral("change-date-symbolic");
82 return QStringLiteral("start-over");
84 return QStringLiteral("go-home");
87 return QStringLiteral("bookmark");
89 return QStringLiteral("arrow-up");
90 default:
91 return QStringLiteral("search");
92 }
93 }
94 }
95 return QVariant();
96}
97
98int SearchPresetModel::rowCount(const QModelIndex &parent) const
99{
100 if (parent.isValid()) {
101 return 0;
102 }
103 return m_engine->searchPresets().count();
104}
105
106void SearchPresetModel::loadSearch(const QModelIndex &index)
107{
108 if (index.row() >= rowCount() || !index.isValid()) {
109 qCWarning(KNEWSTUFFQUICK) << "index SearchPresetModel::loadSearch invalid" << index;
110 return;
111 }
112 const KNSCore::Provider::SearchPreset preset = m_engine->searchPresets().at(index.row());
113 m_engine->search(preset.request);
114}
115
116#include "moc_searchpresetmodel.cpp"
KNewStuff engine.
Definition enginebase.h:51
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(StandardShortcut id)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
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 Tue Mar 26 2024 11:21:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.