Changeset View
Changeset View
Standalone View
Standalone View
runners/spellchecker/spellcheck.cpp
Show All 12 Lines | |||||
13 | * You should have received a copy of the GNU Library General Public | 13 | * You should have received a copy of the GNU Library General Public | ||
14 | * License along with this program; if not, write to the | 14 | * License along with this program; if not, write to the | ||
15 | * Free Software Foundation, Inc., | 15 | * Free Software Foundation, Inc., | ||
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "spellcheck.h" | 19 | #include "spellcheck.h" | ||
20 | 20 | | |||
21 | #include <QGuiApplication> | ||||
21 | #include <QClipboard> | 22 | #include <QClipboard> | ||
22 | #include <QDebug> | 23 | #include <QDebug> | ||
23 | #include <QLocale> | 24 | #include <QLocale> | ||
24 | #include <QSet> | 25 | #include <QSet> | ||
26 | #include <QIcon> | ||||
27 | #include <QMimeData> | ||||
25 | 28 | | |||
26 | #include <KLocalizedString> | 29 | #include <KLocalizedString> | ||
27 | 30 | | |||
31 | namespace { | ||||
32 | namespace ActionIds { | ||||
33 | inline QString copyToClipboard() { return QStringLiteral("copyToClipboard"); } | ||||
34 | } | ||||
35 | } | ||||
36 | | ||||
28 | SpellCheckRunner::SpellCheckRunner(QObject* parent, const QVariantList &args) | 37 | SpellCheckRunner::SpellCheckRunner(QObject* parent, const QVariantList &args) | ||
29 | : Plasma::AbstractRunner(parent, args) | 38 | : Plasma::AbstractRunner(parent, args) | ||
30 | { | 39 | { | ||
31 | Q_UNUSED(args) | 40 | Q_UNUSED(args) | ||
32 | setObjectName(QLatin1String( "Spell Checker" )); | 41 | setObjectName(QLatin1String( "Spell Checker" )); | ||
33 | setIgnoredTypes(Plasma::RunnerContext::FileSystem | Plasma::RunnerContext::NetworkLocation); | 42 | setIgnoredTypes(Plasma::RunnerContext::FileSystem | Plasma::RunnerContext::NetworkLocation); | ||
34 | setSpeed(AbstractRunner::SlowSpeed); | 43 | setSpeed(AbstractRunner::SlowSpeed); | ||
44 | | ||||
45 | addAction(ActionIds::copyToClipboard(), QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy to Clipboard")); | ||||
35 | } | 46 | } | ||
36 | 47 | | |||
37 | SpellCheckRunner::~SpellCheckRunner() | 48 | SpellCheckRunner::~SpellCheckRunner() | ||
38 | { | 49 | { | ||
39 | } | 50 | } | ||
40 | 51 | | |||
41 | void SpellCheckRunner::init() | 52 | void SpellCheckRunner::init() | ||
42 | { | 53 | { | ||
▲ Show 20 Lines • Show All 165 Lines • ▼ Show 20 Line(s) | 206 | if (!lang.isEmpty()) { | |||
208 | query = terms.join(QLatin1String(" ")); | 219 | query = terms.join(QLatin1String(" ")); | ||
209 | } | 220 | } | ||
210 | } | 221 | } | ||
211 | 222 | | |||
212 | if (query.size() < 2) { | 223 | if (query.size() < 2) { | ||
213 | return; | 224 | return; | ||
214 | } | 225 | } | ||
215 | 226 | | |||
216 | Plasma::QueryMatch match(this); | | |||
217 | match.setType(Plasma::QueryMatch::InformationalMatch); | | |||
218 | | ||||
219 | if (speller->isValid()) { | 227 | if (speller->isValid()) { | ||
220 | QStringList suggestions; | 228 | QStringList suggestions; | ||
221 | const bool correct = speller->checkAndSuggest(query,suggestions); | 229 | const bool correct = speller->checkAndSuggest(query,suggestions); | ||
222 | if (correct) { | 230 | if (correct) { | ||
231 | Plasma::QueryMatch match(this); | ||||
232 | match.setType(Plasma::QueryMatch::InformationalMatch); | ||||
kossebau: Not sure if HelperMatch is the correct thing here. Similar runners use InformationalMatch here… | |||||
223 | match.setIconName(QStringLiteral("checkbox")); | 233 | match.setIconName(QStringLiteral("checkbox")); | ||
224 | match.setText(i18n("Correct")+QLatin1String(": ")+query); | 234 | match.setText(query); | ||
235 | match.setSubtext(i18nc("Term is spelled correctly", "Correct")); | ||||
broulik: Add some context, "Correct" could also be a verb | |||||
236 | match.setData(query); | ||||
237 | context.addMatch(match); | ||||
225 | } else { | 238 | } else { | ||
226 | match.setIconName(QStringLiteral("edit-delete")); | 239 | for (const auto& suggestion : qAsConst(suggestions)) { | ||
227 | const QString recommended = i18n("Suggested words: %1", suggestions.join(i18nc("separator for a list of words", ", "))); | 240 | Plasma::QueryMatch match(this); | ||
228 | //TODO: try setting a text and a subtext, with the subtext being the suggestions | 241 | match.setType(Plasma::QueryMatch::InformationalMatch); | ||
229 | match.setText(recommended); | 242 | match.setIconName(QStringLiteral("edit-rename")); | ||
230 | match.setData(suggestions); | 243 | match.setText(suggestion); | ||
244 | match.setSubtext(i18n("Suggested term")); | ||||
245 | match.setData(suggestion); | ||||
246 | context.addMatch(match); | ||||
247 | } | ||||
231 | } | 248 | } | ||
232 | } else { | 249 | } else { | ||
250 | Plasma::QueryMatch match(this); | ||||
251 | match.setType(Plasma::QueryMatch::InformationalMatch); | ||||
233 | match.setIconName(QStringLiteral("task-attention")); | 252 | match.setIconName(QStringLiteral("task-attention")); | ||
234 | match.setText(i18n("Could not find a dictionary.")); | 253 | match.setText(i18n("Could not find a dictionary.")); | ||
235 | } | | |||
236 | | ||||
237 | context.addMatch(match); | 254 | context.addMatch(match); | ||
238 | } | 255 | } | ||
256 | } | ||||
239 | 257 | | |||
240 | void SpellCheckRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | 258 | void SpellCheckRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | ||
241 | { | 259 | { | ||
242 | Q_UNUSED(context) | 260 | Q_UNUSED(context) | ||
261 | if (match.selectedAction() == action(ActionIds::copyToClipboard())) { | ||||
243 | //Copy words to clipboard | 262 | //Copy words to clipboard | ||
244 | // FIXME: | 263 | const QString text = match.data().toString(); | ||
245 | // kapp->clipboard()->setText(match.data().toString()); | 264 | QGuiApplication::clipboard()->setText(text); | ||
265 | } | ||||
266 | } | ||||
267 | | ||||
268 | QList<QAction *> SpellCheckRunner::actionsForMatch(const Plasma::QueryMatch &match) | ||||
269 | { | ||||
270 | Q_UNUSED(match) | ||||
271 | | ||||
272 | return {action(ActionIds::copyToClipboard())}; | ||||
273 | } | ||||
274 | | ||||
275 | QMimeData * SpellCheckRunner::mimeDataForMatch(const Plasma::QueryMatch &match) | ||||
276 | { | ||||
277 | QMimeData *result = new QMimeData(); | ||||
278 | const QString text = match.data().toString(); | ||||
279 | result->setText(text); | ||||
280 | return result; | ||||
246 | } | 281 | } | ||
247 | 282 | | |||
248 | K_EXPORT_PLASMA_RUNNER(krunner_spellcheck, SpellCheckRunner) | 283 | K_EXPORT_PLASMA_RUNNER(krunner_spellcheck, SpellCheckRunner) | ||
249 | 284 | | |||
250 | #include "spellcheck.moc" | 285 | #include "spellcheck.moc" |
Not sure if HelperMatch is the correct thing here. Similar runners use InformationalMatch here, so on selection the term is copied into the runner.
What is the modern krunner way to support the use-case of someone wanting to know the spelling of some word, and then copy the correct or suggested into the clipboard for further usage?