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 setDefaultButton(Cancel);
00070 showButtonSeparator(true);
00071
00072 setDefaultButton(Cancel);
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->ui.m_language->insertItems(0, speller.availableLanguageNames());
00131 d->ui.m_language->setCurrentIndex(speller.availableLanguages().indexOf(
00132 speller.language()));
00133 d->restart = false;
00134 }
00135
00136 void Dialog::activeAutoCorrect( bool _active )
00137 {
00138 if ( _active )
00139 d->ui.m_autoCorrect->show();
00140 else
00141 d->ui.m_autoCorrect->hide();
00142 }
00143
00144 void Dialog::slotAutocorrect()
00145 {
00146 kDebug()<<"void Dialog::slotAutocorrect()\n";
00147 emit autoCorrect(d->currentWord.word, d->ui.m_replacement->text() );
00148 slotReplaceWord();
00149 }
00150
00151 void Dialog::slotFinished()
00152 {
00153 kDebug()<<"void Dialog::slotFinished() \n";
00154 emit stop();
00155
00156 emit done(d->checker->text());
00157 accept();
00158 }
00159
00160 void Dialog::slotCancel()
00161 {
00162 kDebug()<<"void Dialog::slotCancel() \n";
00163 emit cancel();
00164 reject();
00165 }
00166
00167 QString Dialog::originalBuffer() const
00168 {
00169 return d->originalBuffer;
00170 }
00171
00172 QString Dialog::buffer() const
00173 {
00174 return d->checker->text();
00175 }
00176
00177 void Dialog::setBuffer(const QString &buf)
00178 {
00179 d->originalBuffer = buf;
00180
00181 d->restart = true;
00182 }
00183
00184
00185 void Dialog::updateDialog( const QString& word )
00186 {
00187 d->ui.m_unknownWord->setText( word );
00188 d->ui.m_contextLabel->setText( d->checker->currentContext() );
00189 QStringList suggs = d->checker->suggest( word );
00190
00191 if (suggs.isEmpty())
00192 d->ui.m_replacement->clear();
00193 else
00194 d->ui.m_replacement->setText( suggs.first() );
00195 fillSuggestions( suggs );
00196 }
00197
00198 void Dialog::show()
00199 {
00200 kDebug()<<"Showing dialog";
00201 if (d->originalBuffer.isEmpty())
00202 d->checker->start();
00203 else
00204 d->checker->setText(d->originalBuffer);
00205 }
00206
00207 void Dialog::slotAddWord()
00208 {
00209 d->checker->addWordToPersonal(d->currentWord.word);
00210 d->checker->continueChecking();
00211 }
00212
00213 void Dialog::slotReplaceWord()
00214 {
00215 emit replace( d->currentWord.word, d->currentWord.start,
00216 d->ui.m_replacement->text() );
00217 d->checker->replace(d->currentWord.start,
00218 d->currentWord.word,
00219 d->ui.m_replacement->text());
00220 d->checker->continueChecking();
00221 }
00222
00223 void Dialog::slotReplaceAll()
00224 {
00225 d->replaceAllMap.insert( d->currentWord.word,
00226 d->ui.m_replacement->text() );
00227 slotReplaceWord();
00228 }
00229
00230 void Dialog::slotSkip()
00231 {
00232 d->checker->continueChecking();
00233 }
00234
00235 void Dialog::slotSkipAll()
00236 {
00237
00238 Speller speller = d->checker->speller();
00239 speller.addToPersonal(d->currentWord.word);
00240 d->checker->setSpeller(speller);
00241 d->checker->continueChecking();
00242 }
00243
00244 void Dialog::slotSuggest()
00245 {
00246 QStringList suggs = d->checker->suggest( d->ui.m_replacement->text() );
00247 fillSuggestions( suggs );
00248 }
00249
00250 void Dialog::slotChangeLanguage( const QString& lang )
00251 {
00252 Speller speller = d->checker->speller();
00253 d->checker->changeLanguage(
00254 speller.availableLanguages().at(
00255 speller.availableLanguageNames().indexOf(lang)));
00256 slotSuggest();
00257 }
00258
00259 void Dialog::slotSelectionChanged( QListWidgetItem *item )
00260 {
00261 d->ui.m_replacement->setText( item->text() );
00262 }
00263
00264 void Dialog::fillSuggestions( const QStringList& suggs )
00265 {
00266 d->ui.m_suggestions->clear();
00267 for ( QStringList::ConstIterator it = suggs.begin(); it != suggs.end(); ++it ) {
00268 d->ui.m_suggestions->addItem(*it );
00269 }
00270 }
00271
00272 void Dialog::slotMisspelling(const QString& word, int start)
00273 {
00274 kDebug()<<"Dialog misspelling!!";
00275 d->currentWord = Word( word, start );
00276 if ( d->replaceAllMap.contains( word ) ) {
00277 d->ui.m_replacement->setText( d->replaceAllMap[ word ] );
00278 slotReplaceWord();
00279 } else {
00280 updateDialog( word );
00281 }
00282 KDialog::show();
00283 }
00284
00285 void Dialog::slotDone()
00286 {
00287 kDebug()<<"Dialog done!";
00288 QString buffer(d->originalBuffer);
00289 emit done(d->checker->text());
00290 if (d->restart)
00291 {
00292 d->checker->setText(d->originalBuffer);
00293 d->restart=false;
00294 }
00295 else
00296 accept();
00297 }
00298
00299 }
00300
00301 #include "dialog.moc"