1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
/*
* SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "klinespellchecking.h"
#include <QContextMenuEvent>
#include <QMenu>
#include <KActionCollection>
#include <KStandardActions>
#include <QAction>
#include <sonnet/backgroundchecker.h>
#include <sonnet/dialog.h>
KLineSpellChecking::KLineSpellChecking(QWidget *parent)
: KLineEdit(parent)
{
KActionCollection *ac = new KActionCollection(this);
m_spellAction = KStandardActions::spelling(this, &KLineSpellChecking::slotCheckSpelling, ac);
}
KLineSpellChecking::~KLineSpellChecking()
{
}
void KLineSpellChecking::slotCheckSpelling()
{
if (text().isEmpty()) {
return;
}
Sonnet::Dialog *spellDialog = new Sonnet::Dialog(new Sonnet::BackgroundChecker(this), nullptr);
connect(spellDialog, &Sonnet::Dialog::replace, this, &KLineSpellChecking::spellCheckerCorrected);
connect(spellDialog, &Sonnet::Dialog::misspelling, this, &KLineSpellChecking::spellCheckerMisspelling);
connect(spellDialog, SIGNAL(done(QString)), this, SLOT(slotSpellCheckDone(QString)));
connect(spellDialog, &Sonnet::Dialog::cancel, this, &KLineSpellChecking::spellCheckerFinished);
connect(spellDialog, &Sonnet::Dialog::stop, this, &KLineSpellChecking::spellCheckerFinished);
spellDialog->setBuffer(text());
spellDialog->show();
}
void KLineSpellChecking::spellCheckerMisspelling(const QString &_text, int pos)
{
highLightWord(_text.length(), pos);
}
void KLineSpellChecking::highLightWord(int length, int pos)
{
setSelection(pos, length);
}
void KLineSpellChecking::spellCheckerCorrected(const QString &old, int pos, const QString &corr)
{
if (old != corr) {
setSelection(pos, old.length());
insert(corr);
setSelection(pos, corr.length());
}
}
void KLineSpellChecking::spellCheckerFinished()
{
}
void KLineSpellChecking::slotSpellCheckDone(const QString &s)
{
if (s != text()) {
setText(s);
}
}
void KLineSpellChecking::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *popup = createStandardContextMenu();
if (!popup) {
return;
}
if (echoMode() == QLineEdit::Normal && !isReadOnly()) {
popup->addSeparator();
popup->addAction(m_spellAction);
m_spellAction->setEnabled(!text().isEmpty());
}
popup->exec(e->globalPos());
delete popup;
}
|