File indexing completed on 2023-12-03 09:01:43
0001 /* 0002 * SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 * 0006 */ 0007 #include "klinespellchecking.h" 0008 0009 #include <QContextMenuEvent> 0010 #include <QMenu> 0011 0012 #include <KActionCollection> 0013 #include <KStandardAction> 0014 #include <QAction> 0015 #include <sonnet/backgroundchecker.h> 0016 #include <sonnet/dialog.h> 0017 0018 KLineSpellChecking::KLineSpellChecking(QWidget *parent) 0019 : KLineEdit(parent) 0020 { 0021 KActionCollection *ac = new KActionCollection(this); 0022 m_spellAction = KStandardAction::spelling(this, SLOT(slotCheckSpelling()), ac); 0023 } 0024 0025 KLineSpellChecking::~KLineSpellChecking() 0026 { 0027 } 0028 0029 void KLineSpellChecking::slotCheckSpelling() 0030 { 0031 if (text().isEmpty()) { 0032 return; 0033 } 0034 Sonnet::Dialog *spellDialog = new Sonnet::Dialog(new Sonnet::BackgroundChecker(this), nullptr); 0035 connect(spellDialog, &Sonnet::Dialog::replace, this, &KLineSpellChecking::spellCheckerCorrected); 0036 connect(spellDialog, &Sonnet::Dialog::misspelling, this, &KLineSpellChecking::spellCheckerMisspelling); 0037 connect(spellDialog, SIGNAL(done(QString)), this, SLOT(slotSpellCheckDone(QString))); 0038 connect(spellDialog, &Sonnet::Dialog::cancel, this, &KLineSpellChecking::spellCheckerFinished); 0039 connect(spellDialog, &Sonnet::Dialog::stop, this, &KLineSpellChecking::spellCheckerFinished); 0040 spellDialog->setBuffer(text()); 0041 spellDialog->show(); 0042 } 0043 0044 void KLineSpellChecking::spellCheckerMisspelling(const QString &_text, int pos) 0045 { 0046 highLightWord(_text.length(), pos); 0047 } 0048 0049 void KLineSpellChecking::highLightWord(int length, int pos) 0050 { 0051 setSelection(pos, length); 0052 } 0053 0054 void KLineSpellChecking::spellCheckerCorrected(const QString &old, int pos, const QString &corr) 0055 { 0056 if (old != corr) { 0057 setSelection(pos, old.length()); 0058 insert(corr); 0059 setSelection(pos, corr.length()); 0060 } 0061 } 0062 0063 void KLineSpellChecking::spellCheckerFinished() 0064 { 0065 } 0066 0067 void KLineSpellChecking::slotSpellCheckDone(const QString &s) 0068 { 0069 if (s != text()) { 0070 setText(s); 0071 } 0072 } 0073 0074 void KLineSpellChecking::contextMenuEvent(QContextMenuEvent *e) 0075 { 0076 QMenu *popup = createStandardContextMenu(); 0077 0078 if (!popup) { 0079 return; 0080 } 0081 0082 if (echoMode() == QLineEdit::Normal && !isReadOnly()) { 0083 popup->addSeparator(); 0084 0085 popup->addAction(m_spellAction); 0086 m_spellAction->setEnabled(!text().isEmpty()); 0087 } 0088 popup->exec(e->globalPos()); 0089 delete popup; 0090 }