~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/flags.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (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
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "flags.h"
 
20
 
 
21
#include <kdebug.h>
 
22
#include <kstandarddirs.h>
 
23
#include <kiconloader.h>
 
24
#include <kglobalsettings.h>
 
25
#include <klocalizedstring.h>
 
26
 
 
27
#include <plasma/svg.h>
 
28
#include <plasma/paintutils.h>
 
29
 
 
30
#include <QtCore/QStringList>
 
31
#include <QtGui/QPixmap>
 
32
#include <QtGui/QPainter>
 
33
#include <QtGui/QIcon>
 
34
 
 
35
#include "x11_helper.h"
 
36
 
 
37
//for text handling
 
38
#include "keyboard_config.h"
 
39
#include "xkb_rules.h"
 
40
 
 
41
 
 
42
static const int FLAG_MAX_WIDTH = 21;
 
43
static const int FLAG_MAX_HEIGHT = 14;
 
44
static const char flagTemplate[] = "l10n/%1/flag.png";
 
45
 
 
46
Flags::Flags():
 
47
        svg(NULL)
 
48
{
 
49
        transparentPixmap = new QPixmap(FLAG_MAX_WIDTH, FLAG_MAX_HEIGHT);
 
50
        transparentPixmap->fill(Qt::transparent);
 
51
}
 
52
 
 
53
Flags::~Flags()
 
54
{
 
55
        if( svg != NULL ) {
 
56
                disconnect(svg, SIGNAL(repaintNeeded()), this, SLOT(themeChanged()));
 
57
                delete svg;
 
58
        }
 
59
        delete transparentPixmap;
 
60
}
 
61
 
 
62
const QIcon Flags::getIcon(const QString& layout)
 
63
{
 
64
        if( iconMap.contains(layout) ) {
 
65
                return iconMap[ layout ];
 
66
        }
 
67
 
 
68
        QIcon icon;
 
69
        if( ! layout.isEmpty() ) {
 
70
                if( layout == "epo" ) {
 
71
                        QString file = KStandardDirs::locate("data", "kcmkeyboard/pics/epo.png");
 
72
                        icon.addFile(file);
 
73
                }
 
74
                else {
 
75
                        QString countryCode = getCountryFromLayoutName( layout );
 
76
                        if( ! countryCode.isEmpty() ) {
 
77
                                QString file = KStandardDirs::locate("locale", QString(flagTemplate).arg(countryCode));
 
78
                                //                      kDebug() << "Creating icon for" << layout << "with" << file;
 
79
                                icon.addFile(file);
 
80
                        }
 
81
                }
 
82
        }
 
83
        iconMap[ layout ] = icon;
 
84
        return icon;
 
85
}
 
86
 
 
87
//static
 
88
//const QStringList NON_COUNTRY_LAYOUTS = QString("ara,brai,epo,latam,mao").split(",");
 
89
 
 
90
QString Flags::getCountryFromLayoutName(const QString& layout)  const
 
91
{
 
92
        QString countryCode = layout;
 
93
 
 
94
        if( countryCode == "nec_vndr/jp" )
 
95
                return "jp";
 
96
 
 
97
//      if( NON_COUNTRY_LAYOUTS.contain(layout) )
 
98
        if( countryCode.length() > 2 )
 
99
                return "";
 
100
 
 
101
        return countryCode;
 
102
}
 
103
 
 
104
//TODO: move this to some other class?
 
105
 
 
106
QString Flags::getShortText(const LayoutUnit& layoutUnit, const KeyboardConfig& keyboardConfig)
 
107
{
 
108
        if( layoutUnit.isEmpty() )
 
109
                return QString("--");
 
110
 
 
111
        QString layoutText = layoutUnit.layout;
 
112
 
 
113
        foreach(const LayoutUnit& lu, keyboardConfig.layouts) {
 
114
                if( layoutUnit.layout == lu.layout && layoutUnit.variant == lu.variant ) {
 
115
                        layoutText = lu.getDisplayName();
 
116
                        break;
 
117
                }
 
118
        }
 
119
 
 
120
//TODO: good autolabel
 
121
//      if( layoutText == layoutUnit.layout && layoutUnit.getDisplayName() != layoutUnit.layout ) {
 
122
//              layoutText = layoutUnit.getDisplayName();
 
123
//      }
 
124
 
 
125
        return layoutText;
 
126
}
 
127
 
 
128
QString Flags::getFullText(const LayoutUnit& layoutUnit, const KeyboardConfig& keyboardConfig, const Rules* rules)
 
129
{
 
130
        QString shortText = Flags::getShortText(layoutUnit, keyboardConfig);
 
131
        QString longText = Flags::getLongText(layoutUnit, rules);
 
132
        return i18nc("short layout label - full layout name", "%1 - %2", shortText, longText);
 
133
}
 
134
 
 
135
static QString getDisplayText(const QString& layout, const QString& variant, const Rules* rules)
 
136
{
 
137
        if( variant.isEmpty() )
 
138
                return layout;
 
139
        if( rules == NULL || rules->version == "1.0" )
 
140
                return i18nc("layout - variant", "%1 - %2", layout, variant);
 
141
        return variant;
 
142
}
 
143
 
 
144
QString Flags::getLongText(const LayoutUnit& layoutUnit, const Rules* rules)
 
145
{
 
146
        if( rules == NULL ) {
 
147
                return getDisplayText(layoutUnit.layout, layoutUnit.variant, rules);
 
148
        }
 
149
 
 
150
        QString layoutText = layoutUnit.layout;
 
151
        const LayoutInfo* layoutInfo = rules->getLayoutInfo(layoutUnit.layout);
 
152
        if( layoutInfo != NULL ) {
 
153
                layoutText = layoutInfo->description;
 
154
 
 
155
                if( ! layoutUnit.variant.isEmpty() ) {
 
156
                        const VariantInfo* variantInfo = layoutInfo->getVariantInfo(layoutUnit.variant);
 
157
                        QString variantText = variantInfo != NULL ? variantInfo->description : layoutUnit.variant;
 
158
 
 
159
                        layoutText = getDisplayText(layoutText, variantText, rules);
 
160
                }
 
161
        }
 
162
 
 
163
        return layoutText;
 
164
}
 
165
 
 
166
const QIcon Flags::getIconWithText(const LayoutUnit& layoutUnit, const KeyboardConfig& keyboardConfig)
 
167
{
 
168
        QString keySuffix(keyboardConfig.showFlag ? "_wf" : "_nf");
 
169
        QString key(layoutUnit.toString() + keySuffix);
 
170
        if( iconOrTextMap.contains(key) ) {
 
171
                return iconOrTextMap[ key ];
 
172
        }
 
173
 
 
174
        if( keyboardConfig.showFlag ) {
 
175
                QIcon icon = getIcon(layoutUnit.layout);
 
176
                if( ! icon.isNull() ) {
 
177
                        iconOrTextMap[ key ] = icon;
 
178
                        return icon;
 
179
                }
 
180
        }
 
181
 
 
182
        QString layoutText = Flags::getShortText(layoutUnit, keyboardConfig);
 
183
 
 
184
        QPixmap pm = QPixmap(KIconLoader::SizeLarge, KIconLoader::SizeLarge);
 
185
//      pm.fill(Qt::transparent);
 
186
 
 
187
        QPainter p(&pm);
 
188
//      p.setRenderHint(QPainter::SmoothPixmapTransform);
 
189
//      p.setRenderHint(QPainter::Antialiasing);
 
190
 
 
191
        QFont font = p.font();
 
192
 
 
193
        int height = pm.height();
 
194
        int fontSize = layoutText.length() == 2
 
195
                        ? height * 7 / 10
 
196
                        : height * 5 / 10;
 
197
 
 
198
        int smallestReadableSize = KGlobalSettings::smallestReadableFont().pixelSize();
 
199
        if( fontSize < smallestReadableSize ) {
 
200
                fontSize = smallestReadableSize;
 
201
        }
 
202
        font.setPixelSize(fontSize);
 
203
        
 
204
//      p.setFont(font);
 
205
//      p.setPen(Qt::black);
 
206
//      p.drawText(pm.rect(), Qt::AlignCenter | Qt::AlignHCenter, layoutText);
 
207
//      QIcon icon(pm);
 
208
 
 
209
        // we init svg so that we get notification about theme change
 
210
        //Plasma::Svg* svg =
 
211
        getSvg();
 
212
//    QPixmap pixmap = Plasma::PaintUtils::texturedText(layoutText, font, svg);
 
213
    QPixmap pixmap = Plasma::PaintUtils::shadowText(layoutText, font);
 
214
 
 
215
    QIcon icon(pixmap);
 
216
        iconOrTextMap[ key ] = icon;
 
217
 
 
218
        return icon;
 
219
}
 
220
 
 
221
Plasma::Svg* Flags::getSvg()
 
222
{
 
223
        if( svg == NULL ) {
 
224
                svg = new Plasma::Svg;
 
225
            svg->setImagePath("widgets/labeltexture");
 
226
            svg->setContainsMultipleImages(true);
 
227
            connect(svg, SIGNAL(repaintNeeded()), this, SLOT(themeChanged()));
 
228
        }
 
229
        return svg;
 
230
}
 
231
 
 
232
void Flags::themeChanged()
 
233
{
 
234
//      kDebug() << "Theme changed, new text color" << Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
 
235
        clearCache();
 
236
        emit pixmapChanged();
 
237
}
 
238
 
 
239
void Flags::clearCache()
 
240
{
 
241
//      kDebug() << "Clearing flag pixmap cache";
 
242
        iconOrTextMap.clear();
 
243
}