diff --git a/runners/characters/charrunner.h b/runners/characters/charrunner.h --- a/runners/characters/charrunner.h +++ b/runners/characters/charrunner.h @@ -1,4 +1,5 @@ /* Copyright 2010 Anton Kreuzkamp + * Copyright 2020 Alexander Lohnau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -30,9 +31,10 @@ CharacterRunner(QObject* parent, const QVariantList &args); ~CharacterRunner() override; - void match(Plasma::RunnerContext &context) override; void reloadConfiguration() override; - + void match(Plasma::RunnerContext &context) override; + void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) override; + private: //config-variables QString m_triggerWord; diff --git a/runners/characters/charrunner.cpp b/runners/characters/charrunner.cpp --- a/runners/characters/charrunner.cpp +++ b/runners/characters/charrunner.cpp @@ -1,4 +1,5 @@ /* Copyright 2010 Anton Kreuzkamp + * Copyright 2020 Alexander Lohnau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,83 +19,80 @@ */ #include "charrunner.h" +#include "config_keys.h" // KF #include #include +// Qt +#include +#include +#include - -//Names of config-entries -static const char CONFIG_TRIGGERWORD[] = "triggerWord"; -static const char CONFIG_ALIASES[] = "aliases"; -static const char CONFIG_CODES[] = "codes"; - - -CharacterRunner::CharacterRunner( QObject* parent, const QVariantList &args ) +CharacterRunner::CharacterRunner(QObject *parent, const QVariantList &args) : Plasma::AbstractRunner(parent, args) { Q_UNUSED(args) - setObjectName(QLatin1String( "CharacterRunner" )); + setObjectName(QStringLiteral("CharacterRunner")); setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | - Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable | - Plasma::RunnerContext::ShellCommand); - reloadConfiguration(); + Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable | + Plasma::RunnerContext::ShellCommand); + addSyntax(Plasma::RunnerSyntax(m_triggerWord + QStringLiteral(":q:"), + i18n("Creates Characters from :q: if it is a hexadecimal code or defined alias."))); } CharacterRunner::~CharacterRunner() { } void CharacterRunner::reloadConfiguration() { - KConfigGroup grp = config(); //Create config-object - - m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, "#"); //read out the triggerword - m_aliases = grp.readEntry(CONFIG_ALIASES, QStringList()); - m_codes = grp.readEntry(CONFIG_CODES, QStringList()); - addSyntax(Plasma::RunnerSyntax(m_triggerWord + QLatin1String( ":q:" ), - i18n("Creates Characters from :q: if it is a hexadecimal code or defined alias."))); + const KConfigGroup &grp = config(); + m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD); + m_aliases = grp.readEntry(CONFIG_ALIASES, QStringList()); + m_codes = grp.readEntry(CONFIG_CODES, QStringList()); + if (m_codes.size() != m_aliases.size()) { + m_aliases.clear(); + m_codes.clear(); + qWarning() << "Config entries for alias list and code list have different sizes, ignoring all."; + } } void CharacterRunner::match(Plasma::RunnerContext &context) { - QString term = context.query(); + QString term = context.query().remove(QLatin1Char(' ')); - term = term.replace(QLatin1Char( ' ' ), QLatin1String( "" )); //remove blanks - if (term.length() < 2) //ignore too short queries - { + if (term.length() < 2 || !term.startsWith(m_triggerWord) || !context.isValid()) { return; } - if (!term.startsWith(m_triggerWord)) //ignore queries without the triggerword - { - return; - } + term = term.remove(0, m_triggerWord.length()); //remove the triggerword - if (m_aliases.contains(term)) //replace aliases by their hex.-code - { - term = m_codes[m_aliases.indexOf(term)]; + //replace aliases by their hex.-code + if (m_aliases.contains(term)) { + term = m_codes[m_aliases.indexOf(term)]; } - bool ok; //checkvariable + bool ok; int hex = term.toInt(&ok, 16); //convert query into int - if (!ok) //check if conversion was successful - { - return; + if (!ok) { + return; } //make special character out of the hex.-code const QString specChar = QChar(hex); - - //create match Plasma::QueryMatch match(this); - match.setType(Plasma::QueryMatch::InformationalMatch); + match.setType(Plasma::QueryMatch::ExactMatch); match.setIconName(QStringLiteral("accessories-character-map")); match.setText(specChar); match.setData(specChar); - match.setId(QString()); context.addMatch(match); } +void CharacterRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) +{ + Q_UNUSED(context) + QGuiApplication::clipboard()->setText(match.data().toString()); +} K_EXPORT_PLASMA_RUNNER(CharacterRunner, CharacterRunner) diff --git a/runners/characters/charrunner_config.h b/runners/characters/charrunner_config.h --- a/runners/characters/charrunner_config.h +++ b/runners/characters/charrunner_config.h @@ -1,4 +1,5 @@ /* Copyright 2010 Anton Kreuzkamp + * Copyright 2020 Alexander Lohnau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,8 +21,8 @@ #ifndef CHARRUNNERCONFIG_H #define CHARRUNNERCONFIG_H -//Project-Includes #include "ui_charrunner_config.h" + // KF #include @@ -49,6 +50,8 @@ private Q_SLOTS: void addItem(); void deleteItem(); + void validateAddButton(); + void validateDeleteButton(); private: CharacterRunnerConfigForm* m_ui; diff --git a/runners/characters/charrunner_config.cpp b/runners/characters/charrunner_config.cpp --- a/runners/characters/charrunner_config.cpp +++ b/runners/characters/charrunner_config.cpp @@ -1,4 +1,5 @@ /* Copyright 2010 Anton Kreuzkamp + * Copyright 2020 Alexander Lohnau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,117 +19,112 @@ */ #include "charrunner_config.h" +#include "config_keys.h" // KF #include #include -// Qt -#include +#include -//Names of config-entries -static const char CONFIG_TRIGGERWORD[] = "triggerWord"; -static const char CONFIG_ALIASES[] = "aliases"; -static const char CONFIG_CODES[] = "codes"; - -CharacterRunnerConfigForm::CharacterRunnerConfigForm(QWidget* parent) : QWidget(parent) +CharacterRunnerConfigForm::CharacterRunnerConfigForm(QWidget *parent) + : QWidget(parent) { setupUi(this); } -CharacterRunnerConfig::CharacterRunnerConfig(QWidget* parent, const QVariantList& args) +CharacterRunnerConfig::CharacterRunnerConfig(QWidget *parent, const QVariantList &args) : KCModule(parent, args) { m_ui = new CharacterRunnerConfigForm(this); - QGridLayout* layout = new QGridLayout(this); + QGridLayout *layout = new QGridLayout(this); layout->addWidget(m_ui, 0, 0); - connect(m_ui->edit_trigger, SIGNAL(textChanged(QString)), this, SLOT(changed())); - connect(m_ui->addItem, SIGNAL(clicked()), this, SLOT(addItem())); - connect(m_ui->deleteItem, SIGNAL(clicked()), this, SLOT(deleteItem())); -} - -void CharacterRunnerConfig::addItem() //add Item to the list-view widget -{ - QTreeWidgetItem* item = new QTreeWidgetItem(m_ui->list, 2); - item->setText(0, m_ui->edit_alias->text()); - item->setText(1, m_ui->edit_hex->text()); - m_ui->list->addTopLevelItem(item); - m_ui->edit_alias->clear(); - m_ui->edit_hex->clear(); - - emit changed(true); -} - -void CharacterRunnerConfig::deleteItem() //remove Item to the list-view widget -{ - m_ui->list->takeTopLevelItem(m_ui->list->indexOfTopLevelItem(m_ui->list->currentItem())); - - emit changed(true); + connect(m_ui->edit_trigger, &QLineEdit::textChanged, this, &CharacterRunnerConfig::markAsChanged); + connect(m_ui->addItem, &QPushButton::clicked, this, &CharacterRunnerConfig::addItem); + connect(m_ui->addItem, &QPushButton::clicked, this, &CharacterRunnerConfig::markAsChanged); + connect(m_ui->deleteItem, &QPushButton::clicked, this, &CharacterRunnerConfig::deleteItem); + connect(m_ui->deleteItem, &QPushButton::clicked, this, &CharacterRunnerConfig::markAsChanged); + connect(m_ui->list, &QTreeWidget::itemSelectionChanged, this, &CharacterRunnerConfig::validateDeleteButton); + connect(m_ui->edit_alias, &QLineEdit::textChanged, this, &CharacterRunnerConfig::validateAddButton); + connect(m_ui->edit_hex, &QLineEdit::textChanged, this, &CharacterRunnerConfig::validateAddButton); } void CharacterRunnerConfig::load() { KCModule::load(); - //create config-object - KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String( "krunnerrc" )); - KConfigGroup grp = cfg->group("Runners"); - grp = KConfigGroup(&grp, "CharacterRunner"); + KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); + KConfigGroup grp = cfg->group("Runners").group("CharacterRunner"); - m_ui->edit_trigger->setText(grp.readEntry(CONFIG_TRIGGERWORD, "#")); //read out triggerword and put into the trigger-lineEdit - const auto aliasList = grp.readEntry(CONFIG_ALIASES, QList()); - const auto codeList = grp.readEntry(CONFIG_CODES, QList()); + m_ui->edit_trigger->setText(grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD)); + const auto aliasList = grp.readEntry(CONFIG_ALIASES, QStringList()); + const auto codeList = grp.readEntry(CONFIG_CODES, QStringList()); if (aliasList.size() == codeList.size()) { - for (int i = 0; i < aliasList.size(); ++i) { //read out aliaslist and add Items to the list-view widget - QTreeWidgetItem* item = new QTreeWidgetItem(m_ui->list, 2); + for (int i = 0, size = aliasList.size(); i < size; ++i) { + QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->list, 2); item->setText(0, aliasList[i]); item->setText(1, codeList[i]); m_ui->list->addTopLevelItem(item); } } else { - qWarning() << "Config entries for alias list and code list have different sizes, ignoring all."; + const auto msg = new KMessageWidget( + QStringLiteral("Config entries for alias list and code list have different sizes, ignoring all."), this); + m_ui->verticalLayout->insertWidget(0, msg); } - emit changed(false); } void CharacterRunnerConfig::save() { KCModule::save(); - //create config-object - KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String( "krunnerrc" )); - KConfigGroup grp = cfg->group("Runners"); - grp = KConfigGroup(&grp, "CharacterRunner"); - - grp.writeEntry(CONFIG_TRIGGERWORD,m_ui->edit_trigger->text()); //write content from the triggerword-line Edit into the config - - //Write the content of the List into the config - QList aliaslist; - QList codelist; - for(int i=0; i < m_ui->list->topLevelItemCount(); i++) - { - QString blub = m_ui->list->topLevelItem(i)->text(0); - aliaslist.append(blub); - codelist.append(m_ui->list->topLevelItem(i)->text(1)); - } - grp.writeEntry(CONFIG_ALIASES, aliaslist); - grp.writeEntry(CONFIG_CODES, codelist); + KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); + KConfigGroup grp = cfg->group("Runners").group("CharacterRunner"); - emit changed(false); + grp.writeEntry(CONFIG_TRIGGERWORD, m_ui->edit_trigger->text().isEmpty() + ? DEFAULT_TRIGGERWORD : m_ui->edit_trigger->text()); + + QList aliasList; + QList codeList; + for (int i = 0, count = m_ui->list->topLevelItemCount(); i < count; ++i) { + aliasList.append(m_ui->list->topLevelItem(i)->text(0)); + codeList.append(m_ui->list->topLevelItem(i)->text(1)); + } + grp.writeEntry(CONFIG_ALIASES, aliasList); + grp.writeEntry(CONFIG_CODES, codeList); } void CharacterRunnerConfig::defaults() { KCModule::defaults(); - m_ui->edit_trigger->setText(QLatin1String( "#" )); //set the content of the triggerword-lineEdit to default '#' - for(int i=0; ilist->topLevelItemCount(); i++) //remove every item from the alias-list - { - m_ui->list->takeTopLevelItem(i); - } + m_ui->edit_trigger->setText(DEFAULT_TRIGGERWORD); + m_ui->list->clear(); + + emit markAsChanged(); +} + +void CharacterRunnerConfig::addItem() +{ + QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->list, 2); + item->setText(0, m_ui->edit_alias->text()); + item->setText(1, m_ui->edit_hex->text()); + m_ui->list->addTopLevelItem(item); + m_ui->edit_alias->clear(); + m_ui->edit_hex->clear(); +} - emit changed(true); +void CharacterRunnerConfig::deleteItem() +{ + m_ui->list->takeTopLevelItem(m_ui->list->indexOfTopLevelItem(m_ui->list->currentItem())); +} +void CharacterRunnerConfig::validateAddButton() +{ + m_ui->addItem->setDisabled(m_ui->edit_alias->text().isEmpty() || m_ui->edit_hex->text().isEmpty()); +} +void CharacterRunnerConfig::validateDeleteButton() +{ + m_ui->deleteItem->setDisabled(!m_ui->list->selectedItems().count()); } K_PLUGIN_FACTORY(CharacterRunnerConfigFactory, diff --git a/runners/characters/charrunner_config.ui b/runners/characters/charrunner_config.ui --- a/runners/characters/charrunner_config.ui +++ b/runners/characters/charrunner_config.ui @@ -103,13 +103,19 @@ Add Item + + false + Delete Item + + false + diff --git a/runners/characters/charrunner.h b/runners/characters/config_keys.h copy from runners/characters/charrunner.h copy to runners/characters/config_keys.h --- a/runners/characters/charrunner.h +++ b/runners/characters/config_keys.h @@ -1,4 +1,4 @@ -/* Copyright 2010 Anton Kreuzkamp +/* Copyright 2020 Alexander Lohnau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -17,27 +17,12 @@ * License along with this library. If not, see . */ -#ifndef CHARRUNNER_H -#define CHARRUNNER_H +#ifndef CONFIG_KEYS_H +#define CONFIG_KEYS_H -#include +static const char CONFIG_TRIGGERWORD[] = "triggerWord"; +static const char CONFIG_ALIASES[] = "aliases"; +static const char CONFIG_CODES[] = "codes"; +static const QString DEFAULT_TRIGGERWORD = QStringLiteral("#"); -class CharacterRunner : public Plasma::AbstractRunner -{ - Q_OBJECT - - public: - CharacterRunner(QObject* parent, const QVariantList &args); - ~CharacterRunner() override; - - void match(Plasma::RunnerContext &context) override; - void reloadConfiguration() override; - - private: - //config-variables - QString m_triggerWord; - QList m_aliases; - QList m_codes; -}; - -#endif +#endif //CONFIG_KEYS_H diff --git a/runners/spellchecker/spellcheck.h b/runners/spellchecker/spellcheck.h --- a/runners/spellchecker/spellcheck.h +++ b/runners/spellchecker/spellcheck.h @@ -50,13 +50,15 @@ void destroydata(); private: - QString findlang(const QStringList &terms); + QString findLang(const QStringList &terms); QString m_triggerWord; QMap m_languages;//key=language name, value=language code bool m_requireTriggerWord; QMap > m_spellers; //spellers QMutex m_spellLock; //Lock held when constructing a new speller + QString m_copyActionId; + QList m_actions; }; #endif diff --git a/runners/spellchecker/spellcheck.cpp b/runners/spellchecker/spellcheck.cpp --- a/runners/spellchecker/spellcheck.cpp +++ b/runners/spellchecker/spellcheck.cpp @@ -28,21 +28,17 @@ #include -namespace { -namespace ActionIds { -inline QString copyToClipboard() { return QStringLiteral("copyToClipboard"); } -} -} - SpellCheckRunner::SpellCheckRunner(QObject* parent, const QVariantList &args) : Plasma::AbstractRunner(parent, args) { Q_UNUSED(args) setObjectName(QLatin1String( "Spell Checker" )); setIgnoredTypes(Plasma::RunnerContext::FileSystem | Plasma::RunnerContext::NetworkLocation); setSpeed(AbstractRunner::SlowSpeed); - addAction(ActionIds::copyToClipboard(), QIcon::fromTheme(QStringLiteral("edit-copy")), i18nc("@action", "Copy to Clipboard")); + m_actions = {addAction(m_copyActionId, + QIcon::fromTheme(QStringLiteral("edit-copy")), + i18nc("@action", "Copy to Clipboard"))}; } SpellCheckRunner::~SpellCheckRunner() @@ -54,8 +50,8 @@ Plasma::AbstractRunner::init(); //Connect prepare and teardown signals - connect(this, SIGNAL(prepare()), this, SLOT(loaddata())); - connect(this, SIGNAL(teardown()), this, SLOT(destroydata())); + connect(this, &SpellCheckRunner::prepare, this, &SpellCheckRunner::loaddata); + connect(this, &SpellCheckRunner::teardown, this, &SpellCheckRunner::destroydata); } //Load a default dictionary and some locale names @@ -75,12 +71,12 @@ //name (eg. 'german') with one sub-code. QSet families; //First get the families - foreach (const QString &code, avail) { + for (const QString &code: avail) { families +=code.left(2); } //Now for each family figure out which is the main code. - foreach (const QString &fcode,families) { - QStringList family = avail.filter(fcode); + for (const QString &fcode: qAsConst(families)) { + const QStringList family = avail.filter(fcode); QString code; //If we only have one code, use it. //If a string is the default language, use it @@ -122,6 +118,7 @@ m_triggerWord = config().readEntry("trigger", i18n("spell")); //Processing will be triggered by "keyword " m_triggerWord += QLatin1Char( ' ' ); + m_copyActionId = QStringLiteral("copyToClipboard"); m_requireTriggerWord = config().readEntry("requireTriggerWord", true); @@ -133,18 +130,16 @@ s.addExampleQuery(QLatin1String( ":q:" )); } - QList syns; - syns << s; - setSyntaxes(syns); + setSyntaxes({s}); } /* Take the input query, split into a list, and see if it contains a language to spell in. * Return the empty string if we can't match a language. */ -QString SpellCheckRunner::findlang(const QStringList& terms) +QString SpellCheckRunner::findLang(const QStringList& terms) { - auto defaultSpeller = m_spellers[QString()]; + const auto &defaultSpeller = m_spellers[QString()]; //If first term is a language code (like en_GB), set it as the spell-check language - if (terms.count() >= 1 && defaultSpeller->availableLanguages().contains(terms[0])) { + if (!terms.isEmpty() && defaultSpeller->availableLanguages().contains(terms[0])) { return terms[0]; } //If we have two terms and the first is a language name (eg 'french'), @@ -201,7 +196,7 @@ if (speller->isValid()) { QStringList terms = query.split(QLatin1Char(' '), QString::SkipEmptyParts); - QString lang = findlang(terms); + const QString lang = findLang(terms); //If we found a language, create a new speller object using it. if (!lang.isEmpty()) { //First term is the language @@ -258,7 +253,7 @@ void SpellCheckRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) { Q_UNUSED(context) - if (match.selectedAction() == action(ActionIds::copyToClipboard())) { + if (match.selectedAction() == action(m_copyActionId)) { //Copy words to clipboard const QString text = match.data().toString(); QGuiApplication::clipboard()->setText(text); @@ -269,7 +264,7 @@ { Q_UNUSED(match) - return {action(ActionIds::copyToClipboard())}; + return m_actions; } QMimeData * SpellCheckRunner::mimeDataForMatch(const Plasma::QueryMatch &match) diff --git a/runners/spellchecker/spellcheck_config.cpp b/runners/spellchecker/spellcheck_config.cpp --- a/runners/spellchecker/spellcheck_config.cpp +++ b/runners/spellchecker/spellcheck_config.cpp @@ -43,27 +43,21 @@ layout->addWidget(m_ui, 0, 0); - connect(m_ui->m_requireTriggerWord, SIGNAL(stateChanged(int)), this, SLOT(changed())); - connect(m_ui->m_requireTriggerWord, SIGNAL(stateChanged(int)), this, SLOT(toggleTriggerWord(int))); - connect(m_ui->m_triggerWord, SIGNAL(textChanged(QString)), this, SLOT(changed())); - - m_ui->m_openKcmButton->setIcon(QIcon::fromTheme(QStringLiteral("tools-check-spelling"))); + connect(m_ui->m_requireTriggerWord, &QCheckBox::stateChanged, this, &SpellCheckConfig::markAsChanged); + connect(m_ui->m_requireTriggerWord, &QCheckBox::stateChanged, this, &SpellCheckConfig::toggleTriggerWord); + connect(m_ui->m_triggerWord, &QLineEdit::textChanged, this, &SpellCheckConfig::markAsChanged); connect(m_ui->m_openKcmButton, &QPushButton::clicked, this, &SpellCheckConfig::openKcm); - load(); + m_ui->m_openKcmButton->setIcon(QIcon::fromTheme(QStringLiteral("tools-check-spelling"))); } SpellCheckConfig::~SpellCheckConfig() { } void SpellCheckConfig::toggleTriggerWord(int state) { - if (state == Qt::Unchecked) { - m_ui->m_triggerWord->setEnabled(false); - } else { - m_ui->m_triggerWord->setEnabled(true); - } + m_ui->m_triggerWord->setEnabled(state == Qt::Checked); } void SpellCheckConfig::openKcm() @@ -76,29 +70,27 @@ KCModule::load(); //FIXME: This shouldn't be hardcoded! - KSharedConfig::Ptr cfg = KSharedConfig::openConfig( QLatin1String( "krunnerrc" ) ); - KConfigGroup conf = cfg->group( "Runners" ); - KConfigGroup grp = KConfigGroup( &conf, "Spell Checker"); + const KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); + const KConfigGroup grp = cfg->group("Runners").group("Spell Checker"); const bool requireTrigger = grp.readEntry("requireTriggerWord", true); const QString trigger = grp.readEntry("trigger", i18n("spell")); if (!requireTrigger) { m_ui->m_triggerWord->setEnabled(false); } - m_ui->m_requireTriggerWord->setCheckState( (requireTrigger) ? Qt::Checked : Qt::Unchecked ); - m_ui->m_triggerWord->setText( trigger ); + m_ui->m_requireTriggerWord->setCheckState((requireTrigger) ? Qt::Checked : Qt::Unchecked); + m_ui->m_triggerWord->setText(trigger); emit changed(false); } void SpellCheckConfig::save() { //FIXME: This shouldn't be hardcoded! - KSharedConfig::Ptr cfg = KSharedConfig::openConfig( QLatin1String( "krunnerrc" ) ); - KConfigGroup conf = cfg->group( "Runners" ); - KConfigGroup grp = KConfigGroup( &conf, "Spell Checker"); + KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc")); + KConfigGroup grp = cfg->group("Runners").group("Spell Checker"); bool requireTrigger = m_ui->m_requireTriggerWord->checkState() == Qt::Checked; if (requireTrigger) {