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 | |
19 | namespace KRunner |
20 | { |
21 | class 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 | */ |
30 | class KRUNNER_EXPORT ResultsModel : public QSortFilterProxyModel |
31 | { |
32 | Q_OBJECT |
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 CONSTANT) |
63 | Q_PROPERTY(QStringList favoriteIds READ favoriteIds WRITE setFavoriteIds NOTIFY favoriteIdsChanged) |
64 | |
65 | public: |
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 | FavoriteCountRole, /// @internal |
83 | }; |
84 | Q_ENUM(Roles) |
85 | |
86 | QString queryString() const; |
87 | void setQueryString(const QString &queryString); |
88 | Q_SIGNAL void queryStringChanged(const QString &queryString); |
89 | |
90 | /** |
91 | * IDs of favorite plugins. Those plugins are always in a fixed order before the other ones. |
92 | * @param ids KPluginMetaData::pluginId values of plugins |
93 | */ |
94 | void setFavoriteIds(const QStringList &ids); |
95 | QStringList favoriteIds() const; |
96 | Q_SIGNAL void favoriteIdsChanged(); |
97 | |
98 | int limit() const; |
99 | void setLimit(int limit); |
100 | void resetLimit(); |
101 | Q_SIGNAL void limitChanged(); |
102 | |
103 | bool querying() const; |
104 | Q_SIGNAL void queryingChanged(); |
105 | |
106 | QString singleRunner() const; |
107 | void setSingleRunner(const QString &runner); |
108 | Q_SIGNAL void singleRunnerChanged(); |
109 | |
110 | KPluginMetaData singleRunnerMetaData() const; |
111 | |
112 | QHash<int, QByteArray> roleNames() const override; |
113 | |
114 | /** |
115 | * Clears the model content and resets the runner context, i.e. no new items will appear. |
116 | */ |
117 | Q_INVOKABLE void clear(); |
118 | |
119 | /** |
120 | * Run the result at the given model index @p idx |
121 | */ |
122 | Q_INVOKABLE bool run(const QModelIndex &idx); |
123 | /** |
124 | * Run the action @p actionNumber at given model index @p idx |
125 | */ |
126 | Q_INVOKABLE bool runAction(const QModelIndex &idx, int actionNumber); |
127 | |
128 | /** |
129 | * Get mime data for the result at given model index @p idx |
130 | */ |
131 | Q_INVOKABLE QMimeData *getMimeData(const QModelIndex &idx) const; |
132 | |
133 | /** |
134 | * Get match for the result at given model index @p idx |
135 | */ |
136 | KRunner::QueryMatch getQueryMatch(const QModelIndex &idx) const; |
137 | |
138 | KRunner::RunnerManager *runnerManager() const; |
139 | |
140 | Q_SIGNALS: |
141 | /** |
142 | * This signal is emitted when a an InformationalMatch is run, and it is advised |
143 | * to update the search term, e.g. used for calculator runner results |
144 | */ |
145 | void queryStringChangeRequested(const QString &queryString, int pos); |
146 | |
147 | private: |
148 | const std::unique_ptr<ResultsModelPrivate> d; |
149 | }; |
150 | |
151 | } // namespace KRunner |
152 | #endif |
153 | |