kmail

dictionarycombobox.cpp

Go to the documentation of this file.
00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002     dictionarycombobox.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2003 Ingo Kloecker <kloecker@kde.org>
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMail is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
00030 */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035 
00036 #include "dictionarycombobox.h"
00037 
00038 #include <ksconfig.h>
00039 #include <kdebug.h>
00040 
00041 #include <qstringlist.h>
00042 
00043 namespace KMail {
00044 
00045   DictionaryComboBox::DictionaryComboBox( QWidget * parent, const char * name )
00046     : QComboBox( false, parent, name ),
00047       mSpellConfig( 0 ),
00048       mDefaultDictionary( 0 )
00049   {
00050     reloadCombo();
00051     connect( this, SIGNAL( activated( int ) ),
00052              this, SLOT( slotDictionaryChanged( int ) ) );
00053     connect( this, SIGNAL( dictionaryChanged( int ) ),
00054              mSpellConfig, SLOT( sSetDictionary( int ) ) );
00055   }
00056 
00057   DictionaryComboBox::~DictionaryComboBox()
00058   {
00059     delete mSpellConfig;
00060     mSpellConfig = 0;
00061   }
00062 
00063   QString DictionaryComboBox::currentDictionaryName() const
00064   {
00065     return currentText();
00066   }
00067 
00068   QString DictionaryComboBox::currentDictionary() const
00069   {
00070     QString dict = mDictionaries[ currentItem() ];
00071     if ( dict.isEmpty() )
00072       return "<default>";
00073     else
00074       return dict;
00075   }
00076 
00077   void DictionaryComboBox::setCurrentByDictionaryName( const QString & name )
00078   {
00079     if ( name.isEmpty() )
00080       return;
00081 
00082     for ( int i = 0; i < count(); ++i ) {
00083       if ( text( i ) == name ) {
00084         if ( i != currentItem() ) {
00085           setCurrentItem( i );
00086           slotDictionaryChanged( i );
00087         }
00088         return;
00089       }
00090     }
00091   }
00092 
00093   void DictionaryComboBox::setCurrentByDictionary( const QString & dictionary )
00094   {
00095     if ( !dictionary.isEmpty() ) {
00096       // first handle the special case of the default dictionary
00097       if ( dictionary == "<default>" ) {
00098         if ( 0 != currentItem() ) {
00099           setCurrentItem( 0 );
00100           slotDictionaryChanged( 0 );
00101         }
00102         return;
00103       }
00104 
00105       int i = 0;
00106       for ( QStringList::ConstIterator it = mDictionaries.begin();
00107             it != mDictionaries.end();
00108             ++it, ++i ) {
00109         if ( *it == dictionary ) {
00110           if ( i != currentItem() ) {
00111             setCurrentItem( i );
00112             slotDictionaryChanged( i );
00113           }
00114           return;
00115         }
00116       }
00117     }
00118 
00119     // If dictionary is empty or doesn't exist fall back to the global default
00120     if ( mDefaultDictionary != currentItem() ) {
00121       setCurrentItem( mDefaultDictionary );
00122       slotDictionaryChanged( mDefaultDictionary );
00123     }
00124   }
00125 
00126   KSpellConfig* DictionaryComboBox::spellConfig() const
00127   {
00128     return mSpellConfig;
00129   }
00130 
00131   void DictionaryComboBox::reloadCombo()
00132   {
00133     delete mSpellConfig;
00134     mSpellConfig = new KSpellConfig( 0, 0, 0, false );
00135     mSpellConfig->fillDicts( this, &mDictionaries );
00136     mDefaultDictionary = currentItem();
00137   }
00138 
00139   void DictionaryComboBox::slotDictionaryChanged( int idx )
00140   {
00141     kdDebug( 5006 ) << "DictionaryComboBox::slotDictionaryChanged( " << idx
00142                     << " )" << endl;
00143     emit dictionaryChanged( mDictionaries[idx] );
00144     emit dictionaryChanged( idx );
00145   }
00146 
00147 } // namespace KMail
00148 
00149 #include "dictionarycombobox.moc"