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