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