23#include "ui_sonnetui.h"
37#include <QtGui/QListView>
38#include <QtGui/QStringListModel>
39#include <QtGui/QPushButton>
40#include <QtGui/QComboBox>
41#include <QtGui/QLabel>
42#include <QtCore/QTimer>
49#define NONSORTINGCOLUMN 2
51class ReadOnlyStringListModel:
public QStringListModel
54 ReadOnlyStringListModel(
QObject* parent):QStringListModel(parent){}
55 Qt::ItemFlags flags(
const QModelIndex& index)
const {Q_UNUSED(index);
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}
62 ReadOnlyStringListModel *suggestionsModel;
65 QString originalBuffer;
66 BackgroundChecker *checker;
74 int progressDialogTimeout;
75 bool showCompletionMessageBox;
76 bool spellCheckContinuedAfterReplacement;
79 void deleteProgressDialog(
bool directly)
83 progressDialog->hide();
86 delete progressDialog;
90 progressDialog->deleteLater();
92 progressDialog = NULL;
108 d->checker = checker;
111 d->showCompletionMessageBox =
false;
112 d->spellCheckContinuedAfterReplacement =
true;
113 d->progressDialogTimeout = -1;
114 d->progressDialog = NULL;
127void Dialog::initConnections()
129 connect( d->ui.m_addBtn, SIGNAL(clicked()),
130 SLOT(slotAddWord()) );
131 connect( d->ui.m_replaceBtn, SIGNAL(clicked()),
132 SLOT(slotReplaceWord()) );
133 connect( d->ui.m_replaceAllBtn, SIGNAL(clicked()),
134 SLOT(slotReplaceAll()) );
135 connect( d->ui.m_skipBtn, SIGNAL(clicked()),
137 connect( d->ui.m_skipAllBtn, SIGNAL(clicked()),
138 SLOT(slotSkipAll()) );
139 connect( d->ui.m_suggestBtn, SIGNAL(clicked()),
140 SLOT(slotSuggest()) );
141 connect( d->ui.m_language, SIGNAL(activated(QString)),
142 SLOT(slotChangeLanguage(QString)) );
143 connect( d->ui.m_suggestions, SIGNAL(clicked(QModelIndex)),
144 SLOT(slotSelectionChanged(QModelIndex)) );
145 connect( d->checker, SIGNAL(
misspelling(QString,
int)),
146 SLOT(slotMisspelling(QString,
int)) );
147 connect( d->checker, SIGNAL(
done()),
149 connect( d->ui.m_suggestions, SIGNAL(doubleClicked(QModelIndex)),
150 SLOT(slotReplaceWord()) );
151 connect(
this, SIGNAL(
user1Clicked()),
this, SLOT(slotFinished()) );
152 connect(
this, SIGNAL(
cancelClicked()),
this, SLOT(slotCancel()) );
153 connect( d->ui.m_replacement, SIGNAL(returnPressed()),
this, SLOT(slotReplaceWord()) );
154 connect( d->ui.m_autoCorrect, SIGNAL(clicked()),
155 SLOT(slotAutocorrect()) );
158 d->ui.m_autoCorrect->hide();
161void Dialog::initGui()
164 d->ui.setupUi(d->wdg);
165 setGuiEnabled(
false);
168 fillDictionaryComboBox();
171 d->suggestionsModel=
new ReadOnlyStringListModel(
this);
172 d->ui.m_suggestions->setModel(d->suggestionsModel);
178 d->ui.m_autoCorrect->show();
180 d->ui.m_autoCorrect->hide();
185 d->progressDialogTimeout =
timeout;
190 d->showCompletionMessageBox = b;
195 d->spellCheckContinuedAfterReplacement = b;
198void Dialog::slotAutocorrect()
200 setGuiEnabled(
false);
201 setProgressDialogVisible(
true);
203 emit
autoCorrect(d->currentWord.word, d->ui.m_replacement->text() );
207void Dialog::setGuiEnabled(
bool b)
209 d->wdg->setEnabled(b);
212void Dialog::setProgressDialogVisible(
bool b)
216 d->deleteProgressDialog(
true);
218 else if(d->progressDialogTimeout >= 0)
220 if (d->progressDialog)
225 i18nc(
"progress label",
"Spell checking in progress..."));
226 d->progressDialog->setModal(
true);
227 d->progressDialog->setAutoClose(
false);
228 d->progressDialog->setAutoReset(
false);
231 d->progressDialog->progressBar()->reset();
232 d->progressDialog->progressBar()->setRange(0, 0);
233 d->progressDialog->progressBar()->setValue(0);
234 connect(d->progressDialog, SIGNAL(
cancelClicked()),
this, SLOT(slotCancel()));
235 d->progressDialog->setMinimumDuration(d->progressDialogTimeout);
239void Dialog::slotFinished()
242 setProgressDialogVisible(
false);
245 emit
done(d->checker->text());
250void Dialog::slotCancel()
254 d->deleteProgressDialog(
false);
263 return d->originalBuffer;
268 return d->checker->text();
273 d->originalBuffer = buf;
278void Dialog::fillDictionaryComboBox()
280 Speller speller = d->checker->speller();
282 QStringList langs = d->dictsMap.keys();
283 d->ui.m_language->clear();
284 d->ui.m_language->addItems(langs);
285 updateDictionaryComboBox();
288void Dialog::updateDictionaryComboBox()
290 Speller speller = d->checker->speller();
291 d->ui.m_language->setCurrentIndex(d->dictsMap.values().indexOf(speller.language()));
294void Dialog::updateDialog(
const QString& word )
296 d->ui.m_unknownWord->setText( word );
297 d->ui.m_contextLabel->setText( d->checker->currentContext() );
298 const QStringList suggs = d->checker->suggest( word );
301 d->ui.m_replacement->clear();
303 d->ui.m_replacement->setText( suggs.first() );
304 fillSuggestions( suggs );
309 kDebug()<<
"Showing dialog";
311 fillDictionaryComboBox();
312 updateDictionaryComboBox();
313 if (d->originalBuffer.isEmpty())
319 d->checker->setText(d->originalBuffer);
321 setProgressDialogVisible(
true);
324void Dialog::slotAddWord()
326 setGuiEnabled(
false);
327 setProgressDialogVisible(
true);
328 d->checker->addWordToPersonal(d->currentWord.word);
329 d->checker->continueChecking();
332void Dialog::slotReplaceWord()
334 setGuiEnabled(
false);
335 setProgressDialogVisible(
true);
336 QString replacementText = d->ui.m_replacement->text();
337 emit
replace( d->currentWord.word, d->currentWord.start,
340 if( d->spellCheckContinuedAfterReplacement ) {
341 d->checker->replace(d->currentWord.start,
344 d->checker->continueChecking();
351void Dialog::slotReplaceAll()
353 setGuiEnabled(
false);
354 setProgressDialogVisible(
true);
355 d->replaceAllMap.insert( d->currentWord.word,
356 d->ui.m_replacement->text() );
360void Dialog::slotSkip()
362 setGuiEnabled(
false);
363 setProgressDialogVisible(
true);
364 d->checker->continueChecking();
367void Dialog::slotSkipAll()
369 setGuiEnabled(
false);
370 setProgressDialogVisible(
true);
372 Speller speller = d->checker->speller();
373 speller.addToPersonal(d->currentWord.word);
374 d->checker->setSpeller(speller);
375 d->checker->continueChecking();
378void Dialog::slotSuggest()
380 QStringList suggs = d->checker->suggest( d->ui.m_replacement->text() );
381 fillSuggestions( suggs );
384void Dialog::slotChangeLanguage(
const QString &lang)
386 Speller speller = d->checker->speller();
387 QString languageCode = d->dictsMap[lang];
388 if (!languageCode.isEmpty()) {
389 d->checker->changeLanguage(languageCode);
395void Dialog::slotSelectionChanged(
const QModelIndex &item)
397 d->ui.m_replacement->setText( item.data().toString() );
400void Dialog::fillSuggestions(
const QStringList& suggs )
402 d->suggestionsModel->setStringList(suggs);
405void Dialog::slotMisspelling(
const QString& word,
int start)
408 setProgressDialogVisible(
false);
413 if (!updatesEnabled())
416 kDebug()<<
"Dialog misspelling!!";
417 d->currentWord = Word( word, start );
418 if ( d->replaceAllMap.contains( word ) ) {
419 d->ui.m_replacement->setText( d->replaceAllMap[ word ] );
422 updateDialog( word );
427void Dialog::slotDone()
430 emit
done(d->checker->text());
433 updateDictionaryComboBox();
434 d->checker->setText(d->originalBuffer);
439 setProgressDialogVisible(
false);
443 if(!d->canceled && d->showCompletionMessageBox)
A dialog base class with standard buttons and predefined layouts.
void setButtonGuiItem(ButtonCode id, const KGuiItem &item)
Sets the KGuiItem directly for the button instead of using 3 methods to set the text,...
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
void setHelp(const QString &anchor, const QString &appname=QString())
Sets the help path and topic.
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
@ Help
Show Help button. (this button will run the help set with setHelp)
@ User1
Show User defined button 1.
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
void cancelClicked()
The Cancel button was pressed.
void setDefaultButton(ButtonCode id)
Sets the button that will be activated when the Enter key is pressed.
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
void user1Clicked()
The User1 button was pressed.
An abstract class for GUI data such as ToolTip and Icon.
static void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
Display an "Information" dialog.
A dialog with a progress bar.
void autoCorrect(const QString ¤tWord, const QString &replaceWord)
void setSpellCheckContinuedAfterReplacement(bool b)
Controls whether the spell checking is continued after the replacement of a misspelled word has been ...
void misspelling(const QString &word, int start)
void spellCheckStatus(const QString &)
Signal sends when spell checking is finished/stopped/completed.
Dialog(BackgroundChecker *checker, QWidget *parent)
void setBuffer(const QString &)
void activeAutoCorrect(bool _active)
void showProgressDialog(int timeout=500)
Controls whether an (indefinite) progress dialog is shown when the spell checking takes longer than t...
void done(const QString &newBuffer)
The dialog won't be closed if you setBuffer() in slot connected to this signal.
void languageChanged(const QString &language)
Emitted when the user changes the language used for spellchecking, which is shown in a combobox of th...
void showSpellCheckCompletionMessage(bool b=true)
Controls whether a message box indicating the completion of the spell checking is shown or not.
QString originalBuffer() const
void replace(const QString &oldWord, int start, const QString &newWord)
QMap< QString, QString > availableDictionaries() const
QString i18n(const char *text)
QString i18nc(const char *ctxt, const char *text)