kspelldlg.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1997 David Sweet <dsweet@kde.org>
00003    Copyright (C) 2000 Rik Hemsley <rik@kde.org>
00004    Copyright (C) 2000-2001 Wolfram Diestel <wolfram@steloj.de>
00005    Copyright (C) 2003 Zack Rusin <zack@kde.org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License version 2 as published by the Free Software Foundation.
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    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <qstringlist.h>
00023 #include <qpushbutton.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 
00027 #include <kapplication.h>
00028 #include <klocale.h>
00029 #include <klistbox.h>
00030 #include <kcombobox.h>
00031 #include <klistview.h>
00032 #include <klineedit.h>
00033 #include <kpushbutton.h>
00034 #include <kprogress.h>
00035 #include <kbuttonbox.h>
00036 #include <kdebug.h>
00037 
00038 #include "ksconfig.h"
00039 #include "kspelldlg.h"
00040 #include "kspellui.h"
00041 
00042 //to initially disable sorting in the suggestions listview
00043 #define NONSORTINGCOLUMN 2
00044 
00045 class KSpellDlg::KSpellDlgPrivate {
00046 public:
00047   KSpellUI* ui;
00048   KSpellConfig* spellConfig;
00049 };
00050 
00051 KSpellDlg::KSpellDlg( QWidget * parent, const char * name, bool _progressbar, bool _modal )
00052   : KDialogBase(
00053       parent, name, _modal, i18n("Check Spelling"), Help|Cancel|User1,
00054       Cancel, true, i18n("&Finished")
00055     ),
00056     progressbar( false )
00057 {
00058   Q_UNUSED( _progressbar );
00059   d = new KSpellDlgPrivate;
00060   d->ui = new KSpellUI( this );
00061   setMainWidget( d->ui );
00062 
00063   connect( d->ui->m_replaceBtn, SIGNAL(clicked()),
00064            this, SLOT(replace()));
00065   connect( this, SIGNAL(ready(bool)),
00066            d->ui->m_replaceBtn, SLOT(setEnabled(bool)) );
00067 
00068   connect( d->ui->m_replaceAllBtn, SIGNAL(clicked()), this, SLOT(replaceAll()));
00069   connect(this, SIGNAL(ready(bool)), d->ui->m_replaceAllBtn, SLOT(setEnabled(bool)));
00070 
00071   connect( d->ui->m_skipBtn, SIGNAL(clicked()), this, SLOT(ignore()));
00072   connect( this, SIGNAL(ready(bool)), d->ui->m_skipBtn, SLOT(setEnabled(bool)));
00073 
00074   connect( d->ui->m_skipAllBtn, SIGNAL(clicked()), this, SLOT(ignoreAll()));
00075   connect( this, SIGNAL(ready(bool)), d->ui->m_skipAllBtn, SLOT(setEnabled(bool)));
00076 
00077   connect( d->ui->m_addBtn, SIGNAL(clicked()), this, SLOT(add()));
00078   connect( this, SIGNAL(ready(bool)), d->ui->m_addBtn, SLOT(setEnabled(bool)));
00079 
00080   connect( d->ui->m_suggestBtn, SIGNAL(clicked()), this, SLOT(suggest()));
00081   connect( this, SIGNAL(ready(bool)), d->ui->m_suggestBtn, SLOT(setEnabled(bool)) );
00082   d->ui->m_suggestBtn->hide();
00083 
00084   connect(this, SIGNAL(user1Clicked()), this, SLOT(stop()));
00085 
00086   connect( d->ui->m_replacement, SIGNAL(textChanged(const QString &)),
00087            SLOT(textChanged(const QString &)) );
00088 
00089   connect( d->ui->m_replacement, SIGNAL(returnPressed()),   SLOT(replace()) );
00090   connect( d->ui->m_suggestions, SIGNAL(selectionChanged(QListViewItem*)),
00091            SLOT(slotSelectionChanged(QListViewItem*)) );
00092 
00093   connect( d->ui->m_suggestions, SIGNAL( doubleClicked ( QListViewItem *, const QPoint &, int ) ),
00094            SLOT( replace() ) );
00095   d->spellConfig = new KSpellConfig( 0, 0 ,0, false );
00096   d->spellConfig->fillDicts( d->ui->m_language );
00097   connect( d->ui->m_language, SIGNAL(activated(int)),
00098        d->spellConfig, SLOT(sSetDictionary(int)) );
00099   connect( d->spellConfig, SIGNAL(configChanged()),
00100            SLOT(slotConfigChanged()) );
00101 
00102   setHelp( "spelldlg", "kspell" );
00103   setMinimumSize( sizeHint() );
00104   emit ready( false );
00105 }
00106 
00107 KSpellDlg::~KSpellDlg()
00108 {
00109   delete d;
00110 }
00111 
00112 void
00113 KSpellDlg::init( const QString & _word, QStringList * _sugg )
00114 {
00115   sugg = _sugg;
00116   word = _word;
00117 
00118   d->ui->m_suggestions->clear();
00119   d->ui->m_suggestions->setSorting( NONSORTINGCOLUMN );
00120   for ( QStringList::Iterator it = _sugg->begin(); it != _sugg->end(); ++it ) {
00121     QListViewItem *item = new QListViewItem( d->ui->m_suggestions,
00122                                              d->ui->m_suggestions->lastItem() );
00123     item->setText( 0, *it );
00124   }
00125   kdDebug(750) << "KSpellDlg::init [" << word << "]" << endl;
00126 
00127   emit ready( true );
00128 
00129   d->ui->m_unknownWord->setText( _word );
00130 
00131   if ( sugg->count() == 0 ) {
00132     d->ui->m_replacement->setText( _word );
00133     d->ui->m_replaceBtn->setEnabled( false );
00134     d->ui->m_replaceAllBtn->setEnabled( false );
00135     d->ui->m_suggestBtn->setEnabled( false );
00136   } else {
00137     d->ui->m_replacement->setText( (*sugg)[0] );
00138     d->ui->m_replaceBtn->setEnabled( true );
00139     d->ui->m_replaceAllBtn->setEnabled( true );
00140     d->ui->m_suggestBtn->setEnabled( false );
00141     d->ui->m_suggestions->setSelected( d->ui->m_suggestions->firstChild(), true );
00142   }
00143 }
00144 
00145 void
00146 KSpellDlg::init( const QString& _word, QStringList* _sugg,
00147                  const QString& context )
00148 {
00149   sugg = _sugg;
00150   word = _word;
00151 
00152   d->ui->m_suggestions->clear();
00153   d->ui->m_suggestions->setSorting( NONSORTINGCOLUMN );
00154   for ( QStringList::Iterator it = _sugg->begin(); it != _sugg->end(); ++it ) {
00155       QListViewItem *item = new QListViewItem( d->ui->m_suggestions,
00156                                                d->ui->m_suggestions->lastItem() );
00157       item->setText( 0, *it );
00158   }
00159 
00160   kdDebug(750) << "KSpellDlg::init [" << word << "]" << endl;
00161 
00162   emit ready( true );
00163 
00164   d->ui->m_unknownWord->setText( _word );
00165   d->ui->m_contextLabel->setText( context );
00166 
00167   if ( sugg->count() == 0 ) {
00168     d->ui->m_replacement->setText( _word );
00169     d->ui->m_replaceBtn->setEnabled( false );
00170     d->ui->m_replaceAllBtn->setEnabled( false );
00171     d->ui->m_suggestBtn->setEnabled( false );
00172   } else {
00173     d->ui->m_replacement->setText( (*sugg)[0] );
00174     d->ui->m_replaceBtn->setEnabled( true );
00175     d->ui->m_replaceAllBtn->setEnabled( true );
00176     d->ui->m_suggestBtn->setEnabled( false );
00177     d->ui->m_suggestions->setSelected( d->ui->m_suggestions->firstChild(), true );
00178   }
00179 }
00180 
00181 void
00182 KSpellDlg::slotProgress( unsigned int p )
00183 {
00184   if (!progressbar)
00185     return;
00186 
00187   progbar->setValue( (int) p );
00188 }
00189 
00190 void
00191 KSpellDlg::textChanged( const QString & )
00192 {
00193   d->ui->m_replaceBtn->setEnabled( true );
00194   d->ui->m_replaceAllBtn->setEnabled( true );
00195   d->ui->m_suggestBtn->setEnabled( true );
00196 }
00197 
00198 void
00199 KSpellDlg::slotSelectionChanged( QListViewItem* item )
00200 {
00201   if ( item )
00202     d->ui->m_replacement->setText( item->text( 0 ) );
00203 }
00204 
00205 /*
00206   exit functions
00207   */
00208 
00209 void
00210 KSpellDlg::closeEvent( QCloseEvent * )
00211 {
00212   cancel();
00213 }
00214 
00215 void
00216 KSpellDlg::done( int result )
00217 {
00218   emit command( result );
00219 }
00220 void
00221 KSpellDlg::ignore()
00222 {
00223   newword = word;
00224   done( KS_IGNORE );
00225 }
00226 
00227 void
00228 KSpellDlg::ignoreAll()
00229 {
00230   newword = word;
00231   done( KS_IGNOREALL );
00232 }
00233 
00234 void
00235 KSpellDlg::add()
00236 {
00237   newword = word;
00238   done( KS_ADD );
00239 }
00240 
00241 
00242 void
00243 KSpellDlg::cancel()
00244 {
00245   newword = word;
00246   done( KS_CANCEL );
00247 }
00248 
00249 void
00250 KSpellDlg::replace()
00251 {
00252   newword = d->ui->m_replacement->text();
00253   done( KS_REPLACE );
00254 }
00255 
00256 void
00257 KSpellDlg::stop()
00258 {
00259   newword = word;
00260   done( KS_STOP );
00261 }
00262 
00263 void
00264 KSpellDlg::replaceAll()
00265 {
00266   newword = d->ui->m_replacement->text();
00267   done( KS_REPLACEALL );
00268 }
00269 
00270 void
00271 KSpellDlg::suggest()
00272 {
00273   newword = d->ui->m_replacement->text();
00274   done( KS_SUGGEST );
00275 }
00276 
00277 void
00278 KSpellDlg::slotConfigChanged()
00279 {
00280   d->spellConfig->writeGlobalSettings();
00281   done( KS_CONFIG );
00282 }
00283 
00284 #include "kspelldlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys