KRunner

resultsmodel.h
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 <QSortFilterProxyModel>
17#include <memory>
18
19namespace KRunner
20{
21class ResultsModelPrivate;
22
23/**
24 * @class ResultsModel resultsmodel.h <KRunner/ResultsModel>
25 *
26 * A model that exposes and sorts results for a given query.
27 *
28 * @since 6.0
29 */
30class KRUNNER_EXPORT ResultsModel : public QSortFilterProxyModel
31{
33
34 /**
35 * The query string to run
36 */
37 Q_PROPERTY(QString queryString READ queryString WRITE setQueryString NOTIFY queryStringChanged)
38 /**
39 * The preferred maximum number of matches in the model
40 *
41 * If there are lots of results from different categories,
42 * the limit can be slightly exceeded.
43 *
44 * Default is 0, which means no limit.
45 */
46 Q_PROPERTY(int limit READ limit WRITE setLimit RESET resetLimit NOTIFY limitChanged)
47 /**
48 * Whether the query is currently being run
49 *
50 * This can be used to show a busy indicator
51 */
52 Q_PROPERTY(bool querying READ querying NOTIFY queryingChanged)
53
54 /**
55 * The single runner to use for querying in single runner mode
56 *
57 * Defaults to empty string which means all runners
58 */
59 Q_PROPERTY(QString singleRunner READ singleRunner WRITE setSingleRunner NOTIFY singleRunnerChanged)
60 Q_PROPERTY(KPluginMetaData singleRunnerMetaData READ singleRunnerMetaData NOTIFY singleRunnerChanged)
61
62 Q_PROPERTY(KRunner::RunnerManager *runnerManager READ runnerManager WRITE setRunnerManager NOTIFY runnerManagerChanged)
63 Q_PROPERTY(QStringList favoriteIds READ favoriteIds WRITE setFavoriteIds NOTIFY favoriteIdsChanged)
64
65public:
66 explicit ResultsModel(const KConfigGroup &configGroup, const KConfigGroup &stateConfigGroup, QObject *parent = nullptr);
67 explicit ResultsModel(QObject *parent = nullptr);
68 ~ResultsModel() override;
69
70 enum Roles {
71 IdRole = Qt::UserRole + 1,
72 CategoryRelevanceRole,
73 RelevanceRole,
74 EnabledRole,
75 CategoryRole,
76 SubtextRole,
77 ActionsRole,
78 MultiLineRole,
79 UrlsRole,
80 QueryMatchRole, /// @internal
81 FavoriteIndexRole, /// @internal
82 };
83 Q_ENUM(Roles)
84
85 QString queryString() const;
86 void setQueryString(const QString &queryString);
87 Q_SIGNAL void queryStringChanged(const QString &queryString);
88
89 /**
90 * IDs of favorite plugins. Those plugins are always in a fixed order before the other ones.
91 * @param ids KPluginMetaData::pluginId values of plugins
92 */
93 void setFavoriteIds(const QStringList &ids);
94 QStringList favoriteIds() const;
95 Q_SIGNAL void favoriteIdsChanged();
96
97 int limit() const;
98 void setLimit(int limit);
99 void resetLimit();
100 Q_SIGNAL void limitChanged();
101
102 bool querying() const;
103 Q_SIGNAL void queryingChanged();
104
105 QString singleRunner() const;
106 void setSingleRunner(const QString &runner);
107 Q_SIGNAL void singleRunnerChanged();
108
109 KPluginMetaData singleRunnerMetaData() const;
110
111 QHash<int, QByteArray> roleNames() const override;
112
113 /**
114 * Clears the model content and resets the runner context, i.e. no new items will appear.
115 */
116 Q_INVOKABLE void clear();
117
118 /**
119 * Run the result at the given model index @p idx
120 */
121 Q_INVOKABLE bool run(const QModelIndex &idx);
122 /**
123 * Run the action @p actionNumber at given model index @p idx
124 */
125 Q_INVOKABLE bool runAction(const QModelIndex &idx, int actionNumber);
126
127 /**
128 * Get mime data for the result at given model index @p idx
129 */
130 Q_INVOKABLE QMimeData *getMimeData(const QModelIndex &idx) const;
131
132 /**
133 * Get match for the result at given model index @p idx
134 */
135 KRunner::QueryMatch getQueryMatch(const QModelIndex &idx) const;
136
137 KRunner::RunnerManager *runnerManager() const;
138 /// @since 6.9
139 void setRunnerManager(KRunner::RunnerManager *manager);
140 /// @since 6.9
142
144 /**
145 * This signal is emitted when a an InformationalMatch is run, and it is advised
146 * to update the search term, e.g. used for calculator runner results
147 */
149
150private:
151 const std::unique_ptr<ResultsModelPrivate> d;
152};
153
154} // namespace KRunner
155#endif
A match returned by an AbstractRunner in response to a given RunnerContext.
Definition querymatch.h:32
QString singleRunner
The single runner to use for querying in single runner mode.
Q_SIGNAL void runnerManagerChanged()
bool querying
Whether the query is currently being run.
int limit
The preferred maximum number of matches in the model.
void setRunnerManager(KRunner::RunnerManager *manager)
QString queryString
The query string to run.
void queryStringChangeRequested(const QString &queryString, int pos)
This signal is emitted when a an InformationalMatch is run, and it is advised to update the search te...
void setFavoriteIds(const QStringList &ids)
IDs of favorite plugins.
The RunnerManager class decides what installed runners are runnable, and their ratings.
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALQ_SIGNAL
Q_SIGNALSQ_SIGNALS
QSortFilterProxyModel(QObject *parent)
virtual QModelIndex parent(const QModelIndex &child) const const override
UserRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 21 2025 12:05:25 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.