KRunner

querymatch.cpp
1 /*
2  SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "querymatch.h"
8 
9 #include <QAction>
10 #include <QIcon>
11 #include <QPointer>
12 #include <QReadWriteLock>
13 #include <QSharedData>
14 #include <QVariant>
15 
16 #include "krunner_debug.h"
17 
18 #include "abstractrunner.h"
19 
20 namespace Plasma
21 {
22 class QueryMatchPrivate : public QSharedData
23 {
24 public:
25  QueryMatchPrivate(AbstractRunner *r)
26  : QSharedData()
27  , runner(r)
28  {
29  }
30 
31  QueryMatchPrivate(const QueryMatchPrivate &other)
32  : QSharedData(other)
33  {
34  QReadLocker l(other.lock);
35  runner = other.runner;
36  type = other.type;
37  relevance = other.relevance;
38  selAction = other.selAction;
39  enabled = other.enabled;
40  idSetByData = other.idSetByData;
41  matchCategory = other.matchCategory;
42  id = other.id;
43  text = other.text;
44  subtext = other.subtext;
45  icon = other.icon;
46  iconName = other.iconName;
47  data = other.data;
48 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 82)
49  mimeType = other.mimeType;
50 #endif
51  urls = other.urls;
52  actions = other.actions;
53  multiLine = other.multiLine;
54  }
55 
56  ~QueryMatchPrivate()
57  {
58  delete lock;
59  }
60 
64  QString matchCategory;
65  QString id;
66  QString text;
67  QString subtext;
69  QList<QUrl> urls;
70  QIcon icon;
71  QString iconName;
72  QVariant data;
73  qreal relevance = .7;
74  QAction *selAction = nullptr;
75  bool enabled = true;
76  bool idSetByData = false;
77  QList<QAction *> actions;
78  bool multiLine = false;
79 };
80 
82  : d(new QueryMatchPrivate(runner))
83 {
84 }
85 
87  : d(other.d)
88 {
89 }
90 
91 QueryMatch::~QueryMatch()
92 {
93 }
94 
95 bool QueryMatch::isValid() const
96 {
97  return d->runner != nullptr;
98 }
99 
101 {
102  if (d->id.isEmpty() && d->runner) {
103  return d->runner.data()->id();
104  }
105 
106  return d->id;
107 }
108 
109 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 113)
111 {
112  d->type = type;
113 }
114 #endif
115 
117 {
118  return d->type;
119 }
120 
122 {
123  d->matchCategory = category;
124 }
125 
127 {
128  if (d->matchCategory.isEmpty() && d->runner) {
129  return d->runner->name();
130  }
131  return d->matchCategory;
132 }
133 
135 {
136  setType((Type) static_cast<int>(relevance));
137 }
138 
139 void QueryMatch::setRelevance(qreal relevance)
140 {
141  d->relevance = qMax(qreal(0.0), relevance);
142 }
143 
145 {
146  return d->relevance;
147 }
148 
150 {
151  return d->runner.data();
152 }
153 
154 void QueryMatch::setText(const QString &text)
155 {
156  QWriteLocker locker(d->lock);
157  d->text = text;
158 }
159 
160 void QueryMatch::setSubtext(const QString &subtext)
161 {
162  QWriteLocker locker(d->lock);
163  d->subtext = subtext;
164 }
165 
166 void QueryMatch::setData(const QVariant &data)
167 {
168  QWriteLocker locker(d->lock);
169  d->data = data;
170 
171  if (d->id.isEmpty() || d->idSetByData) {
172  const QString matchId = data.toString();
173  if (!matchId.isEmpty()) {
174  setId(matchId);
175  d->idSetByData = true;
176  }
177  }
178 }
179 
180 void QueryMatch::setId(const QString &id)
181 {
182  QWriteLocker locker(d->lock);
183  if (d->runner && d->runner->hasUniqueResults()) {
184  d->id = id;
185  } else {
186  if (d->runner) {
187  d->id = d->runner.data()->id();
188  }
189  if (!id.isEmpty()) {
190  d->id.append(QLatin1Char('_')).append(id);
191  }
192  }
193  d->idSetByData = false;
194 }
195 
196 void QueryMatch::setIcon(const QIcon &icon)
197 {
198  QWriteLocker locker(d->lock);
199  d->icon = icon;
200 }
201 
202 void QueryMatch::setIconName(const QString &iconName)
203 {
204  QWriteLocker locker(d->lock);
205  d->iconName = iconName;
206 }
207 
209 {
210  QReadLocker locker(d->lock);
211  return d->data;
212 }
213 
215 {
216  QReadLocker locker(d->lock);
217  return d->text;
218 }
219 
221 {
222  QReadLocker locker(d->lock);
223  return d->subtext;
224 }
225 
227 {
228  QReadLocker locker(d->lock);
229  return d->icon;
230 }
231 
233 {
234  QReadLocker locker(d->lock);
235  return d->iconName;
236 }
237 
238 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 82)
239 void QueryMatch::setMimeType(const QString &mimeType)
240 {
241  QWriteLocker locker(d->lock);
242  d->mimeType = mimeType;
243 }
244 
246 {
247  QReadLocker locker(d->lock);
248  return d->mimeType;
249 }
250 #endif
251 
253 {
254  QWriteLocker locker(d->lock);
255  d->urls = urls;
256 }
257 
259 {
260  QReadLocker locker(d->lock);
261  return d->urls;
262 }
263 
264 void QueryMatch::setEnabled(bool enabled)
265 {
266  d->enabled = enabled;
267 }
268 
270 {
271  return d->enabled && d->runner;
272 }
273 
275 {
276  return d->selAction;
277 }
278 
280 {
281  d->selAction = action;
282 }
283 
284 bool QueryMatch::operator<(const QueryMatch &other) const
285 {
286  if (d->type == other.d->type) {
287  if (isEnabled() != other.isEnabled()) {
288  return other.isEnabled();
289  }
290 
291  if (!qFuzzyCompare(d->relevance, other.d->relevance)) {
292  return d->relevance < other.d->relevance;
293  }
294 
295  QReadLocker locker(d->lock);
296  QReadLocker otherLocker(other.d->lock);
297  // when resorting to sort by alpha, we want the
298  // reverse sort order!
299  return d->text > other.d->text;
300  }
301 
302  return d->type < other.d->type;
303 }
304 
305 void QueryMatch::setMultiLine(bool multiLine)
306 {
307  d->multiLine = multiLine;
308 }
309 
311 {
312  return d->multiLine;
313 }
314 
315 QueryMatch &QueryMatch::operator=(const QueryMatch &other)
316 {
317  if (d != other.d) {
318  d = other.d;
319  }
320 
321  return *this;
322 }
323 
324 bool QueryMatch::operator==(const QueryMatch &other) const
325 {
326  return (d == other.d);
327 }
328 
329 bool QueryMatch::operator!=(const QueryMatch &other) const
330 {
331  return (d != other.d);
332 }
333 
334 void QueryMatch::run(const RunnerContext &context) const
335 {
336  if (d->runner) {
337  d->runner.data()->run(context, *this);
338  }
339 }
340 
341 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 71)
343 {
344  return false;
345 }
346 #endif
347 
348 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 71)
350 {
351  Q_UNUSED(parent)
352 }
353 #endif
354 
356 {
357  QWriteLocker locker(d->lock);
358  d->actions = actions;
359 }
360 
362 {
363  QWriteLocker locker(d->lock);
364  d->actions << action;
365 }
366 
368 {
369  QReadLocker locker(d->lock);
370  return d->actions;
371 }
372 
373 } // Plasma namespace
bool isValid() const
Definition: querymatch.cpp:95
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...
Definition: querymatch.cpp:154
AbstractRunner * runner() const
Definition: querymatch.cpp:149
void setIconName(const QString &iconName)
Sets the icon name associated with this match.
Definition: querymatch.cpp:202
Type type(const QSqlDatabase &db)
A match returned by an AbstractRunner in response to a given RunnerContext.
Definition: querymatch.h:34
QString mimeType() const
Definition: querymatch.cpp:245
KCALUTILS_EXPORT QString mimeType()
An abstract base class for Plasma Runner plugins.
void setType(Type type)
Sets the type of match this action represents.
Definition: querymatch.cpp:110
bool hasConfigurationInterface() const
Definition: querymatch.cpp:342
QList< QAction * > actions() const
List of actions set for this match.
Definition: querymatch.cpp:367
void setMultiLine(bool multiLine)
Set if the text should be displayed as a multiLine string.
Definition: querymatch.cpp:305
void addAction(QAction *actions)
Adds an action to this match.
Definition: querymatch.cpp:361
void setRelevance(qreal relevance)
Sets the relevance of this action for the search it was created for.
Definition: querymatch.cpp:139
bool isMultiLine() const
If the text should be displayed as a multiLine string If no explicit value is set set using setMultil...
Definition: querymatch.cpp:310
Type type() const
The type of action this is.
Definition: querymatch.cpp:116
QIcon icon() const
Definition: querymatch.cpp:226
QString iconName() const
Definition: querymatch.cpp:232
void run(const RunnerContext &context) const
Requests this match to activae using the given context.
Definition: querymatch.cpp:334
@ ExactMatch
An exact match to the query.
Definition: querymatch.h:64
bool isEmpty() const const
void setEnabled(bool enable)
Sets whether or not this match can be activited.
Definition: querymatch.cpp:264
QString id() const
Definition: querymatch.cpp:100
void setActions(const QList< QAction * > &actions)
Set the actions for this match.
Definition: querymatch.cpp:355
QVariant data() const
Definition: querymatch.cpp:208
QString matchCategory() const
Extra information about the match which can be used to categorize the type.
Definition: querymatch.cpp:126
void setMatchCategory(const QString &category)
Sets information about the type of the match which can be used to categorize the match.
Definition: querymatch.cpp:121
void setSubtext(const QString &text)
Sets the descriptive text for this match; can be longer than the main title text.
Definition: querymatch.cpp:160
QAction * selectedAction() const
The current action.
Definition: querymatch.cpp:274
void setSelectedAction(QAction *action)
Sets the selected action.
Definition: querymatch.cpp:279
QueryMatch(AbstractRunner *runner=nullptr)
Constructs a PossibleMatch associated with a given RunnerContext and runner.
Definition: querymatch.cpp:81
void setUrls(const QList< QUrl > &urls)
Sets the urls, if any, associated with this match.
Definition: querymatch.cpp:252
qreal relevance() const
The relevance of this action to the search.
Definition: querymatch.cpp:144
void setMimeType(const QString &mimeType)
Sets the MimeType, if any, associated with this match.
Definition: querymatch.cpp:239
Type
The type of match.
Definition: querymatch.h:40
QString subtext() const
Definition: querymatch.cpp:220
QString text() const
Definition: querymatch.cpp:214
void setData(const QVariant &data)
Sets data to be used internally by the associated AbstractRunner.
Definition: querymatch.cpp:166
The RunnerContext class provides information related to a search, including the search term,...
Definition: runnercontext.h:31
void setCategoryRelevance(CategoryRelevance relevance)
Relevance for matches in the category.
Definition: querymatch.cpp:134
void setIcon(const QIcon &icon)
Sets the icon associated with this match.
Definition: querymatch.cpp:196
void createConfigurationInterface(QWidget *parent)
If hasConfigurationInterface() returns true, this method may be called to get a widget displaying the...
Definition: querymatch.cpp:349
QList< QUrl > urls() const
Definition: querymatch.cpp:258
CategoryRelevance
Helper for reading standardized category relevance values.
Definition: querymatch.h:122
bool isEnabled() const
Definition: querymatch.cpp:269
void setId(const QString &id)
Sets the id for this match; useful if the id does not match data().toString().
Definition: querymatch.cpp:180
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 2 2023 03:50:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.