kexi

kexicharencodingcombobox.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kexicharencodingcombobox.h"
00021 
00022 #include <qtextcodec.h>
00023 
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kglobal.h>
00027 #include <kcharsets.h>
00028 
00029 KexiCharacterEncodingComboBox::KexiCharacterEncodingComboBox( 
00030     QWidget* parent, const QString& selectedEncoding )
00031  : KComboBox( parent, "KexiCharacterEncodingComboBox" )
00032  , m_defaultEncodingAdded(false)
00033 {
00034     QString defaultEncoding(QString::fromLatin1(KGlobal::locale()->encoding()));
00035     QString defaultEncodingDescriptiveName;
00036 
00037     QString _selectedEncoding = selectedEncoding;
00038     if (_selectedEncoding.isEmpty())
00039         _selectedEncoding = QString::fromLatin1(KGlobal::locale()->encoding());
00040 
00041     QStringList descEncodings(KGlobal::charsets()->descriptiveEncodingNames());
00042     QStringList::ConstIterator it = descEncodings.constBegin();
00043 
00044     for (uint id = 0; it!=descEncodings.constEnd(); ++it)
00045     {
00046         bool found = false;
00047         QString name( KGlobal::charsets()->encodingForName( *it ) );
00048         QTextCodec *codecForEnc = KGlobal::charsets()->codecForName(name, found);
00049         if (found) {
00050             insertItem(*it);
00051             if (codecForEnc->name() == defaultEncoding || name == defaultEncoding) {
00052                 defaultEncodingDescriptiveName = *it;
00053                 //remember, do not add, will be prepended later
00054             }
00055             else {
00056                 m_encodingDescriptionForName.insert(name, *it);
00057             }
00058             if (codecForEnc->name() == _selectedEncoding || name == _selectedEncoding) {
00059                 setCurrentItem(id);
00060             }
00061             id++;
00062         }
00063     }
00064 
00065     //prepend default encoding, if present
00066     if (!defaultEncodingDescriptiveName.isEmpty()) {
00067         m_defaultEncodingAdded = true;
00068         QString desc = i18n("Text encoding: Default", "Default: %1")
00069             .arg(defaultEncodingDescriptiveName);
00070         insertItem( desc, 0 );
00071         if (_selectedEncoding==defaultEncoding) {
00072             setCurrentItem(0);
00073         }
00074         else
00075             setCurrentItem(currentItem()+1);
00076         m_encodingDescriptionForName.insert(defaultEncoding, desc);
00077     }
00078 }
00079 
00080 KexiCharacterEncodingComboBox::~KexiCharacterEncodingComboBox()
00081 {
00082 }
00083 
00084 QString KexiCharacterEncodingComboBox::selectedEncoding() const
00085 {
00086     if (defaultEncodingSelected()) {
00087         return QString::fromLatin1(KGlobal::locale()->encoding());
00088     }
00089     else {
00090         return KGlobal::charsets()->encodingForName( currentText() );
00091     }
00092 }
00093 
00094 void KexiCharacterEncodingComboBox::setSelectedEncoding(const QString& encodingName)
00095 {
00096     QString desc = m_encodingDescriptionForName[encodingName];
00097     if (desc.isEmpty()) {
00098         kdWarning() << "KexiCharacterEncodingComboBox::setSelectedEncoding(): "
00099             "no such encoding \"" << encodingName << "\"" << endl;
00100         return;
00101     }
00102     setCurrentText(desc);
00103 }
00104 
00105 bool KexiCharacterEncodingComboBox::defaultEncodingSelected() const
00106 {
00107     return m_defaultEncodingAdded && 0==currentItem();
00108 }
00109 
00110 void KexiCharacterEncodingComboBox::selectDefaultEncoding()
00111 {
00112     if (m_defaultEncodingAdded)
00113         setCurrentItem(0);
00114 }
KDE Home | KDE Accessibility Home | Description of Access Keys