dialog.cpp
00001 /* 00002 * dialog.cpp 00003 * 00004 * Copyright (C) 2003 Zack Rusin <zack@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 * 02110-1301 USA 00020 */ 00021 #include "dialog.h" 00022 #include "tdespell2ui.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 <tdeconfig.h> 00031 #include <tdelocale.h> 00032 #include <kdebug.h> 00033 00034 #include <tqlistview.h> 00035 #include <tqpushbutton.h> 00036 #include <tqcombobox.h> 00037 #include <tqlineedit.h> 00038 #include <tqlabel.h> 00039 #include <tqtimer.h> 00040 #include <tqdict.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 TQString originalBuffer; 00053 BackgroundChecker *checker; 00054 00055 Word currentWord; 00056 TQMap<TQString, TQString> replaceAllMap; 00057 }; 00058 00059 Dialog::Dialog( BackgroundChecker *checker, 00060 TQWidget *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( TQT_TQWIDGET(d->ui) ); 00073 } 00074 00075 Dialog::~Dialog() 00076 { 00077 delete d; 00078 } 00079 00080 void Dialog::initConnections() 00081 { 00082 connect( TQT_TQOBJECT(d->ui->m_addBtn), TQT_SIGNAL(clicked()), 00083 TQT_SLOT(slotAddWord()) ); 00084 connect( TQT_TQOBJECT(d->ui->m_replaceBtn), TQT_SIGNAL(clicked()), 00085 TQT_SLOT(slotReplaceWord()) ); 00086 connect( TQT_TQOBJECT(d->ui->m_replaceAllBtn), TQT_SIGNAL(clicked()), 00087 TQT_SLOT(slotReplaceAll()) ); 00088 connect( TQT_TQOBJECT(d->ui->m_skipBtn), TQT_SIGNAL(clicked()), 00089 TQT_SLOT(slotSkip()) ); 00090 connect( TQT_TQOBJECT(d->ui->m_skipAllBtn), TQT_SIGNAL(clicked()), 00091 TQT_SLOT(slotSkipAll()) ); 00092 connect( TQT_TQOBJECT(d->ui->m_suggestBtn), TQT_SIGNAL(clicked()), 00093 TQT_SLOT(slotSuggest()) ); 00094 connect( TQT_TQOBJECT(d->ui->m_language), TQT_SIGNAL(activated(const TQString&)), 00095 TQT_SLOT(slotChangeLanguage(const TQString&)) ); 00096 connect( TQT_TQOBJECT(d->ui->m_suggestions), TQT_SIGNAL(selectionChanged(TQListViewItem*)), 00097 TQT_SLOT(slotSelectionChanged(TQListViewItem*)) ); 00098 connect( TQT_TQOBJECT(d->checker), TQT_SIGNAL(misspelling(const TQString&, int)), 00099 TQT_SIGNAL(misspelling(const TQString&, int)) ); 00100 connect( TQT_TQOBJECT(d->checker), TQT_SIGNAL(misspelling(const TQString&, int)), 00101 TQT_SLOT(slotMisspelling(const TQString&, int)) ); 00102 connect( TQT_TQOBJECT(d->checker), TQT_SIGNAL(done()), 00103 TQT_SLOT(slotDone()) ); 00104 connect( d->ui->m_suggestions, TQT_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)), 00105 TQT_SLOT( slotReplaceWord() ) ); 00106 connect( this, TQT_SIGNAL(user1Clicked()), this, TQT_SLOT(slotFinished()) ); 00107 connect( this, TQT_SIGNAL(cancelClicked()),this, TQT_SLOT(slotCancel()) ); 00108 connect( d->ui->m_replacement, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(slotReplaceWord()) ); 00109 connect( d->ui->m_autoCorrect, TQT_SIGNAL(clicked()), 00110 TQT_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 TQString 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 TQString Dialog::originalBuffer() const 00163 { 00164 return d->originalBuffer; 00165 } 00166 00167 TQString Dialog::buffer() const 00168 { 00169 return d->checker->filter()->buffer(); 00170 } 00171 00172 void Dialog::setBuffer( const TQString& 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 TQString& word ) 00184 { 00185 d->ui->m_unknownWord->setText( word ); 00186 d->ui->m_contextLabel->setText( d->checker->filter()->context() ); 00187 TQStringList 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 TQStringList suggs = d->checker->suggest( d->ui->m_replacement->text() ); 00237 fillSuggestions( suggs ); 00238 } 00239 00240 void Dialog::slotChangeLanguage( const TQString& lang ) 00241 { 00242 d->checker->changeLanguage( lang ); 00243 slotSuggest(); 00244 } 00245 00246 void Dialog::slotSelectionChanged( TQListViewItem *item ) 00247 { 00248 d->ui->m_replacement->setText( item->text( 0 ) ); 00249 } 00250 00251 void Dialog::fillSuggestions( const TQStringList& suggs ) 00252 { 00253 d->ui->m_suggestions->clear(); 00254 for ( TQStringList::ConstIterator it = suggs.begin(); it != suggs.end(); ++it ) { 00255 new TQListViewItem( d->ui->m_suggestions, d->ui->m_suggestions->firstChild(), 00256 *it ); 00257 } 00258 } 00259 00260 void Dialog::slotMisspelling(const TQString& 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"