KRunner

querymatch.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "querymatch.h"
8#include "action.h"
9
10#include <QIcon>
11#include <QPointer>
12#include <QReadWriteLock>
13#include <QSharedData>
14#include <QVariant>
15
16#include "abstractrunner.h"
17#include "abstractrunner_p.h"
18
19namespace KRunner
20{
21class QueryMatchPrivate : public QSharedData
22{
23public:
24 explicit QueryMatchPrivate(AbstractRunner *r)
25 : QSharedData()
26 , runner(r)
27 {
28 }
29
30 QueryMatchPrivate(const QueryMatchPrivate &other)
31 : QSharedData(other)
32 {
33 QReadLocker l(&other.lock);
34 runner = other.runner;
35 categoryRelevance = other.categoryRelevance;
36 relevance = other.relevance;
37 selAction = other.selAction;
38 enabled = other.enabled;
39 idSetByData = other.idSetByData;
40 matchCategory = other.matchCategory;
41 id = other.id;
42 text = other.text;
43 subtext = other.subtext;
44 icon = other.icon;
45 iconName = other.iconName;
46 data = other.data;
47 urls = other.urls;
48 actions = other.actions;
49 multiLine = other.multiLine;
50 }
51
52 void setId(const QString &newId)
53 {
54 if (runner && runner->d->hasUniqueResults) {
55 id = newId;
56 } else {
57 if (runner) {
58 id = runner.data()->id();
59 }
60 if (!id.isEmpty()) {
61 id.append(QLatin1Char('_')).append(newId);
62 }
63 }
64 idSetByData = false;
65 }
66
67 mutable QReadWriteLock lock;
69 QString matchCategory;
70 QString id;
71 QString text;
72 QString subtext;
73 QString mimeType;
74 QList<QUrl> urls;
75 QIcon icon;
76 QString iconName;
77 QVariant data;
78 qreal categoryRelevance = 50;
79 qreal relevance = .7;
80 KRunner::Action selAction;
81 KRunner::Actions actions;
82 bool enabled = true;
83 bool idSetByData = false;
84 bool multiLine = false;
85};
86
88 : d(new QueryMatchPrivate(runner))
89{
90}
91
93 : d(other.d)
94{
95}
96
97QueryMatch::~QueryMatch() = default;
98
100{
101 return d->runner != nullptr;
102}
103
105{
106 if (d->id.isEmpty() && d->runner) {
107 return d->runner.data()->id();
108 }
109
110 return d->id;
111}
112
114{
115 d->categoryRelevance = qBound(0.0, relevance, 100.0);
116}
117
119{
120 return d->categoryRelevance;
121}
122
124{
125 d->matchCategory = category;
126}
127
129{
130 if (d->matchCategory.isEmpty() && d->runner) {
131 return d->runner->name();
132 }
133 return d->matchCategory;
134}
135
136void QueryMatch::setRelevance(qreal relevance)
137{
138 d->relevance = qMax(qreal(0.0), relevance);
139}
140
142{
143 return d->relevance;
144}
145
147{
148 return d->runner.data();
149}
150
152{
153 QWriteLocker locker(&d->lock);
154 d->text = text;
155}
156
157void QueryMatch::setSubtext(const QString &subtext)
158{
159 QWriteLocker locker(&d->lock);
160 d->subtext = subtext;
161}
162
164{
165 QWriteLocker locker(&d->lock);
166 d->data = data;
167
168 if (d->id.isEmpty() || d->idSetByData) {
169 const QString matchId = data.toString();
170 if (!matchId.isEmpty()) {
171 d->setId(matchId);
172 d->idSetByData = true;
173 }
174 }
175}
176
178{
179 QWriteLocker locker(&d->lock);
180 d->setId(id);
181}
182
183void QueryMatch::setIcon(const QIcon &icon)
184{
185 QWriteLocker locker(&d->lock);
186 d->icon = icon;
187}
188
189void QueryMatch::setIconName(const QString &iconName)
190{
191 QWriteLocker locker(&d->lock);
192 d->iconName = iconName;
193}
194
196{
197 QReadLocker locker(&d->lock);
198 return d->data;
199}
200
202{
203 QReadLocker locker(&d->lock);
204 return d->text;
205}
206
208{
209 QReadLocker locker(&d->lock);
210 return d->subtext;
211}
212
214{
215 QReadLocker locker(&d->lock);
216 return d->icon;
217}
218
220{
221 QReadLocker locker(&d->lock);
222 return d->iconName;
223}
224
226{
227 QWriteLocker locker(&d->lock);
228 d->urls = urls;
229}
230
232{
233 QReadLocker locker(&d->lock);
234 return d->urls;
235}
236
237void QueryMatch::setEnabled(bool enabled)
238{
239 d->enabled = enabled;
240}
241
243{
244 return d->enabled && d->runner;
245}
246
248{
249 return d->selAction;
250}
251
252void QueryMatch::setSelectedAction(const KRunner::Action &action)
253{
254 d->selAction = action;
255}
256
257void QueryMatch::setMultiLine(bool multiLine)
258{
259 d->multiLine = multiLine;
260}
261
263{
264 return d->multiLine;
265}
266
267QueryMatch &QueryMatch::operator=(const QueryMatch &other)
268{
269 if (d != other.d) {
270 d = other.d;
271 }
272
273 return *this;
274}
275
276bool QueryMatch::operator==(const QueryMatch &other) const
277{
278 return (d == other.d);
279}
280
281bool QueryMatch::operator!=(const QueryMatch &other) const
282{
283 return (d != other.d);
284}
285
287{
288 QWriteLocker locker(&d->lock);
289 d->actions = actions;
290}
291
293{
294 QWriteLocker locker(&d->lock);
295 d->actions << action;
296}
297
299{
300 QReadLocker locker(&d->lock);
301 return d->actions;
302}
303
304QDebug operator<<(QDebug debug, const KRunner::QueryMatch &match)
305{
306 QDebugStateSaver saver(debug);
307 debug.nospace() << "QueryMatch(category: " << match.matchCategory() << " text:" << match.text() << ")";
308 return debug;
309}
310
311} // KRunner namespace
An abstract base class for Plasma Runner plugins.
This class represents an action that will be shown next to a match.
Definition action.h:23
A match returned by an AbstractRunner in response to a given RunnerContext.
Definition querymatch.h:32
void setEnabled(bool enable)
Sets whether or not this match can be activited.
void setUrls(const QList< QUrl > &urls)
Sets the urls, if any, associated with this match.
bool isValid() const
QString id() const
QString text() const
void setText(const QString &text)
Sets the main title text for this match; should be short enough to fit nicely on one line in a user i...
bool isEnabled() const
void setActions(const QList< KRunner::Action > &actions)
Set the actions for this match.
void addAction(const KRunner::Action &action)
Adds an action to this match.
qreal relevance() const
The relevance of this action to the search.
void setRelevance(qreal relevance)
Sets the relevance of this action for the search it was created for.
void setId(const QString &id)
Sets the id for this match; useful if the id does not match data().toString().
qreal categoryRelevance() const
Category relevance for this match.
void setIcon(const QIcon &icon)
Sets the icon associated with this match.
QString subtext() const
void setData(const QVariant &data)
Sets data to be used internally by the runner's AbstractRunner::run implementation.
void setCategoryRelevance(CategoryRelevance relevance)
Relevance for matches in the category.
Definition querymatch.h:81
void setMatchCategory(const QString &category)
Sets information about the type of the match which is used to group the matches.
KRunner::Action selectedAction() const
The action that the user has selected when running the match.
bool isMultiLine() const
If the text should be displayed as a multiLine string If no explicit value is set set using setMultil...
QIcon icon() const
QList< QUrl > urls() const
void setSubtext(const QString &text)
Sets the descriptive text for this match; can be longer than the main title text.
void setIconName(const QString &iconName)
Sets the icon name associated with this match.
QList< KRunner::Action > actions() const
List of actions set for this match.
QString matchCategory() const
Extra information about the match which can be used to categorize the type.
QString iconName() const
AbstractRunner * runner() const
QVariant data() const
QueryMatch(AbstractRunner *runner=nullptr)
Constructs a PossibleMatch associated with a given RunnerContext and runner.
void setMultiLine(bool multiLine)
Set if the text should be displayed as a multiLine string.
QDebug & nospace()
bool isEmpty() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:55:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.