File indexing completed on 2024-12-01 13:38:50
0001 /* 0002 * This file is part of the KDE Milou Project 0003 * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 * 0007 */ 0008 0009 #ifndef SOURCESMODEL_H 0010 #define SOURCESMODEL_H 0011 0012 #include <QAbstractListModel> 0013 #include <QTimer> 0014 0015 #include <KRunner/QueryMatch> 0016 #include <KRunner/RunnerManager> 0017 0018 #include "milou_export.h" 0019 0020 namespace Milou 0021 { 0022 class MILOU_EXPORT SourcesModel : public QAbstractListModel 0023 { 0024 Q_OBJECT 0025 Q_PROPERTY(QString queryString READ queryString WRITE setQueryString) 0026 Q_PROPERTY(int queryLimit READ queryLimit WRITE setQueryLimit) 0027 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 81) 0028 Q_PROPERTY(QString runner READ runner WRITE setRunner NOTIFY runnerChanged) 0029 Q_PROPERTY(QString runnerName READ runnerName NOTIFY runnerChanged) 0030 Q_PROPERTY(QIcon runnerIcon READ runnerIcon NOTIFY runnerChanged) 0031 #endif 0032 0033 public: 0034 explicit SourcesModel(QObject *parent = nullptr); 0035 ~SourcesModel() override; 0036 0037 enum Roles { 0038 TypeRole = Qt::UserRole + 1, 0039 SubtextRole, 0040 ActionsRole, 0041 DuplicateRole, 0042 PreviewTypeRole, 0043 PreviewUrlRole, 0044 PreviewLabelRole, 0045 }; 0046 0047 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0048 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0049 bool hasChildren(const QModelIndex &) const override 0050 { 0051 return false; 0052 } 0053 0054 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 81) 0055 QString runner() const; 0056 void setRunner(const QString &runner); 0057 0058 QString runnerName() const; 0059 QIcon runnerIcon() const; 0060 #endif 0061 0062 QString queryString() const; 0063 int queryLimit() const; 0064 0065 QHash<int, QByteArray> roleNames() const override; 0066 0067 Q_SIGNALS: 0068 /** 0069 * This signal is emitted when a an InformationalMatch is run, and it is advised 0070 * to update the search term. 0071 * Eg - Calculator runner 0072 */ 0073 void updateSearchTerm(const QString &text, int pos); 0074 0075 void runnerChanged(); 0076 0077 public Q_SLOTS: 0078 void reloadConfiguration(); 0079 void setQueryString(const QString &str); 0080 void setQueryLimit(int limit); 0081 /** 0082 * Clears the model content and resets the runner context, i.e. no new items will appear. 0083 */ 0084 void clear(); 0085 0086 bool run(int index); 0087 bool runAction(int index, int actionIndex); 0088 0089 Q_INVOKABLE QString getType(int index) const 0090 { 0091 return data(createIndex(index, 0), TypeRole).toString(); 0092 } 0093 0094 Q_INVOKABLE QMimeData *getMimeData(int index) const; 0095 0096 private Q_SLOTS: 0097 void slotMatchesChanged(const QList<Plasma::QueryMatch> &list); 0098 void slotMatchAdded(const Plasma::QueryMatch &match); 0099 void slotResetTimeout(); 0100 void slotSettingsFileChanged(const QString &path); 0101 0102 public: 0103 // A list of all the types that are being shown 0104 QList<QString> m_types; 0105 0106 struct TypeData { 0107 QList<Plasma::QueryMatch> shown; 0108 QList<Plasma::QueryMatch> hidden; 0109 }; 0110 QHash<QString, TypeData> m_matches; 0111 int m_size; 0112 0113 /// Counts the number of results for each visible Plasma::QueryMatch::text 0114 /// We use this to show additional info when there are multiple visible 0115 /// results with the same text 0116 QHash<QString, int> m_duplicates; 0117 0118 QString m_queryString; 0119 int m_queryLimit; 0120 QString m_runner; 0121 0122 Plasma::RunnerManager *m_manager; 0123 bool m_modelPopulated; 0124 QTimer m_resetTimer; 0125 0126 Plasma::QueryMatch fetchMatch(int row) const; 0127 }; 0128 0129 } 0130 0131 #endif // SOURCESMODEL_H