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