2
* Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
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.
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.
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.
22
#include <kstandarddirs.h>
23
#include <kiconloader.h>
24
#include <kglobalsettings.h>
25
#include <klocalizedstring.h>
27
#include <plasma/svg.h>
28
#include <plasma/paintutils.h>
30
#include <QtCore/QStringList>
31
#include <QtGui/QPixmap>
32
#include <QtGui/QPainter>
33
#include <QtGui/QIcon>
35
#include "x11_helper.h"
38
#include "keyboard_config.h"
39
#include "xkb_rules.h"
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";
49
transparentPixmap = new QPixmap(FLAG_MAX_WIDTH, FLAG_MAX_HEIGHT);
50
transparentPixmap->fill(Qt::transparent);
56
disconnect(svg, SIGNAL(repaintNeeded()), this, SLOT(themeChanged()));
59
delete transparentPixmap;
62
const QIcon Flags::getIcon(const QString& layout)
64
if( iconMap.contains(layout) ) {
65
return iconMap[ layout ];
69
if( ! layout.isEmpty() ) {
70
if( layout == "epo" ) {
71
QString file = KStandardDirs::locate("data", "kcmkeyboard/pics/epo.png");
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;
83
iconMap[ layout ] = icon;
88
//const QStringList NON_COUNTRY_LAYOUTS = QString("ara,brai,epo,latam,mao").split(",");
90
QString Flags::getCountryFromLayoutName(const QString& layout) const
92
QString countryCode = layout;
94
if( countryCode == "nec_vndr/jp" )
97
// if( NON_COUNTRY_LAYOUTS.contain(layout) )
98
if( countryCode.length() > 2 )
104
//TODO: move this to some other class?
106
QString Flags::getShortText(const LayoutUnit& layoutUnit, const KeyboardConfig& keyboardConfig)
108
if( layoutUnit.isEmpty() )
109
return QString("--");
111
QString layoutText = layoutUnit.layout;
113
foreach(const LayoutUnit& lu, keyboardConfig.layouts) {
114
if( layoutUnit.layout == lu.layout && layoutUnit.variant == lu.variant ) {
115
layoutText = lu.getDisplayName();
120
//TODO: good autolabel
121
// if( layoutText == layoutUnit.layout && layoutUnit.getDisplayName() != layoutUnit.layout ) {
122
// layoutText = layoutUnit.getDisplayName();
128
QString Flags::getFullText(const LayoutUnit& layoutUnit, const KeyboardConfig& keyboardConfig, const Rules* rules)
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);
135
static QString getDisplayText(const QString& layout, const QString& variant, const Rules* rules)
137
if( variant.isEmpty() )
139
if( rules == NULL || rules->version == "1.0" )
140
return i18nc("layout - variant", "%1 - %2", layout, variant);
144
QString Flags::getLongText(const LayoutUnit& layoutUnit, const Rules* rules)
146
if( rules == NULL ) {
147
return getDisplayText(layoutUnit.layout, layoutUnit.variant, rules);
150
QString layoutText = layoutUnit.layout;
151
const LayoutInfo* layoutInfo = rules->getLayoutInfo(layoutUnit.layout);
152
if( layoutInfo != NULL ) {
153
layoutText = layoutInfo->description;
155
if( ! layoutUnit.variant.isEmpty() ) {
156
const VariantInfo* variantInfo = layoutInfo->getVariantInfo(layoutUnit.variant);
157
QString variantText = variantInfo != NULL ? variantInfo->description : layoutUnit.variant;
159
layoutText = getDisplayText(layoutText, variantText, rules);
166
const QIcon Flags::getIconWithText(const LayoutUnit& layoutUnit, const KeyboardConfig& keyboardConfig)
168
QString keySuffix(keyboardConfig.showFlag ? "_wf" : "_nf");
169
QString key(layoutUnit.toString() + keySuffix);
170
if( iconOrTextMap.contains(key) ) {
171
return iconOrTextMap[ key ];
174
if( keyboardConfig.showFlag ) {
175
QIcon icon = getIcon(layoutUnit.layout);
176
if( ! icon.isNull() ) {
177
iconOrTextMap[ key ] = icon;
182
QString layoutText = Flags::getShortText(layoutUnit, keyboardConfig);
184
QPixmap pm = QPixmap(KIconLoader::SizeLarge, KIconLoader::SizeLarge);
185
// pm.fill(Qt::transparent);
188
// p.setRenderHint(QPainter::SmoothPixmapTransform);
189
// p.setRenderHint(QPainter::Antialiasing);
191
QFont font = p.font();
193
int height = pm.height();
194
int fontSize = layoutText.length() == 2
198
int smallestReadableSize = KGlobalSettings::smallestReadableFont().pixelSize();
199
if( fontSize < smallestReadableSize ) {
200
fontSize = smallestReadableSize;
202
font.setPixelSize(fontSize);
205
// p.setPen(Qt::black);
206
// p.drawText(pm.rect(), Qt::AlignCenter | Qt::AlignHCenter, layoutText);
209
// we init svg so that we get notification about theme change
212
// QPixmap pixmap = Plasma::PaintUtils::texturedText(layoutText, font, svg);
213
QPixmap pixmap = Plasma::PaintUtils::shadowText(layoutText, font);
216
iconOrTextMap[ key ] = icon;
221
Plasma::Svg* Flags::getSvg()
224
svg = new Plasma::Svg;
225
svg->setImagePath("widgets/labeltexture");
226
svg->setContainsMultipleImages(true);
227
connect(svg, SIGNAL(repaintNeeded()), this, SLOT(themeChanged()));
232
void Flags::themeChanged()
234
// kDebug() << "Theme changed, new text color" << Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
236
emit pixmapChanged();
239
void Flags::clearCache()
241
// kDebug() << "Clearing flag pixmap cache";
242
iconOrTextMap.clear();