dialog.cpp

00001 
00021 #include "dialog.h"
00022 #include "kspell2ui.h"
00023 
00024 #include "backgroundchecker.h"
00025 #include "broker.h"
00026 #include "filter.h"
00027 #include "dictionary.h"
00028 #include "settings.h"
00029 
00030 #include <kconfig.h>
00031 #include <klocale.h>
00032 #include <kdebug.h>
00033 
00034 #include <qlistview.h>
00035 #include <qpushbutton.h>
00036 #include <qcombobox.h>
00037 #include <qlineedit.h>
00038 #include <qlabel.h>
00039 #include <qtimer.h>
00040 #include <qdict.h>
00041 
00042 namespace KSpell2
00043 {
00044 
00045 //to initially disable sorting in the suggestions listview
00046 #define NONSORTINGCOLUMN 2
00047 
00048 class Dialog::Private
00049 {
00050 public:
00051     KSpell2UI *ui;
00052     QString   originalBuffer;
00053     BackgroundChecker *checker;
00054 
00055     Word   currentWord;
00056     QMap<QString, QString> replaceAllMap;
00057 };
00058 
00059 Dialog::Dialog( BackgroundChecker *checker,
00060                 QWidget *parent, const char *name )
00061     : KDialogBase( parent, name, true,
00062                    i18n( "Check Spelling" ),
00063                    Help|Cancel|User1, Cancel,  true,
00064                    i18n( "&Finished" ) )
00065 {
00066     d = new Private;
00067 
00068     d->checker = checker;
00069 
00070     initGui();
00071     initConnections();
00072     setMainWidget( d->ui );
00073 }
00074 
00075 Dialog::~Dialog()
00076 {
00077     delete d;
00078 }
00079 
00080 void Dialog::initConnections()
00081 {
00082     connect( d->ui->m_addBtn, SIGNAL(clicked()),
00083              SLOT(slotAddWord()) );
00084     connect( d->ui->m_replaceBtn, SIGNAL(clicked()),
00085              SLOT(slotReplaceWord()) );
00086     connect( d->ui->m_replaceAllBtn, SIGNAL(clicked()),
00087              SLOT(slotReplaceAll()) );
00088     connect( d->ui->m_skipBtn, SIGNAL(clicked()),
00089              SLOT(slotSkip()) );
00090     connect( d->ui->m_skipAllBtn, SIGNAL(clicked()),
00091              SLOT(slotSkipAll()) );
00092     connect( d->ui->m_suggestBtn, SIGNAL(clicked()),
00093              SLOT(slotSuggest()) );
00094     connect( d->ui->m_language, SIGNAL(activated(const QString&)),
00095              SLOT(slotChangeLanguage(const QString&)) );
00096     connect( d->ui->m_suggestions, SIGNAL(selectionChanged(QListViewItem*)),
00097              SLOT(slotSelectionChanged(QListViewItem*)) );
00098     connect( d->checker, SIGNAL(misspelling(const QString&, int)),
00099              SIGNAL(misspelling(const QString&, int)) );
00100     connect( d->checker, SIGNAL(misspelling(const QString&, int)),
00101              SLOT(slotMisspelling(const QString&, int)) );
00102     connect( d->checker, SIGNAL(done()),
00103              SLOT(slotDone()) );
00104     connect( d->ui->m_suggestions, SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),
00105              SLOT( slotReplaceWord() ) );
00106     connect( this, SIGNAL(user1Clicked()), this, SLOT(slotFinished()) );
00107     connect( this, SIGNAL(cancelClicked()),this, SLOT(slotCancel()) );
00108     connect( d->ui->m_replacement, SIGNAL(returnPressed()), this, SLOT(slotReplaceWord()) );
00109     connect( d->ui->m_autoCorrect, SIGNAL(clicked()),
00110              SLOT(slotAutocorrect()) );
00111     // button use by kword/kpresenter
00112     // hide by default
00113     d->ui->m_autoCorrect->hide();
00114 }
00115 
00116 void Dialog::initGui()
00117 {
00118     d->ui = new KSpell2UI( this );
00119     d->ui->m_suggestions->setSorting( NONSORTINGCOLUMN );
00120     d->ui->m_language->clear();
00121     d->ui->m_language->insertStringList( d->checker->broker()->languages() );
00122     for ( int i = 0; !d->ui->m_language->text( i ).isNull(); ++i ) {
00123         QString ct = d->ui->m_language->text( i );
00124         if ( ct == d->checker->broker()->settings()->defaultLanguage() ) {
00125             d->ui->m_language->setCurrentItem( i );
00126             break;
00127         }
00128     }
00129 }
00130 
00131 void Dialog::activeAutoCorrect( bool _active )
00132 {
00133     if ( _active )
00134         d->ui->m_autoCorrect->show();
00135     else
00136         d->ui->m_autoCorrect->hide();
00137 }
00138 
00139 void Dialog::slotAutocorrect()
00140 {
00141     kdDebug()<<"void Dialog::slotAutocorrect()\n";
00142     emit autoCorrect(d->currentWord.word, d->ui->m_replacement->text() );
00143     slotReplaceWord();
00144 }
00145 
00146 void Dialog::slotFinished()
00147 {
00148     kdDebug()<<"void Dialog::slotFinished() \n";
00149     emit stop();
00150     //FIXME: should we emit done here?
00151     emit done( d->checker->filter()->buffer() );
00152     accept();
00153 }
00154 
00155 void Dialog::slotCancel()
00156 {
00157     kdDebug()<<"void Dialog::slotCancel() \n";
00158     emit cancel();
00159     reject();
00160 }
00161 
00162 QString Dialog::originalBuffer() const
00163 {
00164     return d->originalBuffer;
00165 }
00166 
00167 QString Dialog::buffer() const
00168 {
00169     return d->checker->filter()->buffer();
00170 }
00171 
00172 void Dialog::setBuffer( const QString& buf )
00173 {
00174     d->originalBuffer = buf;
00175 }
00176 
00177 void Dialog::setFilter( Filter *filter )
00178 {
00179     filter->setBuffer( d->checker->filter()->buffer() );
00180     d->checker->setFilter( filter );
00181 }
00182 
00183 void Dialog::updateDialog( const QString& word )
00184 {
00185     d->ui->m_unknownWord->setText( word );
00186     d->ui->m_contextLabel->setText( d->checker->filter()->context() );
00187     QStringList suggs = d->checker->suggest( word );
00188     d->ui->m_replacement->setText( suggs.first() );
00189     fillSuggestions( suggs );
00190 }
00191 
00192 void Dialog::show()
00193 {
00194     kdDebug()<<"Showing dialog"<<endl;
00195     if ( d->originalBuffer.isEmpty() )
00196         d->checker->start();
00197     else
00198         d->checker->checkText( d->originalBuffer );
00199 }
00200 
00201 void Dialog::slotAddWord()
00202 {
00203    d->checker->addWord( d->currentWord.word ); 
00204    d->checker->continueChecking();
00205 }
00206 
00207 void Dialog::slotReplaceWord()
00208 {
00209     emit replace( d->currentWord.word, d->currentWord.start,
00210                   d->ui->m_replacement->text() );
00211     d->checker->filter()->replace( d->currentWord, d->ui->m_replacement->text() );
00212     d->checker->continueChecking();
00213 }
00214 
00215 void Dialog::slotReplaceAll()
00216 {
00217     d->replaceAllMap.insert( d->currentWord.word,
00218                              d->ui->m_replacement->text() );
00219     slotReplaceWord();
00220 }
00221 
00222 void Dialog::slotSkip()
00223 {
00224     d->checker->continueChecking();
00225 }
00226 
00227 void Dialog::slotSkipAll()
00228 {
00229     //### do we want that or should we have a d->ignoreAll list?
00230     d->checker->broker()->settings()->addWordToIgnore( d->ui->m_replacement->text() );
00231     d->checker->continueChecking();
00232 }
00233 
00234 void Dialog::slotSuggest()
00235 {
00236     QStringList suggs = d->checker->suggest( d->ui->m_replacement->text() );
00237     fillSuggestions( suggs );
00238 }
00239 
00240 void Dialog::slotChangeLanguage( const QString& lang )
00241 {
00242     d->checker->changeLanguage( lang );
00243     slotSuggest();
00244 }
00245 
00246 void Dialog::slotSelectionChanged( QListViewItem *item )
00247 {
00248     d->ui->m_replacement->setText( item->text( 0 ) );
00249 }
00250 
00251 void Dialog::fillSuggestions( const QStringList& suggs )
00252 {
00253     d->ui->m_suggestions->clear();
00254     for ( QStringList::ConstIterator it = suggs.begin(); it != suggs.end(); ++it ) {
00255         new QListViewItem( d->ui->m_suggestions, d->ui->m_suggestions->firstChild(),
00256                            *it );
00257     }
00258 }
00259 
00260 void Dialog::slotMisspelling(const QString& word, int start )
00261 {
00262     kdDebug()<<"Dialog misspelling!!"<<endl;
00263     d->currentWord = Word( word, start );
00264     if ( d->replaceAllMap.contains( word ) ) {
00265         d->ui->m_replacement->setText( d->replaceAllMap[ word ] );
00266         slotReplaceWord();
00267     } else {
00268         updateDialog( word );
00269     }
00270     KDialogBase::show();
00271 }
00272 
00273 void Dialog::slotDone()
00274 {
00275     kdDebug()<<"Dialog done!"<<endl;
00276     emit done( d->checker->filter()->buffer() );
00277     accept();
00278 }
00279 
00280 }
00281 
00282 #include "dialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys