File indexing completed on 2024-03-24 17:08:40

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 <QAbstractItemModel>
0012 #include <QHash>
0013 #include <QString>
0014 
0015 #include <KRunner/QueryMatch>
0016 
0017 namespace Plasma
0018 {
0019 class RunnerManager;
0020 }
0021 
0022 namespace Milou
0023 {
0024 class RunnerResultsModel : public QAbstractItemModel
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     explicit RunnerResultsModel(QObject *parent = nullptr);
0030     ~RunnerResultsModel() override;
0031 
0032     QString queryString() const;
0033     void setQueryString(const QString &queryString, const QString &runner);
0034     Q_SIGNAL void queryStringChanged(const QString &queryString);
0035 
0036     bool querying() const;
0037     Q_SIGNAL void queryingChanged();
0038 
0039     /**
0040      * Clears the model content and resets the runner context, i.e. no new items will appear.
0041      */
0042     void clear();
0043 
0044     bool run(const QModelIndex &idx);
0045     bool runAction(const QModelIndex &idx, int actionNumber);
0046 
0047     int columnCount(const QModelIndex &parent) const override;
0048     int rowCount(const QModelIndex &parent) const override;
0049 
0050     QVariant data(const QModelIndex &index, int role) const override;
0051 
0052     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0053     QModelIndex parent(const QModelIndex &child) const override;
0054 
0055     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0056 
0057     Plasma::RunnerManager *runnerManager() const;
0058 
0059 Q_SIGNALS:
0060     void queryStringChangeRequested(const QString &queryString, int pos);
0061 
0062     void matchesChanged();
0063 
0064 private:
0065     void setQuerying(bool querying);
0066 
0067     Plasma::QueryMatch fetchMatch(const QModelIndex &idx) const;
0068 
0069     void onMatchesChanged(const QList<Plasma::QueryMatch> &matches);
0070 
0071     Plasma::RunnerManager *m_manager;
0072 
0073     QString m_queryString;
0074     bool m_querying = false;
0075 
0076     QString m_prevRunner;
0077 
0078     bool m_hasMatches = false;
0079 
0080     QStringList m_categories;
0081     QHash<QString /*category*/, QVector<Plasma::QueryMatch>> m_matches;
0082 };
0083 
0084 } // namespace Milou