kexi

kexicharencodingcombobox.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kexicharencodingcombobox.h"
21
22#include <tqtextcodec.h>
23
24#include <kdebug.h>
25#include <tdelocale.h>
26#include <tdeglobal.h>
27#include <kcharsets.h>
28
30 TQWidget* parent, const TQString& selectedEncoding )
31 : KComboBox( parent, "KexiCharacterEncodingComboBox" )
32 , m_defaultEncodingAdded(false)
33{
34 TQString defaultEncoding(TQString::fromLatin1(TDEGlobal::locale()->encoding()));
35 TQString defaultEncodingDescriptiveName;
36
37 TQString _selectedEncoding = selectedEncoding;
38 if (_selectedEncoding.isEmpty())
39 _selectedEncoding = TQString::fromLatin1(TDEGlobal::locale()->encoding());
40
41 TQStringList descEncodings(TDEGlobal::charsets()->descriptiveEncodingNames());
42 TQStringList::ConstIterator it = descEncodings.constBegin();
43
44 for (uint id = 0; it!=descEncodings.constEnd(); ++it)
45 {
46 bool found = false;
47 TQString name( TDEGlobal::charsets()->encodingForName( *it ) );
48 TQTextCodec *codecForEnc = TDEGlobal::charsets()->codecForName(name, found);
49 if (found) {
50 insertItem(*it);
51 if (codecForEnc->name() == defaultEncoding || name == defaultEncoding) {
52 defaultEncodingDescriptiveName = *it;
53 //remember, do not add, will be prepended later
54 }
55 else {
56 m_encodingDescriptionForName.insert(name, *it);
57 }
58 if (codecForEnc->name() == _selectedEncoding || name == _selectedEncoding) {
59 setCurrentItem(id);
60 }
61 id++;
62 }
63 }
64
65 //prepend default encoding, if present
66 if (!defaultEncodingDescriptiveName.isEmpty()) {
67 m_defaultEncodingAdded = true;
68 TQString desc = i18n("Text encoding: Default", "Default: %1")
69 .arg(defaultEncodingDescriptiveName);
70 insertItem( desc, 0 );
71 if (_selectedEncoding==defaultEncoding) {
72 setCurrentItem(0);
73 }
74 else
75 setCurrentItem(currentItem()+1);
76 m_encodingDescriptionForName.insert(defaultEncoding, desc);
77 }
78}
79
80KexiCharacterEncodingComboBox::~KexiCharacterEncodingComboBox()
81{
82}
83
84TQString KexiCharacterEncodingComboBox::selectedEncoding() const
85{
86 if (defaultEncodingSelected()) {
87 return TQString::fromLatin1(TDEGlobal::locale()->encoding());
88 }
89 else {
90 return TDEGlobal::charsets()->encodingForName( currentText() );
91 }
92}
93
94void KexiCharacterEncodingComboBox::setSelectedEncoding(const TQString& encodingName)
95{
96 TQString desc = m_encodingDescriptionForName[encodingName];
97 if (desc.isEmpty()) {
98 kdWarning() << "KexiCharacterEncodingComboBox::setSelectedEncoding(): "
99 "no such encoding \"" << encodingName << "\"" << endl;
100 return;
101 }
102 setCurrentText(desc);
103}
104
105bool KexiCharacterEncodingComboBox::defaultEncodingSelected() const
106{
107 return m_defaultEncodingAdded && 0==currentItem();
108}
109
111{
112 if (m_defaultEncodingAdded)
113 setCurrentItem(0);
114}
void selectDefaultEncoding()
Selects default encoding, if present.
KexiCharacterEncodingComboBox(TQWidget *parent=0, const TQString &selectedEncoding=TQString())
Constructs a new combobox.