1/*
2 * SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
3 * SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 *
7 */
8
9#ifndef KRUNNER_RESULTSMODEL
10#define KRUNNER_RESULTSMODEL
11
12#include "krunner_export.h"
13
14#include <KRunner/RunnerManager>
15#include <QIcon>
16#include <QMimeData>
17#include <QSortFilterProxyModel>
18#include <memory>
19
20namespace KRunner
21{
22class ResultsModelPrivate;
23
24/*!
25 * \class KRunner::ResultsModel
26 * \inheaderfile KRunner/ResultsModel
27 * \inmodule KRunner
28 *
29 * \brief A model that exposes and sorts results for a given query.
30 *
31 * \since 6.0
32 */
33class KRUNNER_EXPORT ResultsModel : public QSortFilterProxyModel
34{
35 Q_OBJECT
36
37 /*!
38 * \property KRunner::ResultsModel::queryString
39 *
40 * The query string to run
41 */
42 Q_PROPERTY(QString queryString READ queryString WRITE setQueryString NOTIFY queryStringChanged)
43 /*!
44 * \property KRunner::ResultsModel::limit
45 *
46 * The preferred maximum number of matches in the model
47 *
48 * If there are lots of results from different categories,
49 * the limit can be slightly exceeded.
50 *
51 * Default is 0, which means no limit.
52 */
53 Q_PROPERTY(int limit READ limit WRITE setLimit RESET resetLimit NOTIFY limitChanged)
54 /*!
55 * \property KRunner::ResultsModel::querying
56 *
57 * Whether the query is currently being run
58 *
59 * This can be used to show a busy indicator
60 */
61 Q_PROPERTY(bool querying READ querying NOTIFY queryingChanged)
62
63 /*!
64 * \property KRunner::ResultsModel::singleRunner
65 *
66 * The single runner to use for querying in single runner mode
67 *
68 * Defaults to empty string which means all runners
69 */
70 Q_PROPERTY(QString singleRunner READ singleRunner WRITE setSingleRunner NOTIFY singleRunnerChanged)
71
72 /*!
73 * \property KRunner::ResultsModel::singleRunnerMetaData
74 */
75 Q_PROPERTY(KPluginMetaData singleRunnerMetaData READ singleRunnerMetaData NOTIFY singleRunnerChanged)
76
77 /*!
78 * \property KRunner::ResultsModel::runnerManager
79 */
80 Q_PROPERTY(KRunner::RunnerManager *runnerManager READ runnerManager WRITE setRunnerManager NOTIFY runnerManagerChanged)
81
82 /*!
83 * \property KRunner::ResultsModel::favoriteIds
84 */
85 Q_PROPERTY(QStringList favoriteIds READ favoriteIds WRITE setFavoriteIds NOTIFY favoriteIdsChanged)
86
87public:
88 /*!
89 *
90 */
91 explicit ResultsModel(const KConfigGroup &configGroup, const KConfigGroup &stateConfigGroup, QObject *parent = nullptr);
92
93 /*!
94 *
95 */
96 explicit ResultsModel(QObject *parent = nullptr);
97 ~ResultsModel() override;
98
99 /*!
100 * \value IdRole
101 * \value CategoryRelevanceRole
102 * \value RelevanceRole
103 * \value EnabledRole
104 * \value CategoryRole
105 * \value SubtextRole
106 * \value ActionsRole
107 * \value MultiLineRole
108 * \value UrlsRole
109 * \omitvalue QueryMatchRole
110 * \omitvalue FavoriteIndexRole
111 * \omitvalue FavoriteCountRole
112 */
113 enum Roles {
114 IdRole = Qt::UserRole + 1,
115 CategoryRelevanceRole,
116 RelevanceRole,
117 EnabledRole,
118 CategoryRole,
119 SubtextRole,
120 ActionsRole,
121 MultiLineRole,
122 UrlsRole,
123 QueryMatchRole,
124 FavoriteIndexRole,
125 FavoriteCountRole,
126 };
127 Q_ENUM(Roles)
128
129 QString queryString() const;
130 void setQueryString(const QString &queryString);
131 Q_SIGNAL void queryStringChanged(const QString &queryString);
132
133 /*!
134 * IDs of favorite plugins. Those plugins are always in a fixed order before the other ones.
135 *
136 * \a ids KPluginMetaData::pluginId values of plugins
137 */
138 void setFavoriteIds(const QStringList &ids);
139 QStringList favoriteIds() const;
140 Q_SIGNAL void favoriteIdsChanged();
141
142 int limit() const;
143 void setLimit(int limit);
144 void resetLimit();
145 Q_SIGNAL void limitChanged();
146
147 bool querying() const;
148 Q_SIGNAL void queryingChanged();
149
150 QString singleRunner() const;
151 void setSingleRunner(const QString &runner);
152 Q_SIGNAL void singleRunnerChanged();
153
154 KPluginMetaData singleRunnerMetaData() const;
155
156 QHash<int, QByteArray> roleNames() const override;
157
158 /*!
159 * Clears the model content and resets the runner context, i.e. no new items will appear.
160 */
161 Q_INVOKABLE void clear();
162
163 /*!
164 * Run the result at the given model index \a idx
165 */
166 Q_INVOKABLE bool run(const QModelIndex &idx);
167 /*!
168 * Run the action \a actionNumber at given model index \a idx
169 */
170 Q_INVOKABLE bool runAction(const QModelIndex &idx, int actionNumber);
171
172 /*!
173 * Get mime data for the result at given model index \a idx
174 */
175 Q_INVOKABLE QMimeData *getMimeData(const QModelIndex &idx) const;
176
177 /*!
178 * Get match for the result at given model index \a idx
179 */
180 KRunner::QueryMatch getQueryMatch(const QModelIndex &idx) const;
181
182 KRunner::RunnerManager *runnerManager() const;
183 /*!
184 * \since 6.9
185 */
186 void setRunnerManager(KRunner::RunnerManager *manager);
187 /*!
188 * \since 6.9
189 */
190 Q_SIGNAL void runnerManagerChanged();
191
192Q_SIGNALS:
193 /*!
194 * This signal is emitted when a an InformationalMatch is run, and it is advised
195 * to update the search term, e.g. used for calculator runner results
196 */
197 void queryStringChangeRequested(const QString &queryString, int pos);
198
199private:
200 const std::unique_ptr<ResultsModelPrivate> d;
201};
202
203} // namespace KRunner
204#endif
205

source code of krunner/src/model/resultsmodel.h