1 /*
2  * This file is part of the KDE Milou Project
3  * SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
4  *
5  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6  *
7  */
8 
9 #pragma once
10 
11 #include <KRunner/RunnerManager>
12 #include <QIcon>
13 #include <QScopedPointer>
14 #include <QSortFilterProxyModel>
15 
16 #include "milou_export.h"
17 
18 namespace Milou
19 {
20 class MILOU_EXPORT ResultsModel : public QSortFilterProxyModel
21 {
22     Q_OBJECT
23 
24     /**
25      * The query string to run
26      */
27     Q_PROPERTY(QString queryString READ queryString WRITE setQueryString NOTIFY queryStringChanged)
28     /**
29      * The preferred maximum number of matches in the model
30      *
31      * If there are lots of results from different catergories,
32      * the limit can be slightly exceeded.
33      *
34      * Default is 0, which means no limit.
35      */
36     Q_PROPERTY(int limit READ limit WRITE setLimit RESET resetLimit NOTIFY limitChanged)
37     /**
38      * Whether the query is currently being run
39      *
40      * This can be used to show a busy indicator
41      */
42     Q_PROPERTY(bool querying READ querying NOTIFY queryingChanged)
43 
44     /**
45      * The single runner to use for querying in single runner mode
46      *
47      * Defaults to empty string which means all runners
48      */
49     Q_PROPERTY(QString runner READ runner WRITE setRunner NOTIFY runnerChanged)
50     // FIXME rename to singleModeRunnerName or something
51     Q_PROPERTY(QString runnerName READ runnerName NOTIFY runnerChanged)
52     Q_PROPERTY(QIcon runnerIcon READ runnerIcon NOTIFY runnerChanged)
53     Q_PROPERTY(Plasma::RunnerManager *runnerManager READ runnerManager CONSTANT)
54 
55 public:
56     explicit ResultsModel(QObject *parent = nullptr);
57     ~ResultsModel() override;
58 
59     enum Roles {
60         IdRole = Qt::UserRole + 1,
61         TypeRole,
62         RelevanceRole,
63         EnabledRole,
64         CategoryRole,
65         SubtextRole,
66         DuplicateRole,
67         ActionsRole,
68         MultiLineRole,
69     };
70     Q_ENUM(Roles)
71 
72     QString queryString() const;
73     void setQueryString(const QString &queryString);
74     Q_SIGNAL void queryStringChanged(const QString &queryString);
75 
76     int limit() const;
77     void setLimit(int limit);
78     void resetLimit();
79     Q_SIGNAL void limitChanged();
80 
81     bool querying() const;
82     Q_SIGNAL void queryingChanged();
83 
84     QString runner() const;
85     void setRunner(const QString &runner);
86     Q_SIGNAL void runnerChanged();
87 
88     QString runnerName() const;
89     QIcon runnerIcon() const;
90 
91     QHash<int, QByteArray> roleNames() const override;
92 
93     /**
94      * Clears the model content and resets the runner context, i.e. no new items will appear.
95      */
96     Q_INVOKABLE void clear();
97 
98     /**
99      * Run the result at the given model index @p idx
100      */
101     Q_INVOKABLE bool run(const QModelIndex &idx);
102     /**
103      * Run the action @p actionNumber at given model index @p idx
104      */
105     Q_INVOKABLE bool runAction(const QModelIndex &idx, int actionNumber);
106 
107     /**
108      * Get mime data for the result at given model index @p idx
109      */
110     Q_INVOKABLE QMimeData *getMimeData(const QModelIndex &idx) const;
111 
112     Plasma::RunnerManager *runnerManager() const;
113 
114 Q_SIGNALS:
115     /**
116      * This signal is emitted when a an InformationalMatch is run, and it is advised
117      * to update the search term, e.g. used for calculator runner results
118      */
119     void queryStringChangeRequested(const QString &queryString, int pos);
120 
121 private:
122     class Private;
123     QScopedPointer<Private> d;
124 };
125 
126 } // namespace Milou
127