~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/widget/kexicharencodingcombobox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <qtextcodec.h>
 
23
 
 
24
#include <kdebug.h>
 
25
#include <klocale.h>
 
26
#include <kglobal.h>
 
27
#include <kcharsets.h>
 
28
 
 
29
KexiCharacterEncodingComboBox::KexiCharacterEncodingComboBox( 
 
30
        QWidget* parent, const QString& selectedEncoding )
 
31
 : KComboBox( parent, "KexiCharacterEncodingComboBox" )
 
32
 , m_defaultEncodingAdded(false)
 
33
{
 
34
        QString defaultEncoding(QString::fromLatin1(KGlobal::locale()->encoding()));
 
35
        QString defaultEncodingDescriptiveName;
 
36
 
 
37
        QString _selectedEncoding = selectedEncoding;
 
38
        if (_selectedEncoding.isEmpty())
 
39
                _selectedEncoding = QString::fromLatin1(KGlobal::locale()->encoding());
 
40
 
 
41
        QStringList descEncodings(KGlobal::charsets()->descriptiveEncodingNames());
 
42
        QStringList::ConstIterator it = descEncodings.constBegin();
 
43
 
 
44
        for (uint id = 0; it!=descEncodings.constEnd(); ++it)
 
45
        {
 
46
                bool found = false;
 
47
                QString name( KGlobal::charsets()->encodingForName( *it ) );
 
48
                QTextCodec *codecForEnc = KGlobal::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
                QString 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
 
 
80
KexiCharacterEncodingComboBox::~KexiCharacterEncodingComboBox()
 
81
{
 
82
}
 
83
 
 
84
QString KexiCharacterEncodingComboBox::selectedEncoding() const
 
85
{
 
86
        if (defaultEncodingSelected()) {
 
87
                return QString::fromLatin1(KGlobal::locale()->encoding());
 
88
        }
 
89
        else {
 
90
                return KGlobal::charsets()->encodingForName( currentText() );
 
91
        }
 
92
}
 
93
 
 
94
void KexiCharacterEncodingComboBox::setSelectedEncoding(const QString& encodingName)
 
95
{
 
96
        QString 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
 
 
105
bool KexiCharacterEncodingComboBox::defaultEncodingSelected() const
 
106
{
 
107
        return m_defaultEncodingAdded && 0==currentItem();
 
108
}
 
109
 
 
110
void KexiCharacterEncodingComboBox::selectDefaultEncoding()
 
111
{
 
112
        if (m_defaultEncodingAdded)
 
113
                setCurrentItem(0);
 
114
}