8#include "spellcheckbar.h"
9#include "ui_spellcheckbar.h"
10#include <KLocalizedString>
12#include "sonnet/backgroundchecker.h"
13#include "sonnet/speller.h"
19#include <QProgressDialog>
22#include <QDialogButtonBox>
27#include <QStringListModel>
31#define NONSORTINGCOLUMN 2
59 Word(
const QString &w,
int st,
bool e =
false)
65 Word(
const Word &other)
72 Word &operator=(
const Word &) =
default;
79class SpellCheckBar::Private
83 ReadOnlyStringListModel *suggestionsModel;
85 QDialogButtonBox *buttonBox;
86 QProgressDialog *progressDialog;
87 QString originalBuffer;
88 Sonnet::BackgroundChecker *checker;
91 std::map<QString, QString> replaceAllMap;
94 QMap<QString, QString> dictsMap;
96 int progressDialogTimeout;
97 bool showCompletionMessageBox;
98 bool spellCheckContinuedAfterReplacement;
101 void deleteProgressDialog(
bool directly)
103 if (progressDialog) {
104 progressDialog->hide();
106 delete progressDialog;
108 progressDialog->deleteLater();
110 progressDialog =
nullptr;
116 : KateViewBarWidget(true, parent)
119 d->checker = checker;
122 d->showCompletionMessageBox =
false;
123 d->spellCheckContinuedAfterReplacement =
true;
124 d->progressDialogTimeout = -1;
125 d->progressDialog =
nullptr;
131SpellCheckBar::~SpellCheckBar()
136void SpellCheckBar::closed()
139 viewBar()->removeBarWidget(
this);
144 d->deleteProgressDialog(
false);
145 d->replaceAllMap.clear();
151void SpellCheckBar::initConnections()
171 d->ui.m_autoCorrect->hide();
174void SpellCheckBar::initGui()
176 QVBoxLayout *
layout =
new QVBoxLayout(centralWidget());
177 layout->setContentsMargins(0, 0, 0, 0);
180 d->ui.setupUi(d->wdg);
181 layout->addWidget(d->wdg);
182 setGuiEnabled(
false);
192 fillDictionaryComboBox();
195 d->suggestionsModel =
new ReadOnlyStringListModel(
this);
196 d->ui.cmbReplacement->setModel(d->suggestionsModel);
199void SpellCheckBar::activeAutoCorrect(
bool _active)
202 d->ui.m_autoCorrect->show();
204 d->ui.m_autoCorrect->hide();
210 d->progressDialogTimeout = timeout;
215 d->showCompletionMessageBox = b;
220 d->spellCheckContinuedAfterReplacement = b;
223void SpellCheckBar::slotAutocorrect()
225 setGuiEnabled(
false);
226 setProgressDialogVisible(
true);
227 Q_EMIT autoCorrect(d->currentWord.word, d->ui.cmbReplacement->lineEdit()->text());
231void SpellCheckBar::setGuiEnabled(
bool b)
233 d->wdg->setEnabled(b);
236void SpellCheckBar::setProgressDialogVisible(
bool b)
239 d->deleteProgressDialog(
true);
240 }
else if (d->progressDialogTimeout >= 0) {
241 if (d->progressDialog) {
244 d->progressDialog =
new QProgressDialog(
this);
245 d->progressDialog->setLabelText(
i18nc(
"@info:progress",
"Spell checking in progress…"));
246 d->progressDialog->setWindowTitle(
i18nc(
"@title:window",
"Check Spelling"));
247 d->progressDialog->setModal(
true);
248 d->progressDialog->setAutoClose(
false);
249 d->progressDialog->setAutoReset(
false);
252 d->progressDialog->reset();
253 d->progressDialog->setRange(0, 0);
254 d->progressDialog->setValue(0);
256 d->progressDialog->setMinimumDuration(d->progressDialogTimeout);
260void SpellCheckBar::slotCancel()
265QString SpellCheckBar::originalBuffer()
const
267 return d->originalBuffer;
270QString SpellCheckBar::buffer()
const
272 return d->checker->text();
275void SpellCheckBar::setBuffer(
const QString &buf)
277 d->originalBuffer = buf;
282void SpellCheckBar::fillDictionaryComboBox()
287 Sonnet::Speller speller = d->checker->speller();
290 updateDictionaryComboBox();
293void SpellCheckBar::updateDictionaryComboBox()
295 Sonnet::Speller speller = d->checker->speller();
296 d->ui.m_language->setCurrentByDictionary(speller.
language());
299void SpellCheckBar::updateDialog(
const QString &word)
301 d->ui.m_unknownWord->setText(word);
303 const QStringList suggs = d->checker->
suggest(word);
306 d->ui.cmbReplacement->lineEdit()->clear();
308 d->ui.cmbReplacement->lineEdit()->setText(suggs.
first());
310 fillSuggestions(suggs);
313void SpellCheckBar::show()
316 fillDictionaryComboBox();
317 updateDictionaryComboBox();
318 if (d->originalBuffer.isEmpty()) {
321 d->checker->setText(d->originalBuffer);
323 setProgressDialogVisible(
true);
326void SpellCheckBar::slotAddWord()
328 setGuiEnabled(
false);
329 setProgressDialogVisible(
true);
330 d->checker->addWordToPersonal(d->currentWord.word);
331 d->checker->continueChecking();
334void SpellCheckBar::slotReplaceWord()
336 setGuiEnabled(
false);
337 setProgressDialogVisible(
true);
338 const QString replacementText = d->ui.cmbReplacement->lineEdit()->text();
339 Q_EMIT replace(d->currentWord.word, d->currentWord.start, replacementText);
341 if (d->spellCheckContinuedAfterReplacement) {
342 d->checker->replace(d->currentWord.start, d->currentWord.word, replacementText);
343 d->checker->continueChecking();
345 setProgressDialogVisible(
false);
350void SpellCheckBar::slotReplaceAll()
352 setGuiEnabled(
false);
353 setProgressDialogVisible(
true);
354 d->replaceAllMap.insert_or_assign(d->currentWord.word, d->ui.cmbReplacement->lineEdit()->text());
358void SpellCheckBar::slotSkip()
360 setGuiEnabled(
false);
361 setProgressDialogVisible(
true);
362 d->checker->continueChecking();
365void SpellCheckBar::slotSkipAll()
367 setGuiEnabled(
false);
368 setProgressDialogVisible(
true);
370 Sonnet::Speller speller = d->checker->speller();
372 d->checker->setSpeller(speller);
373 d->checker->continueChecking();
376void SpellCheckBar::slotSuggest()
378 QStringList suggs = d->checker->suggest(d->ui.cmbReplacement->lineEdit()->text());
379 fillSuggestions(suggs);
382void SpellCheckBar::slotChangeLanguage(
const QString &lang)
384 Sonnet::Speller speller = d->checker->speller();
385 QString languageCode = d->dictsMap[lang];
387 d->checker->changeLanguage(languageCode);
393void SpellCheckBar::fillSuggestions(
const QStringList &suggs)
395 d->suggestionsModel->setStringList(suggs);
397 d->ui.cmbReplacement->setCurrentIndex(0);
401void SpellCheckBar::slotMisspelling(
const QString &word,
int start)
404 setProgressDialogVisible(
false);
413 d->currentWord = Word(word,
start);
414 if (d->replaceAllMap.find(word) != d->replaceAllMap.end()) {
415 d->ui.cmbReplacement->lineEdit()->setText(d->replaceAllMap[word]);
422void SpellCheckBar::slotDone()
427 updateDictionaryComboBox();
428 d->checker->setText(d->originalBuffer);
431 setProgressDialogVisible(
false);
434 if (!d->canceled && d->showCompletionMessageBox) {
440#include "moc_spellcheckbar.cpp"
void returnPressed(const QString &text)
void misspelling(const QString &word, int start)
QStringList suggest(const QString &word) const
QMap< QString, QString > availableDictionaries() const
bool addToPersonal(const QString &word)
void setSpellCheckContinuedAfterReplacement(bool b)
Controls whether the spell checking is continued after the replacement of a misspelled word has been ...
void showSpellCheckCompletionMessage(bool b=true)
Controls whether a message box indicating the completion of the spell checking is shown or not.
void languageChanged(const QString &language)
Emitted when the user changes the language used for spellchecking, which is shown in a combobox of th...
void spellCheckStatus(const QString &)
Signal sends when spell checking is finished/stopped/completed.
void done(const QString &newBuffer)
The dialog won't be closed if you setBuffer() in slot connected to this signal.
void showProgressDialog(int timeout=500)
Controls whether an (indefinite) progress dialog is shown when the spell checking takes longer than t...
Q_SCRIPTABLE QString start(QString train="")
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
void textActivated(const QString &text)
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QStringListModel(QObject *parent)