File indexing completed on 2024-05-12 03:57:54

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
0003  * SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #ifndef KRUNNER_RESULTSMODEL
0010 #define KRUNNER_RESULTSMODEL
0011 
0012 #include "krunner_export.h"
0013 
0014 #include <KRunner/RunnerManager>
0015 #include <QIcon>
0016 #include <QSortFilterProxyModel>
0017 #include <memory>
0018 
0019 namespace KRunner
0020 {
0021 class ResultsModelPrivate;
0022 class KRUNNER_EXPORT ResultsModel : public QSortFilterProxyModel
0023 {
0024     Q_OBJECT
0025 
0026     /**
0027      * The query string to run
0028      */
0029     Q_PROPERTY(QString queryString READ queryString WRITE setQueryString NOTIFY queryStringChanged)
0030     /**
0031      * The preferred maximum number of matches in the model
0032      *
0033      * If there are lots of results from different categories,
0034      * the limit can be slightly exceeded.
0035      *
0036      * Default is 0, which means no limit.
0037      */
0038     Q_PROPERTY(int limit READ limit WRITE setLimit RESET resetLimit NOTIFY limitChanged)
0039     /**
0040      * Whether the query is currently being run
0041      *
0042      * This can be used to show a busy indicator
0043      */
0044     Q_PROPERTY(bool querying READ querying NOTIFY queryingChanged)
0045 
0046     /**
0047      * The single runner to use for querying in single runner mode
0048      *
0049      * Defaults to empty string which means all runners
0050      */
0051     Q_PROPERTY(QString singleRunner READ singleRunner WRITE setSingleRunner NOTIFY singleRunnerChanged)
0052     Q_PROPERTY(KPluginMetaData singleRunnerMetaData READ singleRunnerMetaData NOTIFY singleRunnerChanged)
0053 
0054     Q_PROPERTY(KRunner::RunnerManager *runnerManager READ runnerManager CONSTANT)
0055     Q_PROPERTY(QStringList favoriteIds READ favoriteIds WRITE setFavoriteIds NOTIFY favoriteIdsChanged)
0056 
0057 public:
0058     explicit ResultsModel(const KConfigGroup &configGroup, KConfigGroup stateConfigGroup, QObject *parent = nullptr);
0059     explicit ResultsModel(QObject *parent = nullptr);
0060     ~ResultsModel() override;
0061 
0062     enum Roles {
0063         IdRole = Qt::UserRole + 1,
0064         CategoryRelevanceRole,
0065         RelevanceRole,
0066         EnabledRole,
0067         CategoryRole,
0068         SubtextRole,
0069         ActionsRole,
0070         MultiLineRole,
0071         UrlsRole,
0072         QueryMatchRole, /// @internal
0073         FavoriteIndexRole, /// @internal
0074     };
0075     Q_ENUM(Roles)
0076 
0077     QString queryString() const;
0078     void setQueryString(const QString &queryString);
0079     Q_SIGNAL void queryStringChanged(const QString &queryString);
0080 
0081     /**
0082      * IDs of favorite plugins. Those plugins are always in a fixed order before the other ones.
0083      * @param ids KPluginMetaData::pluginId values of plugins
0084      */
0085     void setFavoriteIds(const QStringList &ids);
0086     QStringList favoriteIds() const;
0087     Q_SIGNAL void favoriteIdsChanged();
0088 
0089     int limit() const;
0090     void setLimit(int limit);
0091     void resetLimit();
0092     Q_SIGNAL void limitChanged();
0093 
0094     bool querying() const;
0095     Q_SIGNAL void queryingChanged();
0096 
0097     QString singleRunner() const;
0098     void setSingleRunner(const QString &runner);
0099     Q_SIGNAL void singleRunnerChanged();
0100 
0101     KPluginMetaData singleRunnerMetaData() const;
0102 
0103     QHash<int, QByteArray> roleNames() const override;
0104 
0105     /**
0106      * Clears the model content and resets the runner context, i.e. no new items will appear.
0107      */
0108     Q_INVOKABLE void clear();
0109 
0110     /**
0111      * Run the result at the given model index @p idx
0112      */
0113     Q_INVOKABLE bool run(const QModelIndex &idx);
0114     /**
0115      * Run the action @p actionNumber at given model index @p idx
0116      */
0117     Q_INVOKABLE bool runAction(const QModelIndex &idx, int actionNumber);
0118 
0119     /**
0120      * Get mime data for the result at given model index @p idx
0121      */
0122     Q_INVOKABLE QMimeData *getMimeData(const QModelIndex &idx) const;
0123 
0124     /**
0125      * Get match for the result at given model index @p idx
0126      */
0127     KRunner::QueryMatch getQueryMatch(const QModelIndex &idx) const;
0128 
0129     KRunner::RunnerManager *runnerManager() const;
0130 
0131 Q_SIGNALS:
0132     /**
0133      * This signal is emitted when a an InformationalMatch is run, and it is advised
0134      * to update the search term, e.g. used for calculator runner results
0135      */
0136     void queryStringChangeRequested(const QString &queryString, int pos);
0137 
0138 private:
0139     const std::unique_ptr<ResultsModelPrivate> d;
0140 };
0141 
0142 } // namespace KRunner
0143 #endif