File indexing completed on 2024-04-28 15:29:43
0001 /* 0002 SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef PLASMA_QUERYMATCH_H 0008 #define PLASMA_QUERYMATCH_H 0009 0010 #include <QList> 0011 #include <QSharedDataPointer> 0012 #include <QUrl> 0013 0014 #include "krunner_export.h" 0015 0016 class QAction; 0017 class QIcon; 0018 class QString; 0019 class QVariant; 0020 class QWidget; 0021 0022 namespace Plasma 0023 { 0024 class RunnerContext; 0025 class AbstractRunner; 0026 class QueryMatchPrivate; 0027 0028 /** 0029 * @class QueryMatch querymatch.h <KRunner/QueryMatch> 0030 * 0031 * @short A match returned by an AbstractRunner in response to a given 0032 * RunnerContext. 0033 */ 0034 class KRUNNER_EXPORT QueryMatch 0035 { 0036 public: 0037 /** 0038 * The type of match. Value is important here as it is used for sorting 0039 */ 0040 enum Type { 0041 NoMatch = 0, /**< Null match */ 0042 CompletionMatch = 10, /**< Possible completion for the data of the query */ 0043 PossibleMatch = 30, /**< Something that may match the query */ 0044 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 99) 0045 /** 0046 * A purely informational, non-runnable match, such as the answer to a question or calculation. 0047 * The data of the match will be converted to a string and set in the search field. 0048 * @deprecated Since 5.99, call RunnerContext::requestQueryStringUpdate in AbstractRunner::run method instead 0049 */ 0050 InformationalMatch KRUNNER_ENUMERATOR_DEPRECATED_VERSION(5, 99, "Call RunnerContext::requestQueryStringUpdate in AbstractRunner::run method instead") = 0051 50, 0052 #endif 0053 /** 0054 * A match that represents an action not directly related 0055 * to activating the given search term, such as a search 0056 * in an external tool or a command learning trigger. Helper 0057 * matches tend to be generic to the query and should not 0058 * be autoactivated just because the user hits "Enter" 0059 * while typing. They must be explicitly selected to 0060 * be activated, but unlike InformationalMatch cause 0061 * an action to be triggered. 0062 */ 0063 HelperMatch = 70, 0064 ExactMatch = 100, /**< An exact match to the query */ 0065 }; 0066 0067 /** 0068 * Constructs a PossibleMatch associated with a given RunnerContext 0069 * and runner. 0070 * 0071 * @param runner the runner this match belongs to 0072 */ 0073 explicit QueryMatch(AbstractRunner *runner = nullptr); 0074 0075 /** 0076 * Copy constructor 0077 */ 0078 QueryMatch(const QueryMatch &other); 0079 0080 ~QueryMatch(); 0081 QueryMatch &operator=(const QueryMatch &other); 0082 bool operator==(const QueryMatch &other) const; 0083 bool operator!=(const QueryMatch &other) const; 0084 bool operator<(const QueryMatch &other) const; 0085 0086 /** 0087 * @return the runner associated with this action 0088 */ 0089 AbstractRunner *runner() const; 0090 0091 /** 0092 * Requests this match to activae using the given context 0093 * 0094 * @param context the context to use in conjunction with this run 0095 * 0096 * @sa AbstractRunner::run 0097 */ 0098 void run(const RunnerContext &context) const; 0099 0100 /** 0101 * @return true if the match is valid and can therefore be run, 0102 * an invalid match does not have an associated AbstractRunner 0103 */ 0104 bool isValid() const; 0105 0106 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 113) 0107 /** 0108 * Sets the type of match this action represents. 0109 */ 0110 KRUNNER_DEPRECATED_VERSION(5, 113, "Use setCategoryRelevance instead") 0111 void setType(Type type); 0112 #endif 0113 0114 /** 0115 * The type of action this is. Defaults to PossibleMatch. 0116 */ 0117 Type type() const; 0118 0119 /** 0120 * Helper for reading standardized category relevance values 0121 */ 0122 enum class CategoryRelevance { 0123 Lowest = 0, 0124 Low = 30, 0125 Moderate = 51, 0126 High = 70, 0127 Highest = 100, 0128 }; 0129 0130 /** 0131 * Relevance for matches in the category. The match with the highest relevance is respected for the entire category. 0132 * This value only affects the sorting of categories and not the sorting within the category. Use @ref setRelevance for this. 0133 * The value should be from 0 to 100. 0134 * 0135 * @since 5.113 0136 */ 0137 void setCategoryRelevance(CategoryRelevance relevance); 0138 0139 /** 0140 * Sets information about the type of the match which can 0141 * be used to categorize the match. 0142 * 0143 * This string should be translated as it can be displayed 0144 * in an UI 0145 */ 0146 void setMatchCategory(const QString &category); 0147 0148 /** 0149 * Extra information about the match which can be used 0150 * to categorize the type. 0151 * 0152 * By default this returns the internal name of the runner 0153 * which returned this result 0154 */ 0155 QString matchCategory() const; 0156 0157 /** 0158 * Sets the relevance of this action for the search 0159 * it was created for. 0160 * 0161 * @param relevance a number between 0 and 1. 0162 */ 0163 void setRelevance(qreal relevance); 0164 0165 /** 0166 * The relevance of this action to the search. By default, 0167 * the relevance is 1. 0168 * 0169 * @return a number between 0 and 1 0170 */ 0171 qreal relevance() const; 0172 0173 /** 0174 * Sets data to be used internally by the associated 0175 * AbstractRunner. 0176 * 0177 * When set, it is also used to form 0178 * part of the id() for this match. If that is inappropriate 0179 * as an id, the runner may generate its own id and set that 0180 * with setId(const QString&) directly after calling setData 0181 */ 0182 void setData(const QVariant &data); 0183 0184 /** 0185 * @return the data associated with this match; usually runner-specific 0186 */ 0187 QVariant data() const; 0188 0189 /** 0190 * Sets the id for this match; useful if the id does not 0191 * match data().toString(). The id must be unique to all 0192 * matches from this runner, and should remain constant 0193 * for the same query for best results. 0194 * 0195 * @param id the new identifying string to use to refer 0196 * to this entry 0197 */ 0198 void setId(const QString &id); 0199 0200 /** 0201 * @return a string that can be used as an ID for this match, 0202 * even between different queries. It is based in part 0203 * on the source of the match (the AbstractRunner) and 0204 * distinguishing information provided by the runner, 0205 * ensuring global uniqueness as well as consistency 0206 * between query matches. 0207 */ 0208 QString id() const; 0209 0210 /** 0211 * Sets the main title text for this match; should be short 0212 * enough to fit nicely on one line in a user interface 0213 * 0214 * @param text the text to use as the title 0215 */ 0216 void setText(const QString &text); 0217 0218 /** 0219 * @return the title text for this match 0220 */ 0221 QString text() const; 0222 0223 /** 0224 * Sets the descriptive text for this match; can be longer 0225 * than the main title text 0226 * 0227 * @param text the text to use as the description 0228 */ 0229 void setSubtext(const QString &text); 0230 0231 /** 0232 * @return the descriptive text for this match 0233 */ 0234 QString subtext() const; 0235 0236 /** 0237 * Sets the icon associated with this match 0238 * 0239 * Prefer using setIconName. 0240 * 0241 * @param icon the icon to show along with the match 0242 */ 0243 void setIcon(const QIcon &icon); 0244 0245 /** 0246 * @return the icon for this match 0247 */ 0248 QIcon icon() const; 0249 0250 /** 0251 * Sets the icon name associated with this match 0252 * 0253 * @param icon the name of the icon to show along with the match 0254 * @since 5.24 0255 */ 0256 void setIconName(const QString &iconName); 0257 0258 /** 0259 * @return the name of the icon for this match 0260 * @since 5.24 0261 */ 0262 QString iconName() const; 0263 0264 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 82) 0265 /** 0266 * Sets the MimeType, if any, associated with this match. 0267 * This overrides the MimeType provided by QueryContext, and should only be 0268 * set when it is different from the QueryContext MimeType 0269 * @deprecated Since 5.82, deprecated for lack of usage 0270 */ 0271 KRUNNER_DEPRECATED_VERSION(5, 82, "deprecated for lack of usage") 0272 void setMimeType(const QString &mimeType); 0273 0274 /** 0275 * @return the mimtype for this match, or QString() is none 0276 * @deprecated Since 5.82, deprecated for lack of usage 0277 */ 0278 KRUNNER_DEPRECATED_VERSION(5, 82, "deprecated for lack of usage") 0279 QString mimeType() const; 0280 #endif 0281 0282 /** 0283 * Sets the urls, if any, associated with this match 0284 */ 0285 void setUrls(const QList<QUrl> &urls); 0286 0287 /** 0288 * @return the mimtype for this match, or QString() is none 0289 */ 0290 QList<QUrl> urls() const; 0291 0292 /** 0293 * Sets whether or not this match can be activited 0294 * 0295 * @param enable true if the match is enabled and therefore runnable 0296 */ 0297 void setEnabled(bool enable); 0298 0299 /** 0300 * @return true if the match is enabled and therefore runnable, otherwise false 0301 */ 0302 bool isEnabled() const; 0303 0304 /** 0305 * Set the actions for this match. 0306 * This method allows you to set the actions inside of the AbstractRunner::match() method 0307 * and the default implementation of AbstractRunner::actionsForMatch() will return these. 0308 * @since 5.75 0309 */ 0310 void setActions(const QList<QAction *> &actions); 0311 0312 /** 0313 * Adds an action to this match 0314 * @since 5.75 0315 * @see setActions 0316 */ 0317 void addAction(QAction *actions); 0318 0319 /** 0320 * List of actions set for this match 0321 * @return actions 0322 * @since 5.75 0323 */ 0324 QList<QAction *> actions() const; 0325 0326 /** 0327 * The current action. 0328 */ 0329 QAction *selectedAction() const; 0330 0331 /** 0332 * Sets the selected action 0333 */ 0334 void setSelectedAction(QAction *action); 0335 0336 /** 0337 * Set if the text should be displayed as a multiLine string 0338 * @param multiLine 0339 * @since 5.82 0340 */ 0341 void setMultiLine(bool multiLine); 0342 0343 /** 0344 * If the text should be displayed as a multiLine string 0345 * If no explicit value is set set using setMultiline it will default to false 0346 * @return bool 0347 * @since 5.82 0348 */ 0349 bool isMultiLine() const; 0350 0351 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 71) 0352 /** 0353 * @return true if this match can be configured before being run 0354 * @since 4.3 0355 * @deprecated Since 5.0, this feature has been defunct 0356 */ 0357 KRUNNER_DEPRECATED_VERSION_BELATED(5, 71, 5, 0, "No longer use, feature removed") 0358 bool hasConfigurationInterface() const; 0359 #endif 0360 0361 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 71) 0362 /** 0363 * If hasConfigurationInterface() returns true, this method may be called to get 0364 * a widget displaying the options the user can interact with to modify 0365 * the behaviour of what happens when the match is run. 0366 * 0367 * @param widget the parent of the options widgets. 0368 * @since 4.3 0369 * @deprecated Since 5.0, this feature has been defunct 0370 */ 0371 KRUNNER_DEPRECATED_VERSION_BELATED(5, 71, 5, 0, "No longer use, feature removed") 0372 void createConfigurationInterface(QWidget *parent); 0373 #endif 0374 0375 private: 0376 QSharedDataPointer<QueryMatchPrivate> d; 0377 }; 0378 0379 } 0380 0381 #if !KRUNNER_ENABLE_DEPRECATED_SINCE(5, 91) 0382 namespace KRunner 0383 { 0384 using QueryMatch = Plasma::QueryMatch; 0385 using AbstractRunner = Plasma::AbstractRunner; 0386 } 0387 #endif 0388 0389 #endif