00001
00021 #include "dialog.h"
00022 #include "ui_sonnetui.h"
00023
00024 #include "backgroundchecker.h"
00025 #include "speller.h"
00026 #include "filter_p.h"
00027 #include "settings_p.h"
00028
00029 #include <kconfig.h>
00030 #include <kguiitem.h>
00031 #include <klocale.h>
00032 #include <kdebug.h>
00033
00034 #include <QtGui/QListView>
00035 #include <QtGui/QPushButton>
00036 #include <QtGui/QComboBox>
00037 #include <QtGui/QLineEdit>
00038 #include <QtGui/QLabel>
00039 #include <QtCore/QTimer>
00040
00041 namespace Sonnet
00042 {
00043
00044
00045 #define NONSORTINGCOLUMN 2
00046
00047 class Dialog::Private
00048 {
00049 public:
00050 Ui_SonnetUi ui;
00051 QWidget *wdg;
00052 QString originalBuffer;
00053 BackgroundChecker *checker;
00054
00055 Word currentWord;
00056 QMap<QString, QString> replaceAllMap;
00057 bool restart;
00058 };
00059
00060 Dialog::Dialog(BackgroundChecker *checker,
00061 QWidget *parent)
00062 : KDialog(parent),
00063 d(new Private)
00064 {
00065 setModal(true);
00066 setCaption(i18nc("@title:window", "Check Spelling"));
00067 setButtons(Help | Cancel | User1);
00068 setButtonGuiItem(User1, KGuiItem(i18nc("@action:button", "&Finished")));
00069 showButtonSeparator(true);
00070
00071 setDefaultButton(User1);
00072 d->checker = checker;
00073
00074 initGui();
00075 initConnections();
00076 setMainWidget(d->wdg);
00077 setHelp(QString(),"sonnet");
00078 }
00079
00080 Dialog::~Dialog()
00081 {
00082 delete d;
00083 }
00084
00085 void Dialog::initConnections()
00086 {
00087 connect( d->ui.m_addBtn, SIGNAL(clicked()),
00088 SLOT(slotAddWord()) );
00089 connect( d->ui.m_replaceBtn, SIGNAL(clicked()),
00090 SLOT(slotReplaceWord()) );
00091 connect( d->ui.m_replaceAllBtn, SIGNAL(clicked()),
00092 SLOT(slotReplaceAll()) );
00093 connect( d->ui.m_skipBtn, SIGNAL(clicked()),
00094 SLOT(slotSkip()) );
00095 connect( d->ui.m_skipAllBtn, SIGNAL(clicked()),
00096 SLOT(slotSkipAll()) );
00097 connect( d->ui.m_suggestBtn, SIGNAL(clicked()),
00098 SLOT(slotSuggest()) );
00099 connect( d->ui.m_language, SIGNAL(activated(const QString&)),
00100 SLOT(slotChangeLanguage(const QString&)) );
00101 connect( d->ui.m_suggestions, SIGNAL(itemClicked(QListWidgetItem*)),
00102 SLOT(slotSelectionChanged(QListWidgetItem*)) );
00103 connect( d->checker, SIGNAL(misspelling(const QString&, int)),
00104 SIGNAL(misspelling(const QString&, int)) );
00105 connect( d->checker, SIGNAL(misspelling(const QString&, int)),
00106 SLOT(slotMisspelling(const QString&, int)) );
00107 connect( d->checker, SIGNAL(done()),
00108 SLOT(slotDone()) );
00109 connect( d->ui.m_suggestions, SIGNAL(itemDoubleClicked (QListWidgetItem *)),
00110 SLOT( slotReplaceWord() ) );
00111 connect( this, SIGNAL(user1Clicked()), this, SLOT(slotFinished()) );
00112 connect( this, SIGNAL(cancelClicked()),this, SLOT(slotCancel()) );
00113 connect( d->ui.m_replacement, SIGNAL(returnPressed()), this, SLOT(slotReplaceWord()) );
00114 connect( d->ui.m_autoCorrect, SIGNAL(clicked()),
00115 SLOT(slotAutocorrect()) );
00116
00117
00118 d->ui.m_autoCorrect->hide();
00119 }
00120
00121 void Dialog::initGui()
00122 {
00123 d->wdg = new QWidget(this);
00124 d->ui.setupUi(d->wdg);
00125
00126
00127 d->ui.m_language->clear();
00128 Speller speller = d->checker->speller();
00129 d->ui.m_language->insertItems(0, speller.availableLanguageNames());
00130 d->ui.m_language->setCurrentIndex(speller.availableLanguages().indexOf(
00131 speller.language()));
00132 d->restart = false;
00133 }
00134
00135 void Dialog::activeAutoCorrect( bool _active )
00136 {
00137 if ( _active )
00138 d->ui.m_autoCorrect->show();
00139 else
00140 d->ui.m_autoCorrect->hide();
00141 }
00142
00143 void Dialog::slotAutocorrect()
00144 {
00145 kDebug();
00146 emit autoCorrect(d->currentWord.word, d->ui.m_replacement->text() );
00147 slotReplaceWord();
00148 }
00149
00150 void Dialog::slotFinished()
00151 {
00152 kDebug();
00153 emit stop();
00154
00155 emit done(d->checker->text());
00156 emit spellCheckStatus(i18n("Spell check stopped."));
00157 accept();
00158 }
00159
00160 void Dialog::slotCancel()
00161 {
00162 kDebug();
00163 emit cancel();
00164 emit spellCheckStatus(i18n("Spell check canceled."));
00165 reject();
00166 }
00167
00168 QString Dialog::originalBuffer() const
00169 {
00170 return d->originalBuffer;
00171 }
00172
00173 QString Dialog::buffer() const
00174 {
00175 return d->checker->text();
00176 }
00177
00178 void Dialog::setBuffer(const QString &buf)
00179 {
00180 d->originalBuffer = buf;
00181
00182 d->restart = true;
00183 }
00184
00185
00186 void Dialog::updateDialog( const QString& word )
00187 {
00188 d->ui.m_unknownWord->setText( word );
00189 d->ui.m_contextLabel->setText( d->checker->currentContext() );
00190 const QStringList suggs = d->checker->suggest( word );
00191
00192 if (suggs.isEmpty())
00193 d->ui.m_replacement->clear();
00194 else
00195 d->ui.m_replacement->setText( suggs.first() );
00196 fillSuggestions( suggs );
00197 }
00198
00199 void Dialog::show()
00200 {
00201 kDebug()<<"Showing dialog";
00202 if (d->originalBuffer.isEmpty())
00203 d->checker->start();
00204 else
00205 d->checker->setText(d->originalBuffer);
00206 }
00207
00208 void Dialog::slotAddWord()
00209 {
00210 d->checker->addWordToPersonal(d->currentWord.word);
00211 d->checker->continueChecking();
00212 }
00213
00214 void Dialog::slotReplaceWord()
00215 {
00216 emit replace( d->currentWord.word, d->currentWord.start,
00217 d->ui.m_replacement->text() );
00218 d->checker->replace(d->currentWord.start,
00219 d->currentWord.word,
00220 d->ui.m_replacement->text());
00221 d->checker->continueChecking();
00222 }
00223
00224 void Dialog::slotReplaceAll()
00225 {
00226 d->replaceAllMap.insert( d->currentWord.word,
00227 d->ui.m_replacement->text() );
00228 slotReplaceWord();
00229 }
00230
00231 void Dialog::slotSkip()
00232 {
00233 d->checker->continueChecking();
00234 }
00235
00236 void Dialog::slotSkipAll()
00237 {
00238
00239 Speller speller = d->checker->speller();
00240 speller.addToPersonal(d->currentWord.word);
00241 d->checker->setSpeller(speller);
00242 d->checker->continueChecking();
00243 }
00244
00245 void Dialog::slotSuggest()
00246 {
00247 QStringList suggs = d->checker->suggest( d->ui.m_replacement->text() );
00248 fillSuggestions( suggs );
00249 }
00250
00251 void Dialog::slotChangeLanguage( const QString& lang )
00252 {
00253 Speller speller = d->checker->speller();
00254 QString languageCode = speller.availableLanguages().at(
00255 speller.availableLanguageNames().indexOf(lang));
00256 d->checker->changeLanguage( languageCode );
00257 slotSuggest();
00258 emit languageChanged( languageCode );
00259 }
00260
00261 void Dialog::slotSelectionChanged( QListWidgetItem *item )
00262 {
00263 d->ui.m_replacement->setText( item->text() );
00264 }
00265
00266 void Dialog::fillSuggestions( const QStringList& suggs )
00267 {
00268 d->ui.m_suggestions->clear();
00269 for ( QStringList::ConstIterator it = suggs.begin(); it != suggs.end(); ++it ) {
00270 d->ui.m_suggestions->addItem(*it );
00271 }
00272 }
00273
00274 void Dialog::slotMisspelling(const QString& word, int start)
00275 {
00276 kDebug()<<"Dialog misspelling!!";
00277 d->currentWord = Word( word, start );
00278 if ( d->replaceAllMap.contains( word ) ) {
00279 d->ui.m_replacement->setText( d->replaceAllMap[ word ] );
00280 slotReplaceWord();
00281 } else {
00282 updateDialog( word );
00283 }
00284 KDialog::show();
00285 }
00286
00287 void Dialog::slotDone()
00288 {
00289 kDebug()<<"Dialog done!";
00290 d->restart=false;
00291 emit done(d->checker->text());
00292 if (d->restart)
00293 {
00294 d->checker->setText(d->originalBuffer);
00295 d->restart=false;
00296 }
00297 else
00298 {
00299 emit spellCheckStatus(i18n("Spell check complete."));
00300 accept();
00301 }
00302 }
00303
00304 }
00305
00306 #include "dialog.moc"