File indexing completed on 2024-07-14 15:46:27

0001 /*
0002  * This file is part of the KDE Milou Project
0003  * SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #pragma once
0010 
0011 #include <KRunner/RunnerManager>
0012 #include <QIcon>
0013 #include <QScopedPointer>
0014 #include <QSortFilterProxyModel>
0015 
0016 #include "milou_export.h"
0017 
0018 namespace Milou
0019 {
0020 class MILOU_EXPORT ResultsModel : public QSortFilterProxyModel
0021 {
0022     Q_OBJECT
0023 
0024     /**
0025      * The query string to run
0026      */
0027     Q_PROPERTY(QString queryString READ queryString WRITE setQueryString NOTIFY queryStringChanged)
0028     /**
0029      * The preferred maximum number of matches in the model
0030      *
0031      * If there are lots of results from different catergories,
0032      * the limit can be slightly exceeded.
0033      *
0034      * Default is 0, which means no limit.
0035      */
0036     Q_PROPERTY(int limit READ limit WRITE setLimit RESET resetLimit NOTIFY limitChanged)
0037     /**
0038      * Whether the query is currently being run
0039      *
0040      * This can be used to show a busy indicator
0041      */
0042     Q_PROPERTY(bool querying READ querying NOTIFY queryingChanged)
0043 
0044     /**
0045      * The single runner to use for querying in single runner mode
0046      *
0047      * Defaults to empty string which means all runners
0048      */
0049     Q_PROPERTY(QString runner READ runner WRITE setRunner NOTIFY runnerChanged)
0050     // FIXME rename to singleModeRunnerName or something
0051     Q_PROPERTY(QString runnerName READ runnerName NOTIFY runnerChanged)
0052     Q_PROPERTY(QIcon runnerIcon READ runnerIcon NOTIFY runnerChanged)
0053     Q_PROPERTY(Plasma::RunnerManager *runnerManager READ runnerManager CONSTANT)
0054 
0055 public:
0056     explicit ResultsModel(QObject *parent = nullptr);
0057     ~ResultsModel() override;
0058 
0059     enum Roles {
0060         IdRole = Qt::UserRole + 1,
0061         TypeRole,
0062         RelevanceRole,
0063         EnabledRole,
0064         CategoryRole,
0065         SubtextRole,
0066         DuplicateRole,
0067         ActionsRole,
0068         MultiLineRole,
0069     };
0070     Q_ENUM(Roles)
0071 
0072     QString queryString() const;
0073     void setQueryString(const QString &queryString);
0074     Q_SIGNAL void queryStringChanged(const QString &queryString);
0075 
0076     int limit() const;
0077     void setLimit(int limit);
0078     void resetLimit();
0079     Q_SIGNAL void limitChanged();
0080 
0081     bool querying() const;
0082     Q_SIGNAL void queryingChanged();
0083 
0084     QString runner() const;
0085     void setRunner(const QString &runner);
0086     Q_SIGNAL void runnerChanged();
0087 
0088     QString runnerName() const;
0089     QIcon runnerIcon() const;
0090 
0091     QHash<int, QByteArray> roleNames() const override;
0092 
0093     /**
0094      * Clears the model content and resets the runner context, i.e. no new items will appear.
0095      */
0096     Q_INVOKABLE void clear();
0097 
0098     /**
0099      * Run the result at the given model index @p idx
0100      */
0101     Q_INVOKABLE bool run(const QModelIndex &idx);
0102     /**
0103      * Run the action @p actionNumber at given model index @p idx
0104      */
0105     Q_INVOKABLE bool runAction(const QModelIndex &idx, int actionNumber);
0106 
0107     /**
0108      * Get mime data for the result at given model index @p idx
0109      */
0110     Q_INVOKABLE QMimeData *getMimeData(const QModelIndex &idx) const;
0111 
0112     Plasma::RunnerManager *runnerManager() const;
0113 
0114 Q_SIGNALS:
0115     /**
0116      * This signal is emitted when a an InformationalMatch is run, and it is advised
0117      * to update the search term, e.g. used for calculator runner results
0118      */
0119     void queryStringChangeRequested(const QString &queryString, int pos);
0120 
0121 private:
0122     class Private;
0123     QScopedPointer<Private> d;
0124 };
0125 
0126 } // namespace Milou